[6a3a178] | 1 | const rxParseJson = /position\s(\d+)$/
|
---|
| 2 |
|
---|
| 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)
|
---|
| 8 | try {
|
---|
| 9 | parseJson.position = pos + s.length
|
---|
| 10 | return JSON.parse(s)
|
---|
| 11 | } catch (e) {
|
---|
| 12 | matches = rxParseJson.exec(e.message)
|
---|
| 13 | if (!matches) {
|
---|
| 14 | parseJson.message = "unexpected end"
|
---|
| 15 | return undefined
|
---|
| 16 | }
|
---|
| 17 | endPos = +matches[1]
|
---|
| 18 | const c = s[endPos]
|
---|
| 19 | s = s.slice(0, endPos)
|
---|
| 20 | parseJson.position = pos + endPos
|
---|
| 21 | try {
|
---|
| 22 | return JSON.parse(s)
|
---|
| 23 | } catch (e1) {
|
---|
| 24 | parseJson.message = `unexpected token ${c}`
|
---|
| 25 | return undefined
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | parseJson.message = undefined as string | undefined
|
---|
| 31 | parseJson.position = 0 as number
|
---|
| 32 | parseJson.code = 'require("ajv/dist/runtime/parseJson").parseJson'
|
---|
| 33 |
|
---|
| 34 | export function parseJsonNumber(s: string, pos: number, maxDigits?: number): number | undefined {
|
---|
| 35 | let numStr = ""
|
---|
| 36 | let c: string
|
---|
| 37 | parseJsonNumber.message = undefined
|
---|
| 38 | if (s[pos] === "-") {
|
---|
| 39 | numStr += "-"
|
---|
| 40 | pos++
|
---|
| 41 | }
|
---|
| 42 | if (s[pos] === "0") {
|
---|
| 43 | numStr += "0"
|
---|
| 44 | pos++
|
---|
| 45 | } else {
|
---|
| 46 | if (!parseDigits(maxDigits)) {
|
---|
| 47 | errorMessage()
|
---|
| 48 | return undefined
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | if (maxDigits) {
|
---|
| 52 | parseJsonNumber.position = pos
|
---|
| 53 | return +numStr
|
---|
| 54 | }
|
---|
| 55 | if (s[pos] === ".") {
|
---|
| 56 | numStr += "."
|
---|
| 57 | pos++
|
---|
| 58 | if (!parseDigits()) {
|
---|
| 59 | errorMessage()
|
---|
| 60 | return undefined
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | if (((c = s[pos]), c === "e" || c === "E")) {
|
---|
| 64 | numStr += "e"
|
---|
| 65 | pos++
|
---|
| 66 | if (((c = s[pos]), c === "+" || c === "-")) {
|
---|
| 67 | numStr += c
|
---|
| 68 | pos++
|
---|
| 69 | }
|
---|
| 70 | if (!parseDigits()) {
|
---|
| 71 | errorMessage()
|
---|
| 72 | return undefined
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | parseJsonNumber.position = pos
|
---|
| 76 | return +numStr
|
---|
| 77 |
|
---|
| 78 | function parseDigits(maxLen?: number): boolean {
|
---|
| 79 | let digit = false
|
---|
| 80 | while (((c = s[pos]), c >= "0" && c <= "9" && (maxLen === undefined || maxLen-- > 0))) {
|
---|
| 81 | digit = true
|
---|
| 82 | numStr += c
|
---|
| 83 | pos++
|
---|
| 84 | }
|
---|
| 85 | return digit
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | function errorMessage(): void {
|
---|
| 89 | parseJsonNumber.position = pos
|
---|
| 90 | parseJsonNumber.message = pos < s.length ? `unexpected token ${s[pos]}` : "unexpected end"
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | parseJsonNumber.message = undefined as string | undefined
|
---|
| 95 | parseJsonNumber.position = 0 as number
|
---|
| 96 | parseJsonNumber.code = 'require("ajv/dist/runtime/parseJson").parseJsonNumber'
|
---|
| 97 |
|
---|
| 98 | const escapedChars: {[X in string]?: string} = {
|
---|
| 99 | b: "\b",
|
---|
| 100 | f: "\f",
|
---|
| 101 | n: "\n",
|
---|
| 102 | r: "\r",
|
---|
| 103 | t: "\t",
|
---|
| 104 | '"': '"',
|
---|
| 105 | "/": "/",
|
---|
| 106 | "\\": "\\",
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | const CODE_A: number = "a".charCodeAt(0)
|
---|
| 110 | const CODE_0: number = "0".charCodeAt(0)
|
---|
| 111 |
|
---|
| 112 | export function parseJsonString(s: string, pos: number): string | undefined {
|
---|
| 113 | let str = ""
|
---|
| 114 | let c: string | undefined
|
---|
| 115 | parseJsonString.message = undefined
|
---|
| 116 | // eslint-disable-next-line no-constant-condition, @typescript-eslint/no-unnecessary-condition
|
---|
| 117 | while (true) {
|
---|
| 118 | c = s[pos++]
|
---|
| 119 | if (c === '"') break
|
---|
| 120 | if (c === "\\") {
|
---|
| 121 | c = s[pos]
|
---|
| 122 | if (c in escapedChars) {
|
---|
| 123 | str += escapedChars[c]
|
---|
| 124 | pos++
|
---|
| 125 | } else if (c === "u") {
|
---|
| 126 | pos++
|
---|
| 127 | let count = 4
|
---|
| 128 | let code = 0
|
---|
| 129 | while (count--) {
|
---|
| 130 | code <<= 4
|
---|
| 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")
|
---|
| 138 | return undefined
|
---|
| 139 | } else {
|
---|
| 140 | errorMessage(`unexpected token ${c}`)
|
---|
| 141 | return undefined
|
---|
| 142 | }
|
---|
| 143 | pos++
|
---|
| 144 | }
|
---|
| 145 | str += String.fromCharCode(code)
|
---|
| 146 | } else {
|
---|
| 147 | errorMessage(`unexpected token ${c}`)
|
---|
| 148 | return undefined
|
---|
| 149 | }
|
---|
| 150 | } else if (c === undefined) {
|
---|
| 151 | errorMessage("unexpected end")
|
---|
| 152 | return undefined
|
---|
| 153 | } else {
|
---|
| 154 | if (c.charCodeAt(0) >= 0x20) {
|
---|
| 155 | str += c
|
---|
| 156 | } else {
|
---|
| 157 | errorMessage(`unexpected token ${c}`)
|
---|
| 158 | return undefined
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | parseJsonString.position = pos
|
---|
| 163 | return str
|
---|
| 164 |
|
---|
| 165 | function errorMessage(msg: string): void {
|
---|
| 166 | parseJsonString.position = pos
|
---|
| 167 | parseJsonString.message = msg
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | parseJsonString.message = undefined as string | undefined
|
---|
| 172 | parseJsonString.position = 0 as number
|
---|
| 173 | parseJsonString.code = 'require("ajv/dist/runtime/parseJson").parseJsonString'
|
---|