-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathIonTextReader.ts
440 lines (405 loc) · 11.4 KB
/
IonTextReader.ts
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
/*!
* Copyright 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/
// Text reader. This is a user reader built on top of
// the IonParserTextRaw parser.
//
// Handles system symbols, and conversion from the parsed
// input string to the desired Javascript value (scalar or
// object, such as IonValue).
import IntSize from "./IntSize";
import { Catalog } from "./IonCatalog";
import { Decimal } from "./IonDecimal";
import {
defaultLocalSymbolTable,
LocalSymbolTable,
} from "./IonLocalSymbolTable";
import { get_ion_type, ParserTextRaw } from "./IonParserTextRaw";
import { Reader } from "./IonReader";
import { StringSpan } from "./IonSpan";
import { ion_symbol_table, makeSymbolTable } from "./IonSymbols";
import { fromBase64 } from "./IonText";
import { Timestamp } from "./IonTimestamp";
import { IonType } from "./IonType";
import { IonTypes } from "./IonTypes";
import { isSafeInteger } from "./util";
const BEGINNING_OF_CONTAINER = -2; // cloned from IonParserTextRaw
const EOF = -1;
const T_IDENTIFIER = 9;
const T_STRING1 = 11;
const T_CLOB2 = 14;
const T_CLOB3 = 15;
const T_STRUCT = 19;
export class TextReader implements Reader {
private readonly _parser: ParserTextRaw;
private _depth: number;
private readonly _cat: Catalog;
private _symtab: LocalSymbolTable;
private _type: IonType | null;
private _raw_type: number | undefined;
private _raw: any | undefined;
constructor(source: StringSpan, catalog?: Catalog) {
if (!source) {
throw new Error("a source Span is required to make a reader");
}
this._parser = new ParserTextRaw(source);
this._depth = 0;
this._cat = catalog ? catalog : new Catalog();
this._symtab = defaultLocalSymbolTable();
this._type = null;
this._raw_type = undefined;
this._raw = undefined;
}
load_raw() {
const t: TextReader = this;
if (t._raw !== undefined) {
return;
}
if (t._raw_type === T_CLOB2 || t._raw_type === T_CLOB3) {
t._raw = t._parser.get_value_as_uint8array(t._raw_type);
} else {
t._raw = t._parser.get_value_as_string(t._raw_type!);
}
}
skip_past_container() {
let type;
const d = this.depth(); // we want to have read the EOC that matches the container we just saw
this.stepIn();
while (this.depth() > d) {
type = this.next();
if (type === null) {
// end of container
this.stepOut();
} else if (type.isContainer && !this.isNull()) {
this.stepIn();
}
}
}
isIVM(input: string, depth: number, annotations: string[]): boolean {
if (depth > 0) {
return false;
}
const ivm = "$ion_1_0";
const prefix = "$ion_";
if (input.length < ivm.length || annotations.length > 0) {
return false;
}
let i = 0;
while (i < prefix.length) {
if (prefix.charAt(i) !== input.charAt(i)) {
return false;
}
i++;
}
while (i < input.length && input.charAt(i) != "_") {
const ch = input.charAt(i);
if (ch < "0" || ch > "9") {
return false;
}
i++;
}
i++;
while (i < input.length) {
const ch = input.charAt(i);
if (ch < "0" || ch > "9") {
return false;
}
i++;
}
if (input !== ivm) {
throw new Error("Only Ion version 1.0 is supported.");
}
return true;
}
isLikeIVM(): boolean {
return false;
}
position() {
return this._parser.source().position();
}
next() {
this._raw = undefined;
if (this._raw_type === EOF) {
return null;
}
if (
this._raw_type !== BEGINNING_OF_CONTAINER &&
!this.isNull() &&
this._type &&
this._type.isContainer
) {
this.skip_past_container();
}
const p: ParserTextRaw = this._parser;
for (;;) {
this._raw_type = p.next();
if (this._raw_type === T_IDENTIFIER) {
if (this._depth > 0) {
break;
}
this.load_raw();
if (!this.isIVM(this._raw, this.depth(), this.annotations())) {
break;
}
this._symtab = defaultLocalSymbolTable();
this._raw = undefined;
this._raw_type = undefined;
} else if (this._raw_type === T_STRING1) {
if (this._depth > 0) {
break;
}
this.load_raw();
if (this._raw !== "$ion_1_0") {
break;
}
this._raw = undefined;
this._raw_type = undefined;
} else if (this._raw_type === T_STRUCT) {
if (p.annotations().length !== 1) {
break;
}
if (p.annotations()[0].getText() != ion_symbol_table) {
break;
}
this._type = get_ion_type(this._raw_type);
this._symtab = makeSymbolTable(this._cat, this, this._symtab);
this._raw = undefined;
this._raw_type = undefined;
} else {
break;
}
}
// for system value (IVM's and symbol table's) we continue
// around this
this._type = get_ion_type(this._raw_type!);
return this._type;
}
stepIn() {
if (!this._type!.isContainer) {
throw new Error("can't step in to a scalar value");
}
if (this.isNull()) {
throw new Error("Can't step into a null container");
}
this._parser.clearFieldName();
this._type = null;
this._raw_type = BEGINNING_OF_CONTAINER;
this._depth++;
}
stepOut() {
this._parser.clearFieldName();
while (this._raw_type != EOF) {
this.next();
}
this._raw_type = undefined;
if (this._depth <= 0) {
throw new Error("Cannot stepOut any further, already at top level");
}
this._depth--;
}
type(): IonType | null {
return this._type;
}
depth(): number {
return this._depth;
}
fieldName(): string | null {
const str = this._parser.fieldName();
if (str !== null) {
const raw_type = this._parser.fieldNameType();
if (raw_type === T_IDENTIFIER && str.length > 1 && str[0] === "$") {
const tempStr = str.substr(1, str.length);
if (+tempStr === +tempStr) {
// look up sid, +str === +str is a one line is integer hack
const symbol = this._symtab.getSymbolText(Number(tempStr));
if (symbol === undefined) {
throw new Error(
"Unresolvable symbol ID, symboltokens unsupported."
);
}
return symbol;
}
}
}
return str;
}
annotations(): string[] {
return this._parser.annotations().map((st) => {
const text = st.getText();
if (text !== null) {
return text;
} else {
const symbol = this._symtab.getSymbolText(st.getSid());
if (symbol === undefined || symbol === null) {
throw new Error("Unresolvable symbol ID, symboltokens unsupported.");
}
return symbol;
}
});
}
isNull(): boolean {
if (this._type === IonTypes.NULL) {
return true;
}
return this._parser.isNull();
}
_stringRepresentation(): string | null {
this.load_raw();
if (this.isNull()) {
return this._type === IonTypes.NULL ? "null" : "null." + this._type!.name;
}
return this._raw;
}
booleanValue(): boolean | null {
switch (this._type) {
case IonTypes.NULL:
return null;
case IonTypes.BOOL:
return this._parser.booleanValue();
}
throw new Error("Current value is not a Boolean.");
}
uInt8ArrayValue(): Uint8Array | null {
this.load_raw();
switch (this._type) {
case IonTypes.NULL:
return null;
case IonTypes.BLOB:
if (this.isNull()) {
return null;
}
return fromBase64(this._raw);
case IonTypes.CLOB:
if (this.isNull()) {
return null;
}
return this._raw;
}
throw new Error("Current value is not a blob or clob.");
}
decimalValue(): Decimal | null {
switch (this._type) {
case IonTypes.NULL:
return null;
case IonTypes.DECIMAL:
return Decimal.parse(this._stringRepresentation()!);
}
throw new Error("Current value is not a decimal.");
}
bigIntValue(): bigint | null {
switch (this._type) {
case IonTypes.NULL:
return null;
case IonTypes.INT:
return this._parser.bigIntValue();
}
throw new Error(
"bigIntValue() was called when the current value was a(n) " +
this._type!.name
);
}
intSize(): IntSize {
if (isSafeInteger(this.bigIntValue()!)) {
return IntSize.Number;
}
return IntSize.BigInt;
}
numberValue(): number | null {
switch (this._type) {
case IonTypes.NULL:
return null;
case IonTypes.FLOAT:
case IonTypes.INT:
return this._parser.numberValue();
}
throw new Error("Current value is not a float or int.");
}
stringValue(): string | null {
this.load_raw();
switch (this._type) {
case IonTypes.NULL:
return null;
case IonTypes.STRING:
if (this._parser.isNull()) {
return null;
}
return this._raw;
case IonTypes.SYMBOL:
if (this._parser.isNull()) {
return null;
}
if (
this._raw_type === T_IDENTIFIER &&
this._raw.length > 1 &&
this._raw.charAt(0) === "$".charAt(0)
) {
const tempStr = this._raw.substr(1, this._raw.length);
if (+tempStr === +tempStr) {
// look up sid, +str === +str is a one line is integer hack
const symbolId = Number(tempStr);
const symbol = this._symtab.getSymbolText(symbolId);
if (symbol === undefined) {
throw new Error(
"Unresolvable symbol ID, symboltokens unsupported."
);
}
return symbol;
}
}
return this._raw;
}
throw new Error("Current value is not a string or symbol.");
}
timestampValue(): Timestamp | null {
switch (this._type) {
case IonTypes.NULL:
return null;
case IonTypes.TIMESTAMP:
return Timestamp.parse(this._stringRepresentation()!);
}
throw new Error("Current value is not a timestamp.");
}
value(): any {
if (this._type && this._type.isContainer) {
if (this.isNull()) {
return null;
}
throw new Error(
"Unable to provide a value for " + this._type.name + " containers."
);
}
switch (this._type) {
case IonTypes.NULL:
return null;
case IonTypes.BLOB:
case IonTypes.CLOB:
return this.uInt8ArrayValue();
case IonTypes.BOOL:
return this.booleanValue();
case IonTypes.DECIMAL:
return this.decimalValue();
case IonTypes.INT:
return this.bigIntValue();
case IonTypes.FLOAT:
return this.numberValue();
case IonTypes.STRING:
case IonTypes.SYMBOL:
return this.stringValue();
case IonTypes.TIMESTAMP:
return this.timestampValue();
default:
throw new Error("There is no current value.");
}
}
}