@@ -51,6 +51,8 @@ boolean isValueType() {
51
51
private boolean isUnquoted ;
52
52
private String sourceName ;
53
53
54
+ private final Stack <String > parenthesis = new Stack <>();
55
+
54
56
public JsonLexer (String source , boolean allowComments , boolean allowUnquotedStrings , int line ) throws IOException {
55
57
this .source = source ;
56
58
this .allowComments = allowComments ;
@@ -162,6 +164,7 @@ public JsonLocationData getLastLocationAWS() {
162
164
return lastLocationAWS ;
163
165
}
164
166
167
+
165
168
public void next () throws IOException {
166
169
lastLocationBWS = location .copy ();
167
170
char ch ;
@@ -185,15 +188,25 @@ public void next() throws IOException {
185
188
} while (more () && Utilities .charInSet (ch , ' ' , '\r' , '\n' , '\t' ));
186
189
lastLocationAWS = location .copy ().prev ();
187
190
isUnquoted = false ;
188
-
189
191
if (!more ()) {
190
192
type = TokenType .Eof ;
193
+ if (!parenthesis .empty ()) {
194
+ throw error ("parenthesis matching is not respected. One or more parenthesis were not closed : " + parenthesis );
195
+ }
191
196
} else {
192
197
switch (ch ) {
193
198
case '{' :
194
199
type = TokenType .Open ;
200
+ parenthesis .push ("{" );
195
201
break ;
196
- case '}' :
202
+ case '}' :
203
+ if (parenthesis .empty ()) {
204
+ throw error ("Unexpected close marker '}'. No '{' before." );
205
+ }
206
+ String par = parenthesis .pop ();
207
+ if (!par .equals ("{" )) {
208
+ throw error ("Unexpected close marker '}'. Expected ']'." );
209
+ }
197
210
type = TokenType .Close ;
198
211
break ;
199
212
case '"' :
@@ -229,10 +242,18 @@ public void next() throws IOException {
229
242
case ',' :
230
243
type = TokenType .Comma ;
231
244
break ;
232
- case '[' :
245
+ case '[' :
246
+ parenthesis .push ("[" );
233
247
type = TokenType .OpenArray ;
234
248
break ;
235
- case ']' :
249
+ case ']' :
250
+ if (parenthesis .empty ()) {
251
+ throw error ("Unexpected close marker ']'. No '[' before." );
252
+ }
253
+ par = parenthesis .pop ();
254
+ if (!par .equals ("[" )) {
255
+ throw error ("Unexpected close marker ']'. Expected '}'" );
256
+ }
236
257
type = TokenType .CloseArray ;
237
258
break ;
238
259
default :
0 commit comments