File: tokens_monadUserState_bytestring.x

package info (click to toggle)
alex 3.1.7-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 916 kB
  • sloc: haskell: 4,244; xml: 1,479; yacc: 246; makefile: 99; ansic: 4
file content (64 lines) | stat: -rw-r--r-- 1,664 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import System.Exit
import qualified Data.ByteString.Lazy.Char8 as B
}

%wrapper "monadUserState-bytestring"
%encoding "iso-8859-1"

$digit = 0-9			-- digits
$alpha = [a-zA-Z]		-- alphabetic characters

tokens :-

  $white+				;
  "--".*				;
  let					{ tok (\p s -> Let p) }
  in					{ tok (\p s -> In p) }
  $digit+                               { tok (\p s -> Int p (read (B.unpack s))) }
  [\=\+\-\*\/\(\)]                      { tok (\p s -> Sym p (head (B.unpack s))) }
  $alpha [$alpha $digit \_ \']*         { tok (\p s -> Var p (B.unpack s)) }

{
-- Each right-hand side has type :: AlexPosn -> String -> Token

-- Some action helpers:
tok f (p,_,input,_) len = return (f p (B.take (fromIntegral len) input))

-- The token type:
data Token =
	Let AlexPosn		|
	In  AlexPosn		|
	Sym AlexPosn Char	|
        Var AlexPosn String     |
	Int AlexPosn Int	|
        Err AlexPosn            |
        EOF
        deriving (Eq,Show)

alexEOF = return EOF

main = if test1 /= result1 then do print test1; exitFailure
			   else exitWith ExitSuccess

type AlexUserState = ()
alexInitUserState = ()

scanner str = runAlex str $ do
  let loop = do tok <- alexMonadScan
                if tok == EOF
			then return [tok]
			else do toks <- loop
				return (tok:toks)
  loop  

test1 = case scanner "  let in 012334\n=+*foo bar__'" of
          Left err -> error err
          Right toks -> toks

result1 = [Let (AlexPn 2 1 3),In (AlexPn 6 1 7),Int (AlexPn 9 1 10) 12334,Sym (AlexPn 16 2 1) '=',Sym (AlexPn 17 2 2) '+',Sym (AlexPn 18 2 3) '*',Var (AlexPn 19 2 4) "foo",Var (AlexPn 23 2 8) "bar__'", EOF]


}