Skip to content

Commit b4db59b

Browse files
CST: Improve parse API (#295)
- Update some of the table size reservations to correctly reflect the actual number of properties inserted into the table - Rename some `expr` -> `expression` to align with other references - Append 'Keyword' to property names that match keywords
1 parent 0a94a1e commit b4db59b

File tree

3 files changed

+130
-147
lines changed

3 files changed

+130
-147
lines changed

batteries/syntax/visitor.luau

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,15 @@ end
507507

508508
local function visitIndexName(node: luau.AstExprIndexName, visitor: Visitor)
509509
if visitor.visitIndexName(node) then
510-
visitExpression(node.expr, visitor)
510+
visitExpression(node.expression, visitor)
511511
visitToken(node.accessor, visitor)
512512
visitToken(node.index, visitor)
513513
end
514514
end
515515

516516
local function visitIndexExpr(node: luau.AstExprIndexExpr, visitor: Visitor)
517517
if visitor.visitIndexExpr(node) then
518-
visitExpression(node.expr, visitor)
518+
visitExpression(node.expression, visitor)
519519
visitToken(node.openBrackets, visitor)
520520
visitExpression(node.index, visitor)
521521
visitToken(node.closeBrackets, visitor)
@@ -611,7 +611,7 @@ local function visitTypeTypeof(node: luau.AstTypeTypeof, visitor: Visitor)
611611
if visitor.visitTypeTypeof(node) then
612612
visitToken(node.typeof, visitor)
613613
visitToken(node.openParens, visitor)
614-
visitExpression(node.expr, visitor)
614+
visitExpression(node.expression, visitor)
615615
visitToken(node.closeParens, visitor)
616616
end
617617
end

definitions/luau.luau

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ export type MultiLineComment = {
3232
export type Trivia = Whitespace | SingleLineComment | MultiLineComment
3333

3434
export type Token<Kind = string> = {
35-
read leadingTrivia: { Trivia },
36-
read position: Position,
37-
read text: Kind,
38-
read trailingTrivia: { Trivia },
35+
leadingTrivia: { Trivia },
36+
position: Position,
37+
text: Kind,
38+
trailingTrivia: { Trivia },
3939
}
4040

41-
export type Eof = Token<nil> & { tag: "eof" }
41+
export type Eof = Token<""> & { tag: "eof" }
4242

4343
export type Pair<T, Separator> = { node: T, separator: Token<Separator>? }
4444
export type Punctuated<T, Separator = ","> = { Pair<T, Separator> }
@@ -47,6 +47,7 @@ export type AstLocal = {
4747
name: Token<string>,
4848
colon: Token<":">?,
4949
annotation: AstType?,
50+
shadows: AstLocal?,
5051
}
5152

5253
export type AstExprGroup = {
@@ -84,24 +85,25 @@ export type AstExprVarargs = Token<"..."> & { tag: "vararg" }
8485

8586
export type AstExprCall = {
8687
tag: "call",
87-
func: AstExpr, -- TODO: stricter?
88+
func: AstExpr,
8889
openParens: Token<"(">?,
8990
arguments: Punctuated<AstExpr>,
9091
closeParens: Token<")">?,
9192
self: boolean,
93+
argLocation: Location,
9294
}
9395

9496
export type AstExprIndexName = {
9597
tag: "indexname",
96-
expr: AstExpr,
98+
expression: AstExpr,
9799
accessor: Token<"." | ":">,
98100
index: Token<string>,
99101
indexLocation: Location,
100102
}
101103

102104
export type AstExprIndexExpr = {
103105
tag: "index",
104-
expr: AstExpr,
106+
expression: AstExpr,
105107
openBrackets: Token<"[">,
106108
index: AstExpr,
107109
closeBrackets: Token<"]">,
@@ -112,6 +114,7 @@ export type AstFunctionBody = {
112114
generics: Punctuated<AstGenericType>?,
113115
genericPacks: Punctuated<AstGenericTypePack>?,
114116
closeGenerics: Token<">">?,
117+
self: AstLocal?,
115118
openParens: Token<"(">,
116119
parameters: Punctuated<AstLocal>,
117120
vararg: Token<"...">?,
@@ -121,13 +124,13 @@ export type AstFunctionBody = {
121124
returnSpecifier: Token<":">?,
122125
returnAnnotation: AstTypePack?,
123126
body: AstStatBlock,
124-
["end"]: Token<"end">,
127+
endKeyword: Token<"end">,
125128
}
126129

127130
export type AstExprAnonymousFunction = {
128131
tag: "function",
129132
attributes: { AstAttribute },
130-
["function"]: Token<"function">,
133+
functionKeyword: Token<"function">,
131134
body: AstFunctionBody,
132135
}
133136

@@ -181,20 +184,20 @@ export type AstExprTypeAssertion = {
181184
}
182185

183186
export type AstExprIfElseIfs = {
184-
["elseif"]: Token<"elseif">,
187+
elseifKeyword: Token<"elseif">,
185188
condition: AstExpr,
186-
["then"]: Token<"then">,
189+
thenKeyword: Token<"then">,
187190
consequent: AstExpr,
188191
}
189192

190193
export type AstExprIfElse = {
191194
tag: "conditional",
192-
["if"]: Token<"if">,
195+
ifKeyword: Token<"if">,
193196
condition: AstExpr,
194-
["then"]: Token<"then">,
197+
thenKeyword: Token<"then">,
195198
consequent: AstExpr,
196199
elseifs: { AstExprIfElseIfs },
197-
["else"]: Token<"else">,
200+
elseKeyword: Token<"else">,
198201
antecedent: AstExpr,
199202
}
200203

@@ -224,38 +227,38 @@ export type AstStatBlock = {
224227
}
225228

226229
export type AstStatElseIf = {
227-
["elseif"]: Token<"elseif">,
230+
elseifKeyword: Token<"elseif">,
228231
condition: AstExpr,
229-
["then"]: Token<"then">,
232+
thenKeyword: Token<"then">,
230233
consequent: AstStatBlock,
231234
}
232235

233236
export type AstStatIf = {
234237
tag: "conditional",
235-
["if"]: Token<"if">,
238+
ifKeyword: Token<"if">,
236239
condition: AstExpr,
237-
["then"]: Token<"then">,
240+
thenKeyword: Token<"then">,
238241
consequent: AstStatBlock,
239242
elseifs: { AstStatElseIf },
240-
["else"]: Token<"else">, -- TODO: this could be elseif!
241-
antecedent: AstStatBlock,
242-
["end"]: Token<"end">,
243+
elseKeyword: Token<"else">?, -- TODO: this could be elseif!
244+
antecedent: AstStatBlock?,
245+
endKeyword: Token<"end">,
243246
}
244247

245248
export type AstStatWhile = {
246249
tag: "while",
247-
["while"]: Token<"while">,
250+
whileKeyword: Token<"while">,
248251
condition: AstExpr,
249-
["do"]: Token<"do">,
252+
doKeyword: Token<"do">,
250253
body: AstStatBlock,
251-
["end"]: Token<"end">,
254+
endKeyword: Token<"end">,
252255
}
253256

254257
export type AstStatRepeat = {
255258
tag: "repeat",
256-
["repeat"]: Token<"repeat">,
259+
repeatKeyword: Token<"repeat">,
257260
body: AstStatBlock,
258-
["until"]: Token<"until">,
261+
untilKeyword: Token<"until">,
259262
condition: AstExpr,
260263
}
261264

@@ -265,7 +268,7 @@ export type AstStatContinue = Token<"continue"> & { tag: "continue" }
265268

266269
export type AstStatReturn = {
267270
tag: "return",
268-
["return"]: Token<"return">,
271+
returnKeyword: Token<"return">,
269272
expressions: Punctuated<AstExpr>,
270273
}
271274

@@ -276,36 +279,36 @@ export type AstStatExpr = {
276279

277280
export type AstStatLocal = {
278281
tag: "local",
279-
["local"]: Token<"local">,
282+
localKeyword: Token<"local">,
280283
variables: Punctuated<AstLocal>,
281284
equals: Token<"=">?,
282285
values: Punctuated<AstExpr>,
283286
}
284287

285288
export type AstStatFor = {
286289
tag: "for",
287-
["for"]: Token<"for">,
290+
forKeyword: Token<"for">,
288291
variable: AstLocal,
289292
equals: Token<"=">,
290293
from: AstExpr,
291294
toComma: Token<",">,
292295
to: AstExpr,
293296
stepComma: Token<",">?,
294297
step: AstExpr?,
295-
["do"]: Token<"do">,
298+
doKeyword: Token<"do">,
296299
body: AstStatBlock,
297-
["end"]: Token<"end">,
300+
endKeyword: Token<"end">,
298301
}
299302

300303
export type AstStatForIn = {
301304
tag: "forin",
302-
["for"]: Token<"for">,
305+
forKeyword: Token<"for">,
303306
variables: Punctuated<Token<string>>,
304-
["in"]: Token<"in">,
307+
inKeyword: Token<"in">,
305308
values: Punctuated<AstExpr>,
306-
["do"]: Token<"do">,
309+
doKeyword: Token<"do">,
307310
body: AstStatBlock,
308-
["end"]: Token<"end">,
311+
endKeyword: Token<"end">,
309312
}
310313

311314
export type AstStatAssign = {
@@ -327,23 +330,23 @@ export type AstAttribute = Token<"@checked" | "@native" | "@deprecated"> & { tag
327330
export type AstStatFunction = {
328331
tag: "function",
329332
attributes: { AstAttribute },
330-
["function"]: Token<"function">,
333+
functionKeyword: Token<"function">,
331334
name: AstExpr,
332335
body: AstFunctionBody,
333336
}
334337

335338
export type AstStatLocalFunction = {
336339
tag: "localfunction",
337340
attributes: { AstAttribute },
338-
["local"]: Token<"local">,
339-
["function"]: Token<"function">,
341+
localKeyword: Token<"local">,
342+
functionKeyword: Token<"function">,
340343
name: AstLocal,
341344
body: AstFunctionBody,
342345
}
343346

344347
export type AstStatTypeAlias = {
345348
tag: "typealias",
346-
["export"]: Token<"export">?,
349+
export: Token<"export">?,
347350
typeToken: Token<"type">,
348351
name: Token,
349352
openGenerics: Token<"<">?,
@@ -356,9 +359,9 @@ export type AstStatTypeAlias = {
356359

357360
export type AstStatTypeFunction = {
358361
tag: "typefunction",
359-
["export"]: Token<"export">?,
362+
export: Token<"export">?,
360363
type: Token<"type">,
361-
["function"]: Token<"function">,
364+
functionKeyword: Token<"function">,
362365
name: Token,
363366
body: AstFunctionBody,
364367
}
@@ -421,7 +424,7 @@ export type AstTypeTypeof = {
421424
tag: "typeof",
422425
typeof: Token<"typeof">,
423426
openParens: Token<"(">,
424-
expr: AstExpr,
427+
expression: AstExpr,
425428
closeParens: Token<")">,
426429
}
427430

0 commit comments

Comments
 (0)