-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-grammar.ts
184 lines (169 loc) · 5.1 KB
/
json-grammar.ts
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
import {
Scanner, OPERATIONS, HOOK_MODE, IScanEnv,
IPattern, Token,
IPosition, ISourceLocation,
} from "./scanner"
const {
FINISH,
WRAP, UNWRAP, UNWRAP_ALL,
OPTION,
SPLIT,
NO_COLLECT, NO_CAPTURE,
MARK_AS_ROOT, FORK_IN_PARENT, FORK_IN_ROOT,
END, END_ON_LEFT,
useKey,
node, key, pick, hook, pipe, prev, merge,
MATCH_BEGIN,
MATCH_END,
MATCH_EOF,
MERGE_ALL_TOKENS
} = OPERATIONS;
const TEST_KEYWORD_BOUNDARY = [
[NO_CAPTURE, ["", " ", "\n", `\t`, ",", "(", ")", "{", "}", ":", "[", "]"]]
];
const THROW_TOKEN_ERROR = hook(function (env, start, end) {
console.log("Invalid or unexpected token", start, end);
}, HOOK_MODE.RESOLVE);
const LITERAL_VALUE_MAP = { true: true, false: false, null: null };
const COLLECT_STRING = [
[
pick(function (tokens, env, start, end) {
return [env.input.slice(start.offset, end.offset), tokens[1].value];
}),
`"`,
hook(function (env) {
env.useEscape = true;
}, HOOK_MODE.CAPTURE),
WRAP,
[
[THROW_TOKEN_ERROR, "\n"],
[
`"`,
hook(function (env) {
env.useEscape = false;
}, HOOK_MODE.CAPTURE),
UNWRAP,
]
]
]
];
const MATCH_WHITE_SPACE_CHARACHER = [` `, `\n`, `\t`];
const CLEAR_WHITE_SPACE_CHARACHER = [
[NO_COLLECT, MATCH_WHITE_SPACE_CHARACHER]
];
const Literal = node("Literal").Wrapper;
const PARSE_NUMBER_LITERAL = pipe(
function (token: Token, env, start, end) {
let value: any = token.value;
const result: any = /^(-?[1-9][0-9]*|0)(.[0-9]+)?([eE][-+]?[0-9]+)?$/g.exec(value);
if (result) {
const [, int, frac, exp] = result;
value = int | 0;
frac && (value += frac % 1);
exp && (value *= Math.pow(10, exp.slice(1) | 0));
} else {
debugger;
}
return Object.assign( // Number(value)
new Literal(),
{ raw: token.value, value: value, loc: { start, end } }
);
} ,
);
export default <IPattern>[
[PARSE_NUMBER_LITERAL, CLEAR_WHITE_SPACE_CHARACHER],
[
node("Literal", function ([[raw, value]]: any) {
return [
useKey("raw", raw),
useKey("value", value)
];
}),
[
[COLLECT_STRING],
[
pick(function ({ value }: Token) {
return [value, LITERAL_VALUE_MAP[value]]
}),
"",
["true", "false", "null"]
, TEST_KEYWORD_BOUNDARY
]
]
],
[
node("Object", function ([tokens]: [Array<any>]) {
return useKey("children", tokens.filter(
function (token) {
if (token instanceof Token) {
debugger;
return false;
}
return true;
}
));
}),
NO_COLLECT, "{", WRAP,
[
[NO_COLLECT, "}", UNWRAP],
[CLEAR_WHITE_SPACE_CHARACHER],
[
node("Identifier", function ([[raw, value]]: any) {
return [
useKey("value", value),
useKey("raw", raw)
]
}),
COLLECT_STRING,
node("Property", function ([key, [value]]) {
const props = [useKey("key", key)];
if (value.length === 1 && !(value[0] instanceof Token)) {
props.push(useKey("value", value[0]));
} else {
debugger;
}
return props;
}),
prev(),
WRAP,
[
[CLEAR_WHITE_SPACE_CHARACHER],
[
NO_COLLECT, ":", WRAP,
FORK_IN_ROOT,
PARSE_NUMBER_LITERAL,
[
[NO_CAPTURE, "}", UNWRAP, UNWRAP],
[NO_COLLECT, ",", UNWRAP, UNWRAP],
]
]
]
]
]
],
[
node("Array", function ([tokens]: [Array<any>]) {
const children = [];
for (let i = 0, len = tokens.length; i < len; i += 2) {
let value = tokens[i];
let spearator = tokens[i + 1];
if (!spearator || (spearator.value === "," && i < len - 2)) {
if (!(value instanceof Token)) {
children.push(value);
continue;
}
}
debugger;
break;
}
return useKey("children", children);
}),
NO_COLLECT, "[", WRAP,
FORK_IN_ROOT,
PARSE_NUMBER_LITERAL,
[
[NO_COLLECT, "]", UNWRAP],
[","]
]
]
];