forked from prantlf/jsonlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfail.js
278 lines (254 loc) · 7.29 KB
/
fail.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const createGuard = require('./common/createGuard')
const chevrotainParse = require('./chevrotain/pure')
const handbuiltParse = require('./hand-built/pure')
const { parse: jjuParse } = require('./jju/pure')
const { parse: pegjsParse } = require('./pegjs/pure')
const jisonParser = require('./jison/pure').parser
const JSON5 = require('json5')
const JSON6 = require('json-6')
const { parse: parseWithComments } = require('comment-json')
const inputSources = [
`{
# Sets a value to a property.
"property": "value"
}`,
`{
1,
"property": "value"
}`,
`{
"property" "value"
}`,
`{
"property":
}`,
`{
"property": "value"
"count": 1
}`,
`{
"property": "value",
}`,
`{
"property": "value"`,
' ',
''
]
let inputSource
function getOffset (line, column) {
if (line > 1) {
const breaks = /\r?\n/g
let match
while ((match = breaks.exec(inputSource))) {
if (--line === 1) {
return match.index + column
}
}
}
return column - 1
}
function getLineAndColumn (offset) {
const lines = inputSource
.substr(0, offset)
.split(/\r?\n/)
const line = lines.length
const column = lines[line - 1].length + 1
return { line, column }
}
function pastInput ({ offset }) {
const start = Math.max(0, offset - 20)
const previous = inputSource.substr(start, offset - start)
return (offset > 20 ? '...' : '') + previous.replace(/\r?\n/g, '')
}
function upcomingInput ({ offset }) {
let start = Math.max(0, offset - 20)
start += offset - start
const rest = inputSource.length - start
const next = inputSource.substr(start, Math.min(20, rest))
return next.replace(/\r?\n/g, '') + (rest > 20 ? '...' : '')
}
function showPosition ({ offset }) {
const past = pastInput({ offset })
const upcoming = upcomingInput({ offset })
const c = new Array(past.length + 1).join('-')
return `${past}${upcoming}\n${c}^`
}
// Workaround for missing column number in jison error output.
function parseError (message, hash) {
const match = /Parse error on line (\d+):/.exec(message)
if (match) {
const loc = parseError.yy.yylloc
message = message.substr(0, match.index) +
`Parse error on line ${match[1]}` +
(loc ? `, column ${loc.first_column + 1}:` : ':') +
message.substr(match.index + match[0].length)
}
if (hash.recoverable) {
this.trace(message)
} else {
const error = new SyntaxError(message)
error.hash = hash
throw error
}
}
jisonParser.yy.parseError = parseError
function improveJSONError (error) {
let { message } = error
const match = / in JSON at position (\d+)$/.exec(message)
let offset
if (match) {
offset = +match[1]
message = message.substr(0, match.index)
} else {
offset = inputSource.length
}
const { line, column } = getLineAndColumn(offset)
const cursor = `line ${line}, column ${column}`
const position = showPosition({ offset })
error.message = `Parse error on ${cursor}:\n${position}\n${message}`
throw error
}
function parseBuiltIn () {
try {
JSON.parse(inputSource)
} catch (error) {
improveJSONError(error)
}
}
function parseChevrotain () {
let { lexErrors, parseErrors } = chevrotainParse(inputSource)
let type, line, column, offset, message
if (lexErrors.length || parseErrors.length) {
if (lexErrors.length && parseErrors.length) {
const { line: lexLine, column: lexColumn } = lexErrors[0]
const { startLine: parseLine, startColumn: parseColumn } = parseErrors[0].token
if (lexLine < parseLine || lexColumn < parseColumn) {
parseErrors = []
} else {
lexErrors = []
}
}
}
if (lexErrors.length) {
({ line, column, offset, message } = lexErrors[0])
type = 'Lexical'
} else if (parseErrors.length) {
let token
({ message, token } = parseErrors[0]);
({ startLine: line, startColumn: column, startOffset: offset } = token)
type = 'Parse'
}
if (message) {
if (isNaN(line)) {
offset = inputSource.length;
({ line, column } = getLineAndColumn(offset))
}
const cursor = `line ${line}, column ${column}`
const position = showPosition({ offset })
message = message.charAt(0).toUpperCase() + message.substr(1)
throw new SyntaxError(`${type} error on ${cursor}:\n${position}\n${message}`)
}
}
function parseHandbuilt () {
handbuiltParse(inputSource)
}
function improveJjuError (error) {
const { message, row, column } = error
const cursor = `line ${row}, column ${column}`
const offset = getOffset(row, column)
const position = showPosition({ offset })
const reason = message
.split(/\r?\n/)[0]
.replace(/ at \d+:\d+$/, '')
error.message = `Parse error on ${cursor}:\n${position}\n${reason}`
throw error
}
function parseJju () {
try {
jjuParse(inputSource)
} catch (error) {
improveJjuError(error)
}
}
function parseCommentJson () {
parseWithComments(inputSource)
}
function improvePegjsError (error) {
const { message, location } = error
const { line, column, offset } = location.start
const cursor = `line ${line}, column ${column}`
const position = showPosition({ offset })
error.message = `Parse error on ${cursor}:\n${position}\n${message}`
throw error
}
function parsePegjs () {
try {
pegjsParse(inputSource)
} catch (error) {
improvePegjsError(error)
}
}
function parseJison () {
jisonParser.parse(inputSource, { allowSingleQuotedStrings: true })
}
function improveJSON5Error (error) {
let { message, lineNumber, columnNumber } = error
const cursor = `line ${lineNumber}, column ${columnNumber}`
const offset = getOffset(lineNumber, columnNumber)
const position = showPosition({ offset })
message = message
.replace(/^JSON5: /, '')
.replace(/ at \d+:\d+$/, '')
message = message.charAt(0).toUpperCase() + message.substr(1)
error.message = `Parse error on ${cursor}:\n${position}\n${message}`
throw error
}
function parseJSON5 () {
try {
JSON5.parse(inputSource)
} catch (error) {
improveJSON5Error(error)
}
}
function improveJSON6Error (error) {
let { message } = error
message = message.replaceAll('\n', ' ')
let [, lineNumber, columnNumber] = / \[(\d+):(\d+)\]$/.exec(message)
--columnNumber
const cursor = `line ${lineNumber}, column ${columnNumber}`
const offset = getOffset(lineNumber, columnNumber)
const position = showPosition({ offset })
message = message
.replace(/ \[\d+:\d+\]$/, '')
.replace(/ \(near '.+'\)$/, '')
.replace(/ unexpected at \d+$/, '')
message = message.charAt(0).toUpperCase() + message.substr(1)
error.message = `Parse error on ${cursor}:\n${position}\n${message}`
throw error
}
function parseJSON6 () {
try {
JSON6.parse(inputSource)
} catch (error) {
improveJSON6Error(error)
}
}
for (const test of inputSources) {
inputSource = test
const formattedTest = test
.split(/\r?\n/)
.map(line => ' ' + line)
.join('\n')
createGuard(`Parsing invalid JSON data:\n${formattedTest}\nusing`)
.add('the built-in parser', parseBuiltIn)
.add('the chevrotain parser', parseChevrotain)
.add('the hand-built parser', parseHandbuilt)
.add('the jju parser', parseJju)
.add('the comment-json parser', parseCommentJson)
.add('the pegjs parser', parsePegjs)
.add('the jison parser', parseJison)
.add('the JSON5 parser', parseJSON5)
.add('the JSON6 parser', parseJSON6)
.start()
console.log()
}