1
1
package convex .core .lang .reader ;
2
2
3
3
import java .io .IOException ;
4
- import java .lang .reflect .InvocationTargetException ;
5
- import java .lang .reflect .Method ;
6
4
import java .util .ArrayList ;
7
5
8
6
import org .antlr .v4 .runtime .CharStream ;
41
39
import convex .core .lang .reader .antlr .ConvexLexer ;
42
40
import convex .core .lang .reader .antlr .ConvexListener ;
43
41
import convex .core .lang .reader .antlr .ConvexParser ;
44
- import convex .core .lang .reader .antlr .ConvexParser .*;
42
+ import convex .core .lang .reader .antlr .ConvexParser .AddressContext ;
43
+ import convex .core .lang .reader .antlr .ConvexParser .AllFormsContext ;
44
+ import convex .core .lang .reader .antlr .ConvexParser .AtomContext ;
45
+ import convex .core .lang .reader .antlr .ConvexParser .BlobContext ;
46
+ import convex .core .lang .reader .antlr .ConvexParser .BoolContext ;
47
+ import convex .core .lang .reader .antlr .ConvexParser .CharacterContext ;
48
+ import convex .core .lang .reader .antlr .ConvexParser .CommentedContext ;
49
+ import convex .core .lang .reader .antlr .ConvexParser .DataStructureContext ;
50
+ import convex .core .lang .reader .antlr .ConvexParser .DoubleValueContext ;
51
+ import convex .core .lang .reader .antlr .ConvexParser .FormContext ;
52
+ import convex .core .lang .reader .antlr .ConvexParser .FormsContext ;
53
+ import convex .core .lang .reader .antlr .ConvexParser .ImplicitSymbolContext ;
54
+ import convex .core .lang .reader .antlr .ConvexParser .KeywordContext ;
55
+ import convex .core .lang .reader .antlr .ConvexParser .ListContext ;
56
+ import convex .core .lang .reader .antlr .ConvexParser .LiteralContext ;
57
+ import convex .core .lang .reader .antlr .ConvexParser .LongValueContext ;
58
+ import convex .core .lang .reader .antlr .ConvexParser .MapContext ;
59
+ import convex .core .lang .reader .antlr .ConvexParser .NilContext ;
60
+ import convex .core .lang .reader .antlr .ConvexParser .PathSymbolContext ;
61
+ import convex .core .lang .reader .antlr .ConvexParser .PrimaryContext ;
62
+ import convex .core .lang .reader .antlr .ConvexParser .QuotedContext ;
63
+ import convex .core .lang .reader .antlr .ConvexParser .ResolveContext ;
64
+ import convex .core .lang .reader .antlr .ConvexParser .SetContext ;
65
+ import convex .core .lang .reader .antlr .ConvexParser .SingleFormContext ;
66
+ import convex .core .lang .reader .antlr .ConvexParser .SlashSymbolContext ;
67
+ import convex .core .lang .reader .antlr .ConvexParser .SpecialLiteralContext ;
68
+ import convex .core .lang .reader .antlr .ConvexParser .StringContext ;
69
+ import convex .core .lang .reader .antlr .ConvexParser .SymbolContext ;
70
+ import convex .core .lang .reader .antlr .ConvexParser .SyntaxContext ;
71
+ import convex .core .lang .reader .antlr .ConvexParser .TagContext ;
72
+ import convex .core .lang .reader .antlr .ConvexParser .TaggedFormContext ;
73
+ import convex .core .lang .reader .antlr .ConvexParser .VectorContext ;
45
74
import convex .core .util .Utils ;
46
75
47
76
public class AntlrReader {
@@ -58,18 +87,14 @@ public CRListener() {
58
87
* @param a
59
88
*/
60
89
public void push (ACell a ) {
61
- int n =stack .size ()-1 ;
62
- ArrayList <ACell > top =stack .get (n );
90
+ ArrayList <ACell > top =stack .getLast ();
63
91
top .add (a );
64
92
}
65
93
66
94
@ SuppressWarnings ("unchecked" )
67
95
public <R extends ACell > R pop () {
68
- int n =stack .size ()-1 ;
69
- ArrayList <ACell > top =stack .get (n );
70
- int c =top .size ()-1 ;
71
- ACell cell =top .get (c );
72
- top .remove (c );
96
+ ArrayList <ACell > top =stack .getLast ();
97
+ ACell cell =top .removeLast ();
73
98
return (R ) cell ;
74
99
}
75
100
@@ -79,9 +104,7 @@ private void pushList() {
79
104
}
80
105
81
106
public ArrayList <ACell > popList () {
82
- int n =stack .size ()-1 ;
83
- ArrayList <ACell > top =stack .get (n );
84
- stack .remove (n );
107
+ ArrayList <ACell > top =stack .removeLast ();
85
108
return top ;
86
109
}
87
110
@@ -178,7 +201,7 @@ public void enterMap(MapContext ctx) {
178
201
public void exitMap (MapContext ctx ) {
179
202
ArrayList <ACell > elements =popList ();
180
203
if (Utils .isOdd (elements .size ())) {
181
- throw new ParseException ("Map requires an even number form forms." );
204
+ throw new ParseException ("Map requires an even number of forms." );
182
205
}
183
206
push (Maps .create (elements .toArray (new ACell [elements .size ()])));
184
207
}
@@ -200,7 +223,8 @@ public void enterLongValue(LongValueContext ctx) {
200
223
201
224
@ Override
202
225
public void exitLongValue (LongValueContext ctx ) {
203
- String s =ctx .getText ();
226
+ // Just looking at the last token probably most efficient way to get string?
227
+ String s =ctx .getStop ().getText ();
204
228
AInteger a = AInteger .parse (s );
205
229
if (a ==null ) throw new ParseException ("Unparseable number: " +s );
206
230
push (a );
@@ -246,7 +270,7 @@ public void enterCharacter(CharacterContext ctx) {
246
270
247
271
@ Override
248
272
public void exitCharacter (CharacterContext ctx ) {
249
- String s =ctx .getText ();
273
+ String s =ctx .getStop (). getText ();
250
274
CVMChar c =CVMChar .parse (s );
251
275
if (c ==null ) throw new ParseException ("Bad character literal format: " +s );
252
276
push (c );
@@ -329,9 +353,14 @@ public void enterAddress(AddressContext ctx) {
329
353
@ Override
330
354
public void exitAddress (AddressContext ctx ) {
331
355
String s =ctx .getStop ().getText ();
332
- Address addr =Address .parse (s );
333
- if (addr ==null ) throw new ParseException ("Bad Address format: " +s );
334
- push (addr );
356
+ try {
357
+ long value =Long .parseLong (s .substring (1 ));
358
+ Address addr =Address .create (value );
359
+ if (addr ==null ) throw new ParseException ("Bad Address format: " +s );
360
+ push (addr );
361
+ } catch (NumberFormatException e ) {
362
+ throw new ParseException ("Problem parsing Address: " +s ,e );
363
+ }
335
364
}
336
365
337
366
@ Override
@@ -385,7 +414,7 @@ public void enterResolve(ResolveContext ctx) {
385
414
386
415
@ Override
387
416
public void exitResolve (ResolveContext ctx ) {
388
- String s =ctx .getText ();
417
+ String s =ctx .getStop (). getText ();
389
418
s =s .substring (1 ); // skip leading @
390
419
Symbol sym =Symbol .create (s );
391
420
if (sym ==null ) throw new ParseException ("Invalid @ symbol: @" +s );
@@ -400,7 +429,7 @@ public void enterSlashSymbol(SlashSymbolContext ctx) {
400
429
401
430
@ Override
402
431
public void exitSlashSymbol (SlashSymbolContext ctx ) {
403
- String s =ctx .getText ();
432
+ String s =ctx .getStop (). getText ();
404
433
s =s .substring (1 ); // skip leading /
405
434
Symbol sym =Symbol .create (s );
406
435
if (sym ==null ) throw new ParseException ("Invalid / symbol: /" +s );
@@ -465,7 +494,7 @@ public void exitPathSymbol(PathSymbolContext ctx) {
465
494
466
495
if (!(sym instanceof Symbol )) throw new ParseException ("Expected path element to be a symbol but got: " + RT .getType (sym ));
467
496
// System.out.println(elements);
468
- lookup =Lists . of (Symbols .LOOKUP ,lookup ,sym );
497
+ lookup =List . create (Symbols .LOOKUP ,lookup ,sym );
469
498
}
470
499
push (lookup );
471
500
}
@@ -537,14 +566,12 @@ static ACell read(CharStream cs) {
537
566
// We don't need a parse tree, just want to visit everything in our listener
538
567
parser .setBuildParseTree (false );
539
568
parser .removeErrorListeners ();
540
- parser .getInterpreter ().setPredictionMode (PredictionMode .SLL );
569
+ parser .getInterpreter ().setPredictionMode (PredictionMode .SLL ); // Seems OK for our grammar?
541
570
parser .addErrorListener (ERROR_LISTENER );
542
571
CRListener visitor =new CRListener ();
543
572
parser .addParseListener (visitor );
544
573
545
- // ParseTree tree = parser.singleForm();
546
- Method startRule = parser .getClass ().getMethod ("singleForm" );
547
- startRule .invoke (parser , (Object []) null );
574
+ parser .singleForm ();
548
575
// ParseTreeWalker.DEFAULT.walk(visitor, tree);
549
576
550
577
ArrayList <ACell > top =visitor .popList ();
@@ -553,8 +580,8 @@ static ACell read(CharStream cs) {
553
580
}
554
581
555
582
return top .get (0 );
556
- } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
557
- throw new ParseException ("Missing parse method " ,e );
583
+ } catch (Exception e ) {
584
+ throw new ParseException ("Unexpected Parse Error " ,e );
558
585
}
559
586
}
560
587
0 commit comments