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 as Error).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") {
132 if (c === undefined) {
133 errorMessage("unexpected end")
137 if (c >= "a" && c <= "f") {
138 code += c.charCodeAt(0) - CODE_A + 10
139 } else if (c >= "0" && c <= "9") {
140 code += c.charCodeAt(0) - CODE_0
142 errorMessage(`unexpected token ${c}`)
147 str += String.fromCharCode(code)
149 errorMessage(`unexpected token ${c}`)
152 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
153 } else if (c === undefined) {
154 errorMessage("unexpected end")
157 if (c.charCodeAt(0) >= 0x20) {
160 errorMessage(`unexpected token ${c}`)
165 parseJsonString.position = pos
168 function errorMessage(msg: string): void {
169 parseJsonString.position = pos
170 parseJsonString.message = msg
174 parseJsonString.message = undefined as string | undefined
175 parseJsonString.position = 0 as number
176 parseJsonString.code = 'require("ajv/dist/runtime/parseJson").parseJsonString'