3
3
import java .io .IOException ;
4
4
import java .io .InputStream ;
5
5
import java .util .ArrayList ;
6
+ import java .util .NoSuchElementException ;
6
7
7
8
import org .antlr .v4 .runtime .CharStream ;
8
9
import org .antlr .v4 .runtime .CharStreams ;
9
10
import org .antlr .v4 .runtime .CommonTokenStream ;
10
11
import org .antlr .v4 .runtime .atn .PredictionMode ;
11
12
12
13
import convex .core .data .ACell ;
14
+ import convex .core .data .AMap ;
15
+ import convex .core .data .AString ;
13
16
import convex .core .data .Cells ;
14
17
import convex .core .data .Maps ;
15
18
import convex .core .data .Vectors ;
23
26
import convex .core .json .reader .antlr .JSONParser .*;
24
27
import convex .core .lang .reader .ConvexErrorListener ;
25
28
import convex .core .util .JSONUtils ;
29
+ import convex .core .util .Utils ;
26
30
27
31
/**
28
32
* Reader implementation for pure JSON
@@ -42,23 +46,33 @@ public JSONListener() {
42
46
*/
43
47
protected void push (ACell a ) {
44
48
ArrayList <ACell > top =stack .getLast ();
49
+ // System.err.println("pushed "+a);
45
50
top .add (a );
46
51
}
47
52
48
53
@ SuppressWarnings ("unchecked" )
49
54
protected <R extends ACell > R pop () {
50
55
ArrayList <ACell > top =stack .getLast ();
51
56
ACell cell =top .removeLast ();
57
+ // System.err.println("popped "+cell);
52
58
return (R ) cell ;
53
59
}
54
60
55
61
protected void pushList () {
62
+ // System.err.println(stack);
56
63
stack .add (new ArrayList <>());
64
+ // System.err.println("pushed new list to make "+stack);
57
65
}
58
66
59
67
protected ArrayList <ACell > popList () {
60
- ArrayList <ACell > top =stack .removeLast ();
61
- return top ;
68
+ try {
69
+ // System.err.println(stack);
70
+ ArrayList <ACell > top =stack .removeLast ();
71
+ // System.err.println("popped list "+top+ "to make "+stack);
72
+ return top ;
73
+ } catch (NoSuchElementException e ) {
74
+ throw new ParseException ("Fatal parsing error, no elements on stack" );
75
+ }
62
76
}
63
77
64
78
@ Override
@@ -100,7 +114,7 @@ public void exitNumber(NumberContext ctx) {
100
114
return ;
101
115
}
102
116
} catch (Exception e ) {
103
-
117
+ // fall through to exception
104
118
}
105
119
throw new ParseException ("Can't parse as number: " +num );
106
120
}
@@ -136,13 +150,34 @@ public static ACell read(java.io.Reader r) throws IOException {
136
150
public static ACell read (InputStream is ) throws IOException {
137
151
return read (CharStreams .fromStream (is ));
138
152
}
139
-
140
153
154
+ public static AMap <AString ,ACell > readObject (String s ) {
155
+ return readObject (CharStreams .fromString (s ));
156
+ }
157
+
158
+ public static AMap <AString ,ACell > readObject (java .io .Reader r ) throws IOException {
159
+ return readObject (CharStreams .fromReader (r ));
160
+ }
161
+
162
+ public static AMap <AString ,ACell > readObject (InputStream is ) throws IOException {
163
+ return readObject (CharStreams .fromStream (is ));
164
+ }
165
+
166
+ @ SuppressWarnings ("unchecked" )
167
+ private static AMap <AString ,ACell > readObject (CharStream fromStream ) {
168
+ ACell a =read (fromStream );
169
+ if (a instanceof AMap object ) {
170
+ return object ;
171
+ }
172
+ throw new ParseException ("Not a JSON object, got: " +Utils .getClassName (a ));
173
+ }
174
+
175
+
141
176
private static final ConvexErrorListener ERROR_LISTENER =new ConvexErrorListener ();
142
177
143
178
144
179
static JSONParser getParser (CharStream cs , JSONListener listener ) {
145
- // Create lexer and paser for the CharStream
180
+ // Create lexer and parser for the CharStream
146
181
JSONLexer lexer =new JSONLexer (cs );
147
182
lexer .removeErrorListeners ();
148
183
lexer .addErrorListener (ERROR_LISTENER );
0 commit comments