8d0c3e93ffe43958d3cb1a71f809750b96a8211b
[ghc-hetmet.git] / utils / genprimopcode / Lexer.x
1
2 {
3 module Lexer (lex_tok) where
4
5 import ParserM (ParserM (..), mkT, mkTv, Token(..), St, start_code,
6                 StartCode, Action, set_start_code,
7                 inc_brace_depth, dec_brace_depth,
8                 show_pos, position, input,
9                 AlexInput, alexGetChar, alexInputPrevChar)
10 }
11
12 words :-
13
14     <0>         $white+             ;
15     <0>         "--" [^\n]* \n      ;
16                 "{"                 { \i -> do {
17                                                 set_start_code in_braces;
18                                                 inc_brace_depth;
19                                                 mkT TOpenBrace i
20                                                }
21                                     }
22                 "}"                 { \i -> do {
23                                                 dec_brace_depth;
24                                                 mkT TCloseBrace i
25                                                }
26                                     }
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 }
52
53 {
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
60                             ParserM f -> f i' st
61        AlexToken i' l a -> case a $ take l $ input i of
62                                ParserM f -> f i' st
63
64 lex_tok :: (Token -> ParserM a) -> ParserM a
65 lex_tok cont = get_tok >>= cont
66 }
67