This repository was archived by the owner on Mar 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathoboe-browser.js
2703 lines (2210 loc) · 80.7 KB
/
oboe-browser.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
// This file is the concatenation of many js files.
// See http://github.com/jimhigson/oboe.js for the raw source
// having a local undefined, window, Object etc allows slightly better minification:
(function (window, Object, Array, Error, JSON, undefined ) {
// v2.1.1-1-gb70a959
/*
Copyright (c) 2013, Jim Higson
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Partially complete a function.
*
* var add3 = partialComplete( function add(a,b){return a+b}, 3 );
*
* add3(4) // gives 7
*
* function wrap(left, right, cen){return left + " " + cen + " " + right;}
*
* var pirateGreeting = partialComplete( wrap , "I'm", ", a mighty pirate!" );
*
* pirateGreeting("Guybrush Threepwood");
* // gives "I'm Guybrush Threepwood, a mighty pirate!"
*/
var partialComplete = varArgs(function( fn, args ) {
// this isn't the shortest way to write this but it does
// avoid creating a new array each time to pass to fn.apply,
// otherwise could just call boundArgs.concat(callArgs)
var numBoundArgs = args.length;
return varArgs(function( callArgs ) {
for (var i = 0; i < callArgs.length; i++) {
args[numBoundArgs + i] = callArgs[i];
}
args.length = numBoundArgs + callArgs.length;
return fn.apply(this, args);
});
}),
/**
* Compose zero or more functions:
*
* compose(f1, f2, f3)(x) = f1(f2(f3(x))))
*
* The last (inner-most) function may take more than one parameter:
*
* compose(f1, f2, f3)(x,y) = f1(f2(f3(x,y))))
*/
compose = varArgs(function(fns) {
var fnsList = arrayAsList(fns);
function next(params, curFn) {
return [apply(params, curFn)];
}
return varArgs(function(startParams){
return foldR(next, startParams, fnsList)[0];
});
});
/**
* A more optimised version of compose that takes exactly two functions
* @param f1
* @param f2
*/
function compose2(f1, f2){
return function(){
return f1.call(this,f2.apply(this,arguments));
}
}
/**
* Generic form for a function to get a property from an object
*
* var o = {
* foo:'bar'
* }
*
* var getFoo = attr('foo')
*
* fetFoo(o) // returns 'bar'
*
* @param {String} key the property name
*/
function attr(key) {
return function(o) { return o[key]; };
}
/**
* Call a list of functions with the same args until one returns a
* truthy result. Similar to the || operator.
*
* So:
* lazyUnion([f1,f2,f3 ... fn])( p1, p2 ... pn )
*
* Is equivalent to:
* apply([p1, p2 ... pn], f1) ||
* apply([p1, p2 ... pn], f2) ||
* apply([p1, p2 ... pn], f3) ... apply(fn, [p1, p2 ... pn])
*
* @returns the first return value that is given that is truthy.
*/
var lazyUnion = varArgs(function(fns) {
return varArgs(function(params){
var maybeValue;
for (var i = 0; i < len(fns); i++) {
maybeValue = apply(params, fns[i]);
if( maybeValue ) {
return maybeValue;
}
}
});
});
/**
* This file declares various pieces of functional programming.
*
* This isn't a general purpose functional library, to keep things small it
* has just the parts useful for Oboe.js.
*/
/**
* Call a single function with the given arguments array.
* Basically, a functional-style version of the OO-style Function#apply for
* when we don't care about the context ('this') of the call.
*
* The order of arguments allows partial completion of the arguments array
*/
function apply(args, fn) {
return fn.apply(undefined, args);
}
/**
* Define variable argument functions but cut out all that tedious messing about
* with the arguments object. Delivers the variable-length part of the arguments
* list as an array.
*
* Eg:
*
* var myFunction = varArgs(
* function( fixedArgument, otherFixedArgument, variableNumberOfArguments ){
* console.log( variableNumberOfArguments );
* }
* )
*
* myFunction('a', 'b', 1, 2, 3); // logs [1,2,3]
*
* var myOtherFunction = varArgs(function( variableNumberOfArguments ){
* console.log( variableNumberOfArguments );
* })
*
* myFunction(1, 2, 3); // logs [1,2,3]
*
*/
function varArgs(fn){
var numberOfFixedArguments = fn.length -1,
slice = Array.prototype.slice;
if( numberOfFixedArguments == 0 ) {
// an optimised case for when there are no fixed args:
return function(){
return fn.call(this, slice.call(arguments));
}
} else if( numberOfFixedArguments == 1 ) {
// an optimised case for when there are is one fixed args:
return function(){
return fn.call(this, arguments[0], slice.call(arguments, 1));
}
}
// general case
// we know how many arguments fn will always take. Create a
// fixed-size array to hold that many, to be re-used on
// every call to the returned function
var argsHolder = Array(fn.length);
return function(){
for (var i = 0; i < numberOfFixedArguments; i++) {
argsHolder[i] = arguments[i];
}
argsHolder[numberOfFixedArguments] =
slice.call(arguments, numberOfFixedArguments);
return fn.apply( this, argsHolder);
}
}
/**
* Swap the order of parameters to a binary function
*
* A bit like this flip: http://zvon.org/other/haskell/Outputprelude/flip_f.html
*/
function flip(fn){
return function(a, b){
return fn(b,a);
}
}
/**
* Create a function which is the intersection of two other functions.
*
* Like the && operator, if the first is truthy, the second is never called,
* otherwise the return value from the second is returned.
*/
function lazyIntersection(fn1, fn2) {
return function (param) {
return fn1(param) && fn2(param);
};
}
/**
* A function which does nothing
*/
function noop(){}
/**
* A function which is always happy
*/
function always(){return true}
/**
* Create a function which always returns the same
* value
*
* var return3 = functor(3);
*
* return3() // gives 3
* return3() // still gives 3
* return3() // will always give 3
*/
function functor(val){
return function(){
return val;
}
}
/**
* This file defines some loosely associated syntactic sugar for
* Javascript programming
*/
/**
* Returns true if the given candidate is of type T
*/
function isOfType(T, maybeSomething){
return maybeSomething && maybeSomething.constructor === T;
}
var len = attr('length'),
isString = partialComplete(isOfType, String);
/**
* I don't like saying this:
*
* foo !=== undefined
*
* because of the double-negative. I find this:
*
* defined(foo)
*
* easier to read.
*/
function defined( value ) {
return value !== undefined;
}
/**
* Returns true if object o has a key named like every property in
* the properties array. Will give false if any are missing, or if o
* is not an object.
*/
function hasAllProperties(fieldList, o) {
return (o instanceof Object)
&&
all(function (field) {
return (field in o);
}, fieldList);
}
/**
* Like cons in Lisp
*/
function cons(x, xs) {
/* Internally lists are linked 2-element Javascript arrays.
Ideally the return here would be Object.freeze([x,xs])
so that bugs related to mutation are found fast.
However, cons is right on the critical path for
performance and this slows oboe-mark down by
~25%. Under theoretical future JS engines that freeze more
efficiently (possibly even use immutability to
run faster) this should be considered for
restoration.
*/
return [x,xs];
}
/**
* The empty list
*/
var emptyList = null,
/**
* Get the head of a list.
*
* Ie, head(cons(a,b)) = a
*/
head = attr(0),
/**
* Get the tail of a list.
*
* Ie, head(cons(a,b)) = a
*/
tail = attr(1);
/**
* Converts an array to a list
*
* asList([a,b,c])
*
* is equivalent to:
*
* cons(a, cons(b, cons(c, emptyList)))
**/
function arrayAsList(inputArray){
return reverseList(
inputArray.reduce(
flip(cons),
emptyList
)
);
}
/**
* A varargs version of arrayAsList. Works a bit like list
* in LISP.
*
* list(a,b,c)
*
* is equivalent to:
*
* cons(a, cons(b, cons(c, emptyList)))
*/
var list = varArgs(arrayAsList);
/**
* Convert a list back to a js native array
*/
function listAsArray(list){
return foldR( function(arraySoFar, listItem){
arraySoFar.unshift(listItem);
return arraySoFar;
}, [], list );
}
/**
* Map a function over a list
*/
function map(fn, list) {
return list
? cons(fn(head(list)), map(fn,tail(list)))
: emptyList
;
}
/**
* foldR implementation. Reduce a list down to a single value.
*
* @pram {Function} fn (rightEval, curVal) -> result
*/
function foldR(fn, startValue, list) {
return list
? fn(foldR(fn, startValue, tail(list)), head(list))
: startValue
;
}
/**
* foldR implementation. Reduce a list down to a single value.
*
* @pram {Function} fn (rightEval, curVal) -> result
*/
function foldR1(fn, list) {
return tail(list)
? fn(foldR1(fn, tail(list)), head(list))
: head(list)
;
}
/**
* Return a list like the one given but with the first instance equal
* to item removed
*/
function without(list, test, removedFn) {
return withoutInner(list, removedFn || noop);
function withoutInner(subList, removedFn) {
return subList
? ( test(head(subList))
? (removedFn(head(subList)), tail(subList))
: cons(head(subList), withoutInner(tail(subList), removedFn))
)
: emptyList
;
}
}
/**
* Returns true if the given function holds for every item in
* the list, false otherwise
*/
function all(fn, list) {
return !list ||
( fn(head(list)) && all(fn, tail(list)) );
}
/**
* Call every function in a list of functions with the same arguments
*
* This doesn't make any sense if we're doing pure functional because
* it doesn't return anything. Hence, this is only really useful if the
* functions being called have side-effects.
*/
function applyEach(fnList, args) {
if( fnList ) {
head(fnList).apply(null, args);
applyEach(tail(fnList), args);
}
}
/**
* Reverse the order of a list
*/
function reverseList(list){
// js re-implementation of 3rd solution from:
// http://www.haskell.org/haskellwiki/99_questions/Solutions/5
function reverseInner( list, reversedAlready ) {
if( !list ) {
return reversedAlready;
}
return reverseInner(tail(list), cons(head(list), reversedAlready))
}
return reverseInner(list, emptyList);
}
function first(test, list) {
return list &&
(test(head(list))
? head(list)
: first(test,tail(list)));
}
/*
This is a slightly hacked-up browser only version of clarinet
* some features removed to help keep browser Oboe under
the 5k micro-library limit
* plug directly into event bus
For the original go here:
https://github.com/dscape/clarinet
We receive the events:
STREAM_DATA
STREAM_END
We emit the events:
SAX_KEY
SAX_VALUE_OPEN
SAX_VALUE_CLOSE
FAIL_EVENT
*/
function clarinet(eventBus) {
"use strict";
var
// shortcut some events on the bus
emitSaxKey = eventBus(SAX_KEY).emit,
emitValueOpen = eventBus(SAX_VALUE_OPEN).emit,
emitValueClose = eventBus(SAX_VALUE_CLOSE).emit,
emitFail = eventBus(FAIL_EVENT).emit,
MAX_BUFFER_LENGTH = 64 * 1024
, stringTokenPattern = /[\\"\n]/g
, _n = 0
// states
, BEGIN = _n++
, VALUE = _n++ // general stuff
, OPEN_OBJECT = _n++ // {
, CLOSE_OBJECT = _n++ // }
, OPEN_ARRAY = _n++ // [
, CLOSE_ARRAY = _n++ // ]
, STRING = _n++ // ""
, OPEN_KEY = _n++ // , "a"
, CLOSE_KEY = _n++ // :
, TRUE = _n++ // r
, TRUE2 = _n++ // u
, TRUE3 = _n++ // e
, FALSE = _n++ // a
, FALSE2 = _n++ // l
, FALSE3 = _n++ // s
, FALSE4 = _n++ // e
, NULL = _n++ // u
, NULL2 = _n++ // l
, NULL3 = _n++ // l
, NUMBER_DECIMAL_POINT = _n++ // .
, NUMBER_DIGIT = _n // [0-9]
// setup initial parser values
, bufferCheckPosition = MAX_BUFFER_LENGTH
, latestError
, c
, p
, textNode = ""
, numberNode = ""
, slashed = false
, closed = false
, state = BEGIN
, stack = []
, unicodeS = null
, unicodeI = 0
, depth = 0
, position = 0
, column = 0 //mostly for error reporting
, line = 1
;
function checkBufferLength () {
var maxActual = 0;
if (textNode.length > MAX_BUFFER_LENGTH) {
emitError("Max buffer length exceeded: textNode");
maxActual = Math.max(maxActual, textNode.length);
}
if (numberNode.length > MAX_BUFFER_LENGTH) {
emitError("Max buffer length exceeded: numberNode");
maxActual = Math.max(maxActual, numberNode.length);
}
bufferCheckPosition = (MAX_BUFFER_LENGTH - maxActual)
+ position;
}
eventBus(STREAM_DATA).on(handleData);
/* At the end of the http content close the clarinet
This will provide an error if the total content provided was not
valid json, ie if not all arrays, objects and Strings closed properly */
eventBus(STREAM_END).on(handleStreamEnd);
function emitError (errorString) {
if (textNode) {
emitValueOpen(textNode);
emitValueClose();
textNode = "";
}
latestError = Error(errorString + "\nLn: "+line+
"\nCol: "+column+
"\nChr: "+c);
emitFail(errorReport(undefined, undefined, latestError));
}
function handleStreamEnd() {
if( state == BEGIN ) {
// Handle the case where the stream closes without ever receiving
// any input. This isn't an error - response bodies can be blank,
// particularly for 204 http responses
// Because of how Oboe is currently implemented, we parse a
// completely empty stream as containing an empty object.
// This is because Oboe's done event is only fired when the
// root object of the JSON stream closes.
// This should be decoupled and attached instead to the input stream
// from the http (or whatever) resource ending.
// If this decoupling could happen the SAX parser could simply emit
// zero events on a completely empty input.
emitValueOpen({});
emitValueClose();
closed = true;
return;
}
if (state !== VALUE || depth !== 0)
emitError("Unexpected end");
if (textNode) {
emitValueOpen(textNode);
emitValueClose();
textNode = "";
}
closed = true;
}
function whitespace(c){
return c == '\r' || c == '\n' || c == ' ' || c == '\t';
}
function handleData (chunk) {
// this used to throw the error but inside Oboe we will have already
// gotten the error when it was emitted. The important thing is to
// not continue with the parse.
if (latestError)
return;
if (closed) {
return emitError("Cannot write after close");
}
var i = 0;
c = chunk[0];
while (c) {
p = c;
c = chunk[i++];
if(!c) break;
position ++;
if (c == "\n") {
line ++;
column = 0;
} else column ++;
switch (state) {
case BEGIN:
if (c === "{") state = OPEN_OBJECT;
else if (c === "[") state = OPEN_ARRAY;
else if (!whitespace(c))
return emitError("Non-whitespace before {[.");
continue;
case OPEN_KEY:
case OPEN_OBJECT:
if (whitespace(c)) continue;
if(state === OPEN_KEY) stack.push(CLOSE_KEY);
else {
if(c === '}') {
emitValueOpen({});
emitValueClose();
state = stack.pop() || VALUE;
continue;
} else stack.push(CLOSE_OBJECT);
}
if(c === '"')
state = STRING;
else
return emitError("Malformed object key should start with \" ");
continue;
case CLOSE_KEY:
case CLOSE_OBJECT:
if (whitespace(c)) continue;
if(c===':') {
if(state === CLOSE_OBJECT) {
stack.push(CLOSE_OBJECT);
if (textNode) {
// was previously (in upstream Clarinet) one event
// - object open came with the text of the first
emitValueOpen({});
emitSaxKey(textNode);
textNode = "";
}
depth++;
} else {
if (textNode) {
emitSaxKey(textNode);
textNode = "";
}
}
state = VALUE;
} else if (c==='}') {
if (textNode) {
emitValueOpen(textNode);
emitValueClose();
textNode = "";
}
emitValueClose();
depth--;
state = stack.pop() || VALUE;
} else if(c===',') {
if(state === CLOSE_OBJECT)
stack.push(CLOSE_OBJECT);
if (textNode) {
emitValueOpen(textNode);
emitValueClose();
textNode = "";
}
state = OPEN_KEY;
} else
return emitError('Bad object');
continue;
case OPEN_ARRAY: // after an array there always a value
case VALUE:
if (whitespace(c)) continue;
if(state===OPEN_ARRAY) {
emitValueOpen([]);
depth++;
state = VALUE;
if(c === ']') {
emitValueClose();
depth--;
state = stack.pop() || VALUE;
continue;
} else {
stack.push(CLOSE_ARRAY);
}
}
if(c === '"') state = STRING;
else if(c === '{') state = OPEN_OBJECT;
else if(c === '[') state = OPEN_ARRAY;
else if(c === 't') state = TRUE;
else if(c === 'f') state = FALSE;
else if(c === 'n') state = NULL;
else if(c === '-') { // keep and continue
numberNode += c;
} else if(c==='0') {
numberNode += c;
state = NUMBER_DIGIT;
} else if('123456789'.indexOf(c) !== -1) {
numberNode += c;
state = NUMBER_DIGIT;
} else
return emitError("Bad value");
continue;
case CLOSE_ARRAY:
if(c===',') {
stack.push(CLOSE_ARRAY);
if (textNode) {
emitValueOpen(textNode);
emitValueClose();
textNode = "";
}
state = VALUE;
} else if (c===']') {
if (textNode) {
emitValueOpen(textNode);
emitValueClose();
textNode = "";
}
emitValueClose();
depth--;
state = stack.pop() || VALUE;
} else if (whitespace(c))
continue;
else
return emitError('Bad array');
continue;
case STRING:
// thanks thejh, this is an about 50% performance improvement.
var starti = i-1;
STRING_BIGLOOP: while (true) {
// zero means "no unicode active". 1-4 mean "parse some more". end after 4.
while (unicodeI > 0) {
unicodeS += c;
c = chunk.charAt(i++);
if (unicodeI === 4) {
// TODO this might be slow? well, probably not used too often anyway
textNode += String.fromCharCode(parseInt(unicodeS, 16));
unicodeI = 0;
starti = i-1;
} else {
unicodeI++;
}
// we can just break here: no stuff we skipped that still has to be sliced out or so
if (!c) break STRING_BIGLOOP;
}
if (c === '"' && !slashed) {
state = stack.pop() || VALUE;
textNode += chunk.substring(starti, i-1);
if(!textNode) {
emitValueOpen("");
emitValueClose();
}
break;
}
if (c === '\\' && !slashed) {
slashed = true;
textNode += chunk.substring(starti, i-1);
c = chunk.charAt(i++);
if (!c) break;
}
if (slashed) {
slashed = false;
if (c === 'n') { textNode += '\n'; }
else if (c === 'r') { textNode += '\r'; }
else if (c === 't') { textNode += '\t'; }
else if (c === 'f') { textNode += '\f'; }
else if (c === 'b') { textNode += '\b'; }
else if (c === 'u') {
// \uxxxx. meh!
unicodeI = 1;
unicodeS = '';
} else {
textNode += c;
}
c = chunk.charAt(i++);
starti = i-1;
if (!c) break;
else continue;
}
stringTokenPattern.lastIndex = i;
var reResult = stringTokenPattern.exec(chunk);
if (!reResult) {
i = chunk.length+1;
textNode += chunk.substring(starti, i-1);
break;
}
i = reResult.index+1;
c = chunk.charAt(reResult.index);
if (!c) {
textNode += chunk.substring(starti, i-1);
break;
}
}
continue;
case TRUE:
if (!c) continue; // strange buffers
if (c==='r') state = TRUE2;
else
return emitError( 'Invalid true started with t'+ c);
continue;
case TRUE2:
if (!c) continue;
if (c==='u') state = TRUE3;
else
return emitError('Invalid true started with tr'+ c);
continue;
case TRUE3:
if (!c) continue;
if(c==='e') {
emitValueOpen(true);
emitValueClose();
state = stack.pop() || VALUE;
} else
return emitError('Invalid true started with tru'+ c);
continue;
case FALSE:
if (!c) continue;
if (c==='a') state = FALSE2;
else
return emitError('Invalid false started with f'+ c);
continue;
case FALSE2:
if (!c) continue;
if (c==='l') state = FALSE3;
else
return emitError('Invalid false started with fa'+ c);
continue;
case FALSE3:
if (!c) continue;
if (c==='s') state = FALSE4;
else
return emitError('Invalid false started with fal'+ c);
continue;
case FALSE4:
if (!c) continue;
if (c==='e') {
emitValueOpen(false);
emitValueClose();
state = stack.pop() || VALUE;
} else
return emitError('Invalid false started with fals'+ c);
continue;
case NULL:
if (!c) continue;
if (c==='u') state = NULL2;
else
return emitError('Invalid null started with n'+ c);
continue;
case NULL2:
if (!c) continue;
if (c==='l') state = NULL3;
else
return emitError('Invalid null started with nu'+ c);
continue;
case NULL3:
if (!c) continue;
if(c==='l') {
emitValueOpen(null);
emitValueClose();
state = stack.pop() || VALUE;
} else
return emitError('Invalid null started with nul'+ c);
continue;
case NUMBER_DECIMAL_POINT:
if(c==='.') {
numberNode += c;
state = NUMBER_DIGIT;
} else
return emitError('Leading zero not followed by .');
continue;
case NUMBER_DIGIT:
if('0123456789'.indexOf(c) !== -1) numberNode += c;
else if (c==='.') {
if(numberNode.indexOf('.')!==-1)
return emitError('Invalid number has two dots');
numberNode += c;