Repository navigation
Expand file tree
/
Copy pathparser.ts
More file actions
288 lines (258 loc) · 9.37 KB
/
Copy pathparser.ts
File metadata and controls
288 lines (258 loc) · 9.37 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
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
import { Lexer, Operator, TemplateLexer, Token, TokenType } from './lexer'
export type ExpressionNode =
| { FunctionCall: { name: string; args: ExpressionNode[] } }
| { Identifier: string }
| { Literal: { type: TokenType.String | TokenType.Number; value: string } }
| { Template: { parts: ExpressionNode[] } }
| { Unary: { operator: Operator; expression: ExpressionNode } }
| { Binary: { operator: Operator; left: ExpressionNode; right: ExpressionNode } }
/**
* Parses an expression.
*
* TODO: Operator precedence is not handled correctly. Use parentheses to be explicit about your desired
* precedence.
*/
export class Parser {
protected lexer!: Lexer
public parse(exprStr: string): ExpressionNode {
if (!this.lexer) {
this.lexer = new Lexer()
}
this.lexer.reset(exprStr)
const expr = this.parseExpression()
const token = this.lexer.next()
if (token !== undefined) {
throw new SyntaxError(
`Unexpected token at end of input: ${JSON.stringify(token.value)} (at ${this.lexer.index})`
)
}
return expr
}
// ArgumentList := Expression |
// Expression ',' ArgumentList
private parseArgumentList(): ExpressionNode[] {
const args: ExpressionNode[] = []
while (true) {
const expr = this.parseExpression()
if (expr === undefined) {
throw new Error(
`Parse error on token in arguments list: ${JSON.stringify(this.lexer.peek())} (at ${
this.lexer.index
})`
)
}
args.push(expr)
const token = this.lexer.peek()
if (!matchOp(token, ',')) {
break
}
this.lexer.next()
}
return args
}
// FunctionCall ::= Identifier '(' ')' ||
// Identifier '(' ArgumentList ')'
private parseFunctionCall(name: string): ExpressionNode {
let token: Pick<Token, 'type' | 'value'> | undefined = this.lexer.next()
if (!matchOp(token, '(')) {
throw new SyntaxError(`Expected "(" in function call ${JSON.stringify(name)} (at ${this.lexer.index})`)
}
token = this.lexer.peek()
const args: ExpressionNode[] = matchOp(token, ')') ? [] : this.parseArgumentList()
token = this.lexer.next()
if (!matchOp(token, ')')) {
throw new SyntaxError(`Expected ")" in function call ${JSON.stringify(name)} (at ${this.lexer.index})`)
}
return {
FunctionCall: {
name,
args,
},
}
}
private parseTemplateParts(): ExpressionNode[] {
const parts: ExpressionNode[] = []
while (true) {
const token: Pick<Token, 'type' | 'value'> | undefined = this.lexer.peek()
if (!token) {
break
}
if (token.type === TokenType.TemplateTail) {
if (token.value) {
parts.push({ Literal: { type: TokenType.String, value: token.value } })
}
this.lexer.next()
break
}
if (matchOp(token, '}')) {
this.lexer.next()
} else if (token.type === TokenType.TemplateMiddle) {
if (token.value) {
parts.push({ Literal: { type: TokenType.String, value: token.value } })
}
this.lexer.next()
} else {
parts.push(this.parseExpression())
}
}
return parts
}
protected parseTemplate(): ExpressionNode {
const token = this.lexer.peek()
if (token === undefined) {
throw new SyntaxError(
`Unexpected termination of expression at beginning of template (at ${this.lexer.index})`
)
}
if (token.type === TokenType.NoSubstitutionTemplate) {
this.lexer.next()
// The caller doesn't need to distinguish between NoSubstitutionTemplate and String
// tokens, so collapse both token types into String.
return { Literal: { type: TokenType.String, value: token.value } }
}
if (token.type === TokenType.TemplateHead || token.type === TokenType.TemplateMiddle) {
this.lexer.next()
const parts = this.parseTemplateParts()
if (token.value) {
parts.unshift({ Literal: { type: TokenType.String, value: token.value } })
}
return {
Template: { parts },
}
}
throw new SyntaxError(
`Unexpected token at beginning of template: ${JSON.stringify(token.value)} (at ${this.lexer.index})`
)
}
// Primary ::= Identifier |
// String |
// Template |
// Number |
// FunctionCall
private parsePrimary(): ExpressionNode {
const token = this.lexer.peek()
if (token === undefined) {
throw new SyntaxError(`Unexpected termination of expression (at ${this.lexer.index})`)
}
if (token.type === TokenType.Identifier) {
this.lexer.next()
if (matchOp(this.lexer.peek(), '(')) {
return this.parseFunctionCall(token.value)
}
return {
Identifier: token.value,
}
}
if (token.type === TokenType.String || token.type === TokenType.Number) {
this.lexer.next()
return { Literal: { type: token.type, value: token.value } }
}
if (
token.type === TokenType.NoSubstitutionTemplate ||
token.type === TokenType.TemplateHead ||
token.type === TokenType.TemplateMiddle
) {
return this.parseTemplate()
}
if (matchOp(token, '(')) {
this.lexer.next()
const expr = this.parseAdditive()
const token = this.lexer.next()
if (!matchOp(token, ')')) {
throw new SyntaxError(`Expected ")" (at ${this.lexer.index})`)
}
return expr
}
throw new SyntaxError(`Parse error on token: ${JSON.stringify(token.value)} (at ${this.lexer.index})`)
}
// Unary ::= Primary |
// UnaryOp Unary
private parseUnary(): ExpressionNode {
const token = this.lexer.peek()
if (token !== undefined && (matchOp(token, '-') || matchOp(token, '+') || matchOp(token, '!'))) {
this.lexer.next()
const expr = this.parseUnary()
return {
Unary: {
operator: token.value,
expression: expr,
},
}
}
return this.parsePrimary()
}
// Multiplicative ::= Unary |
// Multiplicative BinaryOp Unary
private parseMultiplicative(): ExpressionNode {
let expr = this.parseUnary()
let token = this.lexer.peek()
while (token !== undefined && (matchOp(token, '*') || matchOp(token, '/') || matchOp(token, '%'))) {
this.lexer.next()
expr = {
Binary: {
operator: token.value,
left: expr,
right: this.parseUnary(),
},
}
token = this.lexer.peek()
}
return expr
}
// Additive ::= Multiplicative |
// Additive BinaryOp Multiplicative
private parseAdditive(): ExpressionNode {
let expr = this.parseMultiplicative()
let token = this.lexer.peek()
while (
token !== undefined &&
(matchOp(token, '+') ||
matchOp(token, '-') ||
matchOp(token, '==') ||
matchOp(token, '!=') ||
matchOp(token, '===') ||
matchOp(token, '!==') ||
matchOp(token, '<') ||
matchOp(token, '>') ||
matchOp(token, '<=') ||
matchOp(token, '>=') ||
matchOp(token, '&&') ||
matchOp(token, '||'))
) {
this.lexer.next()
expr = {
Binary: {
operator: token.value,
left: expr,
right: this.parseMultiplicative(),
},
}
token = this.lexer.peek()
}
return expr
}
// Expression ::= Additive
private parseExpression(): ExpressionNode {
return this.parseAdditive()
}
}
/** Parses a template. */
export class TemplateParser extends Parser {
public parse(templateStr: string): ExpressionNode {
if (!this.lexer) {
this.lexer = new TemplateLexer()
}
this.lexer.reset(templateStr)
const expr = this.parseTemplate()
const token = this.lexer.next()
if (token !== undefined) {
throw new SyntaxError(
`Unexpected token at end of template input: ${JSON.stringify(token.value)} (at ${this.lexer.index})`
)
}
return expr
}
}
function matchOp(token: Pick<Token, 'type' | 'value'> | undefined, op: Operator): boolean {
return token !== undefined && token.type === TokenType.Operator && token.value === op
}