-
Notifications
You must be signed in to change notification settings - Fork 0
/
mungerparser.ts
218 lines (188 loc) · 6.65 KB
/
mungerparser.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
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
import { Locator, Munger, Repeater, Rule, Ruleset, Sequence, SideEffects } from './munger.js';
import { Proc } from "./proc.js";
export class ParseFailure {
message: string;
position: number;
line: number;
column: number;
constructor(message: string, position: number, line: number, column: number) {
this.message = message;
this.position = position;
this.line = line;
this.column = column;
}
}
export function parse(source: string): { munger: Munger, named: Map<string, Munger> } {
let consumed = 0;
const Upcoming = /.*/y;
function fail(message: string): never {
const sofar = source.substring(0, consumed);
let line = 1, col = consumed + 1;
for (let nl of sofar.matchAll(/\n/g)) {
if (nl.index == null) throw Error("Internal error: RegExp match doesn't have index");
if (nl.index > consumed) break;
++line;
col = consumed - nl.index + 1;
}
console.error(`${ line }:${ col } ` + message);
Upcoming.lastIndex = consumed;
const upcoming = Upcoming.exec(source);
if (upcoming) console.error(`Upcoming: ` + upcoming[0]);
throw new ParseFailure(message, consumed, line, col);
}
const WhiteSpaceAndComments = /(?:\s|!.*)+/y;
// no backtracking
function tryParse(tokenPattern: RegExp): RegExpExecArray | undefined {
if (!tokenPattern.sticky) fail(`Token pattern ${tokenPattern} not sticky.`);
WhiteSpaceAndComments.lastIndex = consumed;
consumed += WhiteSpaceAndComments.exec(source)?.[0].length ?? 0;
tokenPattern.lastIndex = consumed;
let match = tokenPattern.exec(source);
if (match) {
consumed += match[0].length;
return match;
}
}
const DoubleStringLiteral = /"(?:[^\\"]|\\.)*"/y;
function parseDoubleStringLiteral(): string | undefined {
let match = tryParse(DoubleStringLiteral);
if (match) return JSON.parse(match[0]);
}
const SingleStringLiteral = /'(?:[^\\']|\\.)*'/y;
function parseSingleStringLiteral(): string | undefined {
let match = tryParse(SingleStringLiteral);
if (match) return JSON.parse(match[0].replace(/^'|'$/g, '"'));
}
const RegExpLiteral = /\/((?:[^\\\n/]|\\.)+)\/([ism]*)/y;
function parseRegExpLiteral(): RegExp | undefined {
let match = tryParse(RegExpLiteral);
if (match) return new RegExp(match[1], match[2] + 'g');
}
const NamedLocator = /get\((\w+)\)/y;
function parseNamedLocator() {
const match = tryParse(NamedLocator);
if (match == null) return undefined;
return { locatorName: match[1] };
}
function parseLocator(): Locator | undefined {
return parseSingleStringLiteral()
?? parseRegExpLiteral()
?? parseNamedLocator();
}
const GoesTo = /=>/y;
function parseRule(): Rule | undefined {
let find = parseLocator();
if (find == null) return undefined;
if (!tryParse(GoesTo)) fail(`Expected '=>'`);
let replace = parseMunger();
if (replace == null) fail(`Expected rule munger`);
return { locator: find, replace };
}
function parseSingleRule(): Ruleset | undefined {
const rule = parseRule();
if (rule == null) return undefined;
return new Ruleset(Number.POSITIVE_INFINITY, rule);
}
const RulesetOpen = /(\d+)?\(/y;
const RulesetClose = /\)/y;
function parseRuleset(): Ruleset | undefined {
let open = tryParse(RulesetOpen);
if (!open) return undefined;
let rules: Rule[] = [], rule: Rule | undefined;
while (rule = parseRule()) rules.push(rule);
if (!tryParse(RulesetClose)) fail(`Expected rule or ')'`);
return new Ruleset(open[1] ? Number(open[1]) : Number.POSITIVE_INFINITY, ...rules);
}
const SequenceOpen = /(\d+)?#\(/y;
const SequenceClose = /\)/y;
function parseSequence(): Sequence | undefined {
let open = tryParse(SequenceOpen);
if (!open) return undefined;
let mungers: Munger[] = [], munger: Munger | undefined;
while ((munger = parseMunger()) != null) mungers.push(munger);
if (!tryParse(SequenceClose)) fail(`Expected munger or ')'`);
return new Sequence(open[1] ? Number(open[1]) : Number.POSITIVE_INFINITY, ...mungers);
}
const ProcOpen = /{/y;
const ProcInstruction = /"(?:[^\\"]|\\.)*"|(?:(?!["{}])\S)+/y;
const ProcClose = /}/y;
function parseProc(): Proc | undefined {
if (!tryParse(ProcOpen)) return undefined;
let instructions: (string | Proc)[] = [], match: RegExpExecArray | undefined;
while (true) {
if (match = tryParse(ProcInstruction)) {
instructions.push(match[0]);
continue;
}
const innerProc = parseProc();
if (innerProc) {
instructions.push(innerProc);
continue;
}
break;
}
if (!tryParse(ProcClose)) fail(`Expected instruction or '}'`);
return new Proc(instructions);
}
const RepeaterPrefix = /@/y;
function parseRepeater(): Repeater | undefined {
if (!tryParse(RepeaterPrefix)) return undefined;
const munger = parseMunger();
if (munger == null) fail(`Expected munger after '@' decorator`);
return new Repeater(munger);
}
const EatPrefix = /eat\b/y;
function parseEater(): Sequence | undefined {
if (!tryParse(EatPrefix)) return undefined;
const munger = parseMunger();
if (munger == null) fail(`Expected munger after 'eat' decorator`);
return new Sequence(Number.POSITIVE_INFINITY, munger, "");
}
const EffectPrefix = /fx\b/y;
function parseEffect(): SideEffects | undefined {
if (!tryParse(EffectPrefix)) return undefined;
const munger = parseMunger();
if (munger == null) fail(`Expected munger after 'fx' decorator`);
return new SideEffects(munger);
}
let namedMungers = new Map<string, Munger>();
const NamedMungerRef = /do\((\w+)\)/y
function parseNamedMungerRef(): Munger | undefined {
const match = tryParse(NamedMungerRef);
if (match == null) return undefined;
const name = match[1];
if (!namedMungers.has(name)) fail(`Undeclared munger reference: '${ name }'`);
return namedMungers.get(name);
}
function parseMunger(): Munger | undefined {
return parseRuleset()
?? parseSequence()
?? parseRepeater()
?? parseEater()
?? parseEffect()
?? parseProc()
?? parseSingleRule()
?? parseDoubleStringLiteral()
?? parseNamedMungerRef();
}
const MungerDeclaration = /def\((\w+)\)/y;
function parseMungerDef(): {name: string, munger: Munger} | undefined {
let decl = tryParse(MungerDeclaration);
if (decl == null) return undefined;
const name = decl[1];
let munger = parseMunger();
if (munger == null) fail(`Expected munger definition after named declaration`);
return { name, munger };
}
while (true) {
const named = parseMungerDef();
if (named == null) break;
if (namedMungers.has(named.name)) fail(`Duplicate def() for ${ named.name }`);
namedMungers.set(named.name, named.munger);
}
const EOF = /$/y;
const munger = parseMunger();
if (munger == null) fail(`Expected munger definition`);
if (!tryParse(EOF)) fail(`Expected EOF following munger`);
return { munger, named: namedMungers };
}