-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.mll
247 lines (219 loc) · 8.54 KB
/
lexer.mll
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
{
open Lexing
open Parser
open String
exception ForbiddenWordError of string
exception SyntaxError of string
let string_buf = ref []
let backslashes = ref 0
let raise_forbidden_error lexbuf =
let pos = Lexing.lexeme_start_p lexbuf in
let lnum = string_of_int pos.pos_lnum in
let column = string_of_int (1 + pos.pos_cnum - pos.pos_bol) in
let token = Lexing.lexeme lexbuf in
let s = "Forbidden word: line " ^ lnum ^ ", column " ^ column ^ ": " ^ token ^
"\n" in
raise (ForbiddenWordError(s))
let raise_syntax_error lexbuf =
let pos = Lexing.lexeme_start_p lexbuf in
let lnum = string_of_int pos.pos_lnum in
let column = string_of_int (1 + pos.pos_cnum - pos.pos_bol) in
let token = Lexing.lexeme lexbuf in
let s = "Syntax error: line " ^ lnum ^ ", column " ^ column ^ ": " ^ token ^
"\n" in
raise (SyntaxError(s))
let parse_char lexbuf =
(* TODO we shouldn't ever really hit error cases here, right? *)
let s = Lexing.lexeme lexbuf in
match length s with
| 4 ->
(match get s 1 with
| '\\' ->
(match get s 2 with
| 't' -> '\t'
| 'n' -> '\n'
| c -> c)
| _ -> raise_syntax_error lexbuf)
| 3 -> get s 1
| _ -> raise_syntax_error lexbuf
let parse_char_in_string lexbuf =
(* TODO we shouldn't ever really hit error cases here, right? *)
let s = Lexing.lexeme lexbuf in
match length s with
| 2 ->
(match get s 0 with
| '\\' ->
(match get s 1 with
| 't' -> '\t'
| 'n' -> raise_syntax_error lexbuf
| c -> c)
| _ -> raise_syntax_error lexbuf)
| 1 -> get s 0
| _ -> raise_syntax_error lexbuf
}
let whitespace = [' ' '\t']+
let newline = '\r' | '\n' | "\r\n"
let lower = ['a'-'z']
let upper = ['A'-'Z']
let letter = lower | upper
let number = ['0'-'9']
let id = ('_' | letter) (letter | number | '_')*
let integer = '0' | (['1'-'9'] number*)
let char = '\'' ([^'\\''\n''\''] | '\\'[^'n''t'] | "\\n" | "\\t") '\''
let charinstring = [^'\\''\n'] | '\\'[^'n''t'] | "\\n" | "\\t"
rule read = parse
(* Whitespace *)
| whitespace { read lexbuf }
| newline { new_line lexbuf; read lexbuf }
(* Decaf keywords *)
| "break" { BREAK }
| "class" { CLASS }
| "continue" { CONTINUE }
| "else" { ELSE }
| "extends" { EXTENDS }
| "if" { IF }
| "new" { NEW }
| "private" { PRIVATE }
| "protected" { PROTECTED }
| "public" { PUBLIC }
| "return" { RETURN }
| "static" { STATIC }
| "super" { SUPER }
| "this" { THIS }
| "while" { WHILE }
(* Decaf forbidden words *)
| "abstract" { raise_forbidden_error lexbuf }
| "byte" { raise_forbidden_error lexbuf }
| "case" { raise_forbidden_error lexbuf }
| "catch" { raise_forbidden_error lexbuf }
| "const" { raise_forbidden_error lexbuf }
| "default" { raise_forbidden_error lexbuf }
| "do" { raise_forbidden_error lexbuf }
| "double" { raise_forbidden_error lexbuf }
| "final" { raise_forbidden_error lexbuf }
| "finally" { raise_forbidden_error lexbuf }
| "for" { raise_forbidden_error lexbuf }
| "implements" { raise_forbidden_error lexbuf }
| "import" { raise_forbidden_error lexbuf }
| "instanceof" { raise_forbidden_error lexbuf }
| "interface" { raise_forbidden_error lexbuf }
| "long" { raise_forbidden_error lexbuf }
| "native" { raise_forbidden_error lexbuf }
| "goto" { raise_forbidden_error lexbuf }
| "package" { raise_forbidden_error lexbuf }
| "short" { raise_forbidden_error lexbuf }
| "switch" { raise_forbidden_error lexbuf }
| "synchronized" { raise_forbidden_error lexbuf }
| "throw" { raise_forbidden_error lexbuf }
| "throws" { raise_forbidden_error lexbuf }
| "transient" { raise_forbidden_error lexbuf }
| "try" { raise_forbidden_error lexbuf }
| "volatile" { raise_forbidden_error lexbuf }
| "byvalue" { raise_forbidden_error lexbuf }
| "cast" { raise_forbidden_error lexbuf }
| "future" { raise_forbidden_error lexbuf }
| "generic" { raise_forbidden_error lexbuf }
| "inner" { raise_forbidden_error lexbuf }
| "none" { raise_forbidden_error lexbuf }
| "operator" { raise_forbidden_error lexbuf }
| "outer" { raise_forbidden_error lexbuf }
| "rest" { raise_forbidden_error lexbuf }
| "var" { raise_forbidden_error lexbuf }
| "byte" { raise_forbidden_error lexbuf }
| "double" { raise_forbidden_error lexbuf }
| "float" { raise_forbidden_error lexbuf }
| "long" { raise_forbidden_error lexbuf }
| "short" { raise_forbidden_error lexbuf }
| "~" { raise_forbidden_error lexbuf }
| "?" { raise_forbidden_error lexbuf }
| ":" { raise_forbidden_error lexbuf }
| "++" { raise_forbidden_error lexbuf }
| "--" { raise_forbidden_error lexbuf }
| "&" { raise_forbidden_error lexbuf }
| "|" { raise_forbidden_error lexbuf }
| "^" { raise_forbidden_error lexbuf }
| "<<" { raise_forbidden_error lexbuf }
| ">>" { raise_forbidden_error lexbuf }
| ">>>" { raise_forbidden_error lexbuf }
| "+=" { raise_forbidden_error lexbuf }
| "-+" { raise_forbidden_error lexbuf }
| "*=" { raise_forbidden_error lexbuf }
| "/+" { raise_forbidden_error lexbuf }
| "&=" { raise_forbidden_error lexbuf }
| "|=" { raise_forbidden_error lexbuf }
| "^=" { raise_forbidden_error lexbuf }
| "%=" { raise_forbidden_error lexbuf }
| "<<=" { raise_forbidden_error lexbuf }
| ">>=" { raise_forbidden_error lexbuf }
| ">>>=" { raise_forbidden_error lexbuf }
| char { CHAR(parse_char lexbuf) }
| "\"" { string_buf := []; backslashes := 0; read_string lexbuf }
| "true" { BOOL(true)}
| "false" { BOOL(false)}
| "null" { NULL }
(* Primitive types *)
| "boolean" { BOOLTYPE }
| "char" { CHARTYPE }
| "int" { INTTYPE }
| "void" { VOIDTYPE }
(* Comments *)
| "//"[^'\r''\n']* { read lexbuf }
| "/*" { swallow lexbuf }
(* Identifier *)
| id { ID(Lexing.lexeme lexbuf)}
(* Literals *)
| integer { INT(int_of_string (Lexing.lexeme lexbuf))}
(* Punctuation *)
| "(" { LPAREN }
| ")" { RPAREN }
| "{" { LBRACE }
| "}" { RBRACE }
| "[" { LBRACKET }
| "]" { RBRACKET }
| ";" { SEMICOLON }
| "," { COMMA }
| "." { DOT }
(* Operators *)
| "=" { ASGN }
| ">" { GREATER }
| "<" { LESS }
| "!" { NOT }
| "==" { EQUALS }
| ">=" { GEQ }
| "<=" { LEQ }
| "!=" { NEQ }
| "+" { PLUS }
| "-" { MINUS }
| "*" { TIMES }
| "/" { DIV }
| "&&" { AND }
| "||" { OR }
| "%" { MOD }
| eof { EOF }
| _ { raise_syntax_error lexbuf }
and swallow = parse
| "*/" { read lexbuf }
| newline { new_line lexbuf; swallow lexbuf }
| eof { EOF }
| _ { swallow lexbuf }
and read_string = parse
| "\"" { if (!backslashes == 0) || (!backslashes mod 2) != 0 then
STRING(String.concat "" (List.rev(!string_buf)))
else
raise_syntax_error lexbuf
}
(* TODO the special-casing of \n and \t is redundant with
charinstring/parse_char_in_string *)
| "\\n" { string_buf := (String.make 1 '\\') :: !string_buf;
string_buf := (String.make 1 'n') :: !string_buf;
read_string lexbuf }
| "\\t" { string_buf := (String.make 1 '\\') :: !string_buf;
string_buf := (String.make 1 't') :: !string_buf;
read_string lexbuf }
| charinstring { string_buf := (String.make 1
(parse_char_in_string lexbuf)) :: !string_buf;
read_string lexbuf }
| '\\' { backslashes := !backslashes + 1; read_string lexbuf }
| eof { EOF }
| _ { read_string lexbuf }
{}