1 /* |
|
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 /* |
|
27 * This is the JavaCC grammar file for a JSON parser. The productions are |
|
28 * based on syntax diagrams as specified at json.org |
|
29 * Every type of JSON is represent by a JSONElement. JSON Objects are mapped to |
|
30 * by LinkedHashMap via JSONObject. JSON Arrays are mapped to ArrayList via JSONArray |
|
31 * String, Number, Boolean and null are wrapped into a JSONPrimitive. |
|
32 * Control characters inside a string (JSONPrimitive(String)) are not escaped and are passed through |
|
33 * to ouput JSON string generated by toJsonString method of JSONElement. |
|
34 * Inside a String, only backslash ('\') and double quote ('"') characters |
|
35 * are escaped by a backslash, if they are not already escaped. |
|
36 * Applications consuming json string generated by JSON* objects must take care |
|
37 * to escape control characters within strings by prefixing with a backslash character, |
|
38 * if not already escaped. |
|
39 */ |
|
40 options { |
|
41 STATIC=false; |
|
42 ERROR_REPORTING=true; |
|
43 JAVA_UNICODE_ESCAPE=true; |
|
44 UNICODE_INPUT=true; |
|
45 IGNORE_CASE=true; |
|
46 CACHE_TOKENS=true; |
|
47 // DEBUG_PARSER=true; |
|
48 // DEBUG_LOOKAHEAD=true; |
|
49 // DEBUG_TOKEN_MANAGER=true; |
|
50 } |
|
51 |
|
52 PARSER_BEGIN(JSONParser) |
|
53 |
|
54 package com.oracle.jmx.remote.rest.json.parser; |
|
55 |
|
56 import java.io.StringReader; |
|
57 import com.oracle.jmx.remote.rest.json.*; |
|
58 |
|
59 public class JSONParser { |
|
60 |
|
61 public JSONParser(String input) { |
|
62 this(new StringReader(input)); |
|
63 } |
|
64 |
|
65 public JSONElement parse() throws ParseException { |
|
66 return jsonValue(); |
|
67 } |
|
68 } |
|
69 |
|
70 PARSER_END(JSONParser) |
|
71 |
|
72 SKIP: { |
|
73 " " | "\b" | "\t" | "\n" | "\r" | "\f" |
|
74 } |
|
75 |
|
76 TOKEN:{ |
|
77 <INTEGER_LITERAL : (["-"])? (<DIGITS>)> |
|
78 | < FLOATING_POINT_LITERAL: |
|
79 <INTEGER_LITERAL> <FRAC> |
|
80 | <INTEGER_LITERAL> <EXPONENT> |
|
81 | <INTEGER_LITERAL> <FRAC> <EXPONENT> |
|
82 > |
|
83 | < #FRAC: "." <DIGITS>> |
|
84 | < #EXPONENT: ["e","E"] (["+","-"])? <DIGITS> > |
|
85 | < #DIGITS : (<DIGIT>)+> |
|
86 | < #DIGIT: ["0"-"9"]> |
|
87 | <QUOTED_STRING: "\"" ((~["\"","\\"]) | ("\\" ( ["n","t","b","r","f","\\","\""])))* "\""> |
|
88 | <BOOL_LITERAL : "true" | "false"> |
|
89 | <NULL: "null"> |
|
90 } |
|
91 |
|
92 JSONElement jsonValue() : { |
|
93 JSONElement x; |
|
94 }{ |
|
95 ( x = object() |
|
96 | x = list() |
|
97 | x = string() |
|
98 | x = number() |
|
99 | x = boolVal() |
|
100 | x = nullVal() |
|
101 ) |
|
102 { return x; } |
|
103 } |
|
104 |
|
105 JSONObject object() : { |
|
106 final JSONObject jobject = new JSONObject(); |
|
107 JSONPrimitive key; |
|
108 JSONElement value; |
|
109 }{ |
|
110 "{" |
|
111 (key = string()) |
|
112 ":" |
|
113 value = jsonValue() |
|
114 { jobject.put((String)key.getValue(), value);} |
|
115 ( |
|
116 "," |
|
117 (key = string()) |
|
118 ":" |
|
119 value = jsonValue() |
|
120 { jobject.put((String)key.getValue(), value);} |
|
121 )* |
|
122 "}" |
|
123 { return jobject; } |
|
124 } |
|
125 |
|
126 JSONArray list() : { |
|
127 final JSONArray jarray = new JSONArray(); |
|
128 JSONElement value; |
|
129 }{ |
|
130 "[" |
|
131 value = jsonValue() |
|
132 { jarray.add(value);} |
|
133 ( |
|
134 "," |
|
135 value = jsonValue() |
|
136 { jarray.add(value); } |
|
137 )* |
|
138 "]" |
|
139 |
|
140 { return jarray; } |
|
141 } |
|
142 |
|
143 JSONPrimitive nullVal(): {} { |
|
144 <NULL> |
|
145 { return null; } |
|
146 } |
|
147 |
|
148 JSONPrimitive boolVal(): {} |
|
149 { |
|
150 <BOOL_LITERAL> |
|
151 {return new JSONPrimitive(Boolean.parseBoolean(token.image));} |
|
152 } |
|
153 |
|
154 JSONPrimitive number(): { |
|
155 Token t; |
|
156 }{ |
|
157 (t = <INTEGER_LITERAL> { return new JSONPrimitive(Long.parseLong(t.image));}) |
|
158 | (t = <FLOATING_POINT_LITERAL> { return new JSONPrimitive(Double.parseDouble(t.image));}) |
|
159 } |
|
160 |
|
161 JSONPrimitive string() : { |
|
162 Token t; |
|
163 }{ |
|
164 (t = <QUOTED_STRING>) |
|
165 { return new JSONPrimitive(t.image.substring(1,t.image.length()-1)); } |
|
166 } |
|