-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmunger.ts
158 lines (131 loc) · 4.49 KB
/
munger.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
import { Proc } from "./proc.js";
export type NamedLocator = { locatorName: string };
export type Locator = string | RegExp | NamedLocator;
export type Rule = { locator: Locator, replace: Munger };
export type Munger = string | Ruleset | Proc | Repeater | Sequence | SideEffects;
export type Context = {
registers: Map<string, string>;
arrays: Map<string, string[]>;
mungers: ReadonlyMap<string, Munger>;
};
export type Match = {
value: string;
groups: string[];
};
export type LocatedMatch = Match & { index: number; };
const emptyMap = new Map;
export function munge(input: string, munger: Munger, mungers: ReadonlyMap<string, Munger> = emptyMap) {
const lineNormalized = input.replace(/\r\n?/g, "\n");
const newContext = { registers: new Map, arrays: new Map, mungers };
return mungeCore({ value: lineNormalized, groups: [] }, munger, newContext);
}
function mungeCore(input: Match, munger: Munger, ctx: Context): string {
if (typeof munger === 'string') return munger;
else if (munger instanceof Proc) return munger.evaluate(input, ctx);
else return munger.apply(input, ctx);
}
function nextMatch(input: string, locator: Locator, startFrom: number): LocatedMatch | undefined {
if (typeof locator === 'string') {
const index = startFrom > input.length ? -1 : input.indexOf(locator, startFrom);
if (index >= 0) return { index, value: locator, groups: [] };
}
else if (locator instanceof RegExp) {
if (!locator.global) throw "gotta be global";
locator.lastIndex = startFrom;
const match = locator.exec(input);
if (match) return { index: match.index, value: match[0], groups: match.slice(1) };
}
else throw `Unimplemented locator type ${ locator }`;
}
function isNamed(locator: Locator): locator is NamedLocator {
return typeof locator === "object" && "locatorName" in locator;
}
export class Ruleset {
rules: Rule[];
howMany: number;
constructor(howMany: number, ...rules: Rule[]) {
this.howMany = howMany;
this.rules = rules;
}
apply(input: Match, ctx: Context): string {
let startOfMatch = -1, endOfMatch = 0, output: string[] = [], lastRuleIndex = -1;
let ruleIndex: number, bestMatch: LocatedMatch | undefined;
const locators = this.rules
.map(r => isNamed(r.locator) ? ctx.registers.get(r.locator.locatorName) : r.locator)
.filter((r): r is Exclude<Locator, NamedLocator> => r != null);
let ruleMatches: (LocatedMatch | undefined)[] = locators.map(() => ({ value: "", index: -1, groups: [] }));
for (let matchCount = 0; endOfMatch <= input.value.length; ) {
bestMatch = undefined;
ruleIndex = -1;
for (let i = 0; i < locators.length; i++) {
let ruleMatch = ruleMatches[i];
if (ruleMatch == null) continue;
const searchStart = i <= lastRuleIndex
? Math.max(startOfMatch + 1, endOfMatch)
: endOfMatch;
ruleMatches[i] = ruleMatch = nextMatch(input.value, locators[i], searchStart);
if (ruleMatch == null) continue;
if (bestMatch == null || ruleMatch.index < bestMatch.index) {
bestMatch = ruleMatch;
ruleIndex = i;
}
}
if (bestMatch == null) break;
output.push(
input.value.substring(endOfMatch, bestMatch.index),
mungeCore(bestMatch, this.rules[ruleIndex].replace, ctx));
lastRuleIndex = ruleIndex;
startOfMatch = bestMatch.index;
endOfMatch = bestMatch.index + bestMatch.value.length;
if (++matchCount === this.howMany) break;
}
return output.join('') + input.value.substring(endOfMatch);
}
repeat() {
return new Repeater(this);
}
}
export class Repeater {
private munger: Munger;
constructor(munger: Munger) {
this.munger = munger;
}
apply(input: Match, ctx: Context): string {
let last = input.value, output = mungeCore(input, this.munger, ctx);
while (last !== output) {
last = output;
output = mungeCore({ value: output, groups: [] }, this.munger, ctx);
}
return output;
}
}
export class Sequence {
steps: Munger[];
howMany: number;
constructor(howMany: number, ...steps: Munger[]) {
this.howMany = howMany;
this.steps = steps;
}
apply(match: Match, ctx: Context): string {
let input = match.value, changes = 0;
for (let munger of this.steps) {
let next = mungeCore({ value: input, groups: [] }, munger, ctx);
if (next !== input && ++changes === this.howMany) return next;
input = next;
}
return input;
}
repeat() {
return new Repeater(this);
}
}
export class SideEffects {
munger: Munger;
constructor(munger: Munger) {
this.munger = munger;
}
apply(match: Match, ctx: Context): string {
mungeCore(match, this.munger, ctx);
return match.value;
}
}