-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.x
146 lines (123 loc) · 5.71 KB
/
Lexer.x
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{
module Lexer where
}
%wrapper "posn"
$digit = 0-9
-- digits
$alpha = [a-zA-Z]
-- alphabetic characters
tokens :-
$white+ ;
";" ;
"(" {tok (\p s -> LPAREN p) }
")" {tok (\p s -> RPAREN p) }
-- int ops
"+" {tok (\p s -> PLUS p) }
"-" {tok (\p s -> MINUS p) }
"*" { tok (\p s ->TIMES p) }
"~" {tok (\p s -> NEGATE p )}
"=" {tok (\p s -> EQUALS p )}
"<" {tok (\p s -> LESSTHAN p) }
">" {tok (\p s -> GREATERTHAN p) }
-- bool ops
"!" {tok (\p s -> NOT p) }
"&&" {tok (\p s -> AND p )}
"||" {tok (\p s -> OR p )}
"^" {tok (\p s -> XOR p) }
-- ite
"if" {tok (\p s -> IF p) }
"then" {tok (\p s -> THEN p) }
"else" {tok (\p s -> ELSE p) }
"fi" {tok (\p s -> FI p )}
-- let
let {tok (\p s -> LET p )}
":=" {tok (\p s -> ASSIGN p )}
in {tok (\p s -> IN p )}
end {tok (\p s -> END p)}
"->" {tok (\p s -> TokenArrow p) }
--atoms
$digit+ {tok (\p s -> TokenInt p (read s)) }
true {tok (\p s -> TokenVarTrue p) }
false {tok (\p s -> TokenVarFalse p) }
-- function
"fun" {tok (\p s -> FUNCTION p) }
"=>" {tok (\p s -> FUNDECL p)}
"fn" {tok (\p s -> TokenFn p) }
"::" {tok (\p s -> TokenCons p) }
-- data types
bool {tok (\p s -> TYPEBOOL p)}
int {tok (\p s -> TYPEINT p) }
$alpha [$alpha $digit \_ \’]* { tok (\p s -> ID p s) }
{
-- Each action has type :: AlexPosn -> String -> MDLToken
-- Helper function
tok f p s = f p s
-- The token type:
data VarToken =
LPAREN AlexPosn |
RPAREN AlexPosn |
PLUS AlexPosn |
MINUS AlexPosn |
TIMES AlexPosn |
NEGATE AlexPosn |
EQUALS AlexPosn |
LESSTHAN AlexPosn |
GREATERTHAN AlexPosn |
NOT AlexPosn |
AND AlexPosn |
OR AlexPosn |
XOR AlexPosn |
IF AlexPosn |
THEN AlexPosn |
ELSE AlexPosn |
FI AlexPosn |
LET AlexPosn |
ASSIGN AlexPosn |
IN AlexPosn |
END AlexPosn |
TokenArrow AlexPosn |
TokenInt AlexPosn Int |
TokenVarTrue AlexPosn |
TokenVarFalse AlexPosn |
FUNCTION AlexPosn |
FUNDECL AlexPosn |
TokenFn AlexPosn |
TokenCons AlexPosn |
TYPEBOOL AlexPosn |
TYPEINT AlexPosn |
ID AlexPosn String
deriving (Eq,Show)
tokenPosn :: VarToken -> String
tokenPosn ( TokenInt (AlexPn a l c) _) = show(l) ++ ":" ++ show(c)
tokenPosn ( ID (AlexPn a l c) _) = show(l) ++ ":" ++ show(c)
tokenPosn ( LPAREN (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( RPAREN (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( PLUS (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( MINUS (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( TIMES (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( NEGATE (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( EQUALS (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( LESSTHAN (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( GREATERTHAN (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( NOT (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( AND (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( OR (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( XOR (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( IF (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( THEN (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( ELSE (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( FI (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( LET (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( ASSIGN (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( IN (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( END (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( TokenArrow (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( TokenVarTrue (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( TokenVarFalse (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( FUNCTION (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( FUNDECL (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( TokenFn (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( TokenCons (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( TYPEBOOL (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
tokenPosn ( TYPEINT (AlexPn a l c)) = show(l) ++ ":" ++ show(c)
}