@loaders.gl/json 3.1.0-alpha.3 → 3.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.d.ts +2 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +1838 -4
- package/dist/es5/bundle.js +7 -0
- package/dist/es5/bundle.js.map +1 -0
- package/dist/es5/geojson-loader.js +95 -0
- package/dist/es5/geojson-loader.js.map +1 -0
- package/dist/es5/index.js +54 -0
- package/dist/es5/index.js.map +1 -0
- package/dist/es5/json-loader.js +59 -0
- package/dist/es5/json-loader.js.map +1 -0
- package/dist/es5/jsonl-loader.js +2 -0
- package/dist/{jsonl-loader.js.map → es5/jsonl-loader.js.map} +0 -0
- package/dist/{lib → es5/lib}/clarinet/LICENSE +0 -0
- package/dist/es5/lib/clarinet/clarinet.js +511 -0
- package/dist/es5/lib/clarinet/clarinet.js.map +1 -0
- package/dist/es5/lib/jsonpath/jsonpath.js +97 -0
- package/dist/es5/lib/jsonpath/jsonpath.js.map +1 -0
- package/dist/es5/lib/parse-json-in-batches.js +99 -0
- package/dist/es5/lib/parse-json-in-batches.js.map +1 -0
- package/dist/es5/lib/parse-json.js +41 -0
- package/dist/es5/lib/parse-json.js.map +1 -0
- package/dist/es5/lib/parse-ndjson-in-batches.js +46 -0
- package/dist/es5/lib/parse-ndjson-in-batches.js.map +1 -0
- package/dist/es5/lib/parse-ndjson.js +18 -0
- package/dist/es5/lib/parse-ndjson.js.map +1 -0
- package/dist/es5/lib/parser/json-parser.js +137 -0
- package/dist/es5/lib/parser/json-parser.js.map +1 -0
- package/dist/es5/lib/parser/streaming-json-parser.js +102 -0
- package/dist/es5/lib/parser/streaming-json-parser.js.map +1 -0
- package/dist/es5/ndjson-loader.js +45 -0
- package/dist/es5/ndjson-loader.js.map +1 -0
- package/dist/es5/workers/geojson-worker.js +8 -0
- package/dist/es5/workers/geojson-worker.js.map +1 -0
- package/dist/esm/bundle.js +5 -0
- package/dist/esm/bundle.js.map +1 -0
- package/dist/esm/geojson-loader.js +81 -0
- package/dist/esm/geojson-loader.js.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/json-loader.js +47 -0
- package/dist/esm/json-loader.js.map +1 -0
- package/dist/esm/jsonl-loader.js +2 -0
- package/dist/esm/jsonl-loader.js.map +1 -0
- package/dist/esm/lib/clarinet/LICENSE +28 -0
- package/dist/esm/lib/clarinet/clarinet.js +518 -0
- package/dist/esm/lib/clarinet/clarinet.js.map +1 -0
- package/dist/esm/lib/jsonpath/jsonpath.js +86 -0
- package/dist/esm/lib/jsonpath/jsonpath.js.map +1 -0
- package/dist/esm/lib/parse-json-in-batches.js +87 -0
- package/dist/esm/lib/parse-json-in-batches.js.map +1 -0
- package/dist/esm/lib/parse-json.js +34 -0
- package/dist/esm/lib/parse-json.js.map +1 -0
- package/dist/esm/lib/parse-ndjson-in-batches.js +37 -0
- package/dist/esm/lib/parse-ndjson-in-batches.js.map +1 -0
- package/dist/esm/lib/parse-ndjson.js +11 -0
- package/dist/esm/lib/parse-ndjson.js.map +1 -0
- package/dist/esm/lib/parser/json-parser.js +128 -0
- package/dist/esm/lib/parser/json-parser.js.map +1 -0
- package/dist/esm/lib/parser/streaming-json-parser.js +93 -0
- package/dist/esm/lib/parser/streaming-json-parser.js.map +1 -0
- package/dist/esm/ndjson-loader.js +32 -0
- package/dist/esm/ndjson-loader.js.map +1 -0
- package/dist/esm/workers/geojson-worker.js +4 -0
- package/dist/esm/workers/geojson-worker.js.map +1 -0
- package/dist/geojson-loader.d.ts +16 -0
- package/dist/geojson-loader.d.ts.map +1 -0
- package/dist/geojson-loader.js +66 -69
- package/dist/geojson-worker.js +1702 -2
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -6
- package/dist/json-loader.d.ts +14 -0
- package/dist/json-loader.d.ts.map +1 -0
- package/dist/json-loader.js +48 -38
- package/dist/jsonl-loader.d.ts +1 -0
- package/dist/jsonl-loader.d.ts.map +1 -0
- package/dist/jsonl-loader.js +53 -1
- package/dist/lib/clarinet/clarinet.d.ts +74 -0
- package/dist/lib/clarinet/clarinet.d.ts.map +1 -0
- package/dist/lib/clarinet/clarinet.js +517 -538
- package/dist/lib/jsonpath/jsonpath.d.ts +32 -0
- package/dist/lib/jsonpath/jsonpath.d.ts.map +1 -0
- package/dist/lib/jsonpath/jsonpath.js +81 -78
- package/dist/lib/parse-json-in-batches.d.ts +4 -0
- package/dist/lib/parse-json-in-batches.d.ts.map +1 -0
- package/dist/lib/parse-json-in-batches.js +75 -82
- package/dist/lib/parse-json.d.ts +3 -0
- package/dist/lib/parse-json.d.ts.map +1 -0
- package/dist/lib/parse-json.js +24 -29
- package/dist/lib/parse-ndjson-in-batches.d.ts +4 -0
- package/dist/lib/parse-ndjson-in-batches.d.ts.map +1 -0
- package/dist/lib/parse-ndjson-in-batches.js +32 -34
- package/dist/lib/parse-ndjson.d.ts +2 -0
- package/dist/lib/parse-ndjson.d.ts.map +1 -0
- package/dist/lib/parse-ndjson.js +13 -10
- package/dist/lib/parser/json-parser.d.ts +22 -0
- package/dist/lib/parser/json-parser.d.ts.map +1 -0
- package/dist/lib/parser/json-parser.js +95 -124
- package/dist/lib/parser/streaming-json-parser.d.ts +37 -0
- package/dist/lib/parser/streaming-json-parser.d.ts.map +1 -0
- package/dist/lib/parser/streaming-json-parser.js +95 -93
- package/dist/ndjson-loader.d.ts +22 -0
- package/dist/ndjson-loader.d.ts.map +1 -0
- package/dist/ndjson-loader.js +28 -23
- package/dist/workers/geojson-worker.d.ts +2 -0
- package/dist/workers/geojson-worker.d.ts.map +1 -0
- package/dist/workers/geojson-worker.js +5 -4
- package/package.json +10 -10
- package/src/lib/clarinet/clarinet.ts +539 -0
- package/src/lib/parser/json-parser.ts +52 -55
- package/src/lib/parser/streaming-json-parser.ts +28 -32
- package/src/ndjson-loader.ts +3 -1
- package/dist/bundle.js.map +0 -1
- package/dist/dist.min.js +0 -2
- package/dist/dist.min.js.map +0 -1
- package/dist/geojson-loader.js.map +0 -1
- package/dist/geojson-worker.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/json-loader.js.map +0 -1
- package/dist/lib/clarinet/clarinet.js.map +0 -1
- package/dist/lib/jsonpath/jsonpath.js.map +0 -1
- package/dist/lib/parse-json-in-batches.js.map +0 -1
- package/dist/lib/parse-json.js.map +0 -1
- package/dist/lib/parse-ndjson-in-batches.js.map +0 -1
- package/dist/lib/parse-ndjson.js.map +0 -1
- package/dist/lib/parser/json-parser.js.map +0 -1
- package/dist/lib/parser/streaming-json-parser.js.map +0 -1
- package/dist/ndjson-loader.js.map +0 -1
- package/dist/workers/geojson-worker.js.map +0 -1
- package/src/lib/clarinet/clarinet.js +0 -578
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/parse-json.ts"],"names":["parseJSONSync","jsonText","options","json","JSON","parse","table","getFirstArray","error","Error","Array","isArray","value","Object","values","array"],"mappings":"AAEA,eAAe,SAASA,aAAT,CAAuBC,QAAvB,EAAyCC,OAAzC,EAAqE;AAClF,MAAI;AAAA;;AACF,UAAMC,IAAI,GAAGC,IAAI,CAACC,KAAL,CAAWJ,QAAX,CAAb;;AACA,yBAAIC,OAAO,CAACC,IAAZ,0CAAI,cAAcG,KAAlB,EAAyB;AACvB,aAAOC,aAAa,CAACJ,IAAD,CAAb,IAAuBA,IAA9B;AACD;;AACD,WAAOA,IAAP;AACD,GAND,CAME,OAAOK,KAAP,EAAc;AACd,UAAM,IAAIC,KAAJ,CAAU,kCAAV,CAAN;AACD;AACF;;AAED,SAASF,aAAT,CAAuBJ,IAAvB,EAA6B;AAC3B,MAAIO,KAAK,CAACC,OAAN,CAAcR,IAAd,CAAJ,EAAyB;AACvB,WAAOA,IAAP;AACD;;AACD,MAAIA,IAAI,IAAI,OAAOA,IAAP,KAAgB,QAA5B,EAAsC;AACpC,SAAK,MAAMS,KAAX,IAAoBC,MAAM,CAACC,MAAP,CAAcX,IAAd,CAApB,EAAyC;AACvC,YAAMY,KAAK,GAAGR,aAAa,CAACK,KAAD,CAA3B;;AACA,UAAIG,KAAJ,EAAW;AACT,eAAOA,KAAP;AACD;AACF;AACF;;AACD,SAAO,IAAP;AACD","sourcesContent":["import type {JSONLoaderOptions} from '../json-loader';\n\nexport default function parseJSONSync(jsonText: string, options: JSONLoaderOptions) {\n try {\n const json = JSON.parse(jsonText);\n if (options.json?.table) {\n return getFirstArray(json) || json;\n }\n return json;\n } catch (error) {\n throw new Error('JSONLoader: failed to parse JSON');\n }\n}\n\nfunction getFirstArray(json) {\n if (Array.isArray(json)) {\n return json;\n }\n if (json && typeof json === 'object') {\n for (const value of Object.values(json)) {\n const array = getFirstArray(value);\n if (array) {\n return array;\n }\n }\n }\n return null;\n}\n"],"file":"parse-json.js"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/parse-ndjson-in-batches.ts"],"names":["TableBatchBuilder","makeLineIterator","makeNumberedLineIterator","makeTextDecoderIterator","parseNDJSONInBatches","binaryAsyncIterator","options","textIterator","lineIterator","numberedLineIterator","schema","shape","tableBatchBuilder","counter","line","row","JSON","parse","addRow","chunkComplete","batch","getFullBatch","error","Error","getFinalBatch"],"mappings":"AACA,SAAQA,iBAAR,QAAgC,oBAAhC;AACA,SAEEC,gBAFF,EAGEC,wBAHF,EAIEC,uBAJF,QAKO,0BALP;AAOA,eAAe,gBAAgBC,oBAAhB,CACbC,mBADa,EAEbC,OAFa,EAGS;AACtB,QAAMC,YAAY,GAAGJ,uBAAuB,CAACE,mBAAD,CAA5C;AACA,QAAMG,YAAY,GAAGP,gBAAgB,CAACM,YAAD,CAArC;AACA,QAAME,oBAAoB,GAAGP,wBAAwB,CAACM,YAAD,CAArD;AAEA,QAAME,MAAM,GAAG,IAAf;AACA,QAAMC,KAAK,GAAG,WAAd;AAEA,QAAMC,iBAAiB,GAAG,IAAIZ,iBAAJ,CAAsBU,MAAtB,EAA8B,EACtD,GAAGJ,OADmD;AAEtDK,IAAAA;AAFsD,GAA9B,CAA1B;;AAKA,aAAW,MAAM;AAACE,IAAAA,OAAD;AAAUC,IAAAA;AAAV,GAAjB,IAAoCL,oBAApC,EAA0D;AACxD,QAAI;AACF,YAAMM,GAAG,GAAGC,IAAI,CAACC,KAAL,CAAWH,IAAX,CAAZ;AACAF,MAAAA,iBAAiB,CAACM,MAAlB,CAAyBH,GAAzB;AACAH,MAAAA,iBAAiB,CAACO,aAAlB,CAAgCL,IAAhC;AACA,YAAMM,KAAK,GAAGR,iBAAiB,CAACS,YAAlB,EAAd;;AACA,UAAID,KAAJ,EAAW;AACT,cAAMA,KAAN;AACD;AACF,KARD,CAQE,OAAOE,KAAP,EAAc;AACd,YAAM,IAAIC,KAAJ,CAAW,8CAA6CV,OAAQ,EAAhE,CAAN;AACD;AACF;;AAED,QAAMO,KAAK,GAAGR,iBAAiB,CAACY,aAAlB,EAAd;;AACA,MAAIJ,KAAJ,EAAW;AACT,UAAMA,KAAN;AACD;AACF","sourcesContent":["import type {Batch} from '@loaders.gl/schema';\nimport {TableBatchBuilder} from '@loaders.gl/schema';\nimport {\n LoaderOptions,\n makeLineIterator,\n makeNumberedLineIterator,\n makeTextDecoderIterator\n} from '@loaders.gl/loader-utils';\n\nexport default async function* parseNDJSONInBatches(\n binaryAsyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,\n options?: LoaderOptions\n): AsyncIterable<Batch> {\n const textIterator = makeTextDecoderIterator(binaryAsyncIterator);\n const lineIterator = makeLineIterator(textIterator);\n const numberedLineIterator = makeNumberedLineIterator(lineIterator);\n\n const schema = null;\n const shape = 'row-table';\n // @ts-ignore\n const tableBatchBuilder = new TableBatchBuilder(schema, {\n ...options,\n shape\n });\n\n for await (const {counter, line} of numberedLineIterator) {\n try {\n const row = JSON.parse(line);\n tableBatchBuilder.addRow(row);\n tableBatchBuilder.chunkComplete(line);\n const batch = tableBatchBuilder.getFullBatch();\n if (batch) {\n yield batch;\n }\n } catch (error) {\n throw new Error(`NDJSONLoader: failed to parse JSON on line ${counter}`);\n }\n }\n\n const batch = tableBatchBuilder.getFinalBatch();\n if (batch) {\n yield batch;\n }\n}\n"],"file":"parse-ndjson-in-batches.js"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/parse-ndjson.ts"],"names":["parseNDJSONSync","ndjsonText","lines","trim","split","map","line","counter","JSON","parse","error","Error"],"mappings":"AAAA,eAAe,SAASA,eAAT,CAAyBC,UAAzB,EAA6C;AAC1D,QAAMC,KAAK,GAAGD,UAAU,CAACE,IAAX,GAAkBC,KAAlB,CAAwB,IAAxB,CAAd;AACA,SAAOF,KAAK,CAACG,GAAN,CAAU,CAACC,IAAD,EAAOC,OAAP,KAAmB;AAClC,QAAI;AACF,aAAOC,IAAI,CAACC,KAAL,CAAWH,IAAX,CAAP;AACD,KAFD,CAEE,OAAOI,KAAP,EAAc;AACd,YAAM,IAAIC,KAAJ,CAAW,8CAA6CJ,OAAO,GAAG,CAAE,EAApE,CAAN;AACD;AACF,GANM,CAAP;AAOD","sourcesContent":["export default function parseNDJSONSync(ndjsonText: string) {\n const lines = ndjsonText.trim().split('\\n');\n return lines.map((line, counter) => {\n try {\n return JSON.parse(line);\n } catch (error) {\n throw new Error(`NDJSONLoader: failed to parse JSON on line ${counter + 1}`);\n }\n });\n}\n"],"file":"parse-ndjson.js"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/parser/json-parser.ts"],"names":["ClarinetParser","JSONPath","JSONParser","constructor","reset","_initializeParser","result","undefined","previousStates","currentState","Object","freeze","container","key","jsonpath","write","chunk","parser","close","_pushOrSet","value","push","_openArray","newContainer","isArray","_closeArray","pop","_openObject","_closeObject","_parser","onready","length","onopenobject","name","onkey","set","oncloseobject","onopenarray","onclosearray","onvalue","onerror","error","onend"],"mappings":";AAEA,OAAOA,cAA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xB;AACD,OAjB+B;AAmBhCG,MAAAA,aAAa,EAAE,MAAM;AACnB,aAAKR,YAAL;AACD,OArB+B;AAuBhCS,MAAAA,WAAW,EAAE,MAAM;AACjB,aAAKf,UAAL;AACD,OAzB+B;AA2BhCgB,MAAAA,YAAY,EAAE,MAAM;AAClB,aAAKb,WAAL;AACD,OA7B+B;AA+BhCc,MAAAA,OAAO,EAAGnB,KAAD,IAAW;AAClB,aAAKD,UAAL,CAAgBC,KAAhB;AACD,OAjC+B;AAmChCoB,MAAAA,OAAO,EAAGC,KAAD,IAAW;AAClB,cAAMA,KAAN;AACD,OArC+B;AAuChCC,MAAAA,KAAK,EAAE,MAAM;AACX,aAAKpC,MAAL,GAAc,KAAKG,YAAL,CAAkBG,SAAlB,CAA4Bc,GAA5B,EAAd;AACD;AAzC+B,KAAnB,CAAf;AA2CD;;AAEmB,MAANT,MAAM,GAAmB;AACrC,WAAO,KAAKY,OAAZ;AACD;;AA5G6B","sourcesContent":["// @ts-nocheck\n\nimport ClarinetParser from '../clarinet/clarinet';\nimport JSONPath from '../jsonpath/jsonpath';\n\n// JSONParser builds a JSON object using the events emitted by the Clarinet parser\n\nexport default class JSONParser {\n jsonpath: JSONPath;\n _parser?: ClarinetParser;\n\n constructor() {\n this.reset();\n this._initializeParser();\n }\n\n reset(): void {\n this.result = undefined;\n this.previousStates = [];\n this.currentState = Object.freeze({container: [], key: null});\n this.jsonpath = new JSONPath();\n }\n\n write(chunk): void {\n this.parser.write(chunk);\n }\n\n close(): void {\n this.parser.close();\n }\n\n // PRIVATE METHODS\n\n _pushOrSet(value): void {\n const {container, key} = this.currentState;\n if (key !== null) {\n container[key] = value;\n this.currentState.key = null;\n } else {\n container.push(value);\n }\n }\n\n _openArray(newContainer = []): void {\n this.jsonpath.push(null);\n this._pushOrSet(newContainer);\n this.previousStates.push(this.currentState);\n this.currentState = {container: newContainer, isArray: true, key: null};\n }\n\n _closeArray(): void {\n this.jsonpath.pop();\n this.currentState = this.previousStates.pop();\n }\n\n _openObject(newContainer = {}): void {\n this.jsonpath.push(null);\n this._pushOrSet(newContainer);\n this.previousStates.push(this.currentState);\n this.currentState = {container: newContainer, isArray: false, key: null};\n }\n\n _closeObject(): void {\n this.jsonpath.pop();\n this.currentState = this.previousStates.pop();\n }\n\n _initializeParser(): void {\n this._parser = new ClarinetParser({\n onready: () => {\n this.jsonpath = new JSONPath();\n this.previousStates.length = 0;\n this.currentState.container.length = 0;\n },\n\n onopenobject: (name) => {\n this._openObject({});\n if (typeof name !== 'undefined') {\n this.parser.onkey(name);\n }\n },\n\n onkey: (name) => {\n this.jsonpath.set(name);\n this.currentState.key = name;\n },\n\n oncloseobject: () => {\n this._closeObject();\n },\n\n onopenarray: () => {\n this._openArray();\n },\n\n onclosearray: () => {\n this._closeArray();\n },\n\n onvalue: (value) => {\n this._pushOrSet(value);\n },\n\n onerror: (error) => {\n throw error;\n },\n\n onend: () => {\n this.result = this.currentState.container.pop();\n }\n });\n }\n\n protected get parser(): ClarinetParser {\n return this._parser as ClarinetParser;\n }\n}\n"],"file":"json-parser.js"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/parser/streaming-json-parser.ts"],"names":["default","JSONParser","JSONPath","StreamingJSONParser","constructor","options","jsonpaths","jsonPaths","map","jsonpath","_extendParser","write","chunk","array","streamingArray","length","getPartialResult","topLevelObject","getStreamingJsonPath","streamingJsonPath","getStreamingJsonPathAsString","toString","getJsonPath","_matchJSONPath","currentPath","jsonPath","equals","parser","onopenarray","clone","_openArray","onopenobject","name","_openObject","onkey"],"mappings":";AAAA,SAAQA,OAAO,IAAIC,UAAnB,QAAoC,eAApC;AACA,OAAOC,QAAP,MAAqB,sBAArB;AAMA,eAAe,MAAMC,mBAAN,SAAkCF,UAAlC,CAA6C;AAM1DG,EAAAA,WAAW,CAACC,OAA6B,GAAG,EAAjC,EAAqC;AAC9C;;AAD8C;;AAAA,+CAJH,IAIG;;AAAA,4CAHT,IAGS;;AAA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,EAAJ,EAA2B;AAEzB,eAAKJ,iBAAL,GAAyB,KAAKG,WAAL,GAAmBO,KAAnB,EAAzB;AACA,eAAKf,cAAL,GAAsB,EAAtB;;AACA,eAAKgB,UAAL,CAAgB,KAAKhB,cAArB;;AACA;AACD;AACF;;AAED,WAAKgB,UAAL;AACD,KAZD;;AAeA,SAAKH,MAAL,CAAYI,YAAZ,GAA4BC,IAAD,IAAU;AACnC,UAAI,CAAC,KAAKf,cAAV,EAA0B;AACxB,aAAKA,cAAL,GAAsB,EAAtB;;AACA,aAAKgB,WAAL,CAAiB,KAAKhB,cAAtB;AACD,OAHD,MAGO;AACL,aAAKgB,WAAL,CAAiB,EAAjB;AACD;;AACD,UAAI,OAAOD,IAAP,KAAgB,WAApB,EAAiC;AAC/B,aAAKL,MAAL,CAAYO,KAAZ,CAAkBF,IAAlB;AACD;AACF,KAVD;AAWD;;AAvGyD","sourcesContent":["import {default as JSONParser} from './json-parser';\nimport JSONPath from '../jsonpath/jsonpath';\n\n/**\n * The `StreamingJSONParser` looks for the first array in the JSON structure.\n * and emits an array of chunks\n */\nexport default class StreamingJSONParser extends JSONParser {\n private jsonPaths: JSONPath[];\n private streamingJsonPath: JSONPath | null = null;\n private streamingArray: any[] | null = null;\n private topLevelObject: object | null = null;\n\n constructor(options: {[key: string]: any} = {}) {\n super();\n const jsonpaths = options.jsonpaths || [];\n this.jsonPaths = jsonpaths.map((jsonpath) => new JSONPath(jsonpath));\n this._extendParser();\n }\n\n /**\n * write REDEFINITION\n * - super.write() chunk to parser\n * - get the contents (so far) of \"topmost-level\" array as batch of rows\n * - clear top-level array\n * - return the batch of rows\\\n */\n write(chunk) {\n super.write(chunk);\n let array: any[] = [];\n if (this.streamingArray) {\n array = [...this.streamingArray];\n this.streamingArray.length = 0;\n }\n return array;\n }\n\n /**\n * Returns a partially formed result object\n * Useful for returning the \"wrapper\" object when array is not top level\n * e.g. GeoJSON\n */\n getPartialResult() {\n return this.topLevelObject;\n }\n\n getStreamingJsonPath() {\n return this.streamingJsonPath;\n }\n\n getStreamingJsonPathAsString() {\n return this.streamingJsonPath && this.streamingJsonPath.toString();\n }\n\n getJsonPath() {\n return this.jsonpath;\n }\n\n // PRIVATE METHODS\n\n /**\n * Checks is this.getJsonPath matches the jsonpaths provided in options\n */\n _matchJSONPath() {\n const currentPath = this.getJsonPath();\n // console.debug(`Testing JSONPath`, currentPath);\n\n // Backwards compatibility, match any array\n // TODO implement using wildcard once that is supported\n if (this.jsonPaths.length === 0) {\n return true;\n }\n\n for (const jsonPath of this.jsonPaths) {\n if (jsonPath.equals(currentPath)) {\n return true;\n }\n }\n\n return false;\n }\n\n _extendParser() {\n // Redefine onopenarray to locate and inject value for top-level array\n this.parser.onopenarray = () => {\n if (!this.streamingArray) {\n if (this._matchJSONPath()) {\n // @ts-ignore\n this.streamingJsonPath = this.getJsonPath().clone();\n this.streamingArray = [];\n this._openArray(this.streamingArray as []);\n return;\n }\n }\n\n this._openArray();\n };\n\n // Redefine onopenarray to inject value for top-level object\n this.parser.onopenobject = (name) => {\n if (!this.topLevelObject) {\n this.topLevelObject = {};\n this._openObject(this.topLevelObject);\n } else {\n this._openObject({});\n }\n if (typeof name !== 'undefined') {\n this.parser.onkey(name);\n }\n };\n }\n}\n"],"file":"streaming-json-parser.js"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ndjson-loader.ts"],"names":["parseNDJSONSync","parseNDJSONInBatches","VERSION","NDJSONLoader","name","id","module","version","extensions","mimeTypes","category","text","parse","parseTextSync","parseInBatches","options","arrayBuffer","TextDecoder","decode","asyncIterator"],"mappings":"AAEA,OAAOA,eAAP,MAA4B,oBAA5B;AACA,OAAOC,oBAAP,MAAiC,+BAAjC;AAIA,MAAMC,OAAO,GAAG,2BAAuB,WAAvB,qBAAmD,QAAnE;AAEA,OAAO,MAAMC,YAA8B,GAAG;AAC5CC,EAAAA,IAAI,EAAE,QADsC;AAE5CC,EAAAA,EAAE,EAAE,QAFwC;AAG5CC,EAAAA,MAAM,EAAE,MAHoC;AAI5CC,EAAAA,OAAO,EAAEL,OAJmC;AAK5CM,EAAAA,UAAU,EAAE,CAAC,QAAD,CALgC;AAM5CC,EAAAA,SAAS,EAAE,CAAC,sBAAD,CANiC;AAO5CC,EAAAA,QAAQ,EAAE,OAPkC;AAQ5CC,EAAAA,IAAI,EAAE,IARsC;AAS5CC,EAAAA,KAT4C;AAU5CC,EAAAA,aAV4C;AAW5CC,EAAAA,cAX4C;AAY5CC,EAAAA,OAAO,EAAE;AAZmC,CAAvC;;AAeP,eAAeH,KAAf,CAAqBI,WAArB,EAA+C;AAC7C,SAAOH,aAAa,CAAC,IAAII,WAAJ,GAAkBC,MAAlB,CAAyBF,WAAzB,CAAD,CAApB;AACD;;AAED,SAASH,aAAT,CAAuBF,IAAvB,EAAqC;AACnC,SAAOX,eAAe,CAACW,IAAD,CAAtB;AACD;;AAED,SAASG,cAAT,CACEK,aADF,EAEEJ,OAFF,EAGwB;AACtB,SAAOd,oBAAoB,CAACkB,aAAD,EAAgBJ,OAAhB,CAA3B;AACD","sourcesContent":["import type {Batch} from '@loaders.gl/schema';\nimport type {LoaderWithParser, LoaderOptions} from '@loaders.gl/loader-utils';\nimport parseNDJSONSync from './lib/parse-ndjson';\nimport parseNDJSONInBatches from './lib/parse-ndjson-in-batches';\n\n// __VERSION__ is injected by babel-plugin-version-inline\n// @ts-ignore TS2304: Cannot find name '__VERSION__'.\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';\n\nexport const NDJSONLoader: LoaderWithParser = {\n name: 'NDJSON',\n id: 'ndjson',\n module: 'json',\n version: VERSION,\n extensions: ['ndjson'],\n mimeTypes: ['application/x-ndjson'],\n category: 'table',\n text: true,\n parse,\n parseTextSync,\n parseInBatches,\n options: {}\n};\n\nasync function parse(arrayBuffer: ArrayBuffer) {\n return parseTextSync(new TextDecoder().decode(arrayBuffer));\n}\n\nfunction parseTextSync(text: string) {\n return parseNDJSONSync(text);\n}\n\nfunction parseInBatches(\n asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,\n options?: LoaderOptions\n): AsyncIterable<Batch> {\n return parseNDJSONInBatches(asyncIterator, options);\n}\n"],"file":"ndjson-loader.js"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/workers/geojson-worker.ts"],"names":["createLoaderWorker","GeoJSONLoader"],"mappings":"AAAA,SAAQA,kBAAR,QAAiC,0BAAjC;AACA,SAAQC,aAAR,QAA4B,mBAA5B;AAEAD,kBAAkB,CAACC,aAAD,CAAlB","sourcesContent":["import {createLoaderWorker} from '@loaders.gl/loader-utils';\nimport {GeoJSONLoader} from '../geojson-loader';\n\ncreateLoaderWorker(GeoJSONLoader);\n"],"file":"geojson-worker.js"}
|
|
@@ -1,578 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
// @ts-nocheck
|
|
3
|
-
const env = {};
|
|
4
|
-
|
|
5
|
-
export const EVENTS = [
|
|
6
|
-
'value',
|
|
7
|
-
'string',
|
|
8
|
-
'key',
|
|
9
|
-
'openobject',
|
|
10
|
-
'closeobject',
|
|
11
|
-
'openarray',
|
|
12
|
-
'closearray',
|
|
13
|
-
'error',
|
|
14
|
-
'end',
|
|
15
|
-
'ready'
|
|
16
|
-
];
|
|
17
|
-
|
|
18
|
-
// Removes the MAX_BUFFER_LENGTH, originally set to 64 * 1024
|
|
19
|
-
const MAX_BUFFER_LENGTH = Number.MAX_SAFE_INTEGER;
|
|
20
|
-
const DEBUG = env.CDEBUG === 'debug';
|
|
21
|
-
|
|
22
|
-
const buffers = {
|
|
23
|
-
textNode: undefined,
|
|
24
|
-
numberNode: ''
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
let S = 0;
|
|
28
|
-
|
|
29
|
-
const STATE = {
|
|
30
|
-
BEGIN: S++,
|
|
31
|
-
VALUE: S++, // general stuff
|
|
32
|
-
OPEN_OBJECT: S++, // {
|
|
33
|
-
CLOSE_OBJECT: S++, // }
|
|
34
|
-
OPEN_ARRAY: S++, // [
|
|
35
|
-
CLOSE_ARRAY: S++, // ]
|
|
36
|
-
TEXT_ESCAPE: S++, // \ stuff
|
|
37
|
-
STRING: S++, // ""
|
|
38
|
-
BACKSLASH: S++,
|
|
39
|
-
END: S++, // No more stack
|
|
40
|
-
OPEN_KEY: S++, // , "a"
|
|
41
|
-
CLOSE_KEY: S++, // :
|
|
42
|
-
TRUE: S++, // r
|
|
43
|
-
TRUE2: S++, // u
|
|
44
|
-
TRUE3: S++, // e
|
|
45
|
-
FALSE: S++, // a
|
|
46
|
-
FALSE2: S++, // l
|
|
47
|
-
FALSE3: S++, // s
|
|
48
|
-
FALSE4: S++, // e
|
|
49
|
-
NULL: S++, // u
|
|
50
|
-
NULL2: S++, // l
|
|
51
|
-
NULL3: S++, // l
|
|
52
|
-
NUMBER_DECIMAL_POINT: S++, // .
|
|
53
|
-
NUMBER_DIGIT: S++ // [0-9]
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
for (var s_ in STATE) STATE[STATE[s_]] = s_;
|
|
57
|
-
|
|
58
|
-
// switcharoo
|
|
59
|
-
S = STATE;
|
|
60
|
-
|
|
61
|
-
const Char = {
|
|
62
|
-
tab: 0x09, // \t
|
|
63
|
-
lineFeed: 0x0a, // \n
|
|
64
|
-
carriageReturn: 0x0d, // \r
|
|
65
|
-
space: 0x20, // " "
|
|
66
|
-
|
|
67
|
-
doubleQuote: 0x22, // "
|
|
68
|
-
plus: 0x2b, // +
|
|
69
|
-
comma: 0x2c, // ,
|
|
70
|
-
minus: 0x2d, // -
|
|
71
|
-
period: 0x2e, // .
|
|
72
|
-
|
|
73
|
-
_0: 0x30, // 0
|
|
74
|
-
_9: 0x39, // 9
|
|
75
|
-
|
|
76
|
-
colon: 0x3a, // :
|
|
77
|
-
|
|
78
|
-
E: 0x45, // E
|
|
79
|
-
|
|
80
|
-
openBracket: 0x5b, // [
|
|
81
|
-
backslash: 0x5c, // \
|
|
82
|
-
closeBracket: 0x5d, // ]
|
|
83
|
-
|
|
84
|
-
a: 0x61, // a
|
|
85
|
-
b: 0x62, // b
|
|
86
|
-
e: 0x65, // e
|
|
87
|
-
f: 0x66, // f
|
|
88
|
-
l: 0x6c, // l
|
|
89
|
-
n: 0x6e, // n
|
|
90
|
-
r: 0x72, // r
|
|
91
|
-
s: 0x73, // s
|
|
92
|
-
t: 0x74, // t
|
|
93
|
-
u: 0x75, // u
|
|
94
|
-
|
|
95
|
-
openBrace: 0x7b, // {
|
|
96
|
-
closeBrace: 0x7d // }
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
function checkBufferLength(parser) {
|
|
100
|
-
const maxAllowed = Math.max(MAX_BUFFER_LENGTH, 10);
|
|
101
|
-
let maxActual = 0;
|
|
102
|
-
|
|
103
|
-
for (var buffer in buffers) {
|
|
104
|
-
var len = parser[buffer] === undefined ? 0 : parser[buffer].length;
|
|
105
|
-
if (len > maxAllowed) {
|
|
106
|
-
switch (buffer) {
|
|
107
|
-
case 'text':
|
|
108
|
-
closeText(parser);
|
|
109
|
-
break;
|
|
110
|
-
|
|
111
|
-
default:
|
|
112
|
-
error(parser, 'Max buffer length exceeded: ' + buffer);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
maxActual = Math.max(maxActual, len);
|
|
116
|
-
}
|
|
117
|
-
parser.bufferCheckPosition = MAX_BUFFER_LENGTH - maxActual + parser.position;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
var stringTokenPattern = /[\\"\n]/g;
|
|
121
|
-
|
|
122
|
-
export default class ClarinetParser {
|
|
123
|
-
constructor(options = {}) {
|
|
124
|
-
this._initialize(options);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
_initialize(options) {
|
|
128
|
-
this._clearBuffers(this);
|
|
129
|
-
this.bufferCheckPosition = MAX_BUFFER_LENGTH;
|
|
130
|
-
this.q = '';
|
|
131
|
-
this.c = '';
|
|
132
|
-
this.p = '';
|
|
133
|
-
this.options = options || {};
|
|
134
|
-
this.closed = false;
|
|
135
|
-
this.closedRoot = false;
|
|
136
|
-
this.sawRoot = false;
|
|
137
|
-
this.tag = null;
|
|
138
|
-
this.error = null;
|
|
139
|
-
this.state = S.BEGIN;
|
|
140
|
-
this.stack = new Array();
|
|
141
|
-
// mostly just for error reporting
|
|
142
|
-
this.position = this.column = 0;
|
|
143
|
-
this.line = 1;
|
|
144
|
-
this.slashed = false;
|
|
145
|
-
this.unicodeI = 0;
|
|
146
|
-
this.unicodeS = null;
|
|
147
|
-
this.depth = 0;
|
|
148
|
-
|
|
149
|
-
// install callbacks
|
|
150
|
-
if ('onready' in options) {
|
|
151
|
-
this.onready = options.onready;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if ('onopenobject' in options) {
|
|
155
|
-
this.onopenobject = options.onopenobject;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if ('onkey' in options) {
|
|
159
|
-
this.onkey = options.onkey;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if ('oncloseobject' in options) {
|
|
163
|
-
this.oncloseobject = options.oncloseobject;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if ('onopenarray' in options) {
|
|
167
|
-
this.onopenarray = options.onopenarray;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if ('onclosearray' in options) {
|
|
171
|
-
this.onclosearray = options.onclosearray;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if ('onvalue' in options) {
|
|
175
|
-
this.onvalue = options.onvalue;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if ('onerror' in options) {
|
|
179
|
-
this.onerror = options.onerror;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if ('onend' in options) {
|
|
183
|
-
this.onend = options.onend;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if ('onchunkparsed' in options) {
|
|
187
|
-
this.onchunkparsed = options.onchunkparsed;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
emit(this, 'onready');
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
_clearBuffers() {
|
|
194
|
-
for (var buffer in buffers) {
|
|
195
|
-
this[buffer] = buffers[buffer];
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
end() {
|
|
200
|
-
if (this.state !== S.VALUE || this.depth !== 0) error(this, 'Unexpected end');
|
|
201
|
-
|
|
202
|
-
closeValue(this);
|
|
203
|
-
this.c = '';
|
|
204
|
-
this.closed = true;
|
|
205
|
-
emit(this, 'onend');
|
|
206
|
-
this._initialize(this.options);
|
|
207
|
-
return this;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
resume() {
|
|
211
|
-
this.error = null;
|
|
212
|
-
return this;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
close() {
|
|
216
|
-
return this.write(null);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
write(chunk) {
|
|
220
|
-
if (this.error) {
|
|
221
|
-
throw this.error;
|
|
222
|
-
}
|
|
223
|
-
if (this.closed) {
|
|
224
|
-
return error(this, 'Cannot write after close. Assign an onready handler.');
|
|
225
|
-
}
|
|
226
|
-
if (chunk === null) {
|
|
227
|
-
return this.end();
|
|
228
|
-
}
|
|
229
|
-
var i = 0,
|
|
230
|
-
c = chunk.charCodeAt(0),
|
|
231
|
-
p = this.p;
|
|
232
|
-
if (DEBUG) console.log('write -> [' + chunk + ']');
|
|
233
|
-
while (c) {
|
|
234
|
-
p = c;
|
|
235
|
-
this.c = c = chunk.charCodeAt(i++);
|
|
236
|
-
// if chunk doesnt have next, like streaming char by char
|
|
237
|
-
// this way we need to check if previous is really previous
|
|
238
|
-
// if not we need to reset to what the this says is the previous
|
|
239
|
-
// from buffer
|
|
240
|
-
if (p !== c) {
|
|
241
|
-
this.p = p;
|
|
242
|
-
} else {
|
|
243
|
-
p = this.p;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (!c) break;
|
|
247
|
-
|
|
248
|
-
if (DEBUG) console.log(i, c, STATE[this.state]);
|
|
249
|
-
this.position++;
|
|
250
|
-
if (c === Char.lineFeed) {
|
|
251
|
-
this.line++;
|
|
252
|
-
this.column = 0;
|
|
253
|
-
} else this.column++;
|
|
254
|
-
|
|
255
|
-
switch (this.state) {
|
|
256
|
-
case S.BEGIN:
|
|
257
|
-
if (c === Char.openBrace) this.state = S.OPEN_OBJECT;
|
|
258
|
-
else if (c === Char.openBracket) this.state = S.OPEN_ARRAY;
|
|
259
|
-
else if (!isWhitespace(c)) {
|
|
260
|
-
error(this, 'Non-whitespace before {[.');
|
|
261
|
-
}
|
|
262
|
-
continue;
|
|
263
|
-
|
|
264
|
-
case S.OPEN_KEY:
|
|
265
|
-
case S.OPEN_OBJECT:
|
|
266
|
-
if (isWhitespace(c)) continue;
|
|
267
|
-
if (this.state === S.OPEN_KEY) this.stack.push(S.CLOSE_KEY);
|
|
268
|
-
else {
|
|
269
|
-
if (c === Char.closeBrace) {
|
|
270
|
-
emit(this, 'onopenobject');
|
|
271
|
-
this.depth++;
|
|
272
|
-
emit(this, 'oncloseobject');
|
|
273
|
-
this.depth--;
|
|
274
|
-
this.state = this.stack.pop() || S.VALUE;
|
|
275
|
-
continue;
|
|
276
|
-
} else this.stack.push(S.CLOSE_OBJECT);
|
|
277
|
-
}
|
|
278
|
-
if (c === Char.doubleQuote) this.state = S.STRING;
|
|
279
|
-
else error(this, 'Malformed object key should start with "');
|
|
280
|
-
continue;
|
|
281
|
-
|
|
282
|
-
case S.CLOSE_KEY:
|
|
283
|
-
case S.CLOSE_OBJECT:
|
|
284
|
-
if (isWhitespace(c)) continue;
|
|
285
|
-
var event = this.state === S.CLOSE_KEY ? 'key' : 'object';
|
|
286
|
-
if (c === Char.colon) {
|
|
287
|
-
if (this.state === S.CLOSE_OBJECT) {
|
|
288
|
-
this.stack.push(S.CLOSE_OBJECT);
|
|
289
|
-
closeValue(this, 'onopenobject');
|
|
290
|
-
this.depth++;
|
|
291
|
-
} else closeValue(this, 'onkey');
|
|
292
|
-
this.state = S.VALUE;
|
|
293
|
-
} else if (c === Char.closeBrace) {
|
|
294
|
-
emitNode(this, 'oncloseobject');
|
|
295
|
-
this.depth--;
|
|
296
|
-
this.state = this.stack.pop() || S.VALUE;
|
|
297
|
-
} else if (c === Char.comma) {
|
|
298
|
-
if (this.state === S.CLOSE_OBJECT) this.stack.push(S.CLOSE_OBJECT);
|
|
299
|
-
closeValue(this);
|
|
300
|
-
this.state = S.OPEN_KEY;
|
|
301
|
-
} else error(this, 'Bad object');
|
|
302
|
-
continue;
|
|
303
|
-
|
|
304
|
-
case S.OPEN_ARRAY: // after an array there always a value
|
|
305
|
-
case S.VALUE:
|
|
306
|
-
if (isWhitespace(c)) continue;
|
|
307
|
-
if (this.state === S.OPEN_ARRAY) {
|
|
308
|
-
emit(this, 'onopenarray');
|
|
309
|
-
this.depth++;
|
|
310
|
-
this.state = S.VALUE;
|
|
311
|
-
if (c === Char.closeBracket) {
|
|
312
|
-
emit(this, 'onclosearray');
|
|
313
|
-
this.depth--;
|
|
314
|
-
this.state = this.stack.pop() || S.VALUE;
|
|
315
|
-
continue;
|
|
316
|
-
} else {
|
|
317
|
-
this.stack.push(S.CLOSE_ARRAY);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
if (c === Char.doubleQuote) this.state = S.STRING;
|
|
321
|
-
else if (c === Char.openBrace) this.state = S.OPEN_OBJECT;
|
|
322
|
-
else if (c === Char.openBracket) this.state = S.OPEN_ARRAY;
|
|
323
|
-
else if (c === Char.t) this.state = S.TRUE;
|
|
324
|
-
else if (c === Char.f) this.state = S.FALSE;
|
|
325
|
-
else if (c === Char.n) this.state = S.NULL;
|
|
326
|
-
else if (c === Char.minus) {
|
|
327
|
-
// keep and continue
|
|
328
|
-
this.numberNode += '-';
|
|
329
|
-
} else if (Char._0 <= c && c <= Char._9) {
|
|
330
|
-
this.numberNode += String.fromCharCode(c);
|
|
331
|
-
this.state = S.NUMBER_DIGIT;
|
|
332
|
-
} else error(this, 'Bad value');
|
|
333
|
-
continue;
|
|
334
|
-
|
|
335
|
-
case S.CLOSE_ARRAY:
|
|
336
|
-
if (c === Char.comma) {
|
|
337
|
-
this.stack.push(S.CLOSE_ARRAY);
|
|
338
|
-
closeValue(this, 'onvalue');
|
|
339
|
-
this.state = S.VALUE;
|
|
340
|
-
} else if (c === Char.closeBracket) {
|
|
341
|
-
emitNode(this, 'onclosearray');
|
|
342
|
-
this.depth--;
|
|
343
|
-
this.state = this.stack.pop() || S.VALUE;
|
|
344
|
-
} else if (isWhitespace(c)) continue;
|
|
345
|
-
else error(this, 'Bad array');
|
|
346
|
-
continue;
|
|
347
|
-
|
|
348
|
-
case S.STRING:
|
|
349
|
-
if (this.textNode === undefined) {
|
|
350
|
-
this.textNode = '';
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// thanks thejh, this is an about 50% performance improvement.
|
|
354
|
-
var starti = i - 1,
|
|
355
|
-
slashed = this.slashed,
|
|
356
|
-
unicodeI = this.unicodeI;
|
|
357
|
-
STRING_BIGLOOP: while (true) {
|
|
358
|
-
if (DEBUG) console.log(i, c, STATE[this.state], slashed);
|
|
359
|
-
// zero means "no unicode active". 1-4 mean "parse some more". end after 4.
|
|
360
|
-
while (unicodeI > 0) {
|
|
361
|
-
this.unicodeS += String.fromCharCode(c);
|
|
362
|
-
c = chunk.charCodeAt(i++);
|
|
363
|
-
this.position++;
|
|
364
|
-
if (unicodeI === 4) {
|
|
365
|
-
// TODO this might be slow? well, probably not used too often anyway
|
|
366
|
-
this.textNode += String.fromCharCode(parseInt(this.unicodeS, 16));
|
|
367
|
-
unicodeI = 0;
|
|
368
|
-
starti = i - 1;
|
|
369
|
-
} else {
|
|
370
|
-
unicodeI++;
|
|
371
|
-
}
|
|
372
|
-
// we can just break here: no stuff we skipped that still has to be sliced out or so
|
|
373
|
-
if (!c) break STRING_BIGLOOP;
|
|
374
|
-
}
|
|
375
|
-
if (c === Char.doubleQuote && !slashed) {
|
|
376
|
-
this.state = this.stack.pop() || S.VALUE;
|
|
377
|
-
this.textNode += chunk.substring(starti, i - 1);
|
|
378
|
-
this.position += i - 1 - starti;
|
|
379
|
-
break;
|
|
380
|
-
}
|
|
381
|
-
if (c === Char.backslash && !slashed) {
|
|
382
|
-
slashed = true;
|
|
383
|
-
this.textNode += chunk.substring(starti, i - 1);
|
|
384
|
-
this.position += i - 1 - starti;
|
|
385
|
-
c = chunk.charCodeAt(i++);
|
|
386
|
-
this.position++;
|
|
387
|
-
if (!c) break;
|
|
388
|
-
}
|
|
389
|
-
if (slashed) {
|
|
390
|
-
slashed = false;
|
|
391
|
-
if (c === Char.n) {
|
|
392
|
-
this.textNode += '\n';
|
|
393
|
-
} else if (c === Char.r) {
|
|
394
|
-
this.textNode += '\r';
|
|
395
|
-
} else if (c === Char.t) {
|
|
396
|
-
this.textNode += '\t';
|
|
397
|
-
} else if (c === Char.f) {
|
|
398
|
-
this.textNode += '\f';
|
|
399
|
-
} else if (c === Char.b) {
|
|
400
|
-
this.textNode += '\b';
|
|
401
|
-
} else if (c === Char.u) {
|
|
402
|
-
// \uxxxx. meh!
|
|
403
|
-
unicodeI = 1;
|
|
404
|
-
this.unicodeS = '';
|
|
405
|
-
} else {
|
|
406
|
-
this.textNode += String.fromCharCode(c);
|
|
407
|
-
}
|
|
408
|
-
c = chunk.charCodeAt(i++);
|
|
409
|
-
this.position++;
|
|
410
|
-
starti = i - 1;
|
|
411
|
-
if (!c) break;
|
|
412
|
-
else continue;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
stringTokenPattern.lastIndex = i;
|
|
416
|
-
var reResult = stringTokenPattern.exec(chunk);
|
|
417
|
-
if (reResult === null) {
|
|
418
|
-
i = chunk.length + 1;
|
|
419
|
-
this.textNode += chunk.substring(starti, i - 1);
|
|
420
|
-
this.position += i - 1 - starti;
|
|
421
|
-
break;
|
|
422
|
-
}
|
|
423
|
-
i = reResult.index + 1;
|
|
424
|
-
c = chunk.charCodeAt(reResult.index);
|
|
425
|
-
if (!c) {
|
|
426
|
-
this.textNode += chunk.substring(starti, i - 1);
|
|
427
|
-
this.position += i - 1 - starti;
|
|
428
|
-
break;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
this.slashed = slashed;
|
|
432
|
-
this.unicodeI = unicodeI;
|
|
433
|
-
continue;
|
|
434
|
-
|
|
435
|
-
case S.TRUE:
|
|
436
|
-
if (c === Char.r) this.state = S.TRUE2;
|
|
437
|
-
else error(this, 'Invalid true started with t' + c);
|
|
438
|
-
continue;
|
|
439
|
-
|
|
440
|
-
case S.TRUE2:
|
|
441
|
-
if (c === Char.u) this.state = S.TRUE3;
|
|
442
|
-
else error(this, 'Invalid true started with tr' + c);
|
|
443
|
-
continue;
|
|
444
|
-
|
|
445
|
-
case S.TRUE3:
|
|
446
|
-
if (c === Char.e) {
|
|
447
|
-
emit(this, 'onvalue', true);
|
|
448
|
-
this.state = this.stack.pop() || S.VALUE;
|
|
449
|
-
} else error(this, 'Invalid true started with tru' + c);
|
|
450
|
-
continue;
|
|
451
|
-
|
|
452
|
-
case S.FALSE:
|
|
453
|
-
if (c === Char.a) this.state = S.FALSE2;
|
|
454
|
-
else error(this, 'Invalid false started with f' + c);
|
|
455
|
-
continue;
|
|
456
|
-
|
|
457
|
-
case S.FALSE2:
|
|
458
|
-
if (c === Char.l) this.state = S.FALSE3;
|
|
459
|
-
else error(this, 'Invalid false started with fa' + c);
|
|
460
|
-
continue;
|
|
461
|
-
|
|
462
|
-
case S.FALSE3:
|
|
463
|
-
if (c === Char.s) this.state = S.FALSE4;
|
|
464
|
-
else error(this, 'Invalid false started with fal' + c);
|
|
465
|
-
continue;
|
|
466
|
-
|
|
467
|
-
case S.FALSE4:
|
|
468
|
-
if (c === Char.e) {
|
|
469
|
-
emit(this, 'onvalue', false);
|
|
470
|
-
this.state = this.stack.pop() || S.VALUE;
|
|
471
|
-
} else error(this, 'Invalid false started with fals' + c);
|
|
472
|
-
continue;
|
|
473
|
-
|
|
474
|
-
case S.NULL:
|
|
475
|
-
if (c === Char.u) this.state = S.NULL2;
|
|
476
|
-
else error(this, 'Invalid null started with n' + c);
|
|
477
|
-
continue;
|
|
478
|
-
|
|
479
|
-
case S.NULL2:
|
|
480
|
-
if (c === Char.l) this.state = S.NULL3;
|
|
481
|
-
else error(this, 'Invalid null started with nu' + c);
|
|
482
|
-
continue;
|
|
483
|
-
|
|
484
|
-
case S.NULL3:
|
|
485
|
-
if (c === Char.l) {
|
|
486
|
-
emit(this, 'onvalue', null);
|
|
487
|
-
this.state = this.stack.pop() || S.VALUE;
|
|
488
|
-
} else error(this, 'Invalid null started with nul' + c);
|
|
489
|
-
continue;
|
|
490
|
-
|
|
491
|
-
case S.NUMBER_DECIMAL_POINT:
|
|
492
|
-
if (c === Char.period) {
|
|
493
|
-
this.numberNode += '.';
|
|
494
|
-
this.state = S.NUMBER_DIGIT;
|
|
495
|
-
} else error(this, 'Leading zero not followed by .');
|
|
496
|
-
continue;
|
|
497
|
-
|
|
498
|
-
case S.NUMBER_DIGIT:
|
|
499
|
-
if (Char._0 <= c && c <= Char._9) this.numberNode += String.fromCharCode(c);
|
|
500
|
-
else if (c === Char.period) {
|
|
501
|
-
if (this.numberNode.indexOf('.') !== -1) error(this, 'Invalid number has two dots');
|
|
502
|
-
this.numberNode += '.';
|
|
503
|
-
} else if (c === Char.e || c === Char.E) {
|
|
504
|
-
if (this.numberNode.indexOf('e') !== -1 || this.numberNode.indexOf('E') !== -1)
|
|
505
|
-
error(this, 'Invalid number has two exponential');
|
|
506
|
-
this.numberNode += 'e';
|
|
507
|
-
} else if (c === Char.plus || c === Char.minus) {
|
|
508
|
-
if (!(p === Char.e || p === Char.E)) error(this, 'Invalid symbol in number');
|
|
509
|
-
this.numberNode += String.fromCharCode(c);
|
|
510
|
-
} else {
|
|
511
|
-
closeNumber(this);
|
|
512
|
-
i--; // go back one
|
|
513
|
-
this.state = this.stack.pop() || S.VALUE;
|
|
514
|
-
}
|
|
515
|
-
continue;
|
|
516
|
-
|
|
517
|
-
default:
|
|
518
|
-
error(this, 'Unknown state: ' + this.state);
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
if (this.position >= this.bufferCheckPosition) {
|
|
522
|
-
checkBufferLength(this);
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
emit(this, 'onchunkparsed');
|
|
526
|
-
|
|
527
|
-
return this;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
function emit(parser, event, data) {
|
|
532
|
-
if (DEBUG) {
|
|
533
|
-
console.log('-- emit', event, data);
|
|
534
|
-
}
|
|
535
|
-
if (parser[event]) {
|
|
536
|
-
parser[event](data, parser);
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
function emitNode(parser, event, data) {
|
|
541
|
-
closeValue(parser);
|
|
542
|
-
emit(parser, event, data);
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
function closeValue(parser, event) {
|
|
546
|
-
parser.textNode = textopts(parser.options, parser.textNode);
|
|
547
|
-
if (parser.textNode !== undefined) {
|
|
548
|
-
emit(parser, event ? event : 'onvalue', parser.textNode);
|
|
549
|
-
}
|
|
550
|
-
parser.textNode = undefined;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
function closeNumber(parser) {
|
|
554
|
-
if (parser.numberNode) emit(parser, 'onvalue', parseFloat(parser.numberNode));
|
|
555
|
-
parser.numberNode = '';
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
function textopts(opt, text) {
|
|
559
|
-
if (text === undefined) {
|
|
560
|
-
return text;
|
|
561
|
-
}
|
|
562
|
-
if (opt.trim) text = text.trim();
|
|
563
|
-
if (opt.normalize) text = text.replace(/\s+/g, ' ');
|
|
564
|
-
return text;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
function error(parser, er) {
|
|
568
|
-
closeValue(parser);
|
|
569
|
-
er += '\nLine: ' + parser.line + '\nColumn: ' + parser.column + '\nChar: ' + parser.c;
|
|
570
|
-
er = new Error(er);
|
|
571
|
-
parser.error = er;
|
|
572
|
-
emit(parser, 'onerror', er);
|
|
573
|
-
return parser;
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
function isWhitespace(c) {
|
|
577
|
-
return c === Char.carriageReturn || c === Char.lineFeed || c === Char.space || c === Char.tab;
|
|
578
|
-
}
|