lexer_1.anubis
26.5 KB
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
_____ ___. .__ ________
/ _ \ ____ __ _\_ |__ |__| ______ \_____ \
/ /_\ \ / \| | \ __ \| |/ ___/ / ____/
/ | \ | \ | / \_\ \ |\___ \ / \
\____|__ /___| /____/|___ /__/____ > \_______ \
\/ The \/ Anubis \/ 2 \/ Project \/
Name of this file: lexer_1.anubis
Purpose of this file: The lexer of the Anubis 2 compiler.
Authors (Name [initials]): Alain Proute' [AP]
Updates ([initials] (date) comment):
[AP] (2007 jul 06) creation of this file
---------------------------------------------------------------------------------------
read tools/basis.anubis
read tools/streams.anubis
read types1.anubis
read dictionary_1.anubis
read memory_1.anubis
read preparser_1.apg.anubis
read types2.anubis
The role of the lexer is to cut up Anubis source texts into ``tokens''. The lexer also
has the responsability to skip comments and to handle 'read' paragraphs. The lexer is
called only by the parser, which may want to get the ``next token'' from the current
source.
The type 'Lexer' is defined in 'parser_1.apg.anubis'. Such a lexer is used by the
parser. The lexer needs to receive an input stream, and the dictionary of signs, which
is used for recognizing signs.
public define Lexer
make_lexer
(
Stream input,
Dictionary1(SignEntry,String) sign_dictionary
).
--- That's all for the public part ! --------------------------------------------------
The lexer needed by the parser (of type 'Lexer' defined in 'parser_1.apg.anubis') is
made of two functions:
(One -> Token_Value_preparser) read_token
(Token_Value_preparser -> One) unput_token
The first thing we need is a state for the lexer:
type InOffPar:
in, // we are within a paragraph
off. // we are off any paragraph
type LexerState:
lexer_state
(
Var(InOffPar),
Var(Position), // position of beginning of currently read token
String, // name of module (absolute path for a file)
Stream, // input stream
Var(List(Token_Value_preparser)), // unput stack
Dictionary1(SignEntry,String) // dictionary of all signs
).
This state contains:
- the indication that we are within a paragraph or not,
- the position of the first character of the token currently read,
- the name of the currently read module,
- the input stream for that module,
- the unput stack (a list of valued tokens),
- the whole state of the compiler (this allows access to dictionaries).
The next function generates a state for a new stream:
define LexerState
make_lexer_state
(
String module_name, // absolute_path for a file
Stream stream,
Dictionary1(SignEntry,String) sign_dict
) =
with pos_v = var(position(module_name,1,0)),
lexer_state
(
var(off),
pos_v,
module_name,
stream,
var([ ]),
sign_dict
).
*** [] Invisible sign.
Anubis 2 recognizes two invisible signs, which are:
- the ``white'' binary infix sign,
- the ``juxtaposition'' binary infix sign.
The white sign is made of a sequence of (at least one) white characters (spaces,
carriage return, line feed, and comments), and the juxtaposition sign is made of no
character at all. However, these signs are present in the source texts only if they
are between a ``final'' and an ``initial'' token (in this order).
Roughly speaking, a final token is a token which may be the last one in an expression.
An initial token is a token which may be the first one in an expression. Precisely,
this property is defined by the two functions 'is_final' and 'is_initial' below.
Examples:
f(a) juxtaposition between 'f' and '('
f (a) white between 'f' and '('
a + b no invisible sign
? x : A E white sign between 'A' and 'E' only
define Bool
is_initial
(
Token_Value_preparser tok
) =
if tok is
{
eof(_) then false,
_y_par_end(_) then false,
_y_par_begin(_) then false,
_y_parameter(_) then true,
_y_symbol(_) then true,
_y_decimal(_) then true,
_y_hexadecimal(_) then true,
_y_string(_) then true,
_y_forall(_) then true,
_y_iplus(_) then true,
_y_unary_keyword(_) then true,
_y_comma(_) then false,
_y_binary_keyword(_) then false,
_y_or(_) then false,
_y_and(_) then false,
_y_implies(_) then false,
_y_equals(_) then false,
_y_arrow(_) then false,
_y_plus(_) then false,
_y_dot(_) then false,
_y_caret(_) then false,
_y_white(_) then false,
_y_juxtapos(_) then false,
_y_minus(_) then true,
_y_star(_) then true,
_y_rbracket(_) then false,
_y_lbracket(_) then true,
_y_rbrace(_) then false,
_y_lbrace(_) then true,
_y_rpar(_) then false,
_y_lparcolon(_) then false,
_y_colonrpar(_) then false,
_y_lpar(_) then true
}.
define Bool
is_final
(
Token_Value_preparser tok
) =
if tok is
{
eof(_) then false,
_y_par_end(_) then false,
_y_par_begin(_) then false,
_y_parameter(_) then true,
_y_symbol(_) then true,
_y_decimal(_) then true,
_y_hexadecimal(_) then true,
_y_string(_) then true,
_y_forall(_) then false,
_y_iplus(_) then false,
_y_unary_keyword(_) then false,
_y_comma(_) then false,
_y_binary_keyword(_) then false,
_y_or(_) then false,
_y_and(_) then false,
_y_implies(_) then false,
_y_equals(_) then false,
_y_arrow(_) then false,
_y_plus(_) then false,
_y_dot(_) then false,
_y_caret(_) then false,
_y_white(_) then false,
_y_juxtapos(_) then false,
_y_minus(_) then false,
_y_star(_) then false,
_y_rbracket(_) then true,
_y_lbracket(_) then false,
_y_rbrace(_) then true,
_y_lbrace(_) then false,
_y_rpar(_) then true,
_y_lparcolon(_) then false,
_y_colonrpar(_) then false,
_y_lpar(_) then false
}.
*** [] Reading a token from a stream.
*** [] Lexical errors.
type LexicalError:
invalid_character (Position, Word8 character),
invalid_escape_sequence (Position, Word8 character_after_backslash),
run_away_string (Position, String beginning_of_string),
run_away_comment (Position),
unknown_sign (Position, String sign).
define Position
current_position
(
LexerState ls
) =
if ls is lexer_state(iop,sp,mn,in,us,st) then *sp.
define String
name
(
LexerState ls
) =
if ls is lexer_state(iop,sp,mn,in,us,st) then mn.
define Stream
input
(
LexerState ls
) =
if ls is lexer_state(iop,sp,mn,in,us,st) then in.
Recording the current position.
define One
record_position
(
LexerState ls,
) =
if ls is lexer_state(iop,sp,mn,in,us,st) then
sp <- position(mn,to_Nat(current_line(in)),to_Nat(current_column(in))).
Getting the unput stack.
define Var(List(Token_Value_preparser))
unput_stack
(
LexerState ls
) =
if ls is lexer_state(iop,sp,mn,in,us,st) then us.
*** [] Skipping blanks and comments.
Blank characters are defined as follows:
define Bool
is_blank
(
Word8 c
) =
member(c," \n\r\t").
*** [] Skipping to next paragraph (or end of input).
When the lexer is off any paragraph it may be asked to skip to next paragraph or end of
file.
What it does is skipping everything until one of the following happens:
- the end of file is reached. In this case, the 'eof' token is pushed on the token
unput stack.
- a non blank character is found in the leftmost column. In that case, the character
is pushed back into the input stream, and a 'par_begin' token is pushed on the token
unput stack.
define One
skip_to_next_paragraph
(
Var(InOffPar) iop,
Var(List(Token_Value_preparser)) unput_stack,
Stream input
) =
if read_byte(input) is
{
failure then unput_stack <- [eof(unique) . *unput_stack],
success(c) then
if current_column(input) = 1
then if is_blank(c)
then skip_to_next_paragraph(iop,unput_stack,input)
else (unput_byte(c,input);
unput_stack <- [_y_par_begin(unique) . *unput_stack];
iop <- in)
else skip_to_next_paragraph(iop,unput_stack,input)
}.
*** [] Skipping to the end of line (or end of input).
Lines may end by:
LF (Unix)
CR LF (Windows)
CR (MacIntosh)
define One
skip_to_end_of_line
(
Stream input
) =
if read_byte(input) is
{
failure then unique,
success(c) then
if c = '\n' /* LF */ then unique else
if c = '\r' /* CR */ then if read_byte(input) is
{
failure then unique,
success(d) then
if d = '\n'
then unique /* CR LF */
else unput_byte(d,input) /* CR alone */
} else
skip_to_end_of_line(input)
}.
*** [] Skipping slash-star comments.
These comments may be nested. This is why there is a nesting level. After the initial
slash-star, the level is 0 by convention.
define Result(LexicalError,One)
skip_slash_star_comment
(
Position pos,
Stream stream,
Int32 level
) =
if read_byte(stream) is
{
failure then error(run_away_comment(pos)),
success(c) then
if c = '/'
then if read_byte(stream) is
{
failure then error(run_away_comment(pos)),
success(d) then
if d = '*'
then skip_slash_star_comment(pos,stream,level+1)
else skip_slash_star_comment(pos,stream,level)
}
else if c = '*'
then if read_byte(stream) is
{
failure then error(run_away_comment(pos)),
success(d) then
if d = '/'
then if level = 0
then ok(unique)
else skip_slash_star_comment(pos,stream,level-1)
else skip_slash_star_comment(pos,stream,level)
}
else skip_slash_star_comment(pos,stream,level)
}.
*** [] Skipping to the next token.
define Result(LexicalError,One)
skip_to_next_token
(
String mod_name,
Stream stream
) =
if read_byte(stream) is
{
failure then ok(unique),
success(c) then
if is_blank(c) then skip_to_next_token(mod_name,stream) else
if c = '/' then // handling in paragraph comments
if read_byte(stream) is
{
failure then ok(unput_byte(c,stream)),
success(d) then
if d = '/'
then (skip_to_end_of_line(stream);
skip_to_next_token(mod_name,stream))
else if d = '*'
then if skip_slash_star_comment(
position(mod_name,
to_Nat(current_line(stream)),
to_Nat(current_column(stream))),
stream,0) is
{
error(msg) then error(msg),
ok(_) then skip_to_next_token(mod_name,
stream)
}
else (unput_byte(d,stream);
ok(unput_byte(c,stream)))
} else
ok(unput_byte(c,stream))
}.
*** [] Skipping all sorts of blanks and comments.
define Result(LexicalError,One)
skip_blanks_and_comments
(
String mod_name,
Var(InOffPar) iop,
Stream stream,
Var(List(Token_Value_preparser)) unput_stack
) =
if *iop is
{
in then skip_to_next_token(mod_name,stream),
off then ok(skip_to_next_paragraph(iop,unput_stack,stream))
}.
*** [] Reading a decimal notation.
define Maybe(DecimalDigit)
is_decimal_digit
(
Word8 c
) =
if c = '0' then success(_0) else
if c = '1' then success(_1) else
if c = '2' then success(_2) else
if c = '3' then success(_3) else
if c = '4' then success(_4) else
if c = '5' then success(_5) else
if c = '6' then success(_6) else
if c = '7' then success(_7) else
if c = '8' then success(_8) else
if c = '9' then success(_9) else
failure.
define Token_Value_preparser
read_decimal
(
Position pos,
Stream stream,
List(DecimalDigit) so_far
) =
if read_byte(stream) is
{
failure then _y_decimal((pos,reverse(so_far))),
success(c) then
if is_decimal_digit(c) is
{
failure then unput_byte(c,stream);
_y_decimal((pos,reverse(so_far))),
success(d) then read_decimal(pos,stream,[d . so_far])
}
}.
*** [] Reading an hexadecimal notation.
define Maybe(HexadecimalDigit)
is_hexadecimal_digit
(
Word8 c
) =
if c = '0' then success(_0) else
if c = '1' then success(_1) else
if c = '2' then success(_2) else
if c = '3' then success(_3) else
if c = '4' then success(_4) else
if c = '5' then success(_5) else
if c = '6' then success(_6) else
if c = '7' then success(_7) else
if c = '8' then success(_8) else
if c = '9' then success(_9) else
if c = 'a' then success(_a) else
if c = 'b' then success(_b) else
if c = 'c' then success(_c) else
if c = 'd' then success(_d) else
if c = 'e' then success(_e) else
if c = 'f' then success(_f) else
if c = 'A' then success(_a) else
if c = 'B' then success(_b) else
if c = 'C' then success(_c) else
if c = 'D' then success(_d) else
if c = 'E' then success(_e) else
if c = 'F' then success(_f) else
failure.
define Token_Value_preparser
read_hexadecimal
(
Position pos,
Stream stream,
List(HexadecimalDigit) so_far
) =
if read_byte(stream) is
{
failure then _y_hexadecimal((pos,reverse(so_far))),
success(c) then
if is_hexadecimal_digit(c) is
{
failure then unput_byte(c,stream);
_y_hexadecimal((pos,reverse(so_far))),
success(d) then read_hexadecimal(pos,stream,[d . so_far])
}
}.
*** [] Reading a symbol or a keyword.
define Bool
is_alpha
(
Word8 c
) =
('a' =< c & c =< 'z') |
('A' =< c & c =< 'Z') |
c = '_'.
define Token_Value_preparser
recognize_keyword
(
Position pos,
String text
) =
if text = "as" then _y_binary_keyword((pos,"as")) else
if text = "with" then _y_unary_keyword((pos,"with")) else
if text = "else" then _y_binary_keyword((pos,"else")) else
if text = "is" then _y_binary_keyword((pos,"is")) else
if text = "then" then _y_binary_keyword((pos,"then")) else
_y_symbol((pos,text)).
define Token_Value_preparser
read_symbol
(
Position pos,
Stream stream,
List(Word8) so_far
) =
if read_byte(stream) is
{
failure then recognize_keyword(pos,implode(reverse(so_far))),
success(c) then
if is_alpha(c)
then read_symbol(pos,stream,[c . so_far])
else unput_byte(c,stream);
recognize_keyword(pos,implode(reverse(so_far)))
}.
*** [] Reading a character string.
Extracting the beginning (n firsts characters) of a list (used for error messages).
define List($T)
firsts
(
Int32 n,
List($T) l
) =
if n =< 0 then [ ] else
if l is
{
[ ] then [ ],
[h . t] then [h . firsts(n-1,t)]
}.
define Result(LexicalError,Token_Value_preparser)
read_string
(
Position pos,
Stream s,
List(Word8) so_far
) =
if read_byte(s) is
{
failure then error(run_away_string(pos,implode(reverse(firsts(50,so_far)))+"...")),
success(c) then
if c = '\\'
then if read_byte(s) is
{
failure then error(run_away_string(pos,implode(reverse(firsts(50,so_far)))+"...")),
success(d) then
if d = 'r' then read_string(pos,s,['\r' . so_far]) else
if d = 'n' then read_string(pos,s,['\n' . so_far]) else
if d = 't' then read_string(pos,s,['\t' . so_far]) else
if d = '\\' then read_string(pos,s,['\\' . so_far]) else
if d = '\"' then read_string(pos,s,['\"' . so_far]) else
error(invalid_escape_sequence(pos,d))
}
else if c = '\"'
then ok(_y_string((pos,reverse(so_far))))
else read_string(pos,s,[c . so_far])
}.
*** [] Reading a sign.
Recognizing that a character may belong to a sign.
define Bool
is_sign_character
(
Word8 c
) =
member(c,"&~#'-|`\\^@+=$%*?.;/:!<>").
define Result(LexicalError,Token_Value_preparser)
recognize_sign
(
Position pos,
String sign,
Dictionary1(SignEntry,String) dict
) =
if get_entry(dict,sign) is
{
[ ] then error(unknown_sign(pos,sign)),
[s . _] then if s is sign_entry(_,token,_) then
if token is
{
_y_minus then ok(_y_minus((pos,sign))),
_y_and then ok(_y_and((pos,sign))),
_y_arrow then ok(_y_arrow((pos,sign))),
_y_caret then ok(_y_caret((pos,sign))),
_y_equals then ok(_y_equals((pos,sign))),
_y_star then ok(_y_star((pos,sign))),
_y_forall then ok(_y_forall((pos,sign))),
_y_implies then ok(_y_implies((pos,sign)))
}
}.
define Result(LexicalError,Token_Value_preparser)
read_sign
(
Position pos,
Stream s,
List(Word8) so_far,
Dictionary1(SignEntry,String) dict
) =
if read_byte(s) is
{
failure then recognize_sign(pos,implode(reverse(so_far)),dict),
success(c) then
if is_sign_character(c)
then read_sign(pos,s,[c . so_far],dict)
else unput_byte(c,s);
recognize_sign(pos,implode(reverse(so_far)),dict)
}.
*** [] Reading all sorts tokens.
The lexer must generate all alternatives of the type 'Token_Value_preparser'. The
alternatives which induce some special work are:
- eof(_) must be returned when end of file is reached
- end_of_par(_) returned when the token last returned is 'dot(_)' and two blank
lines have been seen or the end of file is reached
- decimal(_) the operand is a list which must be constructed (same for
hexadecimal(_) and string(_))
- white(_) and juxtapos(_) must be generated when appropriate
- in the case of signs, the sign must be recognized in order to be associated to its
model
- in the case of symbol(_), keywords must be recognized
define Result(LexicalError,Token_Value_preparser)
read_tok
(
LexerState ls
) =
if ls is lexer_state(iop,sp,mn,in,us,st) then
if skip_blanks_and_comments(ls) is
{
error(msg) then error(msg),
ok(_) then
if *us is
{
[ ] then
record_position(ls);
if read_byte(input(ls)) is
{
failure then ok(eof(unique)),
success(c) then
if is_decimal_digit(c) then ok(read_decimal(ls,[c])) else
if is_alpha(c) then ok(read_symbol(ls,[c])) else
if is_sign_character(c) then read_sign(ls,[c]) else
if c = '\"' then read_string(ls,[]) else
if c = '(' then ok(_y_lpar(current_position(ls))) else
if c = ')' then ok(_y_rpar(unique)) else
if c = '[' then ok(_y_lbracket(current_position(ls))) else
if c = ']' then ok(_y_rbracket(unique)) else
if c = '{' then ok(_y_lbrace(current_position(ls))) else
if c = '}' then ok(_y_rbrace(unique)) else
error(invalid_character(current_position(ls),c))
},
[h . t] then
us <-t;
ok(h)
}
}.
*** [] The lexer.
The lexer must remember if the last returned token is final or not, and if it has read
white characters or comments after this token.
type LastStatus: // status of last token read
non_final,
final_no_blank,
final_plus_blank.
When it reads the next token, it must
proceed as follows:
- the previous one was not final:
return the next token
- the previous one was final, and no white char read since:
if the next one is initial, return a juxtaposition sign and keep the next token,
otherwise return the next token,
- the previous one was final, with white chars read since:
if the next one is initial, return a white sign and keep the next token,
otherwise return the next token.
The lexer eventually keeps a token (simulates an 'unput'), and returns it at the next
call.
public define Lexer
make_lexer
(
Stream input
) =
with unput_stack = var((List(Token_Value_preparser))[]),
last_status = var((LastStatus)non_final),
lexer(make_read_token(unput_stack,last_status),
make_unput_token(unput_stack,last_status)).