/
opa_lexer.trx
326 lines (271 loc) · 9.4 KB
/
opa_lexer.trx
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
323
324
325
326
(*
Copyright © 2011, 2012 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(**
Utilities for lexing OPA.
Provides useful productions:
- [hexa]
- [spacing]
- [opacomment]
- [csscomment]
- [char]
- [string]
- [int], [strint]
- [float], [strfloat]
- [ml_identifier], [ml_identifier_nosp]
- [keywords]
*)
{{
open Parser_utils
}}
#
# One hexadecimal digit, lower or higher-case accepted
#
/* hexadecimal digit */
hexa <- [0-9A-Fa-f]:c {{ c }}
hexa_digit <- [0-9]:c {{ Char.code c - Char.code '0' }}
/ [A-F]:c {{ Char.code c - Char.code 'A' + 10 }}
/ [a-f]:c {{ Char.code c - Char.code 'a' + 10 }}
hexa4 <- hexa_digit:h1 hexa_digit:h2 hexa_digit:h3 hexa_digit:h4 {{
((h1 lsl 4 + h2) lsl 4 + h3) lsl 4 + h4
}}
hexa8 <- hexa4:h1 hexa4:h2 {{ h1 lsl 16 + h2 }}
#
# Whitespace management
#
#
maybe_whitespace <- [ \t]* $
whitespace <- [ \t]+ $
newline <- ("\r\n" $ / [\r\n] $)
strict_space <- whitespace $ / newline $
/* spacing */
space <- strict_space $
/* spacing */
spaces <- space* $
/* spacing */
spacing <- (space $ / opacomment$)* $
/* strict spacing */
strict_spacing<- (strict_space$ / opacomment$)+ $
spacing_nonl <- (whitespace$ / opacomment$)* $
#
# OPA Comments
#
#
(* opacomment_slash can succeed without parsing a final newline, because
* it happens if your final line is a comment and your file doesn't end with
* a newline *)
opacomment_slash <- "/" (!newline . $)* $
opacomment_star <- "*"
(!"*/" ("/" opacomment_star / . $))*
(=or_eof("*/")):b {{ if not b then error_comment (pos_only _filename _pos_beg _pos_end) }}
/* spacing */
opacomment <- "/" (opacomment_star / opacomment_slash)
#
# Characters and strings
# @TODO Support Unicode
#
stringnosp <-
[\"] stringchar*:sl (=or_eof([\"])):b
{{ if not b then error_string (pos_only _filename _pos_beg _pos_end) else String.concat "" sl }}
/* string literal */
string <- stringnosp
/* char literal */
char <- [\'] char_char [\'] {{ __2 }}
stringcharspecial <-
/ [{] {{ Char.code '{' }} / [}] {{ Char.code '}' }}
/ [n] {{ Char.code '\n' }} / [r] {{ Char.code '\r' }} / [t] {{ Char.code '\t' }}
/ [\'] {{ Char.code '\'' }} / [\"] {{ Char.code '\"' }} / [\\] {{ Char.code '\\' }}
/ (=deco([0-9]+)):p {{ let (i,pos) = p in
try Tgrammar.int_of_chars i
with
| Failure "int_of_string" -> error_char_overflow pos
}}
/ 'u' hexa4:i {{ i }}
/ 'U' hexa8:i {{ i }}
/ (=careful_deco(.)):p {{ error_char_escape (undecorate p) (label p) }}
; stringchar <-
[\\] stringcharspecial {{ Cactutf.cons __2 }}
/ (![\\\"{] utf8char)+:l {{ BaseString.concat_map "" Cactutf.cons l }}
char_char <-
[\\] stringcharspecial {{ __2 }}
/ utf8char
utf8char <- [\000-\127]:i1 {{ Cactutf.one_byte (Char.code i1) }}
/ [\192-\223]:i1 .:i2 {{ Cactutf.two_bytes (Char.code i1) (Char.code i2) }}
/ [\224-\239]:i1 .:i2 .:i3 {{ Cactutf.three_bytes (Char.code i1) (Char.code i2) (Char.code i3) }}
/ [\240-\255]:i1 .:i2 .:i3 .:i4 {{ Cactutf.four_bytes (Char.code i1) (Char.code i2) (Char.code i3) (Char.code i4) }}
#
# Numbers
#
/* integer */
decimal_number <- [0-9][0-9_]* $
binary_number <- [01][01_]* $
octal_number <- [0-7][0-7_]* $
hexadecimal_number <- hexa (hexa/[_])* $
strint <- [-]? ([0] [bB] binary_number $
/[0] [oO] octal_number $
/[0] [xX] hexadecimal_number $
/decimal_number $) $_
/* float */
exponent <- 'e'~ '-'? decimal_number $
strfloat <-
/ [-]? decimal_number [.] decimal_number? exponent? $_
/ [-]? [.] decimal_number exponent? $_
/ [-]? decimal_number exponent $_
strint <- [-]? ([0] [bB] binary_number $
/[0] [oO] octal_number $
/[0] [xX] hexadecimal_number $
/decimal_number $) $_
/* float */
exponent <- 'e'~ '-'? decimal_number $
strfloat <-
/ [-]? decimal_number [.] decimal_number? exponent? $_
/ [-]? [.] decimal_number exponent? $_
/ [-]? decimal_number exponent $_
int <- Opa_parser.pos:pos strint:s {{ try int_of_string s with Failure "int_of_string" -> error_int_overflow pos }}
float <- strfloat {{ float_of_string __1 }}
int3 <- ([2]([5][0-5] $_ / [0-4][0-9] $_) $_ / [0-1][0-9][0-9] $_ / [0-9][0-9]? $_):s {{ s }}
#
# CSS Comments
#
/* css comment */
csscomment <- "/*" (!"*/" .)* "*/" $
spacing_csscomment <- (spaces $ / csscomment)* $
#
# Symbols
#
larrow <- spacing larrow_nosp $
rarrow <- spacing rarrow_nosp $
lbrace <- spacing lbrace_nosp $
rbrace <- spacing rbrace_nosp $
lbracket <- spacing lbracket_nosp $
rbracket <- spacing rbracket_nosp $
colon <- spacing colon_nosp $
gtcolon <- spacing colon_nosp '>' $
comma <- spacing comma_nosp $
semic <- spacing semic_nosp $
slash <- spacing slash_nosp $
lpar <- spacing lpar_nosp $
rpar <- spacing rpar_nosp $
bquote <- spacing bquote_nosp $
quote <- spacing quote_nosp $
ampersand <- spacing ampersand_nosp $
underscore<- spacing underscore_nosp $
alias <- spacing alias_nosp $
dots <- spacing dots_nosp $
assign <- spacing assign_nosp $
sharp <- spacing sharp_nosp $
larrow_nosp <- "<-" $
rarrow_nosp <- "->" $
lbrace_nosp <- '{' $
rbrace_nosp <- '}' $
lbracket_nosp <- '[' $
rbracket_nosp <- ']' $
colon_nosp <- ':' $
comma_nosp <- ',' $
semic_nosp <- ';' $
slash_nosp <- '/' $
lpar_nosp <- '(' $
rpar_nosp <- ')' $
bquote_nosp <- '`' $
quote_nosp <- '\'' $
ampersand_nosp <- '&' $
underscore_nosp<- '_' $
alias_nosp <- "as" $
or_nosp <- "or" $
dots_nosp <- "..."$
assign_nosp <- '=' ![=] $
sharp_nosp <- '#' $
#
# Identifiers
#
/* value identifier */
ml_identifier <- spacing ml_identifier_nosp {{ __2 }}
ml_identifier_nosp <- not_a_keyword field_identifier_nosp {{ __2 }}
field_identifier <- spacing field_identifier_nosp {{ __2 }}
field_identifier_nosp <- ml_identifier_first_letter ml_following_letter* $_
/ arbitrary_identifier_nosp
/* type identifier */
type_identifier <- spacing type_identifier_nosp {{ __2 }}
type_identifier_nosp <- not_a_keyword ml_identifier_first_letter (ml_following_letter $/ '.' $)* $_
/ arbitrary_identifier_nosp
arbitrary_identifier <- spacing arbitrary_identifier_nosp {{ __2 }}
arbitrary_identifier_nosp <- bquote_nosp ((![`\n] .)* $_) bquote_nosp {{ __2 }}
ml_identifier_first_letter <- [a-zA-Z_]
ml_following_letter <- [a-zA-Z0-9_]
not_a_keyword <- !(keyword end_of_ident_nosp)
#
# Keywords
#
(* Don't replace string keyword by the corresponding rule, trx compile
time is multiply *)
keyword <- (
Opa_parser.declaration_directive0_keyword $_
/ "function" / "module"
/ "with" / "type" / "abstract"
/ "recursive" / "and"
/ "match" / "if" / "as" / "case" / "default" / "else"
/ "database"
/ "parser"
/ "_"
/ "css"
/ "server" / "client" / "exposed" / "protected" (* see directive without @ *)
)
(* Keywords : Bindings *)
OR <- (=Opa_lexer.exact_keyword("or"))
FUNCTION <- (=Opa_lexer.exact_keyword("function"))
MODULE <- (=Opa_lexer.exact_keyword("module"))
WITH <- (=Opa_lexer.exact_keyword("with"))
TYPE <- (=Opa_lexer.exact_keyword("type"))
(* Keywords : Recursive bindings *)
REC <- (=Opa_lexer.exact_keyword("recursive"))
AND <- (=Opa_lexer.exact_keyword("and"))
(* Keywords : Matching *)
MATCH <- (=Opa_lexer.exact_keyword("match"))
IF <- (=Opa_lexer.exact_keyword("if"))
AS <- (=Opa_lexer.exact_keyword("as"))
CASE <- (=Opa_lexer.exact_keyword("case"))
DEFAULT <- (=Opa_lexer.exact_keyword("default"))
ELSE <- (=Opa_lexer.exact_keyword("else"))
(* Keywords : Database *)
DATABASE <- (=Opa_lexer.exact_keyword("database"))
PARSER <- (=Opa_lexer.exact_keyword("parser"))
UNDERSCORE <- (=Opa_lexer.exact_keyword("_"))
CSS <- (=Opa_lexer.exact_keyword("css"))
#
# Not keywords but useful anyway
#
end_of_ident_nosp <- !ml_following_letter $
let exact_ident string = spacing not_a_keyword string:s end_of_ident_nosp {{ s }};
let exact_keyword string = spacing string:s end_of_ident_nosp {{ s }};
# not_a_keyword really useful ?
let exact_symbol string = spacing string:s ![%.+\-^*/<>=@|&!] {{ s }};
VOID = exact_ident "void";
OPEN = exact_ident "open";
BAR = exact_symbol "|";
END = exact_ident "end";
BEGIN = exact_ident "begin";
BYPASS <- "%%";
AT = exact_symbol "@";
#
# Utils
#
(* used to make precise error message on unterminated string, for instance *)
let or_eof rule = rule {{ true }}
/ !. {{ false }}
#
# Labelling stuff
#
let careful_deco rule = rule
{{ decorate _filename __1 _pos_beg _pos_end }}
;let deco rule = spacing (rule {{ decorate _filename __1 _pos_beg _pos_end}}) {{ __2 }}
;offset <- "" {{ _pos_beg }}