-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.ts
214 lines (207 loc) · 8.17 KB
/
parsing.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
import {
Token,
TokenStream,
TokenSelector,
selectsToken,
showToken,
ParseError,
} from './lex';
type Maker<From, To> = To | ((x: From) => To);
class ParserFor<T> {
constructor(public run: (stream: TokenStream) => {result: T, rest: TokenStream}) {}
then<S>(other: Maker<T, ParserFor<S>>): ParserFor<T&S> {
return new ParserFor(stream => {
let first = this.run(stream);
let second = (typeof other == "function" ? other(first.result) as any : other).run(first.rest);
if (first.result instanceof Array) {
if (Object.keys(second.result).length == 0) {
return {result: first.result, rest: second.rest};
}
throw {message: "bad - combining array with object", first, second};
}
if (second.result instanceof Array) {
if (Object.keys(first.result).length == 0) {
return {result: second.result, rest: second.rest};
}
throw {message: "bad - combining array with object", first, second};
}
return {
result: Object.assign({}, first.result, second.result),
rest: second.rest,
};
});
}
thenInstead<S>(other: (result: T) => ParserFor<S>): ParserFor<S> {
return new ParserFor(stream => {
let first = this.run(stream);
return other(first.result).run(first.rest);
});
}
thenIn<P>(otherMap: {[k in keyof P]: Maker<T, ParserFor<P[k]>>}): ParserFor<T & P> {
if (Object.keys(otherMap).length != 1) {
throw {message: "thenIn given map with more or less than one key", otherMap};
}
let key: keyof typeof otherMap = Object.keys(otherMap)[0] as any;
let other = otherMap[key];
return new ParserFor(stream => {
let first = this.run(stream);
let second = (typeof other == "function" ? other(first.result) : other).run(first.rest);
return {
result: Object.assign({}, first.result, {[key]: second.result}) as any,
rest: second.rest,
};
});
}
thenToken<P extends {[k: string]: string}>(map: P, fail: Maker<T, string>): ParserFor<T & {[k in keyof P]: Token}> {
let keys: (keyof P)[] = Object.keys(map) as (keyof P)[]; // QUESTION: why isn't this the inferred type?
if (keys.length != 1) {
throw "bad thenToken call";
}
let key: keyof P = keys[0];
let pattern = map[key];
return this.thenWhen({
[pattern as string]: (token: Token) => pure<{[k in keyof P]: Token}>({[key]: token} as any), // QUESTION: why does this need a cast?
}, typeof fail == "function" ? ((x: T) => ParserFor.fail(fail(x))) : ParserFor.fail(fail));
}
manyBetween<S>(between: TokenSelector): ParserFor<T[]> {
return new ParserFor((stream): {result: T[], rest: TokenStream} => {
let first = this.run(stream);
let current = first.rest;
let pieces = [first.result];
while (true) {
if (!selectsToken(between, current.head())) {
return {result: pieces, rest: current};
}
current = current.tail(); // skip the separating token
let next = this.run(current);
pieces.push(next.result);
current = next.rest;
}
});
}
manyUntil(finish: TokenSelector, otherwise: ParserFor<any>): ParserFor<T[]> {
return new ParserFor(stream => {
let current = stream;
let result: T[] = [];
while (true) {
if (!current.head()) {
return otherwise.run(current);
}
if (selectsToken(finish, current.head())) {
return {result, rest: current};
}
let piece = this.run(current);
result.push(piece.result);
current = piece.rest;
}
})
}
manyWhen(leads: TokenSelector[]): ParserFor<{lead: Token, item: T}[]> {
return new ParserFor(stream => {
let current = stream;
let result: {lead: Token, item: T}[] = [];
while (true) {
let found = false;
for (let lead of leads) {
if (selectsToken(lead, current.head())) {
let pieceResult = this.run(current.tail());
result.push({item: pieceResult.result, lead: current.head()});
current = pieceResult.rest;
found = true;
break;
}
}
if (!found) {
break;
}
}
return {result, rest: current};
});
}
map<S>(f: (x: T) => S): ParserFor<S> {
return new ParserFor(stream => {
let okay = this.run(stream);
return {result: f(okay.result), rest: okay.rest};
});
}
merge<M>(m: M): ParserFor<T & M> {
return this.map(v => {
return Object.assign({}, v, m);
});
}
static fail(message: string | ((t: Token) => string)): ParserFor<never> {
let messageMaker = typeof message !== "string" ? message : (token: Token) => message + " near " + showToken(token);
return new ParserFor(stream => {throw new ParseError(messageMaker(stream.head()))});
}
context(message: string): ParserFor<T> {
return new ParserFor(stream => {
try {
return this.run(stream);
} catch (e) {
if (e instanceof ParseError) {
throw new ParseError(message + ("\n" + e.message).replace(/\n/g, "\n\t"));
}
throw e;
}
});
}
thenWhen<M, E>(m: {[K in keyof M]: Maker<Token, ParserFor<M[K]>>}, otherwise: Maker<T, ParserFor<E>>): ParserFor<T & (M[keyof M] | E)> {
if (typeof otherwise == "function") {
return this.then(res => ParserFor.when(m, otherwise(res)));
} else {
return this.then(ParserFor.when(m, otherwise));
}
}
static when<M, E>(m: {[K in keyof M]: Maker<Token, ParserFor<M[K]>>}, otherwise: ParserFor<E>): ParserFor<M[keyof M] | E> {
return new ParserFor<M[keyof M] | E>((stream: TokenStream): {result: M[keyof M] | E, rest: TokenStream} => {
for (let k in m) {
let p = m[k];
if (k.charAt(0) == "$") {
if (stream.head().type == k.substr(1)) {
if (typeof p === "function") {
const intermediate = p(stream.head());
return intermediate.run(stream.tail());
} else {
return p.run(stream.tail());
}
}
} else {
if (stream.head().text == k) {
if (typeof p === "function") {
let parser: ParserFor<M[keyof M]> = p(stream.head());
return parser.run(stream.tail());
} else {
return p.run(stream.tail());
}
}
}
}
return otherwise.run(stream);
});
}
}
function pure<T>(t: T): ParserFor<T> {
return new ParserFor(stream => {
return {result: t, rest: stream};
});
}
function matched<T>(parser: ParserFor<T>): (open: Token) => ParserFor<T> {
const matching = (open: Token): string => {
if (open.text == "(") {
return ")";
}
if (open.text == "[") {
return "]";
}
if (open.text == "{") {
return "}";
}
throw {message: "invalid opening brace", open};
};
return (open: Token) => parser.thenWhen({ [matching(open)]: pure({})}, ParserFor.fail(`expected '${matching(open)}' to close '${open.text}' opened at ${showToken(open)}`));
}
export {
ParserFor,
pure,
matched,
};