1 
2 {
3 module Lexer (lex_tok) where
4 
5 import Control.Monad.State (StateT, get)
6 import ParserM (ParserM (..), mkT, Token(..), St, start_code,
7                 StartCode, Action, set_start_code,
8                 show_pos, position, input,
9                 AlexInput, alexGetByte, alexInputPrevChar)
10 }
11 
12 words :-
13 
14  <0>  $white+  ;
15  <0>  fork     { mkT TFork }
16  <0>  leaf     { mkT TLeaf }
17 
18 {
19 get_tok :: AlexInput -> StateT St (Either String) (Token, AlexInput)
20 get_tok = \i ->
21    do st <- get
22       case alexScan i (start_code st) of
23           AlexEOF -> return (TEOF, i)
24           AlexError _ -> fail $ "Lexical error at " ++ show_pos (position i)
25           AlexSkip i' _ -> get_tok i'
26           AlexToken i' l a -> a (i', take l (input i))
27 
28 begin :: StartCode -> Action
29 begin sc (i, _) = do set_start_code sc
30                      get_tok i
31 
32 lex_tok :: (Token -> ParserM a) -> ParserM a
33 lex_tok cont = ParserM $ \i ->
34    do (tok, iz) <- get_tok i
35       case cont tok of
36           ParserM x -> x iz
37 }
38 
39