-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.js
337 lines (288 loc) · 9.71 KB
/
grammar.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { gsub, regExpToString, wrap } from '#internal/utils';
import Template from '#internal/template';
import Context from '#internal/context';
import Logger from '#internal/logger';
function resolve (value) {
if (typeof value === 'function') { return value(); }
return value;
}
class ParseError extends Error {
constructor (message) {
super(message);
this.name = 'ParseError';
}
}
class Grammar {
constructor (name, rules, options = {}) {
if (typeof name === 'object' && !rules) {
// Anonymous grammar.
this.name = null;
options = rules || options;
rules = name;
} else {
this.name = name;
this.names = [name, ...(options.alias || [])];
this._classNamePattern = new RegExp('\\b(?:' + this.names.join('|') + ')\\b');
}
this.options = options;
this.rules = [];
this.logger = new Logger(`Grammar ${this.name}`);
this._originalRules = rules;
this.extend(rules);
if (this.name) {
this.names.forEach(name => {
Grammar.register(name, this);
});
}
}
_toObject () {
return { ...this._originalRules };
}
parse (text, context = null, { logging = false } = {}) {
let pattern = this.pattern;
pattern.lastIndex = 0;
logging = context?.logging || logging;
this.logger.toggle( resolve(logging) );
if (!context) {
context = new Context({
highlighter: { parse: (x) => x }
});
}
context.logging = logging;
// eslint-disable-next-line
this.logger.debug(`Parsing ${this.name || ''}`, { pattern, text });
if ( !pattern.test(text) ) { return text; }
let parsed = gsub(text, pattern, (match, source) => {
let i = 0, j = 1, rule, index, actualLength;
// Find the rule that matched.
while (rule = this.rules[i++]) {
if (!match[j]) {
j += rule.length;
continue;
}
this.logger.debug('MATCH rule:', rule, match[j]);
if (rule.index) {
// The rule is saying that it might decide that it wants to parse
// less than what it was given. In that case it'll return an index
// representing the last character it's actually interested in.
//
// We'll return this index as a second return parameter from this
// handler in order to let gsub know what's up.
let prelim = [], q = j;
for (let q = 0; q < rule.length; q++) {
prelim.push(match[q + j]);
}
actualLength = rule.index(match[0], context, prelim);
if (actualLength <= 0) {
// -1 is your standard "string not found" index, and 0 is invalid
// because we need to consume at least some of the string to
// avoid an infinite loop. In both cases, ignore the result.
actualLength = undefined;
}
if (typeof actualLength !== 'undefined') {
// Trim the string down to the portion that we retroactively
// decided we care about.
index = actualLength + 1;
source = source.slice(0, match.index + index);
match = pattern.exec(source);
if (!match || !match[j]) {
let err = new ParseError(`Bad "index" callback; requested substring did not match original rule.`);
Object.assign(err, { rule, source, match, index: actualLength });
throw err;
}
}
}
let replacements = [];
for (let k = 0; k <= rule.length; k++) {
replacements.push(match[j + k]);
}
replacements.name = rule.name;
if (rule.captures) {
for (let i = 0; i < replacements.length; i++) {
if (!(i in rule.captures)) { continue; }
let captureValue = resolve(rule.captures[i]);
if (typeof captureValue === 'string') {
// A string capture just specifies the class name(s) this token
// should have. We'll wrap it in a `span` tag.
replacements[i] = wrap(replacements[i], captureValue);
} else if (captureValue instanceof Grammar) {
// A grammar capture tells us to parse this string with the
// grammar in question.
if (replacements[i]) {
replacements[i] = captureValue.parse(replacements[i], context);
}
}
}
}
if (rule.before) {
let beforeResult = rule.before(replacements, context);
if (typeof beforeResult !== 'undefined') {
replacements = beforeResult;
}
}
let replacer = rule.replacement;
if (!replacements.name) {
// Only assign the name if it isn't already there. The `before`
// callback might have changed the name.
replacements.name = rule.name;
}
replacements.index = match.index;
let result = replacer.evaluate(replacements);
if (rule.after) {
let afterResult = rule.after(result, context);
if (typeof afterResult !== 'undefined') {
result = afterResult;
}
}
if (typeof actualLength !== 'undefined') {
return [result, index];
}
return result;
}
// No matches, so let's return an empty string.
return '';
});
return parsed;
}
_makeRules (rules, prevCaptures = 0) {
let results = [];
for (let [ruleName, rawRule] of Object.entries(rules)) {
let name = rawRule.name || ruleName;
let rule = new Rule(name, rawRule, prevCaptures);
results.push(rule);
prevCaptures += rule.length;
}
return results;
}
match (className) {
return this._classNamePattern.test(className);
}
extend (...grammars) {
let grammar;
if (grammars.length === 1) {
grammar = grammars[0];
} else {
grammars.forEach((g) => this.extend(g));
return this;
}
if (grammar instanceof Grammar) {
grammar = grammar.toObject();
}
if (!grammar) {
throw new Error('Nonexistent grammar!');
}
let prevCaptures = 0;
if (this.rules.length) {
prevCaptures = this.rules
.map((r) => r.length)
.reduce((a, b) => a + b);
}
let rules = grammar;
let instances = this._makeRules(rules, prevCaptures);
this.rules.push(...instances);
this.pattern = new RegExp(
this.rules.map( (r) => r.pattern ).join('|'),
this.options.ignoreCase ? 'mi' : 'm'
);
return this;
}
toObject () {
let result = {};
this.rules.forEach((r) => {
result[r.name] = r.toObject();
});
return result;
}
}
const MAP = {};
Grammar.register = (name, grammar) => {
MAP[name] = grammar;
};
Grammar.find = (name) => {
return MAP[name] || null;
};
class Rule {
constructor (name, rule, prevCaptures) {
this.name = name;
let r = rule.replacement;
if (r) {
this.replacement = r instanceof Template ? r : new Template(r);
} else if (rule.captures) {
// If captures are defined, that means this pattern defines groups. We
// want a different default template that breaks those groups out. But we
// won't actually make it until we know how many groups the pattern has.
this.replacement = null;
} else {
this.replacement = Rule.DEFAULT_TEMPLATE;
}
this.debug = rule.debug;
this.before = rule.before;
this.after = rule.after;
this.index = rule.index;
this.captures = rule.captures;
let originalPattern = rule.pattern;
let pattern = rule.pattern;
if (pattern == null) {
throw new Error(`Rule must have pattern: ${name}`);
}
if (typeof pattern !== 'string') {
pattern = regExpToString(pattern);
}
// Alter backreferences so that they point to the right thing. Yes,
// this is ridiculous.
pattern = pattern.replace(/\\(\d+)/g, (m, d) => {
let group = Number(d);
let newGroup = (prevCaptures + group + 1);
// Adjust for the number of groups that already exist, plus the
// surrounding set of parentheses.
return `\\${newGroup}`;
});
// Count all open parentheses.
let parens = (pattern.match(/\(/g) || '').length;
// Subtract the ones that begin non-capturing groups.
let nonCapturing = (pattern.match(/\(\?[:!=]/g) || '').length;
// Subtract the ones that are literal open-parens.
let escaped = (pattern.match(/\\\(/g) || '').length;
// Add back the ones that match the literal pattern `\(?`, because they
// were counted twice instead of once.
let nonCapturingEscaped = (pattern.match(/\\\(\?[:!=]/g) || '').length;
let exceptions = ((nonCapturing + escaped) - nonCapturingEscaped);
// Add one because we're about to surround the whole thing in a
// capturing group.
this.length = (parens + 1) - exceptions;
this.pattern = `(${pattern})`;
this.originalPattern = originalPattern;
if (!this.replacement) {
this.replacement = Rule.makeReplacement(this.length, rule.wrapReplacement);
}
}
toObject () {
return {
// Export the original pattern, not the one we transformed. It'll need to
// be re-transformed in its new context.
pattern: this.originalPattern,
replacement: this.replacement,
before: this.before,
after: this.after,
index: this.index,
captures: this.captures
};
}
}
Rule.DEFAULT_TEMPLATE = new Template('<span class="#{name}">#{0}</span>');
Rule.makeReplacement = (length, wrap) => {
let arr = [];
for (let i = 1; i < length; i++) {
arr.push(i);
}
let captures = arr.join(`}#{`);
captures = `#{${captures}}`;
let contents = wrap ?
`<span class="#{name}">${captures}</span>` :
captures;
return new Template(contents);
};
export {
Grammar as default,
Rule
};