File: JSONParser.java

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 (713 lines) | stat: -rw-r--r-- 21,636 bytes parent folder | download | duplicates (2)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/* JSONParser.java */
/* Generated By:JavaCC: Do not edit this line. JSONParser.java */
/*
 * 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 implements JSONParserConstants {

    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("Expected EOF, but still had content to parse");
        }
        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("Expected EOF, but still had content to parse");
        }
        return toReturn;
    }

    /**
     * Parses any JSON-parseable object, returning the value.
     */
    public Object parse() throws ParseException {
        Object toReturn = anything();
        if (!ensureEOF()) {
            throw new IllegalStateException("Expected EOF, but still had content to parse");
        }
        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;
    }

    final public boolean ensureEOF() throws ParseException {
        jj_consume_token(0);
        {
            if ("" != null) {
                return true;
            }
        }
        throw new Error("Missing return statement in function");
    }

    final public Object anything() throws ParseException {
        Object x;
        switch (jj_nt.kind) {
            case BRACE_OPEN: {
                x = object();
                break;
            }
            case BRACKET_OPEN: {
                x = list();
                break;
            }
            case NUMBER_INTEGER:
            case NUMBER_DECIMAL:
            case TRUE:
            case FALSE:
            case NULL:
            case STRING_SINGLE_EMPTY:
            case STRING_DOUBLE_EMPTY:
            case STRING_SINGLE_NONEMPTY:
            case STRING_DOUBLE_NONEMPTY: {
                x = value();
                break;
            }
            default:
                jj_la1[0] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        {
            if ("" != null) {
                return x;
            }
        }
        throw new Error("Missing return statement in function");
    }

    final public String objectKey() throws ParseException {
        Object o;
        String key;
        switch (jj_nt.kind) {
            case STRING_SINGLE_EMPTY:
            case STRING_DOUBLE_EMPTY:
            case STRING_SINGLE_NONEMPTY:
            case STRING_DOUBLE_NONEMPTY: {
                key = string();
                break;
            }
            case SYMBOL: {
                key = symbol();
                break;
            }
            case NULL: {
                nullValue();
                key = null;
                break;
            }
            case NUMBER_INTEGER:
            case NUMBER_DECIMAL:
            case TRUE:
            case FALSE: {
                switch (jj_nt.kind) {
                    case TRUE:
                    case FALSE: {
                        o = booleanValue();
                        break;
                    }
                    case NUMBER_INTEGER:
                    case NUMBER_DECIMAL: {
                        o = number();
                        break;
                    }
                    default:
                        jj_la1[1] = jj_gen;
                        jj_consume_token(-1);
                        throw new ParseException();
                }
                key = o.toString();
                break;
            }
            default:
                jj_la1[2] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        {
            if ("" != null) {
                return key;
            }
        }
        throw new Error("Missing return statement in function");
    }

    final public java.util.LinkedHashMap<String,Object> object() throws ParseException {
        final java.util.LinkedHashMap<String,Object> map = new java.util.LinkedHashMap<String,Object>();
        String key;
        Object value;
        jj_consume_token(BRACE_OPEN);
        switch (jj_nt.kind) {
            case NUMBER_INTEGER:
            case NUMBER_DECIMAL:
            case TRUE:
            case FALSE:
            case NULL:
            case STRING_SINGLE_EMPTY:
            case STRING_DOUBLE_EMPTY:
            case STRING_SINGLE_NONEMPTY:
            case STRING_DOUBLE_NONEMPTY:
            case SYMBOL: {
                key = objectKey();
                jj_consume_token(COLON);
                value = anything();
                map.put(key, value);
                key = null;
                value = null;
                label_1:
                while (true) {
                    switch (jj_nt.kind) {
                        case COMMA: {
                            ;
                            break;
                        }
                        default:
                            jj_la1[3] = jj_gen;
                            break label_1;
                    }
                    jj_consume_token(COMMA);
                    key = objectKey();
                    jj_consume_token(COLON);
                    value = anything();
                    map.put(key, value);
                    key = null;
                    value = null;
                }
                break;
            }
            default:
                jj_la1[4] = jj_gen;
                ;
        }
        jj_consume_token(BRACE_CLOSE);
        {
            if ("" != null) {
                return map;
            }
        }
        throw new Error("Missing return statement in function");
    }

    final public java.util.ArrayList<Object> list() throws ParseException {
        final java.util.ArrayList<Object> list = new java.util.ArrayList<Object>();
        Object value;
        jj_consume_token(BRACKET_OPEN);
        switch (jj_nt.kind) {
            case BRACE_OPEN:
            case BRACKET_OPEN:
            case NUMBER_INTEGER:
            case NUMBER_DECIMAL:
            case TRUE:
            case FALSE:
            case NULL:
            case STRING_SINGLE_EMPTY:
            case STRING_DOUBLE_EMPTY:
            case STRING_SINGLE_NONEMPTY:
            case STRING_DOUBLE_NONEMPTY: {
                value = anything();
                list.add(value);
                value = null;
                label_2:
                while (true) {
                    switch (jj_nt.kind) {
                        case COMMA: {
                            ;
                            break;
                        }
                        default:
                            jj_la1[5] = jj_gen;
                            break label_2;
                    }
                    jj_consume_token(COMMA);
                    value = anything();
                    list.add(value);
                    value = null;
                }
                break;
            }
            default:
                jj_la1[6] = jj_gen;
                ;
        }
        jj_consume_token(BRACKET_CLOSE);
        list.trimToSize();
        {
            if ("" != null) {
                return list;
            }
        }
        throw new Error("Missing return statement in function");
    }

    final public Object value() throws ParseException {
        Object x;
        switch (jj_nt.kind) {
            case STRING_SINGLE_EMPTY:
            case STRING_DOUBLE_EMPTY:
            case STRING_SINGLE_NONEMPTY:
            case STRING_DOUBLE_NONEMPTY: {
                x = string();
                break;
            }
            case NUMBER_INTEGER:
            case NUMBER_DECIMAL: {
                x = number();
                break;
            }
            case TRUE:
            case FALSE: {
                x = booleanValue();
                break;
            }
            case NULL: {
                x = nullValue();
                break;
            }
            default:
                jj_la1[7] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        {
            if ("" != null) {
                return x;
            }
        }
        throw new Error("Missing return statement in function");
    }

    final public Object nullValue() throws ParseException {
        jj_consume_token(NULL);
        {
            if ("" != null) {
                return null;
            }
        }
        throw new Error("Missing return statement in function");
    }

    final public Boolean booleanValue() throws ParseException {
        Boolean b;
        switch (jj_nt.kind) {
            case TRUE: {
                jj_consume_token(TRUE);
                b = Boolean.TRUE;
                break;
            }
            case FALSE: {
                jj_consume_token(FALSE);
                b = Boolean.FALSE;
                break;
            }
            default:
                jj_la1[8] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        {
            if ("" != null) {
                return b;
            }
        }
        throw new Error("Missing return statement in function");
    }

    final public Number number() throws ParseException {
        Token t;
        switch (jj_nt.kind) {
            case NUMBER_DECIMAL: {
                t = jj_consume_token(NUMBER_DECIMAL);
                if (nativeNumbers) {
                    {
                        if ("" != null) {
                            return Long.valueOf(t.image);
                        }
                    }
                } else {
                    {
                        if ("" != null) {
                            return new java.math.BigDecimal(t.image);
                        }
                    }
                }
                break;
            }
            case NUMBER_INTEGER: {
                t = jj_consume_token(NUMBER_INTEGER);
                if (nativeNumbers) {
                    {
                        if ("" != null) {
                            return Double.valueOf(t.image);
                        }
                    }
                } else {
                    {
                        if ("" != null) {
                            return new java.math.BigInteger(substringBefore(t.image, '.'));
                        }
                    }
                }
                break;
            }
            default:
                jj_la1[9] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        throw new Error("Missing return statement in function");
    }

    final public String string() throws ParseException {
        String s;
        switch (jj_nt.kind) {
            case STRING_DOUBLE_EMPTY:
            case STRING_DOUBLE_NONEMPTY: {
                s = doubleQuoteString();
                break;
            }
            case STRING_SINGLE_EMPTY:
            case STRING_SINGLE_NONEMPTY: {
                s = singleQuoteString();
                break;
            }
            default:
                jj_la1[10] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        {
            if ("" != null) {
                return s;
            }
        }
        throw new Error("Missing return statement in function");
    }

    final public String doubleQuoteString() throws ParseException {
        switch (jj_nt.kind) {
            case STRING_DOUBLE_EMPTY: {
                jj_consume_token(STRING_DOUBLE_EMPTY);
                {
                    if ("" != null) {
                        return "";
                    }
                }
                break;
            }
            case STRING_DOUBLE_NONEMPTY: {
                jj_consume_token(STRING_DOUBLE_NONEMPTY);
                String image = token.image;
                {
                    if ("" != null) {
                        return image.substring(1, image.length() - 1);
                    }
                }
                break;
            }
            default:
                jj_la1[11] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        throw new Error("Missing return statement in function");
    }

    final public String singleQuoteString() throws ParseException {
        switch (jj_nt.kind) {
            case STRING_SINGLE_EMPTY: {
                jj_consume_token(STRING_SINGLE_EMPTY);
                {
                    if ("" != null) {
                        return "";
                    }
                }
                break;
            }
            case STRING_SINGLE_NONEMPTY: {
                jj_consume_token(STRING_SINGLE_NONEMPTY);
                String image = token.image;
                {
                    if ("" != null) {
                        return image.substring(1, image.length() - 1);
                    }
                }
                break;
            }
            default:
                jj_la1[12] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        throw new Error("Missing return statement in function");
    }

    final public String symbol() throws ParseException {
        jj_consume_token(SYMBOL);
        {
            if ("" != null) {
                return token.image;
            }
        }
        throw new Error("Missing return statement in function");
    }

    /** Generated Token Manager. */
    public JSONParserTokenManager token_source;
    JavaCharStream jj_input_stream;
    /** Current token. */
    public Token token;
    /** Next token. */
    public Token jj_nt;
    private int jj_gen;
    final private int[] jj_la1 = new int[13];
    static private int[] jj_la1_0;
    static {
        jj_la1_init_0();
    }

    private static void jj_la1_init_0() {
        jj_la1_0 = new int[] { 0xccf8480, 0x78000, 0x1ccf8000, 0x40, 0x1ccf8000, 0x40, 0xccf8480, 0xccf8000, 0x60000,
                0x18000, 0xcc00000, 0x8800000, 0x4400000, };
    }

    /** Constructor with InputStream. */
    public JSONParser(java.io.InputStream stream) {
        this(stream, null);
    }

    /** Constructor with InputStream and supplied encoding */
    public JSONParser(java.io.InputStream stream, String encoding) {
        try {
            jj_input_stream = new JavaCharStream(stream, encoding, 1, 1);
        } catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        token_source = new JSONParserTokenManager(jj_input_stream);
        token = new Token();
        token.next = jj_nt = token_source.getNextToken();
        jj_gen = 0;
        for (int i = 0; i < 13; i++) {
            jj_la1[i] = -1;
        }
    }

    /** Reinitialise. */
    public void ReInit(java.io.InputStream stream) {
        ReInit(stream, null);
    }

    /** Reinitialise. */
    public void ReInit(java.io.InputStream stream, String encoding) {
        try {
            jj_input_stream.ReInit(stream, encoding, 1, 1);
        } catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        token_source.ReInit(jj_input_stream);
        token = new Token();
        token.next = jj_nt = token_source.getNextToken();
        jj_gen = 0;
        for (int i = 0; i < 13; i++) {
            jj_la1[i] = -1;
        }
    }

    /** Constructor. */
    public JSONParser(java.io.Reader stream) {
        jj_input_stream = new JavaCharStream(stream, 1, 1);
        token_source = new JSONParserTokenManager(jj_input_stream);
        token = new Token();
        token.next = jj_nt = token_source.getNextToken();
        jj_gen = 0;
        for (int i = 0; i < 13; i++) {
            jj_la1[i] = -1;
        }
    }

    /** Reinitialise. */
    public void ReInit(java.io.Reader stream) {
        if (jj_input_stream == null) {
            jj_input_stream = new JavaCharStream(stream, 1, 1);
        } else {
            jj_input_stream.ReInit(stream, 1, 1);
        }
        if (token_source == null) {
            token_source = new JSONParserTokenManager(jj_input_stream);
        }

        token_source.ReInit(jj_input_stream);
        token = new Token();
        token.next = jj_nt = token_source.getNextToken();
        jj_gen = 0;
        for (int i = 0; i < 13; i++) {
            jj_la1[i] = -1;
        }
    }

    /** Constructor with generated Token Manager. */
    public JSONParser(JSONParserTokenManager tm) {
        token_source = tm;
        token = new Token();
        token.next = jj_nt = token_source.getNextToken();
        jj_gen = 0;
        for (int i = 0; i < 13; i++) {
            jj_la1[i] = -1;
        }
    }

    /** Reinitialise. */
    public void ReInit(JSONParserTokenManager tm) {
        token_source = tm;
        token = new Token();
        token.next = jj_nt = token_source.getNextToken();
        jj_gen = 0;
        for (int i = 0; i < 13; i++) {
            jj_la1[i] = -1;
        }
    }

    private Token jj_consume_token(int kind) throws ParseException {
        Token oldToken = token;
        if ((token = jj_nt).next != null) {
            jj_nt = jj_nt.next;
        } else {
            jj_nt = jj_nt.next = token_source.getNextToken();
        }
        if (token.kind == kind) {
            jj_gen++;
            return token;
        }
        jj_nt = token;
        token = oldToken;
        jj_kind = kind;
        throw generateParseException();
    }


    /** Get the next Token. */
    final public Token getNextToken() {
        if ((token = jj_nt).next != null) {
            jj_nt = jj_nt.next;
        } else {
            jj_nt = jj_nt.next = token_source.getNextToken();
        }
        jj_gen++;
        return token;
    }

    /** Get the specific Token. */
    final public Token getToken(int index) {
        Token t = token;
        for (int i = 0; i < index; i++) {
            if (t.next != null) {
                t = t.next;
            } else {
                t = t.next = token_source.getNextToken();
            }
        }
        return t;
    }

    private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
    private int[] jj_expentry;
    private int jj_kind = -1;

    /** Generate ParseException. */
    public ParseException generateParseException() {
        jj_expentries.clear();
        boolean[] la1tokens = new boolean[29];
        if (jj_kind >= 0) {
            la1tokens[jj_kind] = true;
            jj_kind = -1;
        }
        for (int i = 0; i < 13; i++) {
            if (jj_la1[i] == jj_gen) {
                for (int j = 0; j < 32; j++) {
                    if ((jj_la1_0[i] & (1 << j)) != 0) {
                        la1tokens[j] = true;
                    }
                }
            }
        }
        for (int i = 0; i < 29; i++) {
            if (la1tokens[i]) {
                jj_expentry = new int[1];
                jj_expentry[0] = i;
                jj_expentries.add(jj_expentry);
            }
        }
        int[][] exptokseq = new int[jj_expentries.size()][];
        for (int i = 0; i < jj_expentries.size(); i++) {
            exptokseq[i] = jj_expentries.get(i);
        }
        return new ParseException(token, exptokseq, tokenImage);
    }

    private int trace_indent = 0;
    private boolean trace_enabled;

    /** Trace enabled. */
    final public boolean trace_enabled() {
        return trace_enabled;
    }

    /** Enable tracing. */
    final public void enable_tracing() {
    }

    /** Disable tracing. */
    final public void disable_tracing() {
    }

}