3 module Lexer (lex_tok) where
5 import ParserM (ParserM (..), mkT, mkTv, Token(..), St, start_code,
7 inc_brace_depth, dec_brace_depth,
8 show_pos, position, input,
9 AlexInput, alexGetChar, alexInputPrevChar)
17 set_start_code in_braces;
27 <0> "->" { mkT TArrow }
28 <0> "=" { mkT TEquals }
29 <0> "," { mkT TComma }
30 <0> "(" { mkT TOpenParen }
31 <0> ")" { mkT TCloseParen }
32 <0> "(#" { mkT TOpenParenHash }
33 <0> "#)" { mkT THashCloseParen }
34 <0> "section" { mkT TSection }
35 <0> "primop" { mkT TPrimop }
36 <0> "pseudoop" { mkT TPseudoop }
37 <0> "primtype" { mkT TPrimtype }
38 <0> "with" { mkT TWith }
39 <0> "defaults" { mkT TDefaults }
40 <0> "True" { mkT TTrue }
41 <0> "False" { mkT TFalse }
42 <0> "Dyadic" { mkT TDyadic }
43 <0> "Monadic" { mkT TMonadic }
44 <0> "Compare" { mkT TCompare }
45 <0> "GenPrimOp" { mkT TGenPrimOp }
46 <0> "thats_all_folks" { mkT TThatsAllFolks }
47 <0> [a-z][a-zA-Z0-9\#_]* { mkTv TLowerName }
48 <0> [A-Z][a-zA-Z0-9\#_]* { mkTv TUpperName }
49 <0> \" [^\"]* \" { mkTv (TString . tail . init) }
50 <in_braces> [^\{\}]+ { mkTv TNoBraces }
51 <in_braces> \n { mkTv TNoBraces }
54 get_tok :: ParserM Token
55 get_tok = ParserM $ \i st ->
56 case alexScan i (start_code st) of
57 AlexEOF -> Right (i, st, TEOF)
58 AlexError _ -> Left ("Lexical error at " ++ show_pos (position i))
59 AlexSkip i' _ -> case get_tok of
61 AlexToken i' l a -> case a $ take l $ input i of
64 lex_tok :: (Token -> ParserM a) -> ParserM a
65 lex_tok cont = get_tok >>= cont