-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparser.js
347 lines (333 loc) · 9.44 KB
/
parser.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
338
339
340
341
342
343
344
345
346
347
// リテラルの構文解析
function parseLiteral(tokens) {
const head = tokens[0]
switch (head?.type) {
case 'Int':
return {
expression: {
type: 'IntLiteral',
value: head.value,
},
parsedTokensCount: 1,
}
case 'Bool':
return {
expression: {
type: 'BoolLiteral',
value: head.value,
},
parsedTokensCount: 1,
}
case 'Null':
return {
expression: {
type: 'NullLiteral',
},
parsedTokensCount: 1,
}
default:
return {
expression: null,
}
}
}
// 変数参照の構文解析
function parseValue(tokens) {
const head = tokens[0]
if (head?.type === 'Ident') {
return {
expression: {
type: 'Variable',
name: head.name,
},
parsedTokensCount: 1,
}
}
return parseLiteral(tokens)
}
// 優先順位を変える丸括弧の構文解析
function parseParenthesesExpression(tokens) {
if (tokens[0]?.type === 'LParen') {
// eslint-disable-next-line no-use-before-define
const { expression, parsedTokensCount } = parseExpression(tokens.slice(1))
if (tokens[parsedTokensCount + 1]?.type === 'RParen') {
return { expression, parsedTokensCount: parsedTokensCount + 2 }
}
}
return parseValue(tokens)
}
// コンマで区切られた式(例えば引数リスト)の構文解析
// `aaa, b+c` のようなものを解析する
function parseCommaSeparatedExpressions(tokens) {
const {
expression: firstExpression,
parsedTokensCount: firstParsedTokensCount,
// eslint-disable-next-line no-use-before-define
} = parseExpression(tokens)
if (firstExpression === null) {
return {
args: [],
parsedTokensCount: 0,
}
}
const args = [firstExpression]
let readPosition = firstParsedTokensCount
while (tokens[readPosition]?.type === 'Comma') {
readPosition += 1
// eslint-disable-next-line no-use-before-define
const { expression, parsedTokensCount } = parseExpression(tokens.slice(readPosition))
if (expression === null) {
return null
}
args.push(expression)
readPosition += parsedTokensCount
}
return {
args,
parsedTokensCount: readPosition,
}
}
// 関数呼び出しの構文解析
// `func(arg1, arg2)` のようなものを解析する
function parseFunctionCallingExpression(tokens) {
const name = tokens[0]
if (name?.type !== 'Ident' || tokens[1]?.type !== 'LParen') {
return parseParenthesesExpression(tokens)
}
const argsAndParsedTokensCount = parseCommaSeparatedExpressions(tokens.slice(2))
if (argsAndParsedTokensCount === null) {
return parseParenthesesExpression(tokens)
}
const { args, parsedTokensCount } = argsAndParsedTokensCount
if (tokens[parsedTokensCount + 2]?.type !== 'RParen') {
return parseParenthesesExpression(tokens)
}
return {
expression: {
type: 'FuncCall',
name: name.name,
arguments: args,
},
parsedTokensCount: parsedTokensCount + 3,
}
}
// 足し算と引き算の構文解析
// 引き算は勉強会中で機能追加をする
function parseAddSubExpression(tokens) {
let { expression: left, parsedTokensCount: readPosition } = parseFunctionCallingExpression(tokens)
while (tokens[readPosition]?.type === 'Plus') {
const {
expression: right,
parsedTokensCount: rightTokensCount,
} = parseFunctionCallingExpression(tokens.slice(readPosition + 1))
if (right === null) {
return { expression: null }
}
left = { type: 'Add', left, right }
readPosition += rightTokensCount + 1
}
return { expression: left, parsedTokensCount: readPosition }
}
// 式の構文解析であることをわかりやすくするための関数
// 足し算引き算よりも優先順位の低い式の構文を作ったときに書き換える
function parseExpression(tokens) {
return parseAddSubExpression(tokens)
}
// 波括弧で囲まれたブロックの構文解析
// `{ stmt1; stmt2; }` のようなものを解析する
function parseBlock(tokens) {
if (tokens[0]?.type !== 'LBrace') {
return { statements: null }
}
const statements = []
let readPosition = 1
while (tokens[readPosition]?.type !== 'RBrace') {
if (tokens[readPosition] === undefined) {
return { statements: null }
}
const {
statement: stmt,
parsedTokensCount,
// eslint-disable-next-line no-use-before-define
} = parseStatement(tokens.slice(readPosition))
if (stmt === null) {
return { statements: null }
}
statements.push(stmt)
readPosition += parsedTokensCount
}
return {
statements,
parsedTokensCount: readPosition + 1,
}
}
// if文の構文解析
// `if(cond) { stmt1; stmt2; }` のようなものを解析する
function parseIfStatement(tokens) {
if (tokens[0]?.type !== 'If' || tokens[1]?.type !== 'LParen') {
return { ifStatement: null }
}
const {
expression: condition,
parsedTokensCount: parsedConditionTokensCount,
} = parseExpression(tokens.slice(2))
if (
!condition
|| tokens[parsedConditionTokensCount + 2]?.type !== 'RParen') {
return { ifStatement: null }
}
const {
statements,
parsedTokensCount: parsedBlockTokensCount,
} = parseBlock(tokens.slice(parsedConditionTokensCount + 3))
if (!statements) {
return { ifStatement: null }
}
return {
ifStatement: {
type: 'If',
condition,
statements,
},
parsedTokensCount: parsedConditionTokensCount + parsedBlockTokensCount + 3,
}
}
// 代入文の構文解析
// `ident = 12+34;` のようなものを解析する
// この段階ではセミコロンは含まない
function parseAssignment(tokens) {
if (tokens[0]?.type !== 'Ident' || tokens[1]?.type !== 'Equal') {
return { assignment: null }
}
const { expression, parsedTokensCount } = parseExpression(tokens.slice(2))
if (!expression) {
return { assignment: null }
}
return {
assignment: {
type: 'Assignment',
name: tokens[0].name,
expression,
},
parsedTokensCount: parsedTokensCount + 2,
}
}
// 文の構文解析
// 式(式文), 代入文, if文が文
function parseStatement(tokens) {
const { expression, parsedTokensCount: parsedExpressionTokensCount } = parseExpression(tokens)
if (expression && tokens[parsedExpressionTokensCount]?.type === 'Semicolon') {
return {
statement: expression,
parsedTokensCount: parsedExpressionTokensCount + 1,
}
}
const { assignment, parsedTokensCount: parsedAssignmentTokensCount } = parseAssignment(tokens)
if (assignment && tokens[parsedAssignmentTokensCount]?.type === 'Semicolon') {
return {
statement: assignment,
parsedTokensCount: parsedAssignmentTokensCount + 1,
}
}
const { ifStatement, parsedTokensCount: parsedIfTokensCount } = parseIfStatement(tokens)
if (ifStatement) {
return {
statement: ifStatement,
parsedTokensCount: parsedIfTokensCount,
}
}
return { statement: null }
}
// カンマで区切られた識別子の構文解析
// `x, y, z` のようなものを解析する
function parseCommaSeparatedIdentfiers(tokens) {
const head = tokens[0]
if (head?.type !== 'Ident') {
return {
names: [],
parsedTokensCount: 0,
}
}
const names = [head.name]
let readPosition = 1
while (tokens[readPosition]?.type === 'Comma') {
readPosition += 1
// eslint-disable-next-line no-use-before-define
const next = tokens[readPosition]
if (next.type !== 'Ident') {
break
}
names.push(next.name)
readPosition += 1
}
return {
names,
parsedTokensCount: readPosition,
}
}
// 関数定義の構文解析
// `def func(x, y) { stmt1; stmt2; }` のようなものを解析する
function parseFunctionDefinition(tokens) {
if (tokens[0]?.type !== 'Def' || tokens[1]?.type !== 'Ident' || tokens[2]?.type !== 'LParen') {
return { define: null }
}
const { name } = tokens[1]
const {
names: args,
parsedTokensCount: parsedArgumentTokensCount,
} = parseCommaSeparatedIdentfiers(tokens.slice(3))
if (tokens[parsedArgumentTokensCount + 3]?.type !== 'RParen') {
return { define: null }
}
const {
statements,
parsedTokensCount: parsedBlockTokensCount,
} = parseBlock(tokens.slice(parsedArgumentTokensCount + 4))
if (!statements) {
return { define: null }
}
return {
defineFunction: {
type: 'FuncDef',
name,
arguments: args,
statements,
},
parsedTokensCount: parsedArgumentTokensCount + parsedBlockTokensCount + 4,
}
}
// ソースコード全体の構文解析
function parseSource(tokens) {
const partsOfSource = []
let readPosition = 0
while (readPosition < tokens.length) {
const {
statement: stmt,
parsedTokensCount: parsedExpressionTokensCount,
} = parseStatement(tokens.slice(readPosition))
if (stmt) {
partsOfSource.push(stmt)
readPosition += parsedExpressionTokensCount
continue
}
const {
defineFunction,
parsedTokensCount: parsedDefineFunctionTokensCount,
} = parseFunctionDefinition(tokens.slice(readPosition))
if (defineFunction) {
partsOfSource.push(defineFunction)
readPosition += parsedDefineFunctionTokensCount
continue
}
return {
type: 'SyntaxError',
message: `予期しないトークン'${tokens[readPosition]?.type}'が渡されました`,
headToken: tokens[readPosition],
}
}
return {
type: 'Source',
partsOfSource,
}
}
module.exports.parse = parseSource