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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
(**
Lexer using sedlex
*)
open Tokens
open StdLabels
exception UnclosedQuote
exception LexError of string
exception EOF
(* Extract the location name from the pattern *)
let location_name = Str.regexp {|.* \(.*\)|}
(** Remove all the expression state when we are leaving the expression itself. *)
let rec leave_expression buffer =
match Lexbuf.state buffer with
| Some Lexbuf.Expression ->
Lexbuf.leave_state buffer;
leave_expression buffer
| _ -> ()
(** Try to read the identifier and check if this is a function, a keyword, or
just a variable.
See the tests [Syntax.Operator2] and [Syntax.Call Nl] for two cases. *)
let build_ident buffer =
let id = Lexbuf.content buffer |> String.uppercase_ascii in
try
let value = Hashtbl.find Idents.keyword_table id in
let _ =
match value with
| IF | ELIF -> Lexbuf.enter_state buffer Lexbuf.Expression
| _ -> ()
in
value
with Not_found ->
(* If the identifier does not match a keyword and start with [*], then
try it as a '*' operator. *)
if Char.equal '*' id.[0] then (
Lexbuf.rollback buffer;
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with '*' -> STAR | _ -> IDENT id)
else IDENT id
let wait_balance : (Buffer.t -> Lexbuf.t -> 'a) -> Lexbuf.t -> 'a =
fun rule lexbuf ->
let _, position = Lexbuf.positions lexbuf in
Lexbuf.set_start_position lexbuf position;
try
let token = rule (Buffer.create 256) lexbuf in
token
with Not_found -> raise UnclosedQuote
let space = [%sedlex.regexp? ' ' | '\t']
let eol = [%sedlex.regexp? '\r' | '\n' | "\r\n"]
let coma = [%sedlex.regexp? ',']
let digit = [%sedlex.regexp? '0' .. '9']
let letters = [%sedlex.regexp? 'a' .. 'z' | 'A' .. 'Z' | '_']
let ident = [%sedlex.regexp? Opt ('$' | '*'), letters, Star (digit | letters)]
let location_ident = [%sedlex.regexp? letters | digit]
let location_prefix = [%sedlex.regexp? '!' | '$' | '#' | '^']
let location = [%sedlex.regexp? Opt location_prefix, Plus location_ident]
let rec read_long_string ?(nested = false) level buf buffer =
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with
| "<<" ->
if not nested then (
match Buffer.length buf with
| 0 ->
Lexbuf.enter_state buffer Lexbuf.Token;
ENTER_EMBED
| _ ->
let result = Tokens.LITERAL (Buffer.contents buf) in
Buffer.reset buf;
Lexbuf.rollback buffer;
result)
else (
Buffer.add_string buf (Lexbuf.content buffer);
read_long_string ~nested level buf buffer)
| '{' ->
Buffer.add_string buf (Sedlexing.Utf8.lexeme lexbuf);
read_long_string ~nested (level + 1) buf buffer
| '}' -> (
match level with
| 0 ->
Lexbuf.leave_state buffer;
Lexbuf.enter_state buffer
(Lexbuf.EndString Lex_state.readLongStringWraper);
Lexbuf.rollback buffer;
LITERAL (Buffer.contents buf)
| _ ->
(* We have nested strings. Do not terminate end *)
Buffer.add_string buf (Sedlexing.Utf8.lexeme lexbuf);
read_long_string ~nested (level - 1) buf buffer)
| '\'' | '"' ->
Buffer.add_string buf (Lexbuf.content buffer);
read_long_string ~nested:true level buf buffer
| eol ->
Buffer.add_string buf (Lexbuf.content buffer);
read_long_string ~nested level buf buffer
| any ->
Buffer.add_string buf (Lexbuf.content buffer);
read_long_string ~nested level buf buffer
| _ -> raise Not_found
(** Read the text inside a ['] or ['"']
The function return the parsed string, but the closing token has been
rollbacked, leaving the state in [Lexbuf.EndString _].
The next call to [main] will call the associated function, effectively
leaving the string mode in the parser.
*)
let rec read_quoted_string : Lexbuf.stringWraper -> Lexbuf.buffer_builder =
fun f ?(nested = false) buf buffer ->
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with
| "<<" ->
if not nested then (
match Buffer.length buf with
| 0 ->
Lexbuf.enter_state buffer Lexbuf.Token;
ENTER_EMBED
| _ ->
let result = Tokens.LITERAL (Buffer.contents buf) in
Buffer.reset buf;
Lexbuf.rollback buffer;
result)
else (
Buffer.add_string buf (Lexbuf.content buffer);
(f.wrap (read_quoted_string f)) buf buffer)
| eol | any ->
Buffer.add_string buf (Lexbuf.content buffer);
(f.wrap (read_quoted_string f)) buf buffer
| _ -> raise Not_found
let rec skip_comment buffer =
(* Simplified way to skip the content of a string until the end marker.
(expect the string to be well formed) *)
let rec parse_until_end f =
let _ = wait_balance f buffer in
match Lexbuf.state buffer with
| Some Lexbuf.Token ->
Lexbuf.leave_state buffer;
parse_until_end f
| Some (Lexbuf.EndString _) -> ()
| _ -> parse_until_end f
in
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with
| '{' ->
parse_until_end (read_long_string 0);
let _ = Lex_state.readLongStringWraper.end_string buffer in
skip_comment buffer
| '\'' ->
parse_until_end
(Lex_state.quotedStringWraper.wrap
(read_quoted_string Lex_state.quotedStringWraper));
let _ = Lex_state.quotedStringWraper.end_string buffer in
skip_comment buffer
| '"' ->
parse_until_end
(Lex_state.dQuotedStringWraper.wrap
(read_quoted_string Lex_state.dQuotedStringWraper));
let _ = Lex_state.dQuotedStringWraper.end_string buffer in
skip_comment buffer
| eol ->
(* Ugly hack used in order to put the eol in the front of the next
parsing.
This is required because the eol is also a part of the syntax, and do
cannot be discard with the end of the comment. *)
Lexbuf.rollback buffer;
COMMENT
| any -> skip_comment buffer
| _ -> raise Not_found
(** Main lexer *)
let rec parse_token : Lexbuf.t -> token =
fun buffer ->
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with
| 0Xfeff ->
(* Ignore the BOM *)
parse_token buffer
| '#', Star space, location ->
(* Extract the location name *)
let ident = Lexbuf.content buffer in
let () =
match Str.string_match location_name ident 0 with
| false -> ()
| true -> Sedlexing.set_filename lexbuf (Str.matched_group 1 ident)
in
(* Restart the line number (new location here) *)
Lexbuf.start buffer;
LOCATION_START ident
| '-', Plus '-', Star (Sub (any, ('\r' | '\n'))) ->
leave_expression buffer;
LOCATION_END
| Plus digit -> INTEGER (Lexbuf.content buffer)
| '+' -> PLUS
| '-' -> MINUS
| "+=" -> INCR
| "-=" -> DECR
| "*=" -> MULT_EQUAL
| "/=" -> DIV_EQUAL
| '/' -> DIV
| '*' -> STAR
| ':' ->
(* We are leaving the block, the comment will be handled again *)
Lexbuf.leave_state buffer;
COLUMN
| '[' -> L_BRACKET
| ']' -> R_BRACKET
| '(' ->
Lexbuf.enter_state buffer Lexbuf.Expression;
L_PAREN
| ')' ->
Lexbuf.leave_state buffer;
R_PAREN
| ">>" ->
(* Leave the expression if we have any*)
leave_expression buffer;
(* Now leave the token mode and return to the string *)
Lexbuf.leave_state buffer;
LEAVE_EMBED
| '<' -> LT
| '>' -> GT
| coma -> COMA
| '=' ->
Lexbuf.enter_state buffer Lexbuf.Expression;
EQUAL
| ident -> build_ident buffer
| eol ->
leave_expression buffer;
EOL
| '&' ->
leave_expression buffer;
AMPERSAND
| '!' -> (
match Lexbuf.state buffer with
| Some Lexbuf.Expression -> EXCLAMATION
| _ -> skip_comment buffer)
| '}' -> TEXT_MARKER
| eof -> raise EOF
| _ ->
let tok = Lexbuf.content buffer in
let msg = Format.asprintf "Unexpected character %S" tok in
raise @@ LexError msg
let main buffer =
match Lexbuf.state buffer with
| Some (Lexbuf.String w) ->
wait_balance (w.wrap @@ read_quoted_string w) buffer
| Some (Lexbuf.MString level) -> wait_balance (read_long_string level) buffer
| Some (Lexbuf.EndString w) -> w.end_string buffer
| _ ->
let parser =
parse_token |> Lex_state.defaultWraper.start_string
|> Lexbuf.overlay buffer
in
parser buffer
let rec discard buffer =
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with
| '\'' -> (
match Lexbuf.state buffer with
| Some (Lexbuf.String _) | Some (Lexbuf.EndString _) ->
(* If we are inside a string, or just beforce closing it, close it.
*)
Lexbuf.leave_state buffer;
discard buffer
| _ ->
(* Otherwise wait skip until the end of the starting one *)
Lexbuf.enter_state buffer (Lexbuf.String Lex_state.quotedStringWraper);
(try
ignore
(read_quoted_string Lex_state.quotedStringWraper
(Buffer.create 16) buffer)
with e ->
Printexc.print_backtrace stdout;
raise e);
discard buffer)
| '"' -> (
match Lexbuf.state buffer with
| Some (Lexbuf.String _) | Some (Lexbuf.EndString _) ->
Lexbuf.leave_state buffer;
discard buffer
| _ ->
Lexbuf.enter_state buffer
(Lexbuf.String Lex_state.dQuotedStringWraper);
ignore
(read_quoted_string Lex_state.dQuotedStringWraper (Buffer.create 16)
buffer);
discard buffer)
| '}' -> (
match Lexbuf.state buffer with
| Some (Lexbuf.MString _) ->
Lexbuf.leave_state buffer;
discard buffer
| _ ->
ignore (read_long_string 0 (Buffer.create 16) buffer);
discard buffer)
| '{' ->
ignore (read_long_string 0 (Buffer.create 16) buffer);
Lexbuf.leave_state buffer;
discard buffer
| '-', Plus '-', Star (Sub (any, ('\r' | '\n'))) ->
leave_expression buffer;
()
| '!' ->
ignore @@ skip_comment buffer;
discard buffer
| any -> discard buffer
| _ -> raise EOF
|