-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.x
200 lines (179 loc) · 7.6 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
{
module Lexer (Token(..), AlexPosn(..), alexScanTokens, getTokens) where
import System.IO
import System.IO.Unsafe
}
%wrapper "posn"
$digit = 0-9 -- digits
$lowerAlpha = [a-z] -- alphabetic characters
$upperAlpha = [A-Z]
$alpha = [a-zA-Z]
tokens :-
----------------------- FLOW --------------------------
if { \p s -> If p }
then { \p s -> Then p }
else { \p s -> Else p }
while { \p s -> While p }
for { \p s -> For p }
continue { \p s -> Continue p }
break { \p s -> Break p }
----------------------- COMMANDS --------------------------
free { \p s -> Free p }
print { \p s -> Print p }
println { \p s -> PrintLn p }
read { \p s -> Read p }
alloc { \p s -> Alloc p }
cast { \p s -> Cast p }
addr { \p s -> Addr p }
len { \p s -> Len p }
substr { \p s -> Substr p }
---------------------- TYPES -------------------------
Int { \p s -> Int p }
Double { \p s -> Double p }
Bool { \p s -> Bool p }
String { \p s -> String p }
Pointer { \p s -> Pointer p }
List { \p s -> List p }
Array { \p s -> Array p }
Hashmap { \p s -> Hashmap p }
---------------------- SCOPES -------------------------
\( { \p s -> LeftParen p }
\) { \p s -> RightParen p }
\[ { \p s -> LeftBracket p }
\] { \p s -> RightBracket p }
\{ { \p s -> LeftBrace p }
\} { \p s -> RightBrace p }
--------------------- OPERADORES --------------------------
:= { \p s -> Assign p }
"->" { \p s -> Arrow p }
"%" { \p s -> Mod p }
\^ { \p s -> Expo p }
\+ { \p s -> Plus p }
\- { \p s -> Minus p }
\* { \p s -> Star p }
"/" { \p s -> Div p }
"<" { \p s -> LessThan p }
> { \p s -> GreaterThan p }
"<"= { \p s -> LessEquals p }
>= { \p s -> GreaterEquals p }
== { \p s -> Equals p }
!= { \p s -> Difference p }
"<"\_ { \p s -> LessThan_ p }
>\_ { \p s -> GreaterThan_ p }
"<"=\_ { \p s -> LessEquals_ p }
>=\_ { \p s -> GreaterEquals_ p }
==\_ { \p s -> Equals_ p }
!=\_ { \p s -> Difference_ p }
! { \p s -> Negation p }
(&& | and) { \p s -> And p }
(\|\| | or) { \p s -> Or p }
-------------------- LITERALS --------------------------
NULL { \p s -> Null p }
$digit+\.$digit+ { \p s -> DoubleLit p (read s) }
$digit+ { \p s -> IntLit p (read s) }
("true" | "false") { \p s -> BoolLit p (boolVal s) }
\"[^\"]*\" { \p s -> StringLit p (extractString s) }
-------------------------- MAIN ------------------------------------
$white+ ;
"//".* ; -- ignora comentários
return { \p s -> Return p }
import { \p s -> Import p }
struct { \p s -> Struct p }
func { \p s -> Func p }
proc { \p s -> Proc p }
$lowerAlpha [$alpha $digit \_]* { \p s -> Id p s }
$upperAlpha [$alpha $digit \_]* { \p s -> TypeId p s }
"#" { \p s -> Hashtag p }
":" { \p s -> Colon p }
";" { \p s -> Separator p }
"," { \p s -> Comma p }
\. { \p s -> Dot p }
{
data Token =
-- FLOW ---------------------------------------------
If AlexPosn |
Then AlexPosn |
Else AlexPosn |
While AlexPosn |
For AlexPosn |
Continue AlexPosn |
Break AlexPosn |
-- COMMANDS ---------------------------------------------
Free AlexPosn |
Print AlexPosn |
PrintLn AlexPosn |
Read AlexPosn |
Alloc AlexPosn |
Addr AlexPosn |
Cast AlexPosn |
Len AlexPosn |
Substr AlexPosn |
-- TYPES ---------------------------------------------
Int AlexPosn |
Double AlexPosn |
Bool AlexPosn |
String AlexPosn |
Pointer AlexPosn |
List AlexPosn |
Array AlexPosn |
Hashmap AlexPosn |
Tuple AlexPosn |
-- SCOPES ---------------------------------------------
LeftBrace AlexPosn |
RightBrace AlexPosn |
LeftParen AlexPosn |
RightParen AlexPosn |
LeftBracket AlexPosn |
RightBracket AlexPosn |
-- OPERADORES -----------------------------------------
Arrow AlexPosn |
Assign AlexPosn |
Mod AlexPosn |
Expo AlexPosn |
Plus AlexPosn |
Minus AlexPosn |
Star AlexPosn |
Div AlexPosn |
LessThan AlexPosn |
GreaterThan AlexPosn |
LessEquals AlexPosn |
GreaterEquals AlexPosn |
Equals AlexPosn |
Difference AlexPosn |
LessThan_ AlexPosn |
GreaterThan_ AlexPosn |
LessEquals_ AlexPosn |
GreaterEquals_ AlexPosn |
Equals_ AlexPosn |
Difference_ AlexPosn |
Negation AlexPosn |
And AlexPosn |
Or AlexPosn |
-- LITERALS -------------------------------------------
DoubleLit AlexPosn Double |
Null AlexPosn |
IntLit AlexPosn Int |
BoolLit AlexPosn Bool |
StringLit AlexPosn String |
-- MAIN ---------------------------------------------
Struct AlexPosn |
Id AlexPosn String |
TypeId AlexPosn String |
Return AlexPosn |
Import AlexPosn |
Func AlexPosn |
Proc AlexPosn |
Hashtag AlexPosn |
Separator AlexPosn |
Colon AlexPosn |
Comma AlexPosn |
Dot AlexPosn
deriving (Eq,Show)
boolVal "true" = True
boolVal "false" = False
extractString s = (drop 1 (take ((length s)-1) s))
getTokens fn = unsafePerformIO (getTokensAux fn)
getTokensAux fn = do {fh <- openFile fn ReadMode;
s <- hGetContents fh;
return (alexScanTokens s)}
}