1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
import { Ok, Error } from './gleam.mjs'
import { UnexpectedByte, UnexpectedEndOfInput } from './gleam/json.mjs'
const Runtime = {
chromium: 'chromium',
spidermonkey: 'spidermonkey',
jscore: 'jscore',
}
export function json_to_string(json) {
return JSON.stringify(json)
}
export function object(entries) {
return Object.fromEntries(entries)
}
export function identity(x) {
return x
}
export function array(list) {
return list.toArray()
}
export function do_null() {
return null
}
export function decode(string) {
try {
const result = JSON.parse(string)
return new Ok(result)
} catch (err) {
return new Error(getJsonDecodeError(err, string))
}
}
function getJsonDecodeError(stdErr, json) {
if (isUnexpectedEndOfInput(stdErr)) return new UnexpectedEndOfInput()
const unexpectedByteRuntime = getUnexpectedByteRuntime(stdErr)
if (unexpectedByteRuntime) return toUnexpectedByteError(unexpectedByteRuntime, stdErr, json)
return new UnexpectedByte('', 0)
}
/**
* Matches unexpected end of input messages in:
* - Chromium (edge, chrome, node)
* - Spidermonkey (firefox)
* - JavascriptCore (safari)
*
* Note that Spidermonkey and JavascriptCore will both incorrectly report some
* UnexpectedByte errors as UnexpectedEndOfInput errors. For example:
*
* @example
* // in JavascriptCore
* JSON.parse('{"a"]: "b"})
* // => JSON Parse error: Expected ':' before value
*
* JSON.parse('{"a"')
* // => JSON Parse error: Expected ':' before value
*
* // in Chromium (correct)
* JSON.parse('{"a"]: "b"})
* // => Unexpected token ] in JSON at position 4
*
* JSON.parse('{"a"')
* // => Unexpected end of JSON input
*
* // in Chromium (correct)
*
*/
const unexpectedEndOfInputRegex = /((unexpected (end|eof))|(end of data)|(unterminated string)|(json( parse error|\.parse)\: expected '(\:|\}|\])'))/i
function isUnexpectedEndOfInput(err) {
return unexpectedEndOfInputRegex.test(err.message)
}
/**
* Matches unexpected byte messages in:
* - Chromium (edge, chrome, node)
*
* Matches the character and its position.
*/
const chromiumUnexpectedByteRegex = /unexpected token (.) in JSON at position (\d+)/
/**
* Matches unexpected byte messages in:
* - JavascriptCore (safari)
*
* JavascriptCore only reports what the character is and not its position.
*/
const jsCoreUnexpectedByteRegex = /unexpected identifier "(.)"/i
/**
* Matches unexpected byte messages in:
* - Spidermonkey (firefox)
*
* Matches the position in a 2d grid only and not the character.
*/
const spidermonkeyUnexpectedByteRegex = /((unexpected character|expected double-quoted property name) at line (\d+) column (\d+))/i
function getUnexpectedByteRuntime(err) {
if (chromiumUnexpectedByteRegex.test(err.message)) return Runtime.chromium
if (jsCoreUnexpectedByteRegex.test(err.message)) return Runtime.jscore
if (spidermonkeyUnexpectedByteRegex.test(err.message)) return Runtime.spidermonkey
return null
}
function toUnexpectedByteError(runtime, err, json) {
switch (runtime) {
case Runtime.chromium:
return toChromiumUnexpectedByteError(err)
case Runtime.spidermonkey:
return toSpidermonkeyUnexpectedByteError(err, json)
case Runtime.jscore:
return toJsCoreUnexpectedByteError(err)
}
}
function toChromiumUnexpectedByteError(err) {
const match = chromiumUnexpectedByteRegex.exec(err.message)
const byte = toHex(match[1])
const position = Number(match[2])
return new UnexpectedByte(byte, position)
}
function toSpidermonkeyUnexpectedByteError(err, json) {
const match = spidermonkeyUnexpectedByteRegex.exec(err.message)
const line = Number(match[1])
const column = Number(match[2])
const position = getPositionFromMultiline(line, column, json)
const byte = toHex(err.message[position])
return new UnexpectedByte(byte, position)
}
function toJsCoreUnexpectedByteError(err) {
const match = jsCoreUnexpectedByteRegex.exec(err.message)
const byte = toHex(match[1])
return new UnexpectedByte(byte, 0)
}
function toHex(char) {
return "0x" + char.charCodeAt(0).toString(16).toUpperCase()
}
/**
* Gets the position of a character in a flattened (i.e. single line) string
* from a line and column number. Note that the position is 0-indexed and
* the line and column numbers are 1-indexed.
*
* @param {number} line
* @param {number} column
* @param {string} string
*/
function getPositionFromMultiline(line, column, string) {
if (line === 1) return column - 1
let currentLn = 1
let position = 0
string.split('').find((char, idx) => {
if (char === '\n') currentLn += 1
if (currentLn === line) {
position = idx + column
return true
}
return false
})
return position
}
|