-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.java
294 lines (256 loc) · 7.3 KB
/
Parser.java
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
import java.io.*;
import java.util.*;
public class Parser {
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("/home/hanshika/workspace/JSON/src/color.json");
int i;
char c;
ArrayList<Character> jsonList = new ArrayList<Character>();
while ((i = fr.read()) != -1) {
c = (char) i;
if (!Character.isWhitespace(c)) {
jsonList.add(c);
}
}
fr.close();
try{
Start s = new Start();
int index = s.matchStart(jsonList, 0);
if (index+1 == jsonList.size())
System.out.println("Valid JSON");
else
System.out.println("Invalid JSON");
}
catch(ParseException e)
{
System.out.println(e);
}
}
}
class ParseException extends Exception{
ParseException(String s)
{
super(s);
}
}
class Start{
public int matchStart(ArrayList<Character> jsonString,int index) throws ParseException
{
if(jsonString.get(index)=='[')
{
index++;
index=matchArray(jsonString,index);
}
else if(jsonString.get(index)=='{')
{ index++;
index=matchObject(jsonString,index);
}
return index;
}
public int matchObject(ArrayList<Character> jsonString,int index) throws ParseException
{
if(jsonString.get(index)!='}')
{
index=matchPair(jsonString,index);
while(jsonString.get(index)!='}'){
if(jsonString.get(index)!=','){
throw new ParseException("Invalid JSON \n Missing , or } in object at location "+index);
}
index++;
if (index >= jsonString.size()) {
throw new ParseException("Invalid JSON \n Missing } in object at location "+index);
}
index=matchPair(jsonString,index);
}
}
if (jsonString.get(index) != '}') {
throw new ParseException("Invalid JSON \n Missing } for object at location "+index);
}
return index;
}
public int matchPair(ArrayList<Character> jsonString,int index) throws ParseException
{
if(jsonString.get(index)!='"')
throw new ParseException("Invalid JSON \n expected string in object at location "+index);
index++;
index=matchString(jsonString,index);
if(jsonString.get(index)!=':')
throw new ParseException("Invalid JSON \n expected : in object at location "+index);
index++;
index=matchValue(jsonString,index);
return index;
}
public int matchArray(ArrayList<Character> jsonString,int index) throws ParseException
{
if(jsonString.get(index)!=']')
{
index=matchValue(jsonString,index);
while(jsonString.get(index)!=']'){
if(jsonString.get(index)!=','){
throw new ParseException("Invalid JSON \n Missing , or ] in array at location "+index);
}
index++;
if (index >= jsonString.size()) {
throw new ParseException("Invalid JSON \n Missing ] for array at location "+index);
}
index=matchValue(jsonString,index);
}
}
if (jsonString.get(index) != ']') {
throw new ParseException("Invalid JSON \n Missing ] for array at location "+index);
}
/*if(jsonString.get(index)==']')
index++;*/
return index;
}
public int matchValue(ArrayList<Character> jsonString,int index) throws ParseException
{
if(jsonString.get(index)>='0'&&jsonString.get(index)<='9')
index=matchNumber(jsonString,index);
//System.out.println(index);
else if(jsonString.get(index)=='n')
index=matchNull(jsonString,index);
else if(jsonString.get(index)=='t')
index=matchTrue(jsonString,index);
else if(jsonString.get(index)=='f')
index=matchFalse(jsonString,index);
else if(jsonString.get(index)=='[')
{
index=matchArray(jsonString,++index);
index++;
if (index >= jsonString.size()) {
throw new ParseException("Invalid JSON \n Missing ] for array at location "+index);
}
}
else if(jsonString.get(index)=='{')
{
index=matchObject(jsonString,++index);
index++;
if (index >= jsonString.size()) {
throw new ParseException("Invalid JSON \n Missing } for object at location "+index);
}
}
else if(jsonString.get(index)=='"')
{
index=matchString(jsonString,++index);
}
return index;
}
public int matchString(ArrayList<Character> jsonString,int index) throws ParseException
{
while(jsonString.get(index)>='!'&&jsonString.get(index)<='~' )
{
if(jsonString.get(index)=='\\')
{ index++;
if(jsonString.get(index)=='"'||jsonString.get(index)=='\\'||jsonString.get(index)=='/'||jsonString.get(index)=='b'||jsonString.get(index)=='f'||jsonString.get(index)=='n'||jsonString.get(index)=='r'||jsonString.get(index)=='t')
{
index++;
continue;
}
else
{
throw new ParseException("Invalid JSON \n invalid character at location "+index);
}
}
if(jsonString.get(index)=='"')
{
index++;
//System.out.println(index);
break;
}
index++;
if (index >= jsonString.size()) {
throw new ParseException("Invalid JSON \n Missing \" for String at location "+index);
}
}
return index;
}
public int matchNumber(ArrayList<Character> jsonString,int index) throws ParseException
{
if(jsonString.get(index)>='0'&&jsonString.get(index)<='9')
{
while(jsonString.get(index)>='0'&&jsonString.get(index)<='9')
index++;
if(jsonString.get(index)=='.')
{
index++;
if(!(jsonString.get(index)>='0'&&jsonString.get(index)<='9'))
throw new ParseException("Invalid JSON \n expected digits after dot at "+index);
index=matchDigits(jsonString,index);
}
if(jsonString.get(index)=='e'||jsonString.get(index)=='E')
{
index++;
if(!(jsonString.get(index)>='0'&&jsonString.get(index)<='9'))
throw new ParseException("Invalid JSON \n expected digits after exponent at "+index);
index=matchExponent(jsonString,index);
}
}
return index;
}
public int matchExponent(ArrayList<Character> jsonString,int index)
{
if(jsonString.get(index)=='+'||jsonString.get(index)=='-'||(jsonString.get(index)>='0'&&jsonString.get(index)<='9'))
{
index++;
index=matchDigits(jsonString,index);
}
return index;
}
public int matchDigits(ArrayList<Character> jsonString,int index)
{
while(jsonString.get(index)>='0'&&jsonString.get(index)<='9')
index++;
return index;
}
public int matchNull(ArrayList<Character> jsonString,int index)
{
if(jsonString.get(index)=='n')
{ index++;
if(jsonString.get(index)=='u')
{ index++;
if(jsonString.get(index)=='l')
{index++;
if(jsonString.get(index)=='l')
index++;
}
}
}
return index;
}
public int matchTrue(ArrayList<Character> jsonString,int index)
{
if(jsonString.get(index)=='t')
{ index++;
if(jsonString.get(index)=='r')
{ index++;
if(jsonString.get(index)=='u')
{index++;
if(jsonString.get(index)=='e')
index++;
}
}
}
return index;
}
public int matchFalse(ArrayList<Character> jsonString,int index)
{
if(jsonString.get(index)=='f')
{ index++;
if(jsonString.get(index)=='a')
{ index++;
if(jsonString.get(index)=='l')
{index++;
if(jsonString.get(index)=='s')
{
index++;
if(jsonString.get(index)=='e')
index++;
}
}
}
}
return index;
}
}