-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsweet.g
1288 lines (1109 loc) · 49.1 KB
/
sweet.g
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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// BNF grammar for sweet-expressions, an easy-to-read S-expression notation.
// (c) 2012-2013 David A. Wheeler, released under "MIT" license.
// This BNF is an LL(1) grammar, written using ANTLR version 3.
// More info on ANTLR is at "http://www.antlr.org/".
// It is written to be easy to translate to a recursive-descent parser,
// even one that doesn't do full tokenizing.
// The actions in "sweet.g" are written inside {...} using Java syntax,
// but invoke normal Scheme procedures (e.g., "car" and "cons") as follows:
// +------------------|------------|------------------------------------------+
// | Action Construct | Scheme | Discussion |
// |------------------|------------|------------------------------------------|
// | cons(x, y) | (cons x y) | Java syntax used for procedure calls |
// | nullp(x) | (null? x) | "?" in Scheme procedure name becomes "p" |
// | null | '() | Java null represents the empty list |
// | "somename" | 'somename | Java strings represent atoms |
// +------------------|------------|------------------------------------------+
// The program "schemify" translates these actions into Scheme, assuming
// they meet certain formatting rules.
// This BNF specifically shows how non-indent whitespace is handled
// (SRFI-49's BNF is simpler but isn't specific about this, making it
// harder to interpret). Note that this BNF presumes there's a preprocessor
// that does indent processing. While indent processing is enabled, indents
// are marked with INDENT and dedents with DEDENT (one for each dedent).
// Lines with ONLY indent characters are considered blank lines.
// There is no "SAME" token to show "same indent level", but some rules
// use an empty "same" nonterminal to emphasize their lack of INDENT/DEDENT.
// Bad indentation emits BADDENT, which is never accepted.
// If there's an indent when there is NO preceding un-indented line, the
// preprocessor emits INITIAL_INDENT; this
// does *NOT* change the indent level (this is so initial indents
// will disable indentation processing, but only on that line).
// TODO:
// - (Maybe) Handle EOF in weird places.
grammar sweet;
options { k = 1; } // Force grammar to be LL(1).
// LEXER SECTION
tokens {
BADDENT; // Generated by indent processor, always illegal.
}
// ANTLR v3 does not allow the parser to communicate to the lexer
// (the lexer runs completely before the parser begins).
// The lexer *can* store and use state, which we have to do anyway to
// do indentation processing if we're going to do indentation processing
// inside the lexer. Thus, in this implementation, the lexer
// tracks the parentheses enclosure level, so that \n is just a delimiter
// inside (...) but is handled specially in indentation processing,
// while symbols like "$" are just ordinary atoms inside (...).
// The lexer must also specially note "<*" and "*>" and specially modify
// the indentation levels when those are received.
// This ANTLR lexer does indentation processing, so it generates
// INDENT, DEDENT, and so on as appropriate.
@lexer::header {
import java.util.Deque;
}
@lexer::members {
// Track enclosure level. "(" etc. adds 1, ")" etc. subtracts:
long enclosure = 0;
Boolean initial_indent = false;
// Are we doing indent processing?:
private Boolean indent_processing() {
return (enclosure == 0) && !initial_indent;
}
// Permit sending multiple tokens per rule, per ANTLR FAQ.
// This is necessary to support DEDENT, as a single token may cause
// the generation of multiple DEDENTs.
Deque<Token> tokens = new java.util.ArrayDeque<Token>();
@Override
public void emit(Token token) {
state.token = token;
tokens.addLast(token);
}
@Override
public Token nextToken() {
super.nextToken();
if (tokens.isEmpty())
return Token.EOF_TOKEN;
return tokens.removeFirst();
}
// This stack records the string indents; use push, pop, peek.
Deque<String> indents = new java.util.ArrayDeque<String>();
private Boolean outside_t_expr() {
return indent_processing() &&
(indents.isEmpty() || indents.peek().equals(""));
}
private Boolean indentation_greater_than(String indent1, String indent2) {
int len1 = indent1.length();
int len2 = indent2.length();
return (len1 > len2) && indent2.equals(indent1.substring(0, len2));
}
private void process_indent(String indent_text, Token t) {
if (indents.isEmpty()) indents.push("");
if (indents.peek().equals(indent_text)) { // no-op, aka "same"
} else if (indentation_greater_than(indent_text, indents.peek())) {
// System.out.print("Generate INDENT\n");
t.setType(INDENT);
emit(t);
indents.push(indent_text);
} else if (indentation_greater_than(indents.peek(), indent_text)) {
while (!indents.isEmpty() &&
indentation_greater_than(indents.peek(), indent_text)) {
indents.removeFirst(); // drop
// System.out.print("Generate DEDENT(s)\n");
t.setType(DEDENT);
emit(t);
}
if (!indents.peek().equals(indent_text)) {
// System.out.print("Generate BADDENT(s)\n");
t.setType(BADDENT);
emit(t);
}
} else {
// System.out.print("Generate BADDENT\n");
t.setType(BADDENT);
emit(t);
}
}
private void process_initial_indent(String indent_text, Token t) {
t.setType(INITIAL_INDENT);
emit(t);
initial_indent = true;
}
private void restart_indent_level() {
if (indents.isEmpty()) indents.push(""); // Initialize if needed
indents.push(""); // Let's start over!
}
private void emit_type(int token_type) {
CommonToken extra = new CommonToken(input, Token.INVALID_TOKEN_TYPE,
Token.DEFAULT_CHANNEL, getCharIndex(), getCharIndex()-1);
extra.setLine(getLine());
extra.setCharPositionInLine(getCharPositionInLine());
extra.setType(token_type);
emit(extra);
}
private void process_collecting_end(Token t) {
// Must send independent EOL token for ANTLR.
emit_type(EOL);
/* Dedent (if needed) */
CommonToken dedent_it = new CommonToken(input, Token.INVALID_TOKEN_TYPE,
Token.DEFAULT_CHANNEL, getCharIndex(), getCharIndex()-1);
dedent_it.setLine(getLine());
dedent_it.setCharPositionInLine(getCharPositionInLine());
process_indent("", dedent_it); // Emit any dedents needed.
t.setType(COLLECTING_END);
emit(t);
// Restore original indent stack.
indents.removeFirst();
}
}
@parser::header {
import scheme.*;
import static scheme.Pair.*;
}
@parser::members {
// This is a bogus variable to work around an ANTLR bug. ANTLR by default
// is greedy, but warns on ambiguities unless "greedy=true", e.g.:
// part1 (options {greedy = true;} : hspace)* part2
// But there's no greedy variable defined for the parser, so you can't use
// the feature properly. Here we create a settable variable, so you can
// selectively disable unnecessary warnings. If a later ANTLR also
// defines this variable, then remove it:
public Boolean greedy = true;
private void generate_eof() {
System.exit(0);
}
// These similate basic Lisp/Scheme procedures:
public static Object cadr(Pair x) {
return car( (Pair) cdr(x));
}
public static Object cddr(Pair x) {
return cdr( (Pair) cdr(x));
}
public static Boolean nullp(Object x) { // Scheme "null?"
return x == null;
}
public static Boolean pairp(Object x) { // Scheme "pair?"
return x instanceof Pair;
}
public static Boolean equalp(Object x, Object y) { // Scheme "equal?"
if ((x == null) && (y == null)) {
return true;
} else if ((x == null) || (y == null)) {
return false ;
} else if (pairp(x) && pairp(y)) {
Pair px = (Pair) x;
Pair py = (Pair) y;
return equalp(car(px), car(py)) && equalp(cdr(px), cdr(py));
} else if (pairp(x) || pairp(y)) {
return false;
} else {
return x.equals(y);
}
}
// This does not check for cycles
private static Boolean ends_in_null(Pair x) {
if (nullp(cdr(x))) {
return true;
} else if (! pairp(cdr(x))) {
return false;
} else {
return ends_in_null( (Pair) cdr(x));
}
}
public static Boolean listp(Object x) { // Scheme "list?"
if (nullp(x)) return false;
else if (! pairp(x)) return false;
else return ends_in_null( (Pair) x);
}
public static Pair list(Object x) {
return cons(x, null);
}
public static Pair list(Object x, Object y) {
return cons(x, list(y));
}
// "y" really needs to be a Pair, but that makes it harder to
// to work with ANTLR
public static Object append(Object x, Object y) {
if (x == null) {
return y;
} else if (x instanceof Pair) {
Pair p = (Pair) x;
return cons(car(p), append(cdr(p), y));
} else {
throw new Error(); // for now.
}
}
private static String string_datum_tail(Object x) {
if (x == null) {
return ")";
} else if (x instanceof Pair) {
Pair p = (Pair) x;
return " " + string_datum(car(p)) + string_datum_tail(cdr(p));
} else
return " . " + string_datum(x) + ")";
}
public static String string_datum(Object x) {
if (x == null) {
return "()";
} else if (x instanceof Pair) {
Pair p = (Pair) x;
return "(" + string_datum(car(p)) + string_datum_tail(cdr(p));
} else {
return x.toString();
}
}
// Utility declarations and functions
// Special "empty" object to represent empty value, i.e., "no value at all".
// Scheme: (define empty-value (string-copy "empty-value"))
// Common Lisp: (defconstant empty-value (make-symbol "empty-value"))
public static Object empty_value = new Pair(null, null);
// (define (isemptyvaluep x) (eq? x empty_value))
public static Boolean isemptyvaluep(Object x) {
return (x == empty_value);
}
// (define (not_period_and_not_empty x)
// (and (not (isperiodp x)) (not (isemptyvaluep x))))
public static Boolean not_period_and_not_empty(Object x) {
return (!isperiodp(x)) && (!isemptyvaluep(x));
}
// (define (conse x y) ; cons, but handle "empty" values
// (cond
// ((eq? y empty_value) x)
// ((eq? x empty_value) y)
// (#t (cons x y))))
public static Object conse(Object x, Object y) {
if (y == empty_value) {
return x;
} else if (x == empty_value) {
return y;
} else {return cons(x, y);}
}
// (define (appende x y) ; append, but handle "empty" values
// (cond
// ((eq? y empty_value) x)
// ((eq? x empty_value) y)
// (#t (append y))))
public static Object appende(Object x, Object y) {
if (y == empty_value) {
return x;
} else if (x == empty_value) {
return y;
} else {return append(x, y);}
}
// (define (list1e x) ; list, but handle "empty" values
// (if (eq? x empty_value)
// '()
// (list x)))
public static Object list1e(Object x) {
if (x == empty_value) {
return null;
} else {return list(x);}
}
// (define (list2e x y) ; list, but handle "empty" values
// (if (eq? x empty_value)
// y
// (if (eq? y empty_value)
// x
// (list x y))))
public static Object list2e(Object x, Object y) {
if (x == empty_value) {
return list1e(y);
} else if (y == empty_value) {
return list1e(x);
} else {return list(x,y);}
}
// ; If x is a 1-element list, return (car x), else return x
// (define (monify x)
// (cond
// ((not (pair? x)) x)
// ((null? (cdr x)) (car x))
// (#t x)))
public static Object monify(Object x) {
if (! pairp(x)) {
return x;
} else if (nullp(cdr( (Pair) x))) {
return car( (Pair) x);
} else {return x;}
}
// This isn't an accurate implementation of "if" because it always
// evaluates the t_branch AND f_branch. But if t_branch and f_branch
// have no side-effects, it's good enough.
public static Object ifp(Boolean b, Object t_branch, Object f_branch) {
if (b) return t_branch;
else return f_branch;
}
// Is the parameter v a representation of the symbol "."?
public static Boolean isperiodp(Object v) {
if (!(v instanceof String))
return false;
else
return ((String) v).equals(".");
}
// ; --------------------------------------------------------
// ; Curly-infix support procedures - converted from SRFI-105
// ; --------------------------------------------------------
// ; Return true if lyst has an even # of parameters, and the (alternating)
// ; first parameters are "op". Used to determine if a longer lyst is infix.
// ; If passed empty list, returns true (so recursion works correctly).
// (define (even-and-op-prefix? op lyst)
// (cond
// ((null? lyst) #t)
// ((not (pair? lyst)) #f)
// ((not (equal? op (car lyst))) #f) ; fail - operators not the same
// ((not (pair? (cdr lyst))) #f) ; Wrong # of parameters or improper
// (#t (even-and-op-prefix? op (cddr lyst))))) ; recurse.
public static Boolean even_and_op_prefixp(Object op, Object lyst) {
if (nullp(lyst)) {
return true;
} else if (! pairp(lyst)) {
return false;
} else if (! equalp(op, car( (Pair) lyst))) {
return false;
} else if (! pairp( cdr( (Pair) lyst))) {
return false;
} else {
return even_and_op_prefixp(op, cddr( (Pair) lyst));
}
}
// ; Return true if the lyst is in simple infix format
// ; (and thus should be reordered at read time).
// (define (simple-infix-list? lyst)
// (and
// (pair? lyst) ; Must have list; '() doesn't count.
// (pair? (cdr lyst)) ; Must have a second argument.
// (pair? (cddr lyst)) ; Must have a third argument (we check it
// ; this way for performance)
// (even-and-op-prefix? (cadr lyst) (cdr lyst)))) ; true if rest simple
public static Boolean simple_infix_listp(Object lyst) {
return pairp(lyst)
&& pairp(cdr((Pair) lyst))
&& pairp(cddr((Pair) lyst))
&& even_and_op_prefixp(cadr((Pair) lyst), cdr((Pair) lyst));
}
// ; Return alternating parameters in a lyst (1st, 3rd, 5th, etc.)
// (define (alternating-parameters lyst)
// (if (or (null? lyst) (null? (cdr lyst)))
// lyst
// (cons (car lyst) (alternating-parameters (cddr lyst)))))
public static Pair alternating_parameters(Pair lyst) {
if (nullp(lyst) || nullp(cdr(lyst))) {
return lyst;
} else {
return cons(car(lyst), alternating_parameters((Pair) cddr(lyst)));
}
}
// ; Not a simple infix list - transform it. Written as a separate procedure
// ; so that future experiments or SRFIs can easily replace just this piece.
// Handle dollar sign specially for ANTLR.
public static Pair transform_mixed_infix(Pair lyst) {
return cons("$" + "nfx" + "$", lyst);
}
// ; Given curly-infix lyst, map it to its final internal format.
// (define (process-curly lyst)
// (cond
// ((not (pair? lyst)) lyst) ; E.G., map {} to ().
// ((null? (cdr lyst)) ; Map {a} to a.
// (car lyst))
// ((and (pair? (cdr lyst)) (null? (cddr lyst))) ; Map {a b} to (a b).
// lyst)
// ((simple-infix-list? lyst) ; Map {a OP b [OP c...]} to (OP a b [c...])
// (cons (cadr lyst) (alternating-parameters lyst)))
// (#t (transform-mixed-infix lyst))))
public static Object process_curly(Object lyst) {
if (! pairp(lyst)) {
return lyst;
} else {
Pair plyst = (Pair) lyst;
if (nullp(cdr(plyst))) {
return car(plyst); // Map {a} to a.
} else if (pairp(cdr(plyst)) && nullp(cddr(plyst))) {
return plyst; // Map {a b} to (a b).
} else if (simple_infix_listp(plyst)) {
return cons(cadr(plyst), alternating_parameters(plyst));
} else {
return transform_mixed_infix(plyst);
}
}
}
}
// Define start symbol for parser (rest of parser is later).
start : t_expr
{System.out.print(string_datum($t_expr.v) + "\n"); } ;
// LEXER SECTION.
// Lexical token (terminal) names are in all upper case
SPACE : ' ';
TAB : '\t';
// INCLUDE IN SRFI
PERIOD : '.';
// STOP INCLUDING IN SRFI
// Special markers, which only have meaning outside (), [], {}.
GROUP_SPLIT : {indent_processing()}? => '\\' '\\'; // GROUP/split symbol.
SUBLIST : {indent_processing()}? =>'$';
COLLECTING : {indent_processing()}? => '<*' { restart_indent_level(); } ;
// This generates EOL + (any DEDENTs ) + COLLECTING_END, and restores indents:
COLLECTING_END : {indent_processing()}? => t='*>' {process_collecting_end($t);};
RESERVED_TRIPLE_DOLLAR : {indent_processing()}? => '$$$'; // Reserved.
// Abbreviations followed by certain whitespace are special:
QUOTEW : {indent_processing()}? => '\'' (SPACE | TAB) ;
QUASIQUOTEW : {indent_processing()}? => '\`' (SPACE | TAB) ;
UNQUOTE_SPLICEW : {indent_processing()}? => ',@' (SPACE | TAB) ;
UNQUOTEW : {indent_processing()}? => ',' (SPACE | TAB) ;
DATUM_COMMENTW : {indent_processing()}? => '#;' (SPACE | TAB) ;
// Abbreviations followed by EOL also generate abbrevW:
QUOTE_EOL : {indent_processing()}? => '\'' EOL_SEQUENCE
SPECIAL_IGNORED_LINE* i=INDENT_CHARS_PLUS
{emit_type(QUOTEW); emit_type(EOL);
process_indent($i.text, $i);};
QUASIQUOTE_EOL : {indent_processing()}? => '\`' EOL_SEQUENCE
SPECIAL_IGNORED_LINE* i=INDENT_CHARS_PLUS
{emit_type(QUASIQUOTEW); emit_type(EOL);
process_indent($i.text, $i);};
UNQUOTE_SPLICE_EOL: {indent_processing()}? => ',@' EOL_SEQUENCE
SPECIAL_IGNORED_LINE* i=INDENT_CHARS_PLUS
{emit_type(UNQUOTE_SPLICEW); emit_type(EOL);
process_indent($i.text, $i);};
UNQUOTE_EOL : {indent_processing()}? => ',' EOL_SEQUENCE
SPECIAL_IGNORED_LINE* i=INDENT_CHARS_PLUS
{emit_type(UNQUOTEW); emit_type(EOL);
process_indent($i.text, $i);};
DATUM_COMMENT_EOL: {indent_processing()}? => '#;' EOL_SEQUENCE
SPECIAL_IGNORED_LINE* i=INDENT_CHARS_PLUS
{emit_type(DATUM_COMMENTW); emit_type(EOL);
process_indent($i.text, $i);};
// Abbreviations not followed by horizontal space or EOL are ordinary:
QUOTE : '\'';
QUASIQUOTE : '\`';
UNQUOTE_SPLICE : ',@';
UNQUOTE : ',';
fragment EOL_CHAR : '\n' | '\r' ;
fragment NOT_EOL_CHAR : (~ (EOL_CHAR));
fragment NOT_EOL_CHARS : NOT_EOL_CHAR*;
// INCLUDE IN SRFI
// Comments. LCOMMENT=line comment, scomment=special comment.
// SRFI_22_COMMENT and SHARP_BANG_FILE support is RECOMMENDED.
LCOMMENT : ';' NOT_EOL_CHARS ; // Line comment - doesn't include EOL
BLOCK_COMMENT : '#|' (options {greedy=false;} : (BLOCK_COMMENT | .))* '|#' ;
DATUM_COMMENT : '#;' ;
// STOP INCLUDING IN SRFI
// SRFI-105 notes that "implementations could trivially support
// (simultaneously) markers beginning with #! followed by a letter
// (such as the one to identify support for curly-infix-expressions),
// the SRFI-22 #!+space marker as an ignored line, and the
// format #!/ ... !# and #!. ... !# as a multi-line comment."
// We'll implement that approach for maximum flexibility.
// INCLUDE IN SRFI
SRFI_22_COMMENT : '#!' SPACE NOT_EOL_CHARS ;
SHARP_BANG_FILE : '#!' ('/' | '.') (options {greedy=false;} : .)* '!#' ;
// These match #!fold-case, #!no-fold-case, #!sweet, and #!curly-infix:
SHARP_BANG_DIRECTIVE : '#!' ('a'..'z'|'A'..'Z'|'_')
('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'-')* ;
// STOP INCLUDING IN SRFI
fragment EOL_SEQUENCE : ('\r' '\n'? | '\n');
// Specially handle formfeed (\f) and vertical tab (\v).
// We support lone formfeeds on a line to support the GNU Coding Standards
// (http://www.gnu.org/prep/standards/standards.html), which says:
// "Please use formfeed characters (control-L) to divide the program
// into pages at logical places (but not within a function)...
// The formfeeds should appear alone on lines by themselves."
// Some argue against vertical tabs (http://prog21.dadgum.com/76.html).
// Any FF and VT must be at the beginning of a line, must be followed by
// EOL_SEQUENCE, and they terminate a t-expression.
FF : '\f' {if (enclosure==0) initial_indent = true;} ; // Formfeed, \u000c
VT : '\u000b' {if (enclosure==0) initial_indent = true;} ; // Vertical tab, \v
// Various forms of comments - line comments and special comments.
// We'll specifically ignore lines that begin with ";"
LCOMMENT_LINE : {(getCharPositionInLine() == 0)}? =>
';' contents=NOT_EOL_CHARS EOL_SEQUENCE
{
// We can't directly use the ANTLR Lexer to generate ";" comments,
// because the lexer doesn't run synchronously with the parser.
// That's no problem, because the spec is designed to allow that
// to be handled separately and not here.
// if (outside_t_expr()) System.out.println(";" + $contents.text);
skip();
};
// The following implements INDENT/DEDENT tokens, semi-similar to Python in
// http://docs.python.org/2/reference/lexical_analysis.html#indentation
// Page 95 of "The Definitive ANTLR Reference" has a code outline for
// generating INDENT and DEDENT, but it is fundamentally wrong for us.
// That only acts when there is 1+ indent characters on a line, so it
// cannot implement all the DEDENTs necessary when it encounters a blank line.
// End-of-line (EOL) is extremely special in sweet-expressions.
// After reading it, we'll need to read in any following indent characters
// (if indent processing is active) to determine if have an INDENT or DEDENTs.
// As part of tokenizing, we'll consume any following lines that
// are ;-only lines, and treat indent-only lines equivalent to blank lines.
fragment SPECIAL_IGNORED_LINE
: (' ' | '\t' | '!' )* ';' NOT_EOL_CHARS EOL_SEQUENCE ;
fragment INDENT_CHAR : (' ' | '\t' | '!');
fragment INDENT_CHARS : INDENT_CHAR*;
fragment INDENT_CHARS_PLUS : INDENT_CHAR+;
// These are reported as fragments, not tokens, to silence
// spurious warnings:
fragment INITIAL_INDENT: ' ';
fragment INDENT: ' ';
fragment DEDENT: ' ';
// EOL after contents - may have a following indented/dedented line.
EOL: {enclosure==0 &&
((getCharPositionInLine() != 0) && !initial_indent)}? =>
e=EOL_SEQUENCE
SPECIAL_IGNORED_LINE*
i=INDENT_CHARS // This is the indent for the next line
extra=EOL_SEQUENCE* // If this exists, the indents are useless.
{
$e.setType(EOL);
emit($e); // Emit the EOL token
if ($extra != null || ($i.text).equals("<EOF>")) {
process_indent("", $i); // Indented EOL = EOL
emit($e); // Emit the extra EOL token
} else {
// Normal case: EOL, possibly followed by indent; process it.
process_indent($i.text, $i);
}
} ;
BLANK_EOL : {enclosure==0 && (getCharPositionInLine() == 0)}? =>
e=EOL_SEQUENCE
{
$e.setType(EOL);
emit($e);
process_indent("", $e);
} ;
INITIAL_INDENT_EOL : {enclosure==0 && initial_indent}? =>
e=EOL_SEQUENCE
{
$e.setType(EOL);
emit($e);
initial_indent = false;
} ;
// Generate special initial indents for initial indents
// not preceded by lines with contents
INITIAL_INDENT_INIT : {(enclosure == 0) && (getCharPositionInLine() == 0) }? =>
i=INDENT_CHARS_PLUS
{
process_initial_indent($i.text, $i);
} ;
ENCLOSED_EOL_CHAR: {enclosure > 0}? => EOL_CHAR;
// Do not reference '\n' or '\r' inside a non-lexing rule in ANTLR.
// If you do, ANTLR will quietly create new lexical tokens for them, and
// those new tokens will interfere with EOL processing. E.G., do NOT do this:
// eolchar : '\n' | '\r';
// Other simple character or two-character sequences:
LPAREN : '(' {enclosure++;} ;
VECTOR_START : '#(' {enclosure++;};
BYTEVECTOR_START : '#u8(' {enclosure++;};
RPAREN : ')' {if (enclosure>0) {enclosure--;};};
LBRACKET : '[' {enclosure++;};
RBRACKET : ']' {if (enclosure>0) {enclosure--;};};
LBRACE : '{' {enclosure++;};
RBRACE : '}' {if (enclosure>0) {enclosure--;};};
fragment BANG : '!';
// Here we give lexical definitions for the so-called simple datums,
// such as symbols and numbers... what many people would call "atoms".
// Anybody who says "Scheme doesn't have syntax" has never tried
// to parse Scheme identifiers or numbers. Scheme's syntax for
// identifiers and numbers is extremely complicated; the number of
// productions needed are vast, even if (in our case) we're only trying
// to match a string instead something more complex. They're not even
// all that easy to implement when you're trying to directly follow the spec.
// To prove the point, the following shows a typical lexical definition
// for identifiers and numbers in many languages:
// fragment ID_INITIAL: ('A'..'Z' | 'a'..'z' | '_')
// IDENTIFIER: ID_INITIAL (ID_INITIAL | '0'..'9')* ;
// fragment EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
// FLOAT : ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
// | '.' ('0'..'9')+ EXPONENT?
// | ('0'..'9')+ EXPONENT ;
// INT : ('0'..'9')+ ;
// number : INT | FLOAT ;
// Languages might have 1-3 more productions for various bases (2,8,16).
//
// In contrast, the productions below show how to parse Scheme identifiers
// and numbers, which are FULL of complications, and thus are chock-full
// of complicated productions. Numbers, in particular, are frighteningly
// hard to get right. In fact, the Scheme number spec (R7RS draft 8) has
// a number of problems that I had to correct (I have sent my comments
// to the spec authors).
// Note: The following maps most stuff into strings, because we don't
// need to do more than that for a translation. The action rules can
// be changed to generate real values, not just string representations,
// but for our purposes that would add needless complications.
// Note: The spec requires that in numbers case is not relevant,
// so many of the productions show upper/lowercase.
IDENTIFIER :
INITIAL SUBSEQUENT* |
| '|' SYMBOL_ELEMENT* '|'
| PECULIAR_IDENTIFIER ;
fragment INITIAL : LETTER | SPECIAL_INITIAL | INLINE_HEX_ESCAPE ;
fragment LETTER : 'a'..'z' | 'A'..'Z' ;
fragment SPECIAL_INITIAL : '!' | '$' | '%' | '&' | '*' | '/' | ':'
| '<' | '=' | '>' | '?' | '^' | '_' | '~';
fragment SUBSEQUENT : INITIAL | DIGIT | SPECIAL_SUBSEQUENT ;
fragment DIGIT : '0' .. '9' ;
fragment HEX_DIGIT : DIGIT |'a'..'f' ;
fragment EXPLICIT_SIGN : '+' | '-' ;
fragment SPECIAL_SUBSEQUENT : EXPLICIT_SIGN | '.' | '@' ;
fragment INLINE_HEX_ESCAPE : '\\' 'x' HEX_SCALAR_VALUE ';' ;
fragment HEX_SCALAR_VALUE : HEX_DIGIT+ ;
fragment PECULIAR_IDENTIFIER : EXPLICIT_SIGN
( /* empty */
| SIGN_SUBSEQUENT SUBSEQUENT*
| '.' DOT_SUBSEQUENT SUBSEQUENT* )
| '.' DOT_SUBSEQUENT SUBSEQUENT*
// Note: This is a bogus extension for testing purposes, to make sure that
// a \\ is not interpreted in an initial indent:
| '\\' '\\' ;
fragment DOT_SUBSEQUENT : SIGN_SUBSEQUENT | '.' ;
fragment SIGN_SUBSEQUENT : INITIAL | EXPLICIT_SIGN | '@' ;
// Annoyingly, SYMBOL_ELEMENT overlaps with STRING_ELEMENT in
// the R7RS Scheme draft 8 fix; this (below) fixes that:
fragment SYMBOL_ELEMENT :
(~('|' | '\\'))
| SPECIAL_STRING_ELEMENT
// NOTE: Double-quote should NOT be here, already matched above.
| '\\|' ;
// boolean, character, character_name
BOOLEAN : '#t' | '#f' | '#true' | '#false' ;
CHARACTER : '#\\'
((~ ('a'..'z'))
| ('a'..'w' | 'y' | 'z') ('a'..'z')*
| 'x' HEX_SCALAR_VALUE ) ;
STRING : '\"' STRING_ELEMENT* '\"' ;
fragment STRING_ELEMENT :
~( '"' | '\\' ) | SPECIAL_STRING_ELEMENT ;
fragment INTRALINE_WHITESPACE :
(' ' | '\t')* EOL_SEQUENCE (' ' | '\t')* ;
fragment SPECIAL_STRING_ELEMENT :
'\\' ('a' | 'b' | 't' | 'n' | 'r' | '"' | '\\' | INTRALINE_WHITESPACE )
| INLINE_HEX_ESCAPE ;
// The following is structured so that exactness (if stated)
// can precede *OR* follow the radix (base) declaration, while
// (1) forbidding two exactness statements (only 1/number),
// (2) allowing exactness and radix declarations to both be optional without
// creating a useless ambiguity.
// Instead of calling a "NUM_2", we explicitly make statements about
// exactness.
NUMBER : (EXACTNESS
(RADIX_2 COMPLEX_2
| RADIX_8 COMPLEX_8
| RADIX_10 COMPLEX_10
| RADIX_16 COMPLEX_16
| COMPLEX_10))
| RADIX_2 EXACTNESS? COMPLEX_2
| RADIX_8 EXACTNESS? COMPLEX_8
| RADIX_10 EXACTNESS? COMPLEX_10
| RADIX_16 EXACTNESS? COMPLEX_16
| COMPLEX_10 ;
fragment I : 'i' | 'I' ;
// These aren't used directly, they are templates.
// Note that UREAL_R is special for base 10 (supports decimal)
// The PREFIX_R has to handled specially because EXACTNESS can be empty,
// as can RADIX_10.
//
// fragment NUM_R : PREFIX_R COMPLEX_R ;
// fragment COMPLEX_R : REAL_R
// ('@' REAL_R
// | ('+' | '-') UREAL_R? I
// | INFNAN I )?
// | ('+' | '-') UREAL_R I
// | INFNAN I
// | ('+' | '-') I ;
// fragment REAL_R : SIGN UREAL_R | INFNAN ;
// fragment UREAL_R : UINTEGER_R ('/' UINTEGER_R)?
// | DECIMAL_R;
// fragment UINTEGER_R : DIGIT_R+ ;
// fragment PREFIX_R : RADIX_R EXACTNESS | EXACTNESS RADIX_R ;
// fragment NUM_2 : PREFIX_2 COMPLEX_2 ;
fragment COMPLEX_2 : REAL_2
('@' REAL_2
| ('+' | '-') UREAL_2? I
| INFNAN I )?
| ('+' | '-') UREAL_2 I
| INFNAN I
| ('+' | '-') I ;
fragment REAL_2 : SIGN UREAL_2 | INFNAN ;
fragment UREAL_2 : UINTEGER_2 ('/' UINTEGER_2)? ;
fragment UINTEGER_2 : DIGIT_2+ ;
// fragment PREFIX_2 : RADIX_2 EXACTNESS? ;
// fragment NUM_8 : PREFIX_8 COMPLEX_8 ;
fragment COMPLEX_8 : REAL_8
('@' REAL_8
| ('+' | '-') UREAL_8? I
| INFNAN I )?
| ('+' | '-') UREAL_8 I
| INFNAN I
| ('+' | '-') I ;
fragment REAL_8 : SIGN UREAL_8 | INFNAN ;
fragment UREAL_8 : UINTEGER_8 ('/' UINTEGER_8)? ;
fragment UINTEGER_8 : DIGIT_8+ ;
// fragment PREFIX_8 : RADIX_8 EXACTNESS? ;
// fragment NUM_10 : PREFIX_10 COMPLEX_10 ;
fragment COMPLEX_10 : REAL_10
('@' REAL_10
| ('+' | '-') UREAL_10? I
| INFNAN I )?
| ('+' | '-') UREAL_10 I
| INFNAN I
| ('+' | '-') I ;
fragment REAL_10 : SIGN UREAL_10 | INFNAN ;
// UREAL_10 has to be handled carefully. The spec has this:
// fragment UREAL_10 : UINTEGER_10 ('/' UINTEGER_10)? | DECIMAL_10;
// fragment DECIMAL_10 : UINTEGER_10 SUFFIX
// | PERIOD DIGIT_10+ SUFFIX
// | DIGIT_10+ PERIOD DIGIT_10* SUFFIX ;
// which is ambiguous; for UREAL_10, "1" can match branch 1 or 2.
fragment UREAL_10 :
/* SUFFIX can be empty, so making it optional creates unnecessary ambiguity */
UINTEGER_10 ('/' UINTEGER_10 | SUFFIX | PERIOD DIGIT_10* SUFFIX)
| PERIOD DIGIT_10+ SUFFIX ;
fragment UINTEGER_10 : DIGIT_10+ ;
// fragment PREFIX_10 : RADIX_10 EXACTNESS? ;
// fragment NUM_16 : PREFIX_16 COMPLEX_16 ;
fragment COMPLEX_16 : REAL_16
('@' REAL_16
| ('+' | '-') UREAL_16? I
| INFNAN I )?
| ('+' | '-') UREAL_16 I
| INFNAN I
| ('+' | '-') I ;
fragment REAL_16 : SIGN UREAL_16 | INFNAN ;
fragment UREAL_16 : UINTEGER_16 ('/' UINTEGER_16)? ;
fragment UINTEGER_16 : DIGIT_16+ ;
// fragment PREFIX_16 : RADIX_16 EXACTNESS? ;
fragment INFNAN : ('+' | '-') ('inf.0' | 'nan.0') ;
fragment SUFFIX : /*empty*/ | EXPONENT_MARKER SIGN DIGIT_10+ ;
fragment EXPONENT_MARKER : 'e' | 's' | 'f' | 'd' | 'l' |
'E' | 'S' | 'F' | 'D' | 'L' ;
fragment SIGN : /*empty*/ | '+' | '-' ;
// NOTE: This *requires* non-empty.
fragment EXACTNESS : '#i' | '#I' | '#e' | '#E' ;
fragment RADIX_2 : '#b' | '#B' ;
fragment RADIX_8 : '#o' | '#O' ;
// NOTE: This *requires* non-empty.
fragment RADIX_10 : '#d' | '#D';
fragment RADIX_16 : '#x' | '#X';
fragment DIGIT_2 : '0' | '1' ;
fragment DIGIT_8 : '0'..'7' ;
fragment DIGIT_10 : DIGIT ;
fragment DIGIT_16 : DIGIT_10 | 'a'..'f' | 'A'..'F';
// Define ERROR this way so we don't get a spurious ANTLR warning.
// This is used by the later "error" non-terminal.
fragment ERROR: ' ' ;
// PARSER SECTION
// Special non-terminals that act essentially as comments.
// They are used clarify the grammar meaning, as follows:
// empty : ; // This used to identify an empty branch
// // but doing that interfered with ANTLR debugging info
same : ; // Emphasizes where neither indent nor dedent has occurred
error : ERROR; // Specifically identifies an error branch.
// Note that errors can occur elsewhere, and an implementation
// may include an extension where an error is noted in this grammar.
// However, the error non-terminal makes it clear where an action is
// not defined, indicates where a parser might specifically check for
// errors, and also acts as a check on the grammar itself (to ensure that
// there isn't some valid interpretation for that sequence at that point).
// Here we create some non-terminals with the same names as terminals,
// just lowercase instead of uppercase.
// These renames are completely unnecessasry, but they are helpful
// when using the ANTLR debugger.
// collecting_end : COLLECTING_END;
// indent : INDENT;
// dedent : DEDENT;
// This BNF uses the following slightly complicated pattern in some places:
// from_n_expr ((hspace+ (stuff {action1} | /*empty*/ {action2} ))
// | /*empty*/ {action2} )
// This is an expanded form of this BNF pattern (sans actions):
// from_n_expr (hspace+ stuff?)?
// Note that this pattern quietly removes horizontal spaces at the
// end of the line correctly; that's important because you can't see them,
// so quietly handling them eliminates a source of hard-to-find and
// unnecessary errors.
// If from_n_expr (etc.) is as greedy as possible (it needs to be),
// we *could* instead accept this simpler BNF pattern:
// from_n_expr hspace* stuff?
// but while that simpler BNF pattern would correctly accept *good* input,
// it would also accept *incorrect* input like "(x)q" or other n-expressions
// followed immediately by other n-expressions without intervening whitespace.
// We want to detect such situations as errors, so we'll use the
// more complex (and more persnickety) BNF pattern instead.
wspace : hspace | ENCLOSED_EOL_CHAR | FF | VT
| LCOMMENT ; // Separators inside (...) etc.
list_contents_real returns [Object v]
: n1=n_expr
(wspace+
(lc=list_contents_real {$v = cons($n1.v, $lc.v);}
| /*empty*/ {$v = list($n1.v);})
| /*empty*/ {$v = list($n1.v);})
| PERIOD
(wspace+
(n2=n_expr wspace* {$v = $n2.v;}
| /*empty*/ {$v = list(".");})
| /*empty*/ {$v = list(".");})
;
list_contents returns [Object v]
: wspace*
(list_contents_real {$v=$list_contents_real.v;}
| /*empty*/ {$v = null;} ) ;
// The "greedy=true" option here forces n_expr_tail to look at the
// next character and forceably consume in *this* production if it's
// an opening paren, bracket, or brace. Without this,
// ANTLR notices ambiguities and complains with reports like:
// Decision can match input such as "LPAREN" using multiple alternatives: 1, 4
// This is because there are constructs such as:
// #;a#|hi|#(x)
// which technically are ambiguous; do we mean "#;a" or "#;a(x)"?
// The option breaks the ambiguity.
n_expr_tail[Object prefix] returns [Object v]
: (options {greedy=true;}
: LPAREN c1=list_contents RPAREN r1=n_expr_tail[cons(prefix, $c1.v)]
{$v = $r1.v;}
| LBRACKET c2=list_contents RBRACKET
r2=n_expr_tail[cons("$" + "bracket-apply" + "$", cons(prefix, $c2.v))]
{$v = $r2.v;}
| LBRACE c3=list_contents RBRACE
// Map f{} to (f) and not (f ()). Note that f{x} maps to (f x).
r3=n_expr_tail[nullp($c3.v) ? list(prefix)
: list(prefix, process_curly($c3.v))]
{$v = $r3.v;}
| /*empty*/
{$v = prefix;}
) ;
vector returns [Object v]
: VECTOR_START list_contents RPAREN
{$v = cons("vector", $list_contents.v); } ;
// Currently return value ignored because simple_datum ignores it.
bytevector returns [Object v]
: BYTEVECTOR_START list_contents RPAREN