forked from javacc/javacc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONParser.jjt
393 lines (370 loc) · 14 KB
/
JSONParser.jjt
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
/*
* This software is licensed under the terms of the ISC License.
* (ISCL http://www.opensource.org/licenses/isc-license.txt
* It is functionally equivalent to the 2-clause BSD licence,
* with language "made unnecessary by the Berne convention" removed).
*
* Copyright (c) 2011-2013 Mike Norman
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
* USE OR PERFORMANCE OF THIS SOFTWARE.
*
******************************************************************************/
options {
STATIC = false;
SUPPORT_CLASS_VISIBILITY_PUBLIC = true;
ERROR_REPORTING = false;
JAVA_UNICODE_ESCAPE = true;
UNICODE_INPUT = true;
NODE_USES_PARSER = false;
VISITOR = false;
}
PARSER_BEGIN(JSONParser)
/*
* This software is licensed under the terms of the ISC License.
* (ISCL http://www.opensource.org/licenses/isc-license.txt
* It is functionally equivalent to the 2-clause BSD licence,
* with language "made unnecessary by the Berne convention" removed).
*
* Copyright (c) 2011-2013 Mike Norman
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
* USE OR PERFORMANCE OF THIS SOFTWARE.
*
******************************************************************************/
package org.mwnorman.json;
//javase imports
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class JSONParser {
static protected String stripOffQuotes(String quotedString) {
return quotedString.substring(1, quotedString.length() - 1);
}
public static String TRUE_ATOM = stripOffQuotes(tokenImage[K_TRUE]);
public static String FALSE_ATOM = stripOffQuotes(tokenImage[K_FALSE]);
public JSONParser() {
super();
}
//cheat to sorta get generics on productions
public <A> List<A> array() throws ParseException {
return _array();
}
public <T> Map<String, T> object() throws ParseException {
return _object();
}
}
PARSER_END(JSONParser)
TOKEN_MGR_DECLS :
{
static int commentNesting = 0;
}
// Pls see http://www.ietf.org/rfc/rfc4627.txt for JSON spec details
// white space
SKIP: {
" "
| "\t"
| "\n"
| "\r"
| "\f"
}
// comments: not really part of JSON spec, but parser shouldn't blow-up if present
SKIP: {
//single-line 'C'-style comments
< "//" (~["\n","\r"])* ("\n" | "\r" | "\r\n")>
//single-line Bash-style comments
| < "#" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
//multi-line 'C'-style comments with counter for nesting
| "/*" { commentNesting++; } : WithinMLC
//multi-line HTML comments (no nesting)
| "<!--" : WithinMLH
}
<WithinMLC> SKIP :
{
"/*"
{
commentNesting++;
}
| "*/"
{
commentNesting--;
if (commentNesting == 0) {
SwitchTo(DEFAULT);
}
}
}
<WithinMLH> SKIP :
{
"-->" : DEFAULT
}
<WithinMLC, WithinMLH> MORE :
{
<~[]>
}
// JSON reserved keywords (prefix with K_ to avoid naming conflicts): only lower case allowed!
TOKEN: {
<K_TRUE: "true">
| <K_FALSE: "false">
| <K_NULL: "null">
}
// JSON operators (prefix with O_ to avoid naming conflicts)
TOKEN: {
<O_OPENBRACE: "{">
| <O_CLOSEBRACE: "}">
| <O_OPENBRACKET: "[">
| <O_CLOSEBRACKET: "]">
| <O_COMMA: ",">
| <O_COLON: ":">
| <O_DOT: ".">
| <O_PLUS: "+">
| <O_MINUS: "-">
}
// numeric literals
TOKEN: {
<#DIGIT: [
//Unicode "DIGIT" characters - more inclusive than Character.isDigit
"\u0030"-"\u0039", //DIGIT ZERO - DIGIT NINE
"\u0660"-"\u0669", //ARABIC-INDIC DIGIT ZERO -ARABIC-INDIC DIGIT NINE
"\u06f0"-"\u06f9", //EXTENDED ARABIC-INDIC DIGIT ZERO - EXTENDED ARABIC-INDIC DIGIT NINE
"\u07c0"-"\u07c9", //NKO DIGIT ZERO - NKO DIGIT NINE
"\u0966"-"\u096f", //DEVANAGARI DIGIT ZERO - DEVANAGARI DIGIT NINE
"\u09e6"-"\u09ef", //BENGALI DIGIT ZERO - BENGALI DIGIT NINE
"\u0a66"-"\u0a6f", //GURMUKHI DIGIT ZERO - GURMUKHI DIGIT NINE
"\u0ae6"-"\u0aef", //GUJARATI DIGIT ZERO - GUJARATI DIGIT NINE
"\u0b66"-"\u0b6f", //ORIYA DIGIT ZERO - ORIYA DIGIT NINE
"\u0be6"-"\u0bef", //TAMIL DIGIT ZERO - TAMIL DIGIT NINE
"\u0c66"-"\u0c6f", //TELUGU DIGIT ZERO - TELUGU DIGIT NINE
"\u0ce6"-"\u0cef", //KANNADA DIGIT ZERO - KANNADA DIGIT NINE
"\u0d66"-"\u0d6f", //MALAYALAM DIGIT ZERO - MALAYALAM DIGIT NINE
"\u0e50"-"\u0e59", //THAI DIGIT ZERO - THAI DIGIT NINE
"\u0ed0"-"\u0ed9", //LAO DIGIT ZERO - LAO DIGIT NINE
"\u0f20"-"\u0f29", //TIBETAN DIGIT ZERO - TIBETAN DIGIT NINE
"\u1040"-"\u1049", //MYANMAR DIGIT ZERO - MYANMAR DIGIT NINE
"\u1090"-"\u1099", //MYANMAR SHAN DIGIT ZERO - MYANMAR SHAN DIGIT NINE
"\u1369"-"\u1371", //ETHIOPIC DIGIT ZERO - ETHIOPIC DIGIT NINE
"\u17e0"-"\u17e9", //KHMER DIGIT ZERO - KHMER DIGIT NINE
"\u1810"-"\u1819", //MONGOLIAN DIGIT ZERO - MONGOLIAN DIGIT NINE
"\u1946"-"\u194f", //LIMBU DIGIT ZERO - LIMBU DIGIT NINE
"\u19d0"-"\u19d9", //NEW TAI LUE DIGIT ZERO - NEW TAI LUE DIGIT NINE
"\u1a80"-"\u1a89", //TAI THAM HORA DIGIT ZERO - TAI THAM HORA DIGIT NINE
"\u1a90"-"\u1a99", //TAI THAM THAM DIGIT ZERO - TAI THAM THAM DIGIT NINE
"\u1b50"-"\u1b59", //BALINESE DIGIT ZERO - BALINESE DIGIT NINE
"\u1bb0"-"\u1bb9", //SUNDANESE DIGIT ZERO - SUNDANESE DIGIT NINE
"\u1c40"-"\u1c49", //LEPCHA DIGIT ZERO - LEPCHA DIGIT NINE
"\u1c50"-"\u1c59", //OL CHIKI DIGIT ZERO - OL CHIKI DIGIT NINE
"\ua620"-"\ua629", //VAI DIGIT ZERO - VAI DIGIT NINE
"\ua8d0"-"\ua8d9", //SAURASHTRA DIGIT ZERO - SAURASHTRA DIGIT NINE
"\ua8e0"-"\ua8e9", //COMBINING DEVANAGARI DIGIT ZERO - COMBINING DEVANAGARI DIGIT NINE
"\ua900"-"\ua909", //KAYAH LI DIGIT ZERO - KAYAH LI DIGIT NINE
"\ua9d0"-"\ua9d9", //JAVANESE DIGIT ZERO - JAVANESE DIGIT NINE
"\uaa50"-"\uaa59", //CHAM DIGIT ZERO - CHAM DIGIT NINE
"\uabf0"-"\uabf9", //MEETEI MAYEK DIGIT ZERO - MEETEI MAYEK DIGIT NINE
"\uff10"-"\uff19" //FULLWIDTH DIGIT ZERO -FULLWIDTH DIGIT NINE
] >
| <#EXP: ["e", "E"] ( <O_PLUS > | <O_MINUS > )? >
}
// JSON numbers do not support octal or hexadecimal formats
TOKEN: {
<NUMBER: <INTEGER> | <INTEGER> <FRACTIONAL_DIGITS> | <INTEGER> <EXPONENT> | <INTEGER> <FRACTIONAL_DIGITS> <EXPONENT> >
| <INTEGER: (<O_MINUS>)? ( <DIGITS> ) > //support leading zeros
| <FRACTIONAL_DIGITS: <O_DOT> <DIGITS> >
| <EXPONENT: <EXP> <DIGITS> >
| <DIGITS: ( <DIGIT> )+ >
}
// string literals
TOKEN: {
<QUOTED_STRING: "\"" ( (~["\"","\\"]) | ("\\" ( ["n","t","b","r","f","\\","\""] ) ) )* "\"">
| <SINGLE_QUOTED_STRING: "\'" ( (~["\'","\\"]) | ("\\" ( ["n","t","b","r","f","\\","\'"] ) ) )* "\'">
}
TOKEN: {
<IDENTIFIER: <LETTER> (<LETTER> | <DIGIT> | "_")* >
| <#LETTER: [
//Unicode "LETTER" characters - as of Javase 6, Unicode 4 is supported
//http://www.unicode.org/Public/4.1.0/ucd/NamesList.txt
//more inclusive than Character.isLetter
"\u0024", //DOLLAR SIGN: milreis, escudo
"\u0041"-"\u005a", //LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
"\u005f", //LOW LINE: spacing underscore
"\u0061"-"\u007a", //LATIN SMALL LETTER A - LATIN SMALL LETTER Z
"\u00aa"-"\u00ad", // 00AA FEMININE ORDINAL INDICATOR: spanish
// 00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK: left guillemet
// 00AC NOT SIGN: angled dash
// 00AD SOFT HYPHEN: discretionary hyphen
"\u00b5"-"\u00b6", // 00B5 MICRO SIGN: greek small letter mu
// 00B6 PILCROW SIGN: paragraph sign
"\u00ba"-"\u00bb", // 00BA MASCULINE ORDINAL INDICATOR: spanish
// 00BB RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK : right guillemet
"\u00c0"-"\u00d6", //LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
"\u00d8"-"\u00f6", //LATIN CAPITAL LETTER O WITH STROKE - LATIN SMALL LETTER O WITH DIAERESIS
"\u00f8"-"\u00ff", //LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER Y WITH DIAERESIS
"\u0100"-"\u1fff", //LATIN CAPITAL LETTER A WITH MACRON - GREEK DASIA:reversed comma
"\u3040"-"\u318f", //Hiragana - Hangul
"\u3300"-"\u337f", //Squared Katakana
"\u3400"-"\u3d2d", //CJK Unified Ideographs Extension A
"\u4e00"-"\u9fff", //CJK Unified Ideographs
"\uf900"-"\ufaff", //CJK Compatibility Ideographs
"\ufb00"-"\ufb06", //LATIN SMALL LIGATURE FF -LATIN SMALL LIGATURE ST
"\ufb13"-"\ufb17", //ARMENIAN SMALL LIGATURE MEN NOW - ARMENIAN SMALL LIGATURE MEN XEH
"\ufb1f"-"\ufb28", //HEBREW LIGATURE YIDDISH YOD YOD PATAH -HEBREW LETTER WIDE TAV
"\ufb2a"-"\ufb36", //HEBREW LETTER SHIN WITH SHIN DOT - HEBREW LETTER ZAYIN WITH DAGESH
"\ufb38"-"\ufb3e", //HEBREW LETTER TET WITH DAGESH - HEBREW LETTER MEM WITH DAGESH
"\ufb40"-"\ufb41", //HEBREW LETTER NUN WITH DAGESH - HEBREW LETTER SAMEKH WITH DAGESH
"\ufb43"-"\ufb44", //HEBREW LETTER FINAL PE WITH DAGESH - HEBREW LETTER PE WITH DAGESH
"\ufb46"-"\ufb4f", //HEBREW LETTER TSADI WITH DAGESH - HEBREW LIGATURE ALEF LAMED
"\ufb50"-"\ufdd7", //Arabic Presentation Forms(A) - ARABIC LIGATURE BISMILLAH AR-RAHMAN AR-RAHEEM
"\ufdf0"-"\ufdfd", //ARABIC LIGATURE SALLA USED AS KORANIC STOP SIGN ISOLATED FORM -ARABIC LIGATURE BISMILLAH AR-RAHMAN AR-RAHEEM
"\ufe70"-"\ufefc", //Arabic Presentation Forms(B) - ARABIC LIGATURE LAM WITH ALEF FINAL FORM
"\uff1f"-"\uff3a", //FULLWIDTH QUESTION MARK - FULLWIDTH LATIN CAPITAL LETTER Z
"\uff3f"-"\uff5e", //FULLWIDTH LOW LINE - FULLWIDTH TILDE
"\uff61"-"\uffbe", //Halfwidth CJK punctuation:HALFWIDTH IDEOGRAPHIC FULL STOP - HALFWIDTH HANGUL LETTER HIEUH
"\uffc2"-"\uffc7", //HALFWIDTH HANGUL LETTER A - HALFWIDTH HANGUL LETTER E
"\uffca"-"\uffcf", //HALFWIDTH HANGUL LETTER YEO - HALFWIDTH HANGUL LETTER OE
"\uffd2"-"\uffd7", //HALFWIDTH HANGUL LETTER YO - HALFWIDTH HANGUL LETTER YU
"\uffda"-"\uffdc" //HALFWIDTH HANGUL LETTER EU - HALFWIDTH HANGUL LETTER I
]
>
}
Object parse():
{
Object o = null;
}
{
( o=_object() | o=_array() )
{
return o;
}
}
protected Map _object():
{
Map m = new LinkedHashMap();
}
{
<O_OPENBRACE> ( members(m) )? <O_CLOSEBRACE>
{
return m;
}
}
protected void members(Map m):
{
}
{
pair(m) [ <O_COMMA> members(m) ]
}
protected void pair(Map m):
{
Token t = null;
Object o;
String fieldName = null;
}
{
fieldName=fieldName() <O_COLON> o=value()
{
m.put(fieldName, o);
}
}
protected String fieldName():
{
String fieldName = null;
}
{
(
<SINGLE_QUOTED_STRING>
{
fieldName = stripOffQuotes(token.image);
}
|
<QUOTED_STRING>
{
fieldName = stripOffQuotes(token.image);
}
|
//some JSON systems now accept 'naked' JSON Objects
<IDENTIFIER>
{
fieldName = token.image;
}
)
{
return fieldName;
}
}
protected List _array():
{
List a=new ArrayList();
}
{
<O_OPENBRACKET> ( elements(a) )? <O_CLOSEBRACKET>
{
Collections.reverse(a);
return a;
}
}
protected void elements(List a):
{
Object o = null;
}
{
o=value() [ <O_COMMA> elements(a) ]
{
a.add(o);
}
}
protected Object value():
{
Token t = null;
Object o = null;
}
{
( o=_object()
| o=_array()
| t=<IDENTIFIER> {o = t.image;} //'naked' values, too
| t=<SINGLE_QUOTED_STRING> {o = stripOffQuotes(t.image);}
| t=<QUOTED_STRING> {o = stripOffQuotes(t.image);}
| t=<NUMBER>
{
try {
o = Integer.valueOf(t.image);
}
catch (NumberFormatException nfe1) {
try {
o = new BigInteger(t.image);
}
catch (NumberFormatException nfe2) {
try {
o = new BigDecimal(t.image);
}
catch (NumberFormatException nfe3) {
o = Double.NaN;
}
}
}
}
| <K_TRUE> {o = Boolean.TRUE;}
| <K_FALSE> {o = Boolean.FALSE;}
| <K_NULL> )
{
return o;
}
}