1 {
2 {-# LANGUAGE OverloadedStrings #-}
3 module Main (main) where
4 import System.Exit
5 import Data.ByteString.Lazy.Char8 (unpack)
6 }
7 
8 %wrapper "basic-bytestring"
9 %encoding "utf-8"
10 
11 $digit = 0-9      -- digits
12 $alpha = [a-zA-Zαβ]    -- alphabetic characters
13 
14 tokens :-
15 
16   $white+        ;
17   "--".*         ;
18   let            { \_ -> Let }
19   in             { \_ -> In }
20   $digit+                               { \s -> Int (read (unpack s)) }
21   [\=\+\-\*\/\(\)]                      { \s -> Sym (head (unpack s)) }
22   $alpha [$alpha $digit \_ \']*         { \s -> Var (unpack s) }
23 
24 {
25 -- Each right-hand side has type :: ByteString -> Token
26 
27 -- The token type:
28 data Token =
29   Let     |
30   In      |
31   Sym Char  |
32   Var String  |
33   Int Int    |
34   Err
35   deriving (Eq,Show)
36 
37 main = if test1 /= result1 then exitFailure
38                            else exitWith ExitSuccess
39 
40 -- \206\177\206\178\206\178 is "αββ" utf-8 encoded
41 test1 = alexScanTokens "  let in 012334\n=+*foo \206\177\206\178\206\178 bar__'"
42 result1 = [Let,In,Int 12334,Sym '=',Sym '+',Sym '*',Var "foo",Var "\206\177\206\178\206\178",Var "bar__'"]
43 }
44