-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlex.mll
259 lines (217 loc) · 6.33 KB
/
lex.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
248
249
250
251
252
253
254
255
256
257
258
259
{
(* INTUITIONISTIC TYPE THEORY PROGRAMMING LANGUAGE *)
(* *)
(* Copyright (c) 2006-2013 Johan G. Granstroem. *)
(* *)
(* Licensed under the Apache License, Version 2.0 (the "License"); *)
(* you may not use this file except in compliance with the License. *)
(* You may obtain a copy of the License at *)
(* *)
(* http://www.apache.org/licenses/LICENSE-2.0 *)
(* *)
(* Unless required by applicable law or agreed to in writing, software *)
(* distributed under the License is distributed on an "AS IS" BASIS, *)
(* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *)
(* See the License for the specific language governing permissions and *)
(* limitations under the License. *)
open Base
open Syntax
open Lexing
let incr_lineno lexbuf =
let pos = lexbuf.lex_curr_p in
lexbuf.lex_curr_p <- { pos with
pos_lnum = pos.pos_lnum + 1;
pos_bol = pos.pos_cnum;
}
type details =
| Illegal_character of char
| Literal_overflow of string
| Invalid_hex
let format_details (fmt:Format.formatter) (err:details) :unit =
match err with
| Illegal_character c ->
Format.fprintf fmt "illegal character (%s)" (Char.escaped c)
| Literal_overflow ty ->
Format.fprintf fmt "integer literal exceeds representable range for \
integers of type %s" ty
| Invalid_hex ->
Format.fprintf fmt "hex literal must be of size 2, 4, 8, or 16"
exception Error of (string * pos) * details
let format_error (fmt:Format.formatter)
((fname, (a, b)), (err)):unit =
Format.fprintf fmt "%s:%d.%d: %a" fname a b format_details err
let pos_of_lexpos (p:Lexing.position) :pos =
(p.Lexing.pos_lnum, p.Lexing.pos_cnum - p.Lexing.pos_bol)
let currpos (lexbuf:Lexing.lexbuf):string*pos =
lexbuf.lex_curr_p.pos_fname, (pos_of_lexpos (lexbuf.lex_curr_p))
let keyword_table =
let bindings = [
"eq", AEQ;
"assert", ASSERT;
"block", BLOCK;
"call", CALL;
"case", CASE;
"compile", COMPILE;
"dep", DEP;
"do", DO;
"else", ELSE;
"enum", ENUM;
"for", FOR;
"fst", FST;
"fun", FUN;
"get", GET;
"if", IF;
"in", IN;
"interpret", INTERPRET;
"is", IS;
"interface", INTERFACE;
"meth", METH;
"new", NEW;
"opaque", OPAQUE;
"purify", PURIFY;
"refl", REFL;
"snd", SND;
"struct", STRUCT;
"subst", SUBST;
"switch", SWITCH;
"test", TEST;
"tuple", TUPLE;
"type", TYPE;
"union", UNION;
"val", VAL;
"yield", YIELD;
] in
let tab = Hashtbl.create (List.length bindings) in
List.iter (fun (a, b) -> Hashtbl.add tab a b) bindings;
tab
let int_of_hex c =
match c with
| x when '0' <= x && x <= '9' -> (Char.code x) - (Char.code '0')
| x when 'a' <= x && x <= 'h' -> (Char.code x) - (Char.code 'a') + 10
| x when 'A' <= x && x <= 'H' -> (Char.code x) - (Char.code 'A') + 10
| _ -> raise Presupposition_error
let int64_of_hex_string str =
let open Int64 in
let result : int64 ref = ref zero in
for i = 0 to String.length str - 1 do
result := add (shift_left !result 4) (of_int (int_of_hex str.[i]))
done;
!result
let i64_of_string pos str =
try
Imm64(Int64.of_string str)
with
| Failure _ -> raise (Error(pos, Literal_overflow "i64"))
let i32_of_string pos str =
try
Imm32(Int32.of_string str)
with
| Failure _ -> raise (Error(pos, Literal_overflow "i32"))
let i16_of_string pos str =
try
let i = int_of_string str in
if i > 32767 || i < -32768 then raise (Failure "")
else Imm16(i)
with
| Failure _ -> raise (Error(pos, Literal_overflow "i16"))
let i8_of_string pos str =
try
let i = int_of_string str in
if i > 127 || i < -128 then raise (Failure "")
else Imm8(Char.chr i)
with
| Failure _ -> raise (Error(pos, Literal_overflow "i16"))
(* end of preamble *)
}
let newline = ('\010' | '\013' | "\013\010")
let blank = [' ' '\009' '\012']
let letter = ['a'-'z' 'A'-'Z']
let digit = ['0'-'9']
let hex = digit | ['a'-'h' 'A'-'H']
let identchar = letter | digit | '_'
let integer = '-'? digit+
rule token = parse
| "//"
{
comment lexbuf
}
| newline
{
incr_lineno lexbuf;
token lexbuf
}
| blank+
{
token lexbuf
}
| letter identchar*
| '_' identchar+ as s
{
try Hashtbl.find keyword_table s
with Not_found -> IDENT s
}
| "_" { BLANK }
| (integer as d) 'q' { IMM (i64_of_string (currpos lexbuf) d) }
| integer as d { IMM (i32_of_string (currpos lexbuf) d) }
| (integer as d) 's' { IMM (i16_of_string (currpos lexbuf) d) }
| (integer as d) 'c' { IMM (i8_of_string (currpos lexbuf) d) }
| "0x" (hex+ as d)
{
let p = int64_of_hex_string d in
IMM (
match String.length d with
| 2 -> Imm8 (Char.chr (Int64.to_int p))
| 4 -> Imm16 (Int64.to_int p)
| 8 -> Imm32 (Int64.to_int32 p)
| 16 -> Imm64 p
| _ -> raise (Error(currpos lexbuf, Invalid_hex))
)
}
| "&&" { AND_AND }
| "@" { AT }
| "!" { BANG }
| "!=" { BANG_EQ }
| "||" { BAR_BAR }
| "^^" { CARET_CARET }
| "::" { COLON_COLON }
| ":=" { COLON_EQ }
| ":" { COLON }
| "," { COMMA }
| "." { DOT }
| ".." { DOT_DOT }
| "=" { EQ }
| "==" { EQ_EQ }
| "===" { EQ_EQ_EQ }
| "=>" { EQ_GREATER }
| ">" { GREATER }
| ">=" { GREATER_EQ }
| "<" { LESS }
| "<=" { LESS_EQ }
| "-" { MINUS }
| "->" { MINUS_GREATER }
| "+" { PLUS }
| "++" { PLUS_PLUS }
| "?" { QUESTION }
| "??" { QUESTION_QUESTION }
| "'" { QUOTE }
| ";" { SEMI }
| "*" { STAR }
| "**" { STAR_STAR }
| "~" { TILDE }
(* Delimiter tokens *)
| "{" { LBRACE }
| "[" { LBRACKET }
| "(" { LPAREN }
| "}" { RBRACE }
| "]" { RBRACKET }
| ")" { RPAREN }
| eof { EOF }
| _ as c { raise (Error(currpos lexbuf, Illegal_character c)) }
and comment = parse
| newline
{
incr_lineno lexbuf;
token lexbuf
}
| _ { comment lexbuf }
| eof { EOF }