forked from no-context/moo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-syntax.js
More file actions
242 lines (198 loc) · 5.86 KB
/
json-syntax.js
File metadata and controls
242 lines (198 loc) · 5.86 KB
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
(function(root, factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory()
} else {
root.jsonTokenizer = factory()
}
}(this, function() {
const tokens = {"STRING":"11","NUMBER":"12","{":"13","}":"14","[":"15","]":"16",",":"17",":":"18","TRUE":"19","FALSE":"20","NULL":"21","$":"22"};
const EOF = '$';
let tokenizer;
/**
* Generic tokenizer used by the parser in the Syntax tool.
*
* https://www.npmjs.com/package/syntax-cli
*
* See `--custom-tokinzer` to skip this generation, and use a custom one.
*/
const lexRules = [[/^\s+/, function() { return 'WHITESPACE' }, ],
[/^-?(?:[0-9]|[1-9][0-9]+)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\b/, function() { return 'NUMBER'; }, ],
[/^"(?:\\["bfnrt\/\\]|\\u[a-fA-F0-9]{4}|[^"\\])*"/, function() { return 'STRING'; }, ],
[/^\{/, function() { return '{' }, ],
[/^\}/, function() { return '}' }, ],
[/^\[/, function() { return '[' }, ],
[/^\]/, function() { return ']' }, ],
[/^,/, function() { return ',' }, ],
[/^:/, function() { return ':' }, ],
[/^true\b/, function() { return 'TRUE' }, ],
[/^false\b/, function() { return 'FALSE' }, ],
[/^null\b/, function() { return 'NULL' }, ]];
const lexRulesByConditions = {"INITIAL":[0,1,2,3,4,5,6,7,8,9,10,11]};
const EOF_TOKEN = {
type: EOF,
value: EOF,
};
tokenizer = {
initString(string) {
this._originalString = string;
this._string = this._originalString + EOF;
this._cursor = 0;
this._states = ['INITIAL'];
this._tokensQueue = [];
this._currentLine = 1;
this._currentColumn = 0;
this._currentLineBeginOffset = 0;
/**
* Matched token location data.
*/
this._tokenStartOffset = 0;
this._tokenEndOffset = 0;
this._tokenStartLine = 1;
this._tokenEndLine = 1;
this._tokenStartColumn = 0;
this._tokenEndColumn = 0;
return this;
},
/**
* Returns tokenizer states.
*/
getStates() {
return this._states;
},
getCurrentState() {
return this._states[this._states.length - 1];
},
pushState(state) {
this._states.push(state);
},
begin(state) {
this.pushState(state);
},
popState() {
if (this._states.length > 1) {
return this._states.pop();
}
return this._states[0];
},
getNextToken() {
// Something was queued, return it.
if (this._tokensQueue.length > 0) {
return this._toToken(this._tokensQueue.shift());
}
if (!this.hasMoreTokens()) {
return EOF_TOKEN;
} else if (this.isEOF()) {
this._cursor++;
return EOF_TOKEN;
}
let string = this._string.slice(this._cursor);
let lexRulesForState = lexRulesByConditions[this.getCurrentState()];
for (let i = 0; i < lexRulesForState.length; i++) {
let lexRuleIndex = lexRulesForState[i];
let lexRule = lexRules[lexRuleIndex];
let matched = this._match(string, lexRule[0]);
if (matched) {
yytext = matched;
yyleng = yytext.length;
let token = lexRule[1].call(this);
if (!token) {
return this.getNextToken();
}
// If multiple tokens are returned, save them to return
// on next `getNextToken` call.
if (Array.isArray(token)) {
const tokensToQueue = token.slice(1);
token = token[0];
if (tokensToQueue.length > 0) {
this._tokensQueue.unshift(...tokensToQueue);
}
}
return this._toToken(token, yytext);
}
}
this.throwUnexpectedToken(
string[0],
this._currentLine,
this._currentColumn
);
},
/**
* Throws default "Unexpected token" exception, showing the actual
* line from the source, pointing with the ^ marker to the bad token.
* In addition, shows `line:column` location.
*/
throwUnexpectedToken(symbol, line, column) {
const lineSource = this._originalString.split('\n')[line - 1];
let lineData = '';
if (lineSource) {
const pad = ' '.repeat(column);
lineData = '\n\n' + lineSource + '\n' + pad + '^\n';
}
throw new SyntaxError(
`${lineData}Unexpected token: "${symbol}" ` +
`at ${line}:${column}.`
);
},
getCursor() {
return this._cursor;
},
getCurrentLine() {
return this._currentLine;
},
getCurrentColumn() {
return this._currentColumn;
},
_captureLocation(matched) {
const nlRe = /\n/g;
// Absolute offsets.
this._tokenStartOffset = this._cursor;
// Line-based locations, start.
this._tokenStartLine = this._currentLine;
this._tokenStartColumn =
this._tokenStartOffset - this._currentLineBeginOffset;
// Extract `\n` in the matched token.
let nlMatch;
while ((nlMatch = nlRe.exec(matched)) !== null) {
this._currentLine++;
this._currentLineBeginOffset = this._tokenStartOffset + nlMatch.index + 1;
}
this._tokenEndOffset = this._cursor + matched.length;
// Line-based locations, end.
this._tokenEndLine = this._currentLine;
this._tokenEndColumn = this._currentColumn =
(this._tokenEndOffset - this._currentLineBeginOffset);
},
_toToken(tokenType, yytext = '') {
return {
// Basic data.
type: tokenType,
value: yytext,
// Location data.
startOffset: this._tokenStartOffset,
endOffset: this._tokenEndOffset,
startLine: this._tokenStartLine,
endLine: this._tokenEndLine,
startColumn: this._tokenStartColumn,
endColumn: this._tokenEndColumn,
};
},
isEOF() {
return this._string[this._cursor] === EOF &&
this._cursor === this._string.length - 1;
},
hasMoreTokens() {
return this._cursor < this._string.length;
},
_match(string, regexp) {
let matched = string.match(regexp);
if (matched) {
// Handle `\n` in the matched token to track line numbers.
this._captureLocation(matched[0]);
this._cursor += matched[0].length;
return matched[0];
}
return null;
},
};
return tokenizer;
}))