-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathJsonURL.js
1834 lines (1578 loc) · 45.5 KB
/
JsonURL.js
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
MIT License
Copyright (c) 2020 David MacCormack
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.
*/
/** @module JsonURL */
"use strict";
import * as Err from "./error.js";
import * as Chars from "./chars.js";
import { JsonURLParseOptions } from "./JsonURLParseOptions.js";
import { JsonURLStringifyOptions } from "./JsonURLStringifyOptions.js";
import { setupToJsonURLText, toJsonURLText } from "./proto.js";
const RX_DECODE_SPACE = /\+/g;
const RX_ENCODE_SPACE = / /g;
const RX_AQF_DECODE_ESCAPE = /(![\s\S]?)/g;
//
// patterns for use with RegEx.test().
// DO NOT PUT //g ON THESE!!!
//
const RX_ENCODE_STRING_SAFE =
/^[-A-Za-z0-9._~!$*;@?/ ][-A-Za-z0-9._~!$*;@?/' ]*$/;
const RX_ENCODE_STRING_QSAFE = /^[-A-Za-z0-9._~!$*,;@?/(): ]+$/;
const RX_ENCODE_NUMBER = /^-?\d+(?:\.\d+)?(?:[eE][-]?\d+)?$/;
const RX_ENCODE_NUMBER_PLUS = /^-?\d+(?:\.\d+)?[eE]\+\d+$/;
const RX_ENCODE_NUMBER_SPACE = /^-?\d+(?:\.\d+)?[eE] \d+$/;
const RX_ENCODE_BASE = /[(),:]|%2[04]|%3B/gi;
const RX_ENCODE_BASE_MAP = {
"%20": "+",
"%24": "$",
"(": "%28",
")": "%29",
",": "%2C",
":": "%3A",
"%3B": ";",
};
const RX_ENCODE_AQF = /[!(),:]|%2[01489BC]|%3[AB]/gi;
const RX_ENCODE_AQF_MAP = {
"%20": "+",
"%21": "!!",
"!": "!!",
"%24": "$",
"%28": "!(",
"(": "!(",
"%29": "!)",
")": "!)",
"+": "!+",
"%2B": "!+",
"%2C": "!,",
",": "!,",
"%3A": "!:",
":": "!:",
"%3B": ";",
};
const CHAR_BANG = 0x21;
const CHAR_PERCENT = 0x25;
const CHAR_QUOTE = 0x27;
const CHAR_PAREN_OPEN = 0x28;
const CHAR_PAREN_CLOSE = 0x29;
const CHAR_PLUS = 0x2b;
const CHAR_COMMA = 0x2c;
const CHAR_DASH = 0x2d;
const CHAR_DOT = 0x2e;
const CHAR_COLON = 0x3a;
const CHAR_EQUALS = 0x3d;
const CHAR_AMP = 0x26;
const CHAR_0 = 0x30;
const CHAR_A = 0x41;
const CHAR_E = 0x45;
const CHAR_a = 0x61;
const CHAR_e = 0x65;
const CHAR_f = 0x66;
const CHAR_l = 0x6c;
const CHAR_n = 0x6e;
const CHAR_r = 0x72;
const CHAR_s = 0x73;
const CHAR_t = 0x74;
const CHAR_u = 0x75;
const STATE_PAREN = 1;
const STATE_IN_ARRAY = 2;
const STATE_ARRAY_AFTER_ELEMENT = 3;
const STATE_OBJECT_HAVE_KEY = 4;
const STATE_OBJECT_AFTER_ELEMENT = 5;
const STATE_IN_OBJECT = 6;
const UNESCAPE = new Array(111);
UNESCAPE[CHAR_BANG] = "!";
UNESCAPE[CHAR_PAREN_OPEN] = "(";
UNESCAPE[CHAR_PAREN_CLOSE] = ")";
UNESCAPE[CHAR_PLUS] = "+";
UNESCAPE[CHAR_COMMA] = ",";
UNESCAPE[CHAR_DASH] = "-";
UNESCAPE[CHAR_0] = "0";
UNESCAPE[CHAR_0 + 1] = "1";
UNESCAPE[CHAR_0 + 2] = "2";
UNESCAPE[CHAR_0 + 3] = "3";
UNESCAPE[CHAR_0 + 4] = "4";
UNESCAPE[CHAR_0 + 5] = "5";
UNESCAPE[CHAR_0 + 6] = "6";
UNESCAPE[CHAR_0 + 7] = "7";
UNESCAPE[CHAR_0 + 8] = "8";
UNESCAPE[CHAR_0 + 9] = "9";
UNESCAPE[CHAR_COLON] = ":";
UNESCAPE[CHAR_t] = "t";
UNESCAPE[CHAR_f] = "f";
UNESCAPE[CHAR_n] = "n";
const EMPTY_STRING = "";
const EMPTY_STRING_AQF = "!e";
const SPACE = " ";
const PLUS = "+";
function newEmptyString(pos, emptyOK) {
if (emptyOK) {
return EMPTY_STRING;
}
throw new SyntaxError(Err.fmt(Err.MSG_IMPLIED_STRING_EMPTY, pos));
}
function encodeStringLiteral(text, aqf) {
const re = aqf ? RX_ENCODE_AQF : RX_ENCODE_BASE;
const map = aqf ? RX_ENCODE_AQF_MAP : RX_ENCODE_BASE_MAP;
return encodeURIComponent(text).replace(re, function name(match) {
const ret = map[match];
return ret === undefined ? match : ret;
});
}
function hexDecodeOctet(text, pos, end) {
if (end <= pos + 1) {
throw new SyntaxError(Err.fmt(Err.MSG_BAD_PCTENC, pos));
}
const high = hexDecode(pos, text.charCodeAt(pos));
const low = hexDecode(pos, text.charCodeAt(pos + 1));
return (high << 4) | low;
}
function hexDecode(pos, c) {
switch (c) {
case CHAR_0:
return 0;
case CHAR_0 + 1:
return 1;
case CHAR_0 + 2:
return 2;
case CHAR_0 + 3:
return 3;
case CHAR_0 + 4:
return 4;
case CHAR_0 + 5:
return 5;
case CHAR_0 + 6:
return 6;
case CHAR_0 + 7:
return 7;
case CHAR_0 + 8:
return 8;
case CHAR_0 + 9:
return 9;
case CHAR_A:
case CHAR_a:
return 10;
case CHAR_A + 1:
case CHAR_a + 1:
return 11;
case CHAR_A + 2:
case CHAR_a + 2:
return 12;
case CHAR_A + 3:
case CHAR_a + 3:
return 13;
case CHAR_A + 4:
case CHAR_a + 4:
return 14;
case CHAR_A + 5:
case CHAR_a + 5:
return 15;
default:
throw new SyntaxError(Err.fmt(Err.MSG_BAD_PCTENC, pos));
}
}
function isBang(s, offset) {
return (
s.charCodeAt(offset - 1) === CHAR_BANG ||
(offset > 2 &&
s.charCodeAt(offset - 3) === CHAR_PERCENT &&
s.charCodeAt(offset - 2) === CHAR_0 + 2 &&
s.charCodeAt(offset - 1) === CHAR_0 + 1)
);
}
function isBoolNullNoPlusNumber(s) {
if (s === "true" || s === "false" || s === "null") {
return true;
}
return RX_ENCODE_NUMBER.test(s);
}
function toJsonURLText_Null(options) {
if (options.coerceNullToEmptyString) {
return toJsonURLText_EmptyString(options, false);
}
if (options.impliedStringLiterals) {
throw new SyntaxError(Err.MSG_IMPLIED_STRING_NULL);
}
return "null";
}
function toJsonURLText_EmptyString(options, isKey) {
const emptyOK = isKey
? options.allowEmptyUnquotedKeys
: options.allowEmptyUnquotedValues;
if (emptyOK) {
return EMPTY_STRING;
}
if (options.AQF) {
return EMPTY_STRING_AQF;
}
if (options.impliedStringLiterals) {
throw new SyntaxError(Err.MSG_IMPLIED_STRING_EMPTY);
}
return "''";
}
function toJsonURLText_Boolean() {
return this === true ? "true" : "false";
}
function toJsonURLText_Number(options) {
const ret = String(this);
if (options.impliedStringLiterals && ret.indexOf("+") !== -1) {
//
// I don't think this will happen, but just in case...
//
return encodeStringLiteral(ret, options);
}
return ret;
}
function toJsonURLText_String(options, depth, isKey) {
if (this.length === 0) {
return toJsonURLText_EmptyString(options, isKey);
}
if (options.impliedStringLiterals) {
return encodeStringLiteral(this, options.AQF);
}
if (isBoolNullNoPlusNumber(this)) {
//
// this string looks like a boolean, `null`, or number literal without
// a plus char
//
if (isKey === true) {
// keys are assumed to be strings
return this;
}
if (options.AQF) {
return "!" + this;
}
return "'" + this + "'";
}
if (RX_ENCODE_NUMBER_PLUS.test(this)) {
//
// this string looks like a number with an exponent that includes a `+`
//
if (options.AQF) {
return this.replace(PLUS, "!+");
}
return this.replace(PLUS, "%2B");
}
if (RX_ENCODE_NUMBER_SPACE.test(this)) {
//
// this string would look like a number if it were allowed to have a
// space represented as a plus
//
if (options.AQF) {
return "!" + this.replace(SPACE, "+");
}
return "'" + this.replace(SPACE, "+") + "'";
}
if (options.AQF) {
return encodeStringLiteral(this, true);
}
if (RX_ENCODE_STRING_SAFE.test(this)) {
//
// safe to use as long as I encode spaces
//
if (this.indexOf(SPACE) == -1) {
return this;
}
return this.replace(RX_ENCODE_SPACE, "+");
}
if (RX_ENCODE_STRING_QSAFE.test(this)) {
//
// safe to use as long as I quote it and encode spaces
//
if (this.indexOf(SPACE) == -1) {
return "'" + this + "'";
}
return "'" + this.replace(RX_ENCODE_SPACE, "+") + "'";
}
let ret = encodeStringLiteral(this);
if (ret.charCodeAt(0) == CHAR_QUOTE) {
//
// I need to encode the leading quote
//
return "%27" + ret.substring(1);
}
return ret;
}
function toJsonURLText_Array(options = {}, depth = 0) {
let ret = undefined;
this.forEach(function (e) {
if (typeof e === "function") {
if (!options.callFunctions) {
return;
}
while (typeof e === "function") {
e = e();
}
}
if (e === undefined) {
if (options.ignoreUndefinedArrayMembers) {
return;
}
e = toJsonURLText_Null(options);
} else if (e === null) {
if (options.ignoreNullArrayMembers) {
return;
}
e = toJsonURLText_Null(options);
} else {
e = toJsonURLText(e, options, depth + 1);
}
if (ret === undefined) {
ret = e;
} else if (!options.wwwFormUrlEncoded || depth > 0) {
ret += "," + e;
} else {
ret += "&" + e;
}
});
if (!options.isImplied || depth > 0) {
return ret === undefined ? "()" : "(" + ret + ")";
}
return ret === undefined ? EMPTY_STRING : ret;
}
function toJsonURLText_Object(options = {}, depth = 0) {
let ret = undefined;
const keys = Object.keys(this);
const obj = this;
keys.forEach(function (k) {
if (k === undefined || k === null) {
//
// I'm not sure this can actually happen. But, handling just in case.
//
return;
}
let v = obj[k];
if (typeof v === "function") {
if (!options.callFunctions) {
return;
}
while (typeof v === "function") {
v = v();
}
}
if (v === undefined) {
if (options.ignoreUndefinedObjectMembers) {
return;
}
v = toJsonURLText_Null(options);
} else if (v === null) {
if (options.ignoreNullObjectMembers) {
return;
}
v = toJsonURLText_Null(options);
} else {
v = toJsonURLText(v, options, depth + 1);
}
const jk = toJsonURLText(k, options, depth, true);
if (ret === undefined) {
if (!options.wwwFormUrlEncoded || depth > 0) {
ret = jk + ":" + v;
} else {
ret = jk + "=" + v;
}
} else {
if (!options.wwwFormUrlEncoded || depth > 0) {
ret += "," + jk + ":" + v;
} else {
ret += "&" + jk + "=" + v;
}
}
});
if (!options.isImplied || depth > 0) {
if (options.noEmptyComposite && ret === undefined) {
ret = ":";
}
return ret === undefined ? "()" : "(" + ret + ")";
}
return ret === undefined ? EMPTY_STRING : ret;
}
setupToJsonURLText({
toJsonURLText_Array,
toJsonURLText_Boolean,
toJsonURLText_Number,
toJsonURLText_Object,
toJsonURLText_String,
});
/**
* Base syntax parser.
* @private
*/
class Parser {
/**
* Construct a new instance.
* @param {JsonURLParseOptions} options options provided by the user
*/
constructor(text, pos, end, options) {
this.text = text;
this.pos = this.markPos = pos;
this.end = end;
this.options = options;
}
/**
* Skip zero or more amperands.
*/
skipAmps(leading = false) {
const text = this.text;
const end = this.end;
let pos = this.pos;
if (leading) {
//
// skip all leading amps and position `pos` at the first non-amp
// character (or EOF)
//
while (pos < end && text.charCodeAt(pos) === CHAR_AMP) {
pos++;
}
} else if (pos < end && text.charCodeAt(pos) === CHAR_AMP) {
//
// there is at least one amp
//
for (pos++; pos < end && text.charCodeAt(pos) === CHAR_AMP; pos++) {
// skip all consecutive amps
}
if (pos !== end) {
// one or more amps followed by additional text; caller
// is looking to consume a value separator so go back one char.
pos--;
}
}
this.pos = pos;
}
/**
* Read the next char code. If it is a structural character then the
* current position will be advanced and the char will be returned.
* Otherwise, the current position will remain unchanged and undefined
* will be returned.
*/
structChar(parenOnly = false) {
if (this.options.wwwFormUrlEncoded) {
const chr = this.text.charCodeAt(this.pos);
switch (chr) {
case CHAR_AMP:
case CHAR_EQUALS:
if (parenOnly) {
return undefined;
}
this.pos++;
return chr;
default:
break;
}
}
const pos = this.pos;
const c = this.ordinal();
switch (c) {
case CHAR_COMMA:
case CHAR_COLON:
if (parenOnly) {
this.pos = pos;
return undefined;
}
//fallthrough
case CHAR_PAREN_OPEN:
case CHAR_PAREN_CLOSE:
return c;
default:
this.pos = pos;
return undefined;
}
}
/**
* Read the next character code.
*
* If pos is undefined then the characther code will be read and returned,
* and the current position marker will be advanced. Otherwise, the
* character code at the given position will be read and returned by the
* current position marker will remain unchanged.
*
* AQF will apply percent decoding if necessary.
*
* Note, this will *not* perform UTF-8 decoding. That isn't necessary for
* the current use cases.
* @param pos an optional position to read from
*/
ordinal(pos) {
if (pos !== undefined) {
return this.text.charCodeAt(pos);
}
return this.text.charCodeAt(this.pos++);
}
/**
* Read and accept a single character code.
* @returns true if the expected character was accepted; false otherwise
*/
accept(c) {
const pos = this.pos;
if (this.ordinal() === c) {
return true;
}
this.pos = pos;
return false;
}
/**
* Effectively the same as this.accept(CHAR_PLUS) but allows CHAR_PLUS
* to be handled differently for AQF.
* @returns true if the expected character was accepted; false otherwise
*/
acceptPlus() {
//
// Not calling ordinal() because that would decode '+' to a space.
//
if (this.text.charCodeAt(this.pos) == CHAR_PLUS) {
this.pos++;
return true;
}
return false;
}
/**
* Test if all of the parse text has been consumed.
*/
done() {
return this.end <= this.pos;
}
validateLiteral(ret, mask) {
const text = this.text;
const end = this.end;
for (; ret < end; ret++) {
const c = text.charCodeAt(ret);
const bits = Chars.lookup(c) & mask;
switch (bits) {
case 0:
throw new SyntaxError(Err.fmt(Err.MSG_BADCHAR, ret));
case Chars.IS_STRUCT:
return ret;
case Chars.IS_QUOTE:
return ret + 1;
default:
continue;
}
}
}
/**
* Find the end index of the next literal.
*/
findLiteralEnd() {
const text = this.text;
const end = this.end;
let ret = this.pos;
const isQuote = text.charCodeAt(ret) === CHAR_QUOTE;
if (isQuote) {
ret++;
}
const mask = isQuote
? Chars.IS_QSTRSAFE | Chars.IS_STRUCT | Chars.IS_QUOTE
: Chars.IS_NSTRSAFE | Chars.IS_STRUCT;
ret = this.validateLiteral(ret, mask);
if (ret !== undefined) {
return ret;
}
if (isQuote) {
throw new SyntaxError(Err.fmt(Err.MSG_EXPECT_QUOTE, ret));
}
return end;
}
/**
* Test if the next character sequence represents the empty object.
*/
isEmptyObject() {
if (this.options.noEmptyComposite) {
const start = this.pos;
if (!this.accept(CHAR_COLON)) {
return false;
}
const pos = this.pos;
const ret = this.accept(CHAR_PAREN_CLOSE);
//
// this is non-obvious and deserves an explanation.
//
// If I do not match the empty object sequence then I effectively leave
// the current position the same as it was when I entered the function.
// If I do match the empty object sequence then I set the current
// position at the paren that closes it. This allows the caller to
// handle the case with just a small bit of additional logic.
//
this.pos = ret ? pos : start;
return ret;
}
return false;
}
/**
* Parse a single literal value.
*/
parseLiteral(isKey) {
const pos = this.pos;
const options = this.options;
const litend = this.findLiteralEnd();
if (isKey === undefined) {
isKey = this.ordinal(litend) == CHAR_COLON;
}
if (litend <= pos) {
const emptyOK = isKey
? this.options.allowEmptyUnquotedKeys
: this.options.allowEmptyUnquotedValues;
return newEmptyString(pos, emptyOK);
}
//
// I will consume up to this point
//
if (options.impliedStringLiterals === true) {
return this.parseStringLiteral(litend, true);
}
const tfn = this.parseTrueFalseNull(litend, isKey);
if (tfn !== undefined) {
return tfn;
}
const numval = this.parseNumberLiteral(litend, isKey);
if (numval !== undefined) {
return numval;
}
return this.parseStringLiteral(litend, false);
}
parseDigits(litend) {
let ret = false;
let pos;
while (this.pos < litend) {
switch (this.ordinal()) {
case CHAR_0:
case CHAR_0 + 1:
case CHAR_0 + 2:
case CHAR_0 + 3:
case CHAR_0 + 4:
case CHAR_0 + 5:
case CHAR_0 + 6:
case CHAR_0 + 7:
case CHAR_0 + 8:
case CHAR_0 + 9:
pos = this.pos;
ret = true;
continue;
default:
this.pos = pos;
return ret;
}
}
this.pos = pos;
return ret;
}
parseExponentValue(litend) {
const pos = this.pos;
if (litend <= pos) {
return false;
}
//
// consume plus or minus
//
this.acceptPlus() || this.accept(CHAR_DASH);
return this.parseDigits(litend);
}
parseExponent(litend) {
const pos = this.pos;
switch (this.ordinal()) {
case CHAR_E:
case CHAR_e:
if (this.parseExponentValue(litend)) {
return true;
}
break;
default:
break;
}
this.pos = pos;
return false;
}
parseFraction(litend) {
const pos = this.pos;
if (litend <= pos) {
return false;
}
if (this.accept(CHAR_DOT) && this.parseDigits(litend)) {
return true;
}
this.pos = pos;
return false;
}
parseInteger(litend) {
const pos = this.pos;
if (litend <= pos) {
return false;
}
if (this.accept(CHAR_0)) {
return true;
}
return this.parseDigits(litend);
}
parseNumberLiteral(litend, forceString) {
const text = this.text;
const pos = this.pos;
this.accept(CHAR_DASH);
if (this.parseInteger(litend)) {
this.parseFraction(litend);
this.parseExponent(litend);
if (this.pos === litend) {
//
// this literal is a number
//
const s = decodeURIComponent(text.substring(pos, litend));
return forceString ? s : Number(s);
}
}
this.pos = pos;
return undefined;
}
parseStringLiteral(litend, impliedString) {
const text = this.text;
const pos = this.pos;
let ret =
impliedString || text.charCodeAt(pos) !== CHAR_QUOTE
? text.substring(pos, litend)
: text.substring(pos + 1, litend - 1);
ret = decodeURIComponent(ret.replace(RX_DECODE_SPACE, SPACE));
this.pos = litend;
return ret;
}
parseTrueFalseNull(litend, forceString) {
const text = this.text;
const pos = this.pos;
let c1, c2, c3, c4, c5;
switch (litend - pos) {
case 4:
c1 = text.charCodeAt(pos);
c2 = text.charCodeAt(pos + 1);
c3 = text.charCodeAt(pos + 2);
c4 = text.charCodeAt(pos + 3);
if (c1 === CHAR_t && c2 === CHAR_r && c3 === CHAR_u && c4 === CHAR_e) {
this.pos = litend;
return forceString ? "true" : true;
}
if (c1 === CHAR_n && c2 === CHAR_u && c3 === CHAR_l && c4 === CHAR_l) {
this.pos = litend;
return forceString ? "null" : this.newNullValue();
}
break;
case 5:
c1 = text.charCodeAt(pos);
c2 = text.charCodeAt(pos + 1);
c3 = text.charCodeAt(pos + 2);
c4 = text.charCodeAt(pos + 3);
c5 = text.charCodeAt(pos + 4);
if (
c1 === CHAR_f &&
c2 === CHAR_a &&
c3 === CHAR_l &&
c4 === CHAR_s &&
c5 === CHAR_e
) {
this.pos = litend;
return forceString ? "false" : false;
}
break;
default:
break;
}
return undefined;
}
newEmptyValue() {
const options = this.options;
if (options.noEmptyComposite) {
return [];
}
const emptyValue = options.emptyValue;
if (typeof emptyValue === "function") {
return emptyValue();
}
return emptyValue;
}
newNullValue() {
const options = this.options;
let ret = options.nullValue;
if (typeof ret === "function") {
ret = ret();
}
if (ret == null && options.coerceNullToEmptyString) {
ret = EMPTY_STRING;
}
return ret;
}
}
/**
* Parser for Address Bar Query String Friendly (AQF) syntax.
* @private
*/
class ParserAQF extends Parser {
/**
* Construct a new instance.
* @param {JsonURLParseOptions} options options provided by the user
*/
constructor(text, pos, end, options) {
super(text, pos, end, options);
}
ordinal(pos) {
//
// decode position - use what's given; default to current position.
//
const dpos = pos || this.pos;
const c = this.text.charCodeAt(dpos);
let ret, cnt;
if (c === CHAR_PERCENT) {
ret = hexDecodeOctet(this.text, dpos + 1, this.end);
cnt = 3;
} else {
ret = c;
cnt = 1;
}
if (pos === undefined) {
this.pos += cnt;
}
return ret;
}
findLiteralEnd() {
const end = this.end;
const pos = this.pos;
const text = this.text;
let ret = pos;
const mask = Chars.IS_NSTRSAFE | Chars.IS_STRUCT | Chars.IS_WFU;
for (;;) {
if (end <= this.pos) {
this.pos = pos;
return end;
}
const c = text.charCodeAt(this.pos);
const bits = Chars.lookup(c) & mask;