-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEcma404.js
57 lines (51 loc) · 1.49 KB
/
Ecma404.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
import {JsonFeedbackType, unexpected} from './JsonLow.js'
const unexpectedEnd = (context) => {
return {type: JsonFeedbackType.error, message: `Unexpected end ${context}!`}
}
export const Ecma404 = (next) => {
let topValue = false
const commas = []
const openStruct = (codePoint) => {
if (commas.length === 0) {
if (topValue) return unexpected(codePoint, 'after the top-level value')
else topValue = true
}
commas[commas.length - 1] = false
commas.push(false)
}
const closeStruct = (codePoint) => {
if (commas[commas.length - 1]) return unexpected(codePoint, `after ',' (trailing comma)`)
commas.pop()
}
const openValue = (codePoint) => {
if (commas.length === 0) {
if (topValue) return unexpected(codePoint, `after the top-level value`)
else topValue = true
}
commas[commas.length - 1] = false
}
const self = {
openObject: openStruct,
openArray: openStruct,
closeObject: closeStruct,
closeArray: closeStruct,
comma: () => commas[commas.length - 1] = true,
openFalse: openValue,
openTrue: openValue,
openNull: openValue,
openNumber: openValue,
openString: openValue,
end: () => {
if (topValue === false) return unexpectedEnd(`before the top-level value`)
},
}
return new Proxy(next, {
get: (target, prop, rec) => {
const fn = self[prop]
if (fn) return (...args) => {
return fn(...args) || target[prop]?.(...args)
}
return target[prop]
}
})
}