-
Notifications
You must be signed in to change notification settings - Fork 84
/
lexer.mll
711 lines (659 loc) · 23.4 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
{
open Types
open Parser
exception LexError of Range.t * string
(*
* The SATySFi lexer is stateful; the transitions are:
* | to \ from |program|block |inline|active | math |
* |-----------|-------|------|------|-------|--------|
* | program | ( ) | | | ( ) | !( ) |
* | | (| |) | | | (| |) | !(| |) |
* | | [ ] | | | [ ] | ![ ] |
* | block | '< > | < > | < > | < | !< > |
* | inline | { } | { } | { } | { | !{ } |
* | active | | +x ; | \x ; | | |
* | | | #x ; | #x ; | | |
* | math | ${ } | | ${ } | | { } |
*
* Note that the active-block and active-inline transitions are one-way.
*)
type lexer_state =
| ProgramState (* program mode *)
| VerticalState (* block mode *)
| HorizontalState (* inline mode *)
| ActiveState (* active mode *)
| MathState (* math mode *)
let get_pos lexbuf =
let posS = Lexing.lexeme_start_p lexbuf in
let posE = Lexing.lexeme_end_p lexbuf in
let fname = posS.Lexing.pos_fname in
let lnum = posS.Lexing.pos_lnum in
let cnumS = posS.Lexing.pos_cnum - posS.Lexing.pos_bol in
let cnumE = posE.Lexing.pos_cnum - posE.Lexing.pos_bol in
Range.make fname lnum cnumS cnumE
let report_error lexbuf errmsg =
let rng = get_pos lexbuf in
raise (LexError(rng, errmsg))
let pop lexbuf errmsg stack =
if Stack.length stack > 1 then
Stack.pop stack |> ignore
else
report_error lexbuf errmsg
let increment_line lexbuf =
begin
Lexing.new_line lexbuf;
end
let adjust_bol lexbuf amt =
let open Lexing in
let lcp = lexbuf.lex_curr_p in
lexbuf.lex_curr_p <- { lcp with pos_bol = lcp.pos_cnum + amt; }
let rec increment_line_for_each_break lexbuf str =
let len = String.length str in
let has_break = ref false in
let rec aux num tail_spaces prev =
if num >= len then tail_spaces else
begin
match (prev, String.get str num) with
| (Some('\r'), '\n') ->
aux (num + 1) (tail_spaces + 1) (Some('\n'))
| (_, (('\n' | '\r') as c)) ->
has_break := true;
increment_line lexbuf;
aux (num + 1) 0 (Some(c))
| _ ->
aux (num + 1) (tail_spaces + 1) None
end;
in
let amt = aux 0 0 None in
if !has_break then
adjust_bol lexbuf (-amt)
else
()
let initialize state =
let stack = Stack.create () in
Stack.push state stack;
stack
let reset_to_progexpr () =
initialize ProgramState
let reset_to_vertexpr () =
initialize VerticalState
let split_module_list tokstr =
let lst = String.split_on_char '.' tokstr in
match List.rev lst with
| ident :: mdllstrev -> (List.rev mdllstrev, ident)
| [] -> assert false
}
let space = [' ' '\t']
let break = ('\r' '\n' | '\n' | '\r')
let nonbreak = [^ '\n' '\r']
let nzdigit = ['1'-'9']
let digit = (nzdigit | "0")
let hex = (digit | ['A'-'F'])
let capital = ['A'-'Z']
let small = ['a'-'z']
let latin = (small | capital)
let item = "*"+
let identifier = (small (digit | latin | "-")*)
let constructor = (capital (digit | latin | "-")*)
let symbol = ( [' '-'@'] | ['['-'`'] | ['{'-'~'] )
let opsymbol = ( '+' | '-' | '*' | '/' | '^' | '&' | '|' | '!' | ':' | '=' | '<' | '>' | '~' | '\'' | '.' | '?' )
let str = [^ ' ' '\t' '\n' '\r' '@' '`' '\\' '{' '}' '<' '>' '%' '|' '*' '$' '#' ';']
let mathsymboltop = ('+' | '-' | '*' | '/' | ':' | '=' | '<' | '>' | '~' | '.' | ',' | '`')
let mathsymbol = (mathsymboltop | '?')
let mathascii = (small | capital | digit)
let mathstr = [^ '+' '-' '*' '/' ':' '=' '<' '>' '~' '.' ',' '`' '?' ' ' '\t' '\n' '\r' '\\' '{' '}' '%' '|' '$' '#' ';' '\'' '^' '_' '!' 'a'-'z' 'A'-'Z' '0'-'9']
rule progexpr stack = parse
| "%" {
comment lexbuf;
progexpr stack lexbuf
}
| ("@" (identifier as headertype) ":" (" "*) (nonbreak* as content) (break | eof)) {
let pos = get_pos lexbuf in
increment_line lexbuf;
match headertype with
| "require" -> HEADER_REQUIRE(pos, content)
| "import" -> HEADER_IMPORT(pos, content)
| "stage" ->
begin
match content with
| "persistent" -> HEADER_PERSISTENT0(pos)
| "0" -> HEADER_STAGE0(pos)
| "1" -> HEADER_STAGE1(pos)
| _ -> raise (LexError(pos, "undefined stage type '" ^ content ^ "'; should be 'persistent', '0', or '1'."))
end
| _ ->
raise (LexError(pos, "undefined header type '" ^ headertype ^ "'"))
}
| space { progexpr stack lexbuf }
| break {
increment_line lexbuf;
progexpr stack lexbuf
}
| "(" { Stack.push ProgramState stack; LPAREN(get_pos lexbuf) }
| ")" {
let pos = get_pos lexbuf in
pop lexbuf "too many closing" stack;
RPAREN(pos)
}
| "(|" { Stack.push ProgramState stack; BRECORD(get_pos lexbuf) }
| "|)" {
let pos = get_pos lexbuf in
pop lexbuf "too many closing" stack;
ERECORD(pos)
}
| "[" { Stack.push ProgramState stack; BLIST(get_pos lexbuf) }
| "]" {
let pos = get_pos lexbuf in
pop lexbuf "too many closing" stack;
ELIST(pos)
}
| ";" { LISTPUNCT(get_pos lexbuf) }
| "{" {
Stack.push HorizontalState stack;
skip_spaces lexbuf;
BHORZGRP(get_pos lexbuf)
}
| "'<" {
Stack.push VerticalState stack;
BVERTGRP(get_pos lexbuf)
}
| "${" {
Stack.push MathState stack;
BMATHGRP(get_pos lexbuf)
}
| "<[" { BPATH(get_pos lexbuf) }
| "]>" { EPATH(get_pos lexbuf) }
| ".." { PATHCURVE(get_pos lexbuf) }
| "--" { PATHLINE(get_pos lexbuf) } (* -- prior to BINOP_MINUS -- *)
| "`"+ {
let pos_start = get_pos lexbuf in
let quote_length = String.length (Lexing.lexeme lexbuf) in
let buffer = Buffer.create 256 in
let (pos_last, s, omit_post) = literal quote_length buffer lexbuf in
let pos = Range.unite pos_start pos_last in
LITERAL(pos, s, true, omit_post)
}
| ("#" ("`"+ as tok)) {
let pos_start = get_pos lexbuf in
let quote_length = String.length tok in
let buffer = Buffer.create 256 in
let (pos_last, s, omit_post) = literal quote_length buffer lexbuf in
let pos = Range.unite pos_start pos_last in
LITERAL(pos, s, false, omit_post)
}
| ("@" ("`"+ as tok)) {
let pos_start = get_pos lexbuf in
let quote_length = String.length tok in
let buffer = Buffer.create 256 in
let (pos_last, s, omit_post) = literal quote_length buffer lexbuf in
let pos = Range.unite pos_start pos_last in
if not omit_post then Logging.warn_number_sign_end pos_last;
match Range.get_last pos_start with
| None ->
assert false
| Some(last) ->
let (fname, ln, col) = last in
let ipos =
{
input_file_name = fname;
input_line = ln;
input_column = col;
}
in
POSITIONED_LITERAL(pos, ipos, s)
}
| ("\\" (identifier | constructor) "@") {
let tok = Lexing.lexeme lexbuf in HORZMACRO(get_pos lexbuf, tok)
}
| ("\\" (identifier | constructor)) {
let tok = Lexing.lexeme lexbuf in HORZCMD(get_pos lexbuf, tok)
}
| ("+" (identifier | constructor) "@") {
let tok = Lexing.lexeme lexbuf in VERTMACRO(get_pos lexbuf, tok)
}
| ("+" (identifier | constructor)) {
let tok = Lexing.lexeme lexbuf in VERTCMD(get_pos lexbuf, tok)
}
| "#" { ACCESS(get_pos lexbuf) }
| "->" { ARROW(get_pos lexbuf) }
| "<-" { OVERWRITEEQ(get_pos lexbuf) }
| "|" { BAR(get_pos lexbuf) }
| "_" { WILDCARD(get_pos lexbuf) }
| ":" { COLON(get_pos lexbuf) }
| "," { COMMA(get_pos lexbuf) }
| "::" { CONS(get_pos lexbuf) }
| "-" { EXACT_MINUS(get_pos lexbuf) }
| "=" { DEFEQ(get_pos lexbuf) }
| "*" { EXACT_TIMES(get_pos lexbuf) }
| "&" { EXACT_AMP(get_pos lexbuf) }
| "~" { EXACT_TILDE(get_pos lexbuf) }
(* -- binary operators; should be extended -- *)
| ("+" opsymbol*) { BINOP_PLUS(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ("-" opsymbol+) { BINOP_MINUS(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ("*" opsymbol+) { BINOP_TIMES(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ("/" opsymbol*) { BINOP_DIVIDES(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ("=" opsymbol+) { BINOP_EQ(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ("<" opsymbol*) { BINOP_LT(get_pos lexbuf, Lexing.lexeme lexbuf) }
| (">" opsymbol*) { BINOP_GT(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ("&" opsymbol+) { BINOP_AMP(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ("|" opsymbol+) { BINOP_BAR(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ("^" opsymbol*) { BINOP_HAT(get_pos lexbuf, Lexing.lexeme lexbuf) }
| "?" { OPTIONALTYPE(get_pos lexbuf) }
| "?->" { OPTIONALARROW(get_pos lexbuf) }
| "?:" { OPTIONAL(get_pos lexbuf) }
| "?*" { OMISSION(get_pos lexbuf) }
| ("!" opsymbol*) { UNOP_EXCLAM(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ("'" (identifier as xpltyvarnm)) { TYPEVAR(get_pos lexbuf, xpltyvarnm) }
| ((constructor ".")+ identifier) {
let tokstr = Lexing.lexeme lexbuf in
let pos = get_pos lexbuf in
let (mdlnmlst, varnm) = split_module_list tokstr in
VARWITHMOD(pos, mdlnmlst, varnm)
}
| identifier {
let tokstr = Lexing.lexeme lexbuf in
let pos = get_pos lexbuf in
match tokstr with
| "not" -> LNOT(pos)
| "mod" -> MOD(pos)
| "if" -> IF(pos)
| "then" -> THEN(pos)
| "else" -> ELSE(pos)
| "let" -> LETNONREC(pos)
| "let-rec" -> LETREC(pos)
| "and" -> LETAND(pos)
| "in" -> IN(pos)
| "fun" -> LAMBDA(pos)
| "true" -> TRUE(pos)
| "false" -> FALSE(pos)
| "before" -> BEFORE(pos)
| "while" -> WHILE(pos)
| "do" -> DO(pos)
| "let-mutable" -> LETMUTABLE(pos)
| "match" -> MATCH(pos)
| "with" -> WITH(pos)
| "when" -> WHEN(pos)
| "as" -> AS(pos)
| "type" -> TYPE(pos)
| "of" -> OF(pos)
| "module" -> MODULE(pos)
| "struct" -> STRUCT(pos)
| "sig" -> SIG(pos)
| "val" -> VAL(pos)
| "end" -> END(pos)
| "direct" -> DIRECT(pos)
| "constraint" -> CONSTRAINT(pos)
| "let-inline" -> LETHORZ(pos)
| "let-block" -> LETVERT(pos)
| "let-math" -> LETMATH(pos)
| "controls" -> CONTROLS(pos)
| "cycle" -> CYCLE(pos)
| "inline-cmd" -> HORZCMDTYPE(pos)
| "block-cmd" -> VERTCMDTYPE(pos)
| "math-cmd" -> MATHCMDTYPE(pos)
| "command" -> COMMAND(pos)
| "open" -> OPEN(pos)
| _ -> VAR(pos, tokstr)
}
| constructor { CONSTRUCTOR(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ((constructor as mdlnm) ".(") { Stack.push ProgramState stack; OPENMODULE(get_pos lexbuf, mdlnm) }
| (digit | (nzdigit digit+)) { INTCONST(get_pos lexbuf, int_of_string (Lexing.lexeme lexbuf)) }
| (("0x" | "0X") hex+) { INTCONST(get_pos lexbuf, int_of_string (Lexing.lexeme lexbuf)) }
| ((digit+ "." digit*) | ("." digit+)) { FLOATCONST(get_pos lexbuf, float_of_string (Lexing.lexeme lexbuf)) }
| (((("-"? digit) | ("-"? nzdigit digit+)) as i) (identifier as unitnm)) { LENGTHCONST(get_pos lexbuf, float_of_int (int_of_string i), unitnm) }
| ((("-"? digit+ "." digit*) as flt) (identifier as unitnm)) { LENGTHCONST(get_pos lexbuf, float_of_string flt, unitnm) }
| ((("-"? "." digit+) as flt) (identifier as unitnm)) { LENGTHCONST(get_pos lexbuf, float_of_string flt, unitnm) }
| eof {
let pos = get_pos lexbuf in
if Stack.length stack = 1 then EOI(pos) else
report_error lexbuf "text input ended while reading a program area"
}
| _ as c { report_error lexbuf ("illegal token '" ^ (String.make 1 c) ^ "' in a program area") }
and vertexpr stack = parse
| "%" {
comment lexbuf;
vertexpr stack lexbuf
}
| (break | space)* {
increment_line_for_each_break lexbuf (Lexing.lexeme lexbuf);
vertexpr stack lexbuf
}
| ("#" (identifier as varnm)) {
Stack.push ActiveState stack;
VARINVERT(get_pos lexbuf, [], varnm)
}
| ("#" (constructor ".")* (identifier | constructor)) {
let csnmpure = Lexing.lexeme lexbuf in
let csstr = String.sub csnmpure 1 ((String.length csnmpure) - 1) in
let (mdlnmlst, csnm) = split_module_list csstr in
Stack.push ActiveState stack;
VARINVERT(get_pos lexbuf, mdlnmlst, csnm)
}
| ("+" (identifier | constructor) "@") {
Stack.push ActiveState stack;
VERTMACRO(get_pos lexbuf, Lexing.lexeme lexbuf)
}
| ("+" (identifier | constructor)) {
Stack.push ActiveState stack;
VERTCMD(get_pos lexbuf, Lexing.lexeme lexbuf)
}
| ("+" (constructor ".")* (identifier | constructor)) {
let tokstrpure = Lexing.lexeme lexbuf in
let tokstr = String.sub tokstrpure 1 ((String.length tokstrpure) - 1) in
let (mdlnmlst, csnm) = split_module_list tokstr in
Stack.push ActiveState stack;
VERTCMDWITHMOD(get_pos lexbuf, mdlnmlst, "+" ^ csnm)
}
| "<" { Stack.push VerticalState stack; BVERTGRP(get_pos lexbuf) }
| ">" {
let pos = get_pos lexbuf in
pop lexbuf "too many closing" stack;
EVERTGRP(pos)
}
| "{" {
Stack.push HorizontalState stack;
skip_spaces lexbuf;
BHORZGRP(get_pos lexbuf)
}
| eof {
let pos = get_pos lexbuf in
if Stack.length stack = 1 then EOI(pos) else
report_error lexbuf "unexpected end of input while reading a vertical area"
}
| _ as c {
report_error lexbuf ("unexpected character '" ^ (String.make 1 c) ^ "' in a vertical area")
}
and horzexpr stack = parse
| "%" {
comment lexbuf;
skip_spaces lexbuf;
horzexpr stack lexbuf
}
| ((break | space)* "{") {
increment_line_for_each_break lexbuf (Lexing.lexeme lexbuf);
Stack.push HorizontalState stack;
skip_spaces lexbuf;
BHORZGRP(get_pos lexbuf)
}
| ((break | space)* "}") {
increment_line_for_each_break lexbuf (Lexing.lexeme lexbuf);
let pos = get_pos lexbuf in
pop lexbuf "too many closing" stack;
EHORZGRP(pos)
}
| ((break | space)* "<") {
increment_line_for_each_break lexbuf (Lexing.lexeme lexbuf);
Stack.push VerticalState stack;
BVERTGRP(get_pos lexbuf)
}
| ((break | space)* "|") {
increment_line_for_each_break lexbuf (Lexing.lexeme lexbuf);
skip_spaces lexbuf;
SEP(get_pos lexbuf)
}
| break {
increment_line lexbuf;
skip_spaces lexbuf;
BREAK(get_pos lexbuf)
}
| space {
skip_spaces lexbuf;
SPACE(get_pos lexbuf)
}
| ((break | space)* (item as itemstr)) {
increment_line_for_each_break lexbuf (Lexing.lexeme lexbuf);
skip_spaces lexbuf;
ITEM(get_pos lexbuf, String.length itemstr)
}
| ("#" (identifier as varnm)) {
Stack.push ActiveState stack;
VARINHORZ(get_pos lexbuf, [], varnm)
}
| ("#" (constructor ".")* (identifier | constructor)) {
let csnmpure = Lexing.lexeme lexbuf in
let csstr = String.sub csnmpure 1 ((String.length csnmpure) - 1) in
let (mdlnmlst, csnm) = split_module_list csstr in
Stack.push ActiveState stack;
VARINHORZ(get_pos lexbuf, mdlnmlst, csnm)
}
| ("\\" (identifier | constructor)) {
let tok = Lexing.lexeme lexbuf in
let rng = get_pos lexbuf in
Stack.push ActiveState stack;
HORZCMD(rng, tok)
}
| ("\\" (identifier | constructor) "@") {
let tok = Lexing.lexeme lexbuf in
let rng = get_pos lexbuf in
Stack.push ActiveState stack;
HORZMACRO(rng, tok)
}
| ("\\" (constructor ".")* (identifier | constructor)) {
let tokstrpure = Lexing.lexeme lexbuf in
let tokstr = String.sub tokstrpure 1 ((String.length tokstrpure) - 1) in
let (mdlnmlst, csnm) = split_module_list tokstr in
let rng = get_pos lexbuf in
Stack.push ActiveState stack;
HORZCMDWITHMOD(rng, mdlnmlst, "\\" ^ csnm)
}
| ("\\" symbol) {
let tok = String.sub (Lexing.lexeme lexbuf) 1 1 in
begin
CHAR(get_pos lexbuf, tok)
end
}
| "${" {
Stack.push MathState stack;
BMATHGRP(get_pos lexbuf)
}
| "`"+ {
let pos_start = get_pos lexbuf in
let quote_length = String.length (Lexing.lexeme lexbuf) in
let buffer = Buffer.create 256 in
let (pos_last, s, omit_post) = literal quote_length buffer lexbuf in
let pos = Range.unite pos_start pos_last in
LITERAL(pos, s, true, omit_post)
}
| ("#" ("`"+ as tok)) {
let pos_start = get_pos lexbuf in
let quote_length = String.length tok in
let buffer = Buffer.create 256 in
let (pos_last, s, omit_post) = literal quote_length buffer lexbuf in
let pos = Range.unite pos_start pos_last in
LITERAL(pos, s, false, omit_post)
}
| eof {
let pos = get_pos lexbuf in
if Stack.length stack = 1 then EOI(pos) else
report_error lexbuf "unexpected end of input while reading an inline text area"
}
| str+ {
let tok = Lexing.lexeme lexbuf in CHAR(get_pos lexbuf, tok)
}
| _ as c { report_error lexbuf ("illegal token '" ^ (String.make 1 c) ^ "' in an inline text area") }
and mathexpr stack = parse
| space { mathexpr stack lexbuf }
| break { increment_line lexbuf; mathexpr stack lexbuf }
| "%" {
comment lexbuf;
mathexpr stack lexbuf
}
| "?:" {
OPTIONAL(get_pos lexbuf)
}
| "?*" {
OMISSION(get_pos lexbuf)
}
| "!{" {
Stack.push HorizontalState stack;
skip_spaces lexbuf;
BHORZGRP(get_pos lexbuf);
}
| "!<" {
Stack.push VerticalState stack;
BVERTGRP(get_pos lexbuf)
}
| "!(" {
Stack.push ProgramState stack;
LPAREN(get_pos lexbuf)
}
| "![" {
Stack.push ProgramState stack;
BLIST(get_pos lexbuf)
}
| "!(|" {
Stack.push ProgramState stack;
BRECORD(get_pos lexbuf)
}
| "{" {
Stack.push MathState stack;
BMATHGRP(get_pos lexbuf)
}
| "}" {
let pos = get_pos lexbuf in
pop lexbuf "too many closing" stack;
EMATHGRP(pos)
}
| "|" { SEP(get_pos lexbuf) }
| "^" { SUPERSCRIPT(get_pos lexbuf) }
| "_" { SUBSCRIPT(get_pos lexbuf) }
| "'"+ { let n = String.length (Lexing.lexeme lexbuf) in PRIMES(get_pos lexbuf, n) }
| (mathsymboltop (mathsymbol*)) { MATHCHARS(get_pos lexbuf, Lexing.lexeme lexbuf) }
| mathascii { MATHCHARS(get_pos lexbuf, Lexing.lexeme lexbuf) }
| mathstr+ { MATHCHARS(get_pos lexbuf, Lexing.lexeme lexbuf) }
| ("#" (identifier as varnm)) {
VARINMATH(get_pos lexbuf, [], varnm)
}
| ("#" (constructor ".")* (identifier | constructor)) {
let csnmpure = Lexing.lexeme lexbuf in
let csstr = String.sub csnmpure 1 ((String.length csnmpure) - 1) in
let (mdlnmlst, csnm) = split_module_list csstr in
VARINMATH(get_pos lexbuf, mdlnmlst, csnm)
}
| ("\\" (identifier | constructor)) {
let csnm = Lexing.lexeme lexbuf in
MATHCMD(get_pos lexbuf, csnm)
}
| ("\\" (constructor ".")* (identifier | constructor)) {
let csnmpure = Lexing.lexeme lexbuf in
let csstr = String.sub csnmpure 1 ((String.length csnmpure) - 1) in
let (mdlnmlst, csnm) = split_module_list csstr in
MATHCMDWITHMOD(get_pos lexbuf, mdlnmlst, "\\" ^ csnm)
}
| ("\\" symbol) {
let tok = String.sub (Lexing.lexeme lexbuf) 1 1 in
MATHCHARS(get_pos lexbuf, tok)
}
| _ as c { report_error lexbuf ("illegal token '" ^ (String.make 1 c) ^ "' in a math area") }
| eof { report_error lexbuf "unexpected end of file in a math area" }
and active stack = parse
| "%" {
comment lexbuf;
active stack lexbuf
}
| space { active stack lexbuf }
| break { increment_line lexbuf; active stack lexbuf }
| "?:" { OPTIONAL(get_pos lexbuf) }
| "?*" { OMISSION(get_pos lexbuf) }
| "~" { EXACT_TILDE(get_pos lexbuf) }
| "(" {
Stack.push ProgramState stack;
LPAREN(get_pos lexbuf)
}
| "(|" {
Stack.push ProgramState stack;
BRECORD(get_pos lexbuf)
}
| "[" {
Stack.push ProgramState stack;
BLIST(get_pos lexbuf)
}
| "{" {
let pos = get_pos lexbuf in
pop lexbuf "BUG; this cannot happen" stack;
Stack.push HorizontalState stack;
skip_spaces lexbuf;
BHORZGRP(pos)
}
| "<" {
let pos = get_pos lexbuf in
pop lexbuf "BUG; this cannot happen" stack;
Stack.push VerticalState stack;
BVERTGRP(pos)
}
| ";" {
let pos = get_pos lexbuf in
pop lexbuf "BUG; this cannot happen" stack;
ENDACTIVE(pos)
}
| eof {
report_error lexbuf "unexpected end of input while reading an active area"
}
| _ {
let tok = Lexing.lexeme lexbuf in
report_error lexbuf ("unexpected token '" ^ tok ^ "' in an active area")
}
and literal quote_length buffer = parse
| "`"+ {
let tok = Lexing.lexeme lexbuf in
let len = String.length tok in
if len < quote_length then begin
Buffer.add_string buffer tok;
literal quote_length buffer lexbuf
end else if len > quote_length then
report_error lexbuf "literal area was closed with too many '`'s"
else
let pos_last = get_pos lexbuf in
(pos_last, Buffer.contents buffer, true)
}
| (("`"+ as tok) "#") {
let len = String.length tok in
if len < quote_length then begin
Buffer.add_string buffer (tok ^ "#");
literal quote_length buffer lexbuf
end else if len > quote_length then
report_error lexbuf "literal area was closed with too many '`'s"
else
let pos_last = get_pos lexbuf in
(pos_last, Buffer.contents buffer, false)
}
| break {
let tok = Lexing.lexeme lexbuf in
increment_line lexbuf;
Buffer.add_string buffer tok;
literal quote_length buffer lexbuf
}
| eof {
report_error lexbuf "unexpected end of input while reading literal area"
}
| _ {
let tok = Lexing.lexeme lexbuf in
Buffer.add_string buffer tok;
literal quote_length buffer lexbuf
}
and comment = parse
| break { increment_line lexbuf; }
| eof { () }
| _ { comment lexbuf }
and skip_spaces = parse
| break {
increment_line lexbuf;
skip_spaces lexbuf
}
| space {
skip_spaces lexbuf
}
| "%" {
comment lexbuf;
skip_spaces lexbuf
}
| "" { () }
{
let cut_token stack lexbuf =
match Stack.top stack with
| ProgramState -> progexpr stack lexbuf
| VerticalState -> vertexpr stack lexbuf
| HorizontalState -> horzexpr stack lexbuf
| ActiveState -> active stack lexbuf
| MathState -> mathexpr stack lexbuf
}