1 const rxParseJson = /position\s(\d+)$/
3 export function parseJson(s: string, pos: number): unknown {
4 let endPos: number | undefined
5 parseJson.message = undefined
6 let matches: RegExpExecArray | null
7 if (pos) s = s.slice(pos)
9 parseJson.position = pos + s.length
12 matches = rxParseJson.exec(e.message)
14 parseJson.message = "unexpected end"
19 s = s.slice(0, endPos)
20 parseJson.position = pos + endPos
24 parseJson.message = `unexpected token ${c}`
30 parseJson.message = undefined as string | undefined
31 parseJson.position = 0 as number
32 parseJson.code = 'require("ajv/dist/runtime/parseJson").parseJson'
34 export function parseJsonNumber(s: string, pos: number, maxDigits?: number): number | undefined {
37 parseJsonNumber.message = undefined
46 if (!parseDigits(maxDigits)) {
52 parseJsonNumber.position = pos
63 if (((c = s[pos]), c === "e" || c === "E")) {
66 if (((c = s[pos]), c === "+" || c === "-")) {
75 parseJsonNumber.position = pos
78 function parseDigits(maxLen?: number): boolean {
80 while (((c = s[pos]), c >= "0" && c <= "9" && (maxLen === undefined || maxLen-- > 0))) {
88 function errorMessage(): void {
89 parseJsonNumber.position = pos
90 parseJsonNumber.message = pos < s.length ? `unexpected token ${s[pos]}` : "unexpected end"
94 parseJsonNumber.message = undefined as string | undefined
95 parseJsonNumber.position = 0 as number
96 parseJsonNumber.code = 'require("ajv/dist/runtime/parseJson").parseJsonNumber'
98 const escapedChars: {[X in string]?: string} = {
109 const CODE_A: number = "a".charCodeAt(0)
110 const CODE_0: number = "0".charCodeAt(0)
112 export function parseJsonString(s: string, pos: number): string | undefined {
114 let c: string | undefined
115 parseJsonString.message = undefined
116 // eslint-disable-next-line no-constant-condition, @typescript-eslint/no-unnecessary-condition
122 if (c in escapedChars) {
123 str += escapedChars[c]
125 } else if (c === "u") {
131 c = s[pos].toLowerCase()
132 if (c >= "a" && c <= "f") {
133 code += c.charCodeAt(0) - CODE_A + 10
134 } else if (c >= "0" && c <= "9") {
135 code += c.charCodeAt(0) - CODE_0
136 } else if (c === undefined) {
137 errorMessage("unexpected end")
140 errorMessage(`unexpected token ${c}`)
145 str += String.fromCharCode(code)
147 errorMessage(`unexpected token ${c}`)
150 } else if (c === undefined) {
151 errorMessage("unexpected end")
154 if (c.charCodeAt(0) >= 0x20) {
157 errorMessage(`unexpected token ${c}`)
162 parseJsonString.position = pos
165 function errorMessage(msg: string): void {
166 parseJsonString.position = pos
167 parseJsonString.message = msg
171 parseJsonString.message = undefined as string | undefined
172 parseJsonString.position = 0 as number
173 parseJsonString.code = 'require("ajv/dist/runtime/parseJson").parseJsonString'