File: JSONParser.jjt

package info (click to toggle)
tomcat11 11.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,028 kB
  • sloc: java: 366,244; xml: 55,681; jsp: 4,783; sh: 1,304; perl: 324; makefile: 25; ansic: 14
file content (384 lines) | stat: -rw-r--r-- 8,956 bytes parent folder | download | duplicates (8)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

options {
    CHOICE_AMBIGUITY_CHECK=3;
    OTHER_AMBIGUITY_CHECK=2;
    ERROR_REPORTING=true;
    JAVA_UNICODE_ESCAPE=true;
    UNICODE_INPUT=true;
    IGNORE_CASE=true;
    SUPPORT_CLASS_VISIBILITY_PUBLIC=true;
    FORCE_LA_CHECK=true;
    CACHE_TOKENS=true;
    SANITY_CHECK=true;
    STATIC=false;
}

PARSER_BEGIN(JSONParser)

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tomcat.util.json;

/**
* Basic JSON parser generated by JavaCC. It consumes the input provided through the constructor when
* {@code parseObject()}, {@code parseList()}, or {@code parse()} are called, and there is no way to directly
* reset the state.
*/
@SuppressWarnings("all") // Ignore warnings in generated code
public class JSONParser {

    protected static final org.apache.tomcat.util.res.StringManager sm = org.apache.tomcat.util.res.StringManager.getManager(JSONParser.class);

    private boolean nativeNumbers = false;

    public JSONParser(String input) {
        this(new java.io.StringReader(input));
    }

    /**
     * Parses a JSON object into a Java {@code Map}.
     */
    public java.util.LinkedHashMap<String, Object> parseObject() throws ParseException {
        java.util.LinkedHashMap<String, Object> toReturn = object();
        if (!ensureEOF()) {
            throw new IllegalStateException(sm.getString("parser.expectedEOF"));
        }
        return toReturn;
    }

    /**
     * Parses a JSON array into a Java {@code List}.
     */
    public java.util.ArrayList<Object> parseArray() throws ParseException {
        java.util.ArrayList<Object> toReturn = list();
        if (!ensureEOF()) {
            throw new IllegalStateException(sm.getString("parser.expectedEOF"));
        }
        return toReturn;
    }

    /**
     * Parses any JSON-parsable object, returning the value.
     */
    public Object parse() throws ParseException {
        Object toReturn = anything();
        if (!ensureEOF()) {
            throw new IllegalStateException(sm.getString("parser.expectedEOF"));
        }
        return toReturn;
    }

    private static String substringBefore(String str, char delim) {
        int pos = str.indexOf(delim);
        if (pos == -1) {
            return str;
        }
        return str.substring(0, pos);
    }

    public void setNativeNumbers(boolean value) {
        this.nativeNumbers = value;
    }

    public boolean getNativeNumbers() {
        return this.nativeNumbers;
    }

}

PARSER_END(JSONParser)

// Ignore comments
SKIP: {
    <C_SINGLE_COMMENT: "//" (~["\n","\r","\f"])* <EOL>>
| <C_MULTILINE_COMMENT: "/*" (~[])* "*/">
| <SH_SINGLE_COMMENT: "#" (~["\n","\r","\f"])* <EOL>>
| <WHITESPACE: " " | "\t">
| <EOL: "\n" | "\r" | "\f">
}

// Common tokens
TOKEN: {
    <COMMA: ",">
}

// Object tokens
TOKEN:{
    <BRACE_OPEN: "{">
| <BRACE_CLOSE: "}">
| <COLON: ":">
}

// Array tokens
TOKEN:{
    <BRACKET_OPEN: "[">
| <BRACKET_CLOSE: "]">
}

// Number token
TOKEN:{
    <#ZERO: "0">
| <#DIGIT_NONZERO: ["1"-"9"]>
| <#DIGIT: (<DIGIT_NONZERO> | <ZERO>) >
| <NUMBER_INTEGER:
        ("-")?
        ( (<ZERO>)+ | ( <DIGIT_NONZERO> (<DIGIT>)* ) )
    >
| <NUMBER_DECIMAL:
        ("-")?
        ( (<ZERO>)+ | ( <DIGIT_NONZERO> (<DIGIT>)* ) )
        ("."
            (<DIGIT>)+
            (
                ["e","E"]
                ("+" | "-")?
                (<DIGIT>)+
            )?
        )
    >
}

// Boolean tokens
TOKEN:{
    <TRUE: "true">
| <FALSE: "false">
}

// Null token
TOKEN:{
    <NULL: "null">
}

// String tokens
TOKEN:{
    <#QUOTE_DOUBLE: "\"">
| <#QUOTE_SINGLE: "'">
| <STRING_SINGLE_EMPTY: "''">
| <STRING_DOUBLE_EMPTY: "\"\"">
| <#STRING_SINGLE_BODY: (
        (~["'","\\","\r","\n","\f","\t"]) |
        ( "\\" ( "r" | "n" | "f" | "\\" | "/" | "'" | "b" | "t" ) )
    )+>
| <#STRING_DOUBLE_BODY: (
        (~["\"","\\","\r","\n","\f","\t"]) |
        ( "\\" ( "r" | "n" | "f" | "\\" | "/" | "\"" | "b" | "t" ) )
    )+>
| <STRING_SINGLE_NONEMPTY: <QUOTE_SINGLE> <STRING_SINGLE_BODY> <QUOTE_SINGLE>>
| <STRING_DOUBLE_NONEMPTY: <QUOTE_DOUBLE> <STRING_DOUBLE_BODY> <QUOTE_DOUBLE>>
}

// Raw symbol tokens
TOKEN:{
    <SYMBOL: (["a"-"z", "A"-"Z", "0", "1"-"9"])+ >
}


boolean ensureEOF() : {}{
    <EOF>
    { return true; }
}

Object anything() : {
    Object x;
}{
    ( x = object()
    | x = list()
    | x = value()
    )
    { return x; }
}

String objectKey() : {
    Object o;
    String key;
} {
    (
        key = string()
    | key = symbol()
    | (
        nullValue()
        { key = null; }
        )
    | (
            ( o = booleanValue() | o = number() )
            { key = o.toString(); }
        )
    )
    { return key; }
}

java.util.LinkedHashMap<String, Object> object() : {
    final java.util.LinkedHashMap<String, Object> map = new java.util.LinkedHashMap<String, Object>();
    String key;
    Object value;
}{
    <BRACE_OPEN>
    [
        key = objectKey()
        <COLON>
        value = anything()
        { map.put(key, value); }
        { key = null; value = null; }
        (
            <COMMA>
            key = objectKey()
            <COLON>
            value = anything()
            { map.put(key, value); }
            { key = null; value = null; }
        )*
    ]
    <BRACE_CLOSE>
    { return map; }
}

java.util.ArrayList<Object> list() : {
    final java.util.ArrayList<Object> list = new java.util.ArrayList<Object>();
    Object value;
}{
    <BRACKET_OPEN>
    [
        value = anything()
        { list.add(value); }
        { value = null; }
        (
            <COMMA>
            value = anything()
            { list.add(value); }
            { value = null; }
        )*
    ]
    <BRACKET_CLOSE>
    {
        list.trimToSize();
        return list;
    }
}

Object value() : {
    Object x;
}{
    ( x = string()
    | x = number()
    | x = booleanValue()
    | x = nullValue()
    )
    { return x; }
}

Object nullValue(): {}{
    <NULL>
    { return null; }
}

Boolean booleanValue(): {
    Boolean b;
}{
    (
        (
            <TRUE>
            { b = Boolean.TRUE; }
        ) | (
            <FALSE>
            { b = Boolean.FALSE; }
        )
    )
    { return b; }
}

Number number(): {
    Token t;
}{
    (
        t = <NUMBER_DECIMAL>
        {
            if (nativeNumbers) {
                return Long.valueOf(t.image);
            } else {
                return new java.math.BigDecimal(t.image);
            }
        }
    ) | (
        t = <NUMBER_INTEGER>
        {
            if (nativeNumbers) {
                return Double.valueOf(t.image);
            } else {
                return new java.math.BigInteger(substringBefore(t.image, '.'));
            }
        }
    )
}

String string() : {
    String s;
}{
    ( s = doubleQuoteString()
    | s = singleQuoteString()
    )
    { return s; }
}

String doubleQuoteString() : {
}{
    (
        <STRING_DOUBLE_EMPTY>
        { return ""; }
    ) | (
        <STRING_DOUBLE_NONEMPTY>
        {
            String image = token.image;
            return image.substring(1, image.length() - 1);
        }
    )
}

String singleQuoteString() : {
}{
    (
        <STRING_SINGLE_EMPTY>
        { return ""; }
    ) | (
        <STRING_SINGLE_NONEMPTY>
        {
            String image = token.image;
            return image.substring(1, image.length() - 1);
        }
    )
}

String symbol() : {
}{
    <SYMBOL>
    { return token.image; }
}