1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087 |
- const util = require('./util')
-
- let source
- let parseState
- let stack
- let pos
- let line
- let column
- let token
- let key
- let root
-
- module.exports = function parse (text, reviver) {
- source = String(text)
- parseState = 'start'
- stack = []
- pos = 0
- line = 1
- column = 0
- token = undefined
- key = undefined
- root = undefined
-
- do {
- token = lex()
-
- // This code is unreachable.
- // if (!parseStates[parseState]) {
- // throw invalidParseState()
- // }
-
- parseStates[parseState]()
- } while (token.type !== 'eof')
-
- if (typeof reviver === 'function') {
- return internalize({'': root}, '', reviver)
- }
-
- return root
- }
-
- function internalize (holder, name, reviver) {
- const value = holder[name]
- if (value != null && typeof value === 'object') {
- for (const key in value) {
- const replacement = internalize(value, key, reviver)
- if (replacement === undefined) {
- delete value[key]
- } else {
- value[key] = replacement
- }
- }
- }
-
- return reviver.call(holder, name, value)
- }
-
- let lexState
- let buffer
- let doubleQuote
- let sign
- let c
-
- function lex () {
- lexState = 'default'
- buffer = ''
- doubleQuote = false
- sign = 1
-
- for (;;) {
- c = peek()
-
- // This code is unreachable.
- // if (!lexStates[lexState]) {
- // throw invalidLexState(lexState)
- // }
-
- const token = lexStates[lexState]()
- if (token) {
- return token
- }
- }
- }
-
- function peek () {
- if (source[pos]) {
- return String.fromCodePoint(source.codePointAt(pos))
- }
- }
-
- function read () {
- const c = peek()
-
- if (c === '\n') {
- line++
- column = 0
- } else if (c) {
- column += c.length
- } else {
- column++
- }
-
- if (c) {
- pos += c.length
- }
-
- return c
- }
-
- const lexStates = {
- default () {
- switch (c) {
- case '\t':
- case '\v':
- case '\f':
- case ' ':
- case '\u00A0':
- case '\uFEFF':
- case '\n':
- case '\r':
- case '\u2028':
- case '\u2029':
- read()
- return
-
- case '/':
- read()
- lexState = 'comment'
- return
-
- case undefined:
- read()
- return newToken('eof')
- }
-
- if (util.isSpaceSeparator(c)) {
- read()
- return
- }
-
- // This code is unreachable.
- // if (!lexStates[parseState]) {
- // throw invalidLexState(parseState)
- // }
-
- return lexStates[parseState]()
- },
-
- comment () {
- switch (c) {
- case '*':
- read()
- lexState = 'multiLineComment'
- return
-
- case '/':
- read()
- lexState = 'singleLineComment'
- return
- }
-
- throw invalidChar(read())
- },
-
- multiLineComment () {
- switch (c) {
- case '*':
- read()
- lexState = 'multiLineCommentAsterisk'
- return
-
- case undefined:
- throw invalidChar(read())
- }
-
- read()
- },
-
- multiLineCommentAsterisk () {
- switch (c) {
- case '*':
- read()
- return
-
- case '/':
- read()
- lexState = 'default'
- return
-
- case undefined:
- throw invalidChar(read())
- }
-
- read()
- lexState = 'multiLineComment'
- },
-
- singleLineComment () {
- switch (c) {
- case '\n':
- case '\r':
- case '\u2028':
- case '\u2029':
- read()
- lexState = 'default'
- return
-
- case undefined:
- read()
- return newToken('eof')
- }
-
- read()
- },
-
- value () {
- switch (c) {
- case '{':
- case '[':
- return newToken('punctuator', read())
-
- case 'n':
- read()
- literal('ull')
- return newToken('null', null)
-
- case 't':
- read()
- literal('rue')
- return newToken('boolean', true)
-
- case 'f':
- read()
- literal('alse')
- return newToken('boolean', false)
-
- case '-':
- case '+':
- if (read() === '-') {
- sign = -1
- }
-
- lexState = 'sign'
- return
-
- case '.':
- buffer = read()
- lexState = 'decimalPointLeading'
- return
-
- case '0':
- buffer = read()
- lexState = 'zero'
- return
-
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- buffer = read()
- lexState = 'decimalInteger'
- return
-
- case 'I':
- read()
- literal('nfinity')
- return newToken('numeric', Infinity)
-
- case 'N':
- read()
- literal('aN')
- return newToken('numeric', NaN)
-
- case '"':
- case "'":
- doubleQuote = (read() === '"')
- buffer = ''
- lexState = 'string'
- return
- }
-
- throw invalidChar(read())
- },
-
- identifierNameStartEscape () {
- if (c !== 'u') {
- throw invalidChar(read())
- }
-
- read()
- const u = unicodeEscape()
- switch (u) {
- case '$':
- case '_':
- break
-
- default:
- if (!util.isIdStartChar(u)) {
- throw invalidIdentifier()
- }
-
- break
- }
-
- buffer += u
- lexState = 'identifierName'
- },
-
- identifierName () {
- switch (c) {
- case '$':
- case '_':
- case '\u200C':
- case '\u200D':
- buffer += read()
- return
-
- case '\\':
- read()
- lexState = 'identifierNameEscape'
- return
- }
-
- if (util.isIdContinueChar(c)) {
- buffer += read()
- return
- }
-
- return newToken('identifier', buffer)
- },
-
- identifierNameEscape () {
- if (c !== 'u') {
- throw invalidChar(read())
- }
-
- read()
- const u = unicodeEscape()
- switch (u) {
- case '$':
- case '_':
- case '\u200C':
- case '\u200D':
- break
-
- default:
- if (!util.isIdContinueChar(u)) {
- throw invalidIdentifier()
- }
-
- break
- }
-
- buffer += u
- lexState = 'identifierName'
- },
-
- sign () {
- switch (c) {
- case '.':
- buffer = read()
- lexState = 'decimalPointLeading'
- return
-
- case '0':
- buffer = read()
- lexState = 'zero'
- return
-
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- buffer = read()
- lexState = 'decimalInteger'
- return
-
- case 'I':
- read()
- literal('nfinity')
- return newToken('numeric', sign * Infinity)
-
- case 'N':
- read()
- literal('aN')
- return newToken('numeric', NaN)
- }
-
- throw invalidChar(read())
- },
-
- zero () {
- switch (c) {
- case '.':
- buffer += read()
- lexState = 'decimalPoint'
- return
-
- case 'e':
- case 'E':
- buffer += read()
- lexState = 'decimalExponent'
- return
-
- case 'x':
- case 'X':
- buffer += read()
- lexState = 'hexadecimal'
- return
- }
-
- return newToken('numeric', sign * 0)
- },
-
- decimalInteger () {
- switch (c) {
- case '.':
- buffer += read()
- lexState = 'decimalPoint'
- return
-
- case 'e':
- case 'E':
- buffer += read()
- lexState = 'decimalExponent'
- return
- }
-
- if (util.isDigit(c)) {
- buffer += read()
- return
- }
-
- return newToken('numeric', sign * Number(buffer))
- },
-
- decimalPointLeading () {
- if (util.isDigit(c)) {
- buffer += read()
- lexState = 'decimalFraction'
- return
- }
-
- throw invalidChar(read())
- },
-
- decimalPoint () {
- switch (c) {
- case 'e':
- case 'E':
- buffer += read()
- lexState = 'decimalExponent'
- return
- }
-
- if (util.isDigit(c)) {
- buffer += read()
- lexState = 'decimalFraction'
- return
- }
-
- return newToken('numeric', sign * Number(buffer))
- },
-
- decimalFraction () {
- switch (c) {
- case 'e':
- case 'E':
- buffer += read()
- lexState = 'decimalExponent'
- return
- }
-
- if (util.isDigit(c)) {
- buffer += read()
- return
- }
-
- return newToken('numeric', sign * Number(buffer))
- },
-
- decimalExponent () {
- switch (c) {
- case '+':
- case '-':
- buffer += read()
- lexState = 'decimalExponentSign'
- return
- }
-
- if (util.isDigit(c)) {
- buffer += read()
- lexState = 'decimalExponentInteger'
- return
- }
-
- throw invalidChar(read())
- },
-
- decimalExponentSign () {
- if (util.isDigit(c)) {
- buffer += read()
- lexState = 'decimalExponentInteger'
- return
- }
-
- throw invalidChar(read())
- },
-
- decimalExponentInteger () {
- if (util.isDigit(c)) {
- buffer += read()
- return
- }
-
- return newToken('numeric', sign * Number(buffer))
- },
-
- hexadecimal () {
- if (util.isHexDigit(c)) {
- buffer += read()
- lexState = 'hexadecimalInteger'
- return
- }
-
- throw invalidChar(read())
- },
-
- hexadecimalInteger () {
- if (util.isHexDigit(c)) {
- buffer += read()
- return
- }
-
- return newToken('numeric', sign * Number(buffer))
- },
-
- string () {
- switch (c) {
- case '\\':
- read()
- buffer += escape()
- return
-
- case '"':
- if (doubleQuote) {
- read()
- return newToken('string', buffer)
- }
-
- buffer += read()
- return
-
- case "'":
- if (!doubleQuote) {
- read()
- return newToken('string', buffer)
- }
-
- buffer += read()
- return
-
- case '\n':
- case '\r':
- throw invalidChar(read())
-
- case '\u2028':
- case '\u2029':
- separatorChar(c)
- break
-
- case undefined:
- throw invalidChar(read())
- }
-
- buffer += read()
- },
-
- start () {
- switch (c) {
- case '{':
- case '[':
- return newToken('punctuator', read())
-
- // This code is unreachable since the default lexState handles eof.
- // case undefined:
- // return newToken('eof')
- }
-
- lexState = 'value'
- },
-
- beforePropertyName () {
- switch (c) {
- case '$':
- case '_':
- buffer = read()
- lexState = 'identifierName'
- return
-
- case '\\':
- read()
- lexState = 'identifierNameStartEscape'
- return
-
- case '}':
- return newToken('punctuator', read())
-
- case '"':
- case "'":
- doubleQuote = (read() === '"')
- lexState = 'string'
- return
- }
-
- if (util.isIdStartChar(c)) {
- buffer += read()
- lexState = 'identifierName'
- return
- }
-
- throw invalidChar(read())
- },
-
- afterPropertyName () {
- if (c === ':') {
- return newToken('punctuator', read())
- }
-
- throw invalidChar(read())
- },
-
- beforePropertyValue () {
- lexState = 'value'
- },
-
- afterPropertyValue () {
- switch (c) {
- case ',':
- case '}':
- return newToken('punctuator', read())
- }
-
- throw invalidChar(read())
- },
-
- beforeArrayValue () {
- if (c === ']') {
- return newToken('punctuator', read())
- }
-
- lexState = 'value'
- },
-
- afterArrayValue () {
- switch (c) {
- case ',':
- case ']':
- return newToken('punctuator', read())
- }
-
- throw invalidChar(read())
- },
-
- end () {
- // This code is unreachable since it's handled by the default lexState.
- // if (c === undefined) {
- // read()
- // return newToken('eof')
- // }
-
- throw invalidChar(read())
- },
- }
-
- function newToken (type, value) {
- return {
- type,
- value,
- line,
- column,
- }
- }
-
- function literal (s) {
- for (const c of s) {
- const p = peek()
-
- if (p !== c) {
- throw invalidChar(read())
- }
-
- read()
- }
- }
-
- function escape () {
- const c = peek()
- switch (c) {
- case 'b':
- read()
- return '\b'
-
- case 'f':
- read()
- return '\f'
-
- case 'n':
- read()
- return '\n'
-
- case 'r':
- read()
- return '\r'
-
- case 't':
- read()
- return '\t'
-
- case 'v':
- read()
- return '\v'
-
- case '0':
- read()
- if (util.isDigit(peek())) {
- throw invalidChar(read())
- }
-
- return '\0'
-
- case 'x':
- read()
- return hexEscape()
-
- case 'u':
- read()
- return unicodeEscape()
-
- case '\n':
- case '\u2028':
- case '\u2029':
- read()
- return ''
-
- case '\r':
- read()
- if (peek() === '\n') {
- read()
- }
-
- return ''
-
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- throw invalidChar(read())
-
- case undefined:
- throw invalidChar(read())
- }
-
- return read()
- }
-
- function hexEscape () {
- let buffer = ''
- let c = peek()
-
- if (!util.isHexDigit(c)) {
- throw invalidChar(read())
- }
-
- buffer += read()
-
- c = peek()
- if (!util.isHexDigit(c)) {
- throw invalidChar(read())
- }
-
- buffer += read()
-
- return String.fromCodePoint(parseInt(buffer, 16))
- }
-
- function unicodeEscape () {
- let buffer = ''
- let count = 4
-
- while (count-- > 0) {
- const c = peek()
- if (!util.isHexDigit(c)) {
- throw invalidChar(read())
- }
-
- buffer += read()
- }
-
- return String.fromCodePoint(parseInt(buffer, 16))
- }
-
- const parseStates = {
- start () {
- if (token.type === 'eof') {
- throw invalidEOF()
- }
-
- push()
- },
-
- beforePropertyName () {
- switch (token.type) {
- case 'identifier':
- case 'string':
- key = token.value
- parseState = 'afterPropertyName'
- return
-
- case 'punctuator':
- // This code is unreachable since it's handled by the lexState.
- // if (token.value !== '}') {
- // throw invalidToken()
- // }
-
- pop()
- return
-
- case 'eof':
- throw invalidEOF()
- }
-
- // This code is unreachable since it's handled by the lexState.
- // throw invalidToken()
- },
-
- afterPropertyName () {
- // This code is unreachable since it's handled by the lexState.
- // if (token.type !== 'punctuator' || token.value !== ':') {
- // throw invalidToken()
- // }
-
- if (token.type === 'eof') {
- throw invalidEOF()
- }
-
- parseState = 'beforePropertyValue'
- },
-
- beforePropertyValue () {
- if (token.type === 'eof') {
- throw invalidEOF()
- }
-
- push()
- },
-
- beforeArrayValue () {
- if (token.type === 'eof') {
- throw invalidEOF()
- }
-
- if (token.type === 'punctuator' && token.value === ']') {
- pop()
- return
- }
-
- push()
- },
-
- afterPropertyValue () {
- // This code is unreachable since it's handled by the lexState.
- // if (token.type !== 'punctuator') {
- // throw invalidToken()
- // }
-
- if (token.type === 'eof') {
- throw invalidEOF()
- }
-
- switch (token.value) {
- case ',':
- parseState = 'beforePropertyName'
- return
-
- case '}':
- pop()
- }
-
- // This code is unreachable since it's handled by the lexState.
- // throw invalidToken()
- },
-
- afterArrayValue () {
- // This code is unreachable since it's handled by the lexState.
- // if (token.type !== 'punctuator') {
- // throw invalidToken()
- // }
-
- if (token.type === 'eof') {
- throw invalidEOF()
- }
-
- switch (token.value) {
- case ',':
- parseState = 'beforeArrayValue'
- return
-
- case ']':
- pop()
- }
-
- // This code is unreachable since it's handled by the lexState.
- // throw invalidToken()
- },
-
- end () {
- // This code is unreachable since it's handled by the lexState.
- // if (token.type !== 'eof') {
- // throw invalidToken()
- // }
- },
- }
-
- function push () {
- let value
-
- switch (token.type) {
- case 'punctuator':
- switch (token.value) {
- case '{':
- value = {}
- break
-
- case '[':
- value = []
- break
- }
-
- break
-
- case 'null':
- case 'boolean':
- case 'numeric':
- case 'string':
- value = token.value
- break
-
- // This code is unreachable.
- // default:
- // throw invalidToken()
- }
-
- if (root === undefined) {
- root = value
- } else {
- const parent = stack[stack.length - 1]
- if (Array.isArray(parent)) {
- parent.push(value)
- } else {
- parent[key] = value
- }
- }
-
- if (value !== null && typeof value === 'object') {
- stack.push(value)
-
- if (Array.isArray(value)) {
- parseState = 'beforeArrayValue'
- } else {
- parseState = 'beforePropertyName'
- }
- } else {
- const current = stack[stack.length - 1]
- if (current == null) {
- parseState = 'end'
- } else if (Array.isArray(current)) {
- parseState = 'afterArrayValue'
- } else {
- parseState = 'afterPropertyValue'
- }
- }
- }
-
- function pop () {
- stack.pop()
-
- const current = stack[stack.length - 1]
- if (current == null) {
- parseState = 'end'
- } else if (Array.isArray(current)) {
- parseState = 'afterArrayValue'
- } else {
- parseState = 'afterPropertyValue'
- }
- }
-
- // This code is unreachable.
- // function invalidParseState () {
- // return new Error(`JSON5: invalid parse state '${parseState}'`)
- // }
-
- // This code is unreachable.
- // function invalidLexState (state) {
- // return new Error(`JSON5: invalid lex state '${state}'`)
- // }
-
- function invalidChar (c) {
- if (c === undefined) {
- return syntaxError(`JSON5: invalid end of input at ${line}:${column}`)
- }
-
- return syntaxError(`JSON5: invalid character '${formatChar(c)}' at ${line}:${column}`)
- }
-
- function invalidEOF () {
- return syntaxError(`JSON5: invalid end of input at ${line}:${column}`)
- }
-
- // This code is unreachable.
- // function invalidToken () {
- // if (token.type === 'eof') {
- // return syntaxError(`JSON5: invalid end of input at ${line}:${column}`)
- // }
-
- // const c = String.fromCodePoint(token.value.codePointAt(0))
- // return syntaxError(`JSON5: invalid character '${formatChar(c)}' at ${line}:${column}`)
- // }
-
- function invalidIdentifier () {
- column -= 5
- return syntaxError(`JSON5: invalid identifier character at ${line}:${column}`)
- }
-
- function separatorChar (c) {
- console.warn(`JSON5: '${formatChar(c)}' in strings is not valid ECMAScript; consider escaping`)
- }
-
- function formatChar (c) {
- const replacements = {
- "'": "\\'",
- '"': '\\"',
- '\\': '\\\\',
- '\b': '\\b',
- '\f': '\\f',
- '\n': '\\n',
- '\r': '\\r',
- '\t': '\\t',
- '\v': '\\v',
- '\0': '\\0',
- '\u2028': '\\u2028',
- '\u2029': '\\u2029',
- }
-
- if (replacements[c]) {
- return replacements[c]
- }
-
- if (c < ' ') {
- const hexString = c.charCodeAt(0).toString(16)
- return '\\x' + ('00' + hexString).substring(hexString.length)
- }
-
- return c
- }
-
- function syntaxError (message) {
- const err = new SyntaxError(message)
- err.lineNumber = line
- err.columnNumber = column
- return err
- }
|