Imp_Lex.x 1.29 KB
Newer Older
Tadej Borovšak's avatar
Tadej Borovšak committed
1
-- vim: set syntax=alex autoindent
Alex Simpson's avatar
Alex Simpson committed
2
3
4
5
6
7
8
9

{module Imp_Lex where }

%wrapper "basic"

-- character classes

$digit		= [0-9]
Tadej Borovšak's avatar
Tadej Borovšak committed
10
$alpha          = [A-Za-z]
Alex Simpson's avatar
Alex Simpson committed
11
12
13

:-

Tadej Borovšak's avatar
Tadej Borovšak committed
14
15
16
17
18
19
20
21
22
23
24
25
    $white+                     ;                     -- whitespace
    \( | \)                     {\s -> SCOPE s}       -- scope
    \;                          {\s -> DELIM}         -- command delimiter
    \+ | \- | \*                {\s -> OP s}          -- arithmetic ops
    \=\= | \<                   {\s -> OP s}          -- boolean ops
    \:\=                        {\s -> ASSIGN}        -- assignment
    if | then | else            {\s -> KEYWORD s}     -- if command
    while | do                  {\s -> KEYWORD s}     -- while command
    skip                        {\s -> KEYWORD s}     -- skip command
    True | False                {\s -> BOOL $ read s} -- boolean literals
    $digit+                     {\s -> NUM $ read s}  -- numbers
    $alpha [$alpha $digit \_]*  {\s -> LOC s}         -- locations
Alex Simpson's avatar
Alex Simpson committed
26
27
28
29
30

{

-- The Token type - the action for each lexical class has type String -> Token

Tadej Borovšak's avatar
Tadej Borovšak committed
31
32
33
34
35
36
37
38
39
40
data Token = SCOPE String
           | DELIM
           | OP String
           | ASSIGN
           | KEYWORD String
           | BOOL Bool
           | NUM Integer
           | LOC String
           | INVALID
           deriving Show
Alex Simpson's avatar
Alex Simpson committed
41
42
43
44

imp_lex = alexScanTokens

}