12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088 |
- 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
- }
|