Skip to content

Instantly share code, notes, and snippets.

@osa1
Created May 10, 2012 18:30
Show Gist options
  • Save osa1/2654913 to your computer and use it in GitHub Desktop.
Save osa1/2654913 to your computer and use it in GitHub Desktop.
alex
{
module Main (main) where
import Control.Monad.IO.Class (liftIO)
}
%wrapper "monad"
$whitespace = [\ \b\t\n\f\v\r]
$digit = 0-9
$alpha = [a-zA-Z_]
$upper = [A-Z]
$lower = [a-z]
$char = [A-Z a-z \!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~ 0-9 \:\"\']
$charesc = [abfnrtv\\\"\'\&]
@escape = \\ $charesc
@string = $char # [\"] | " " | @escape
tokens :-
$whitespace+ ;
$upper $alpha+ { mkL LTypeId }
$lower $alpha+ { mkL LId }
$digit+ { mkL LInt }
\" @string* \" { mkL LString }
{
data Lexeme = L AlexPosn LexemeClass (Maybe String) deriving (Show)
mkL :: LexemeClass -> AlexInput -> Int -> Alex Lexeme
mkL c (posn,_,_,s) n = return $ L posn c (Just $ take n s)
data LexemeClass
= LTypeId
| LId
| LInt
| LString
| LEOF
deriving (Show, Eq)
alexEOF = return (L undefined LEOF Nothing)
scanner str = runAlex str $ do
let loop i = do l@(L _ c _) <- alexMonadScan
if c == LEOF
then return i
else do let i' = i++[l] in i' `seq` loop i'
loop []
main :: IO ()
main = do
s <- getContents
print (scanner s)
-- let r = runAlex s alexMonadScan
-- print r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment