/
chimera.json.parser.pas
406 lines (376 loc) · 11.6 KB
/
chimera.json.parser.pas
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
// *****************************************************************************
//
// chimera.json.parser;
//
// JSON Chimera project for Delphi
//
// Copyright (c) 2012 by Sivv LLC, All Rights Reserved
//
// Information about this product can be found at
// http://arcana.sivv.com/chimera
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// *****************************************************************************
unit chimera.json.parser;
interface
uses System.SysUtils, System.Classes, System.Generics.Collections,
System.Types, System.Rtti, chimera.json;
type
{$SCOPEDENUMS ON}
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
EParseException = class(Exception)
end;
TParser = class(TObject)
const
Operators : set of Char = [ '{', '}', '[', ']', ',', '"', ':'];
type
TParseToken = (&String, Colon, OpenObject, CloseObject, OpenArray, CloseArray, Comma, EOF, MaxOp, Value);
private
FFmt : TFormatSettings;
FText : string;
FTextLength : integer;
FIndex : integer;
FToken : TParseToken;
FTokenValue : TMultiValue;
FOperatorStack : TStack<TParseToken>;
FValueStack : TStack<TMultiValue>;
FTmpValue : TStringBuilder;
FTmpIdent : TStringBuilder;
function GetToken : boolean;
function ParseArray: IJSONArray; overload;
function ParseObject: IJSONObject;
protected
function OperatorToStr(Token : TParseToken) : string;
public
constructor Create; virtual;
destructor Destroy; override;
function Execute(const AText : string) : IJSONObject;
function ExecuteForArray(const AText : string) : IJSONArray;
class function Parse(const AText : string) : IJSONObject;
class function ParseArray(const AText : string) : IJSONArray; overload;
end;
implementation
uses System.Character, System.Variants;
{ TParser }
constructor TParser.Create;
begin
inherited Create;
FOperatorStack := TStack<TParseToken>.Create;
FValueStack := TStack<TMultiValue>.Create;
FFmt := TFormatSettings.Create('en-us');
FTmpValue := TStringBuilder.Create;
FTmpIdent := TStringBuilder.Create;
end;
destructor TParser.Destroy;
begin
FOperatorStack.Free;
FValueStack.Free;
FTmpValue.Free;
FTmpIdent.Free;
inherited;
end;
function TParser.OperatorToStr(Token : TParseToken) : string;
begin
case Token of
TParser.TParseToken.String:
Result := 'String';
TParser.TParseToken.Colon:
Result := 'Colon';
TParser.TParseToken.OpenObject:
Result := 'OpenObject';
TParser.TParseToken.CloseObject:
Result := 'CloseObject';
TParser.TParseToken.OpenArray:
Result := 'OpenArray';
TParser.TParseToken.CloseArray:
Result := 'CloseArray';
TParser.TParseToken.Comma:
Result := 'Comma';
TParser.TParseToken.EOF:
Result := 'EOF';
TParser.TParseToken.MaxOp:
Result := 'MaxOp';
TParser.TParseToken.Value:
Result := 'Value';
end;
end;
function TParser.GetToken: boolean;
var
sb : TStringBuilder;
d : Double;
b : boolean;
i : integer;
iStart, iLen : integer;
begin
FTmpIdent.Clear;
while FIndex <= FTextLength do
begin
inc(FIndex);
if FText.Chars[FIndex] in Operators then
begin
// Is an Operator
FTmpIdent.Append(FText.Chars[FIndex]);
break;
end else if (FText.Chars[FIndex] <= Char($20)) then
continue
else if (TCharacter.IsLetterOrDigit(FText.Chars[FIndex])) then
begin
// Is an identifier or value
iStart := FIndex;
iLen := 0;
while (FIndex < FTextLength) do
begin
if ( not (FText.Chars[FIndex] in ['0'..'9', 'A'..'Z','a'..'z','.'])) then //.isLetterOrDigit(FText[FIndex])) and (FText[FIndex] <> FFmt.DecimalSeparator)) then
break;
if (FIndex > iStart) then
begin
if ( not (FText.Chars[FIndex-1] in ['0'..'9', 'A'..'Z','a'..'z','.'])) then //.isLetterOrDigit(FText[FIndex])) and (FText[FIndex] <> FFmt.DecimalSeparator)) then
//if (FIndex > iStart) and (( not TCharacter.isLetterOrDigit(FText[FIndex-1])) and (FText[FIndex-1] <> FFmt.DecimalSeparator)) then
break;
inc(iLen,2);
FTmpIdent.Append(FText.Chars[FIndex-1]);
FTmpIdent.Append(FText.Chars[FIndex]);
end else
begin
inc(iLen);
FTmpIdent.Append(FText.Chars[FIndex]);
end;
inc(FIndex,2); // marginally faster to skip by twos, moreso on big tokens
end;
if (FIndex > iStart) and ( not (FText.Chars[FIndex-1] in ['0'..'9', 'A'..'Z','a'..'z','.'])) then //.isLetterOrDigit(FText[FIndex])) and (FText[FIndex] <> FFmt.DecimalSeparator)) then
//if (FIndex > iStart) and ( not TCharacter.isLetterOrDigit(FText[FIndex-1])) and (FText[FIndex-1] <> FFmt.DecimalSeparator) then
dec(FIndex)
else
begin
inc(iLen);
FTmpIdent.Append(FText.Chars[FIndex-1]);
end;
dec(FIndex);
break;
end;
// Ignore Everything Else
end;
if FTmpIdent.Length > 0 then
begin
case FTmpIdent.Chars[0] of
'{': FToken := TParseToken.OpenObject;
'}': FToken := TParseToken.CloseObject;
'[': FToken := TParseToken.OpenArray;
']': FToken := TParseToken.CloseArray;
',': FToken := TParseToken.Comma;
':': FToken := TParseToken.Colon;
'"':
begin
FToken := TParseToken.String;
FTmpValue.Clear;
for i := FIndex+1 to FTextLength do
begin
if (FText.Chars[i] = '"') then
begin
if (FText.Chars[i-1] <> '\') then
break;
if (FText.Chars[i-2] = '\') then
break;
FTmpValue.Append('"');
end else
begin
FTmpValue.Append(FText.Chars[i]);
end;
end;
FIndex := i;
FTokenValue.Initialize(FTmpValue.ToString);
end;
else
begin
FToken := TParseToken.Value;
if TryStrToFloat(FTmpIdent.ToString,d, FFmt) then
FTokenValue.Initialize(d)
else if TryStrToBool(FTmpIdent.ToString,b) then
FTokenValue.Initialize(b)
else if FTmpIdent.ToString = 'null' then
FTokenValue.InitializeNull
else
raise EParseException.Create('Unexpected Value');
end;
end;
end else
begin
FToken := TParseToken.EOF;
end;
Result := False;
end;
function TParser.ParseArray : IJSONArray;
begin
if FToken <> TParseToken.OpenArray then
raise Exception.Create('Array Expected');
Result := JSONArray;
GetToken;
while FToken <> TParseToken.CloseArray do
begin
case FToken of
TParser.TParseToken.String:
Result.Add(@FTokenValue);
//Result.Add(FTokenValue.StringValue);
TParser.TParseToken.OpenObject:
Result.Add(ParseObject);
TParser.TParseToken.OpenArray:
Result.Add(ParseArray);
TParser.TParseToken.Value:
case FTokenValue.ValueType of
TJSONValueType.string:
Result.Add(FTokenValue.StringValue);
TJSONValueType.number:
Result.Add(FTokenValue.NumberValue);
TJSONValueType.array:
Result.Add(FTokenValue.ArrayValue);
TJSONValueType.object:
Result.Add(FTokenValue.ObjectValue);
TJSONValueType.boolean:
Result.Add(FTokenValue.IntegerValue <> 0);
TJSONValueType.null:
Result.AddNull;
end;
TParser.TParseToken.CloseObject,
TParser.TParseToken.CloseArray,
TParser.TParseToken.Comma,
TParser.TParseToken.EOF,
TParser.TParseToken.MaxOp,
TParser.TParseToken.Colon:
if FToken <> TParseToken.Colon then
raise Exception.Create('Value Expected');
end;
GetToken;
if not (FToken in [TParseToken.Comma, TParseToken.CloseArray]) then
begin
raise Exception.Create('Comma or Close Array Expected');
end;
if FToken = TParseToken.Comma then
GetToken;
end;
end;
function TParser.ParseObject : IJSONObject;
var
sName : String;
begin
if FToken <> TParseToken.OpenObject then
raise Exception.Create('Object Expected');
Result := JSON;
GetToken;
while FToken <> TParseToken.CloseObject do
begin
if FToken <> TParseToken.String then
raise Exception.Create('String Expected');
sName := FTokenValue.StringValue;
GetToken;
if FToken <> TParseToken.Colon then
raise Exception.Create('Colon Expected');
GetToken;
case FToken of
TParser.TParseToken.String:
Result.Raw[sName] := @FTokenValue;
TParser.TParseToken.OpenObject:
Result.Objects[sName] := ParseObject;
TParser.TParseToken.OpenArray:
Result.Arrays[sName] := ParseArray;
TParser.TParseToken.Value:
case FTokenValue.ValueType of
TJSONValueType.string:
Result.Strings[sName] := FTokenValue.StringValue;
TJSONValueType.number:
Result.Numbers[sName] := FTokenValue.NumberValue;
TJSONValueType.array:
Result.Arrays[sName] := FTokenValue.ArrayValue;
TJSONValueType.object:
Result.Objects[sName] := FTokenValue.ObjectValue;
TJSONValueType.boolean:
Result.Booleans[sName] := FTokenValue.IntegerValue <> 0;
TJSONValueType.null:
Result.AddNull(sName);
end;
TParser.TParseToken.CloseObject,
TParser.TParseToken.CloseArray,
TParser.TParseToken.Comma,
TParser.TParseToken.EOF,
TParser.TParseToken.MaxOp,
TParser.TParseToken.Colon:
if FToken <> TParseToken.Colon then
raise Exception.Create('Value Expected');
end;
GetToken;
if not (FToken in [TParseToken.Comma, TParseToken.CloseObject]) then
begin
raise Exception.Create('Comma or Close Object Expected');
end;
if FToken = TParseToken.Comma then
GetToken;
end;
end;
function TParser.Execute(const AText: string): IJSONObject;
begin
if Trim(AText) = '' then
begin
Result := JSON;
exit;
end;
FIndex := -1;
FText := AText;
FTextLength := Length(AText);
if GetToken then
exit;
Result := ParseObject;
end;
function TParser.ExecuteForArray(const AText : string) : IJSONArray;
begin
if Trim(AText) = '' then
begin
Result := JSONArray;
exit;
end;
FIndex := -1;
FText := AText;
FTextLength := Length(AText);
if GetToken then
exit;
Result := ParseArray;
end;
class function TParser.Parse(const AText: string): IJSONObject;
var
p : TParser;
begin
p := TParser.Create;
try
Result := p.Execute(AText);
finally
p.Free;
end;
end;
class function TParser.ParseArray(const AText : string) : IJSONArray;
var
p : TParser;
begin
p := TParser.Create;
try
Result := p.ExecuteForArray(AText);
finally
p.Free;
end;
end;
end.