Skip to content

Commit 26b8e25

Browse files
authored
feature: add location field to Luau AST types (#608)
Requiring `location` field in AST types per @skanosue 's request; fixes some type errors too!
1 parent 668b6c1 commit 26b8e25

File tree

1 file changed

+75
-17
lines changed

1 file changed

+75
-17
lines changed

definitions/luau.luau

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type Pair<T, Separator> = { node: T, separator: Token<Separator>? }
4949
export type Punctuated<T, Separator = ","> = { Pair<T, Separator> }
5050

5151
export type AstLocal = {
52+
location: span,
5253
kind: "local",
5354
name: Token<string>,
5455
colon: Token<":">?,
@@ -57,32 +58,43 @@ export type AstLocal = {
5758
}
5859

5960
export type AstExprGroup = {
61+
location: span,
6062
tag: "group",
6163
openparens: Token<"(">,
6264
expression: AstExpr,
6365
closeparens: Token<")">,
6466
}
6567

66-
export type AstExprConstantNil = Token<"nil"> & { kind: "expr", tag: "nil" }
68+
export type AstExprConstantNil = Token<"nil"> & { location: span, kind: "expr", tag: "nil" }
6769

68-
export type AstExprConstantBool = Token<"true" | "false"> & { kind: "expr", tag: "boolean", value: boolean }
69-
70-
export type AstExprConstantNumber = Token<string> & { kind: "expr", tag: "number", value: number }
70+
export type AstExprConstantBool =
71+
Token<"true" | "false">
72+
& { location: span, kind: "expr", tag: "boolean", value: boolean }
7173

74+
export type AstExprConstantNumber = Token<string> & { location: span, kind: "expr", tag: "number", value: number }
7275
export type AstExprConstantString = Token<string> & {
76+
location: span,
7377
kind: "expr",
7478
tag: "string",
7579
quotestyle: "single" | "double" | "block" | "interp",
7680
blockdepth: number,
7781
}
7882

79-
export type AstExprLocal = { kind: "expr", tag: "local", token: Token<string>, ["local"]: AstLocal, upvalue: boolean }
83+
export type AstExprLocal = {
84+
location: span,
85+
kind: "expr",
86+
tag: "local",
87+
token: Token<string>,
88+
["local"]: AstLocal,
89+
upvalue: boolean,
90+
}
8091

81-
export type AstExprGlobal = { kind: "expr", tag: "global", name: Token }
92+
export type AstExprGlobal = { location: span, kind: "expr", tag: "global", name: Token }
8293

83-
export type AstExprVarargs = Token<"..."> & { kind: "expr", tag: "vararg" }
94+
export type AstExprVarargs = Token<"..."> & { location: span, kind: "expr", tag: "vararg" }
8495

8596
export type AstExprCall = {
97+
location: span,
8698
kind: "expr",
8799
tag: "call",
88100
func: AstExpr,
@@ -94,6 +106,7 @@ export type AstExprCall = {
94106
}
95107

96108
export type AstExprIndexName = {
109+
location: span,
97110
kind: "expr",
98111
tag: "indexname",
99112
expression: AstExpr,
@@ -103,6 +116,7 @@ export type AstExprIndexName = {
103116
}
104117

105118
export type AstExprIndexExpr = {
119+
location: span,
106120
kind: "expr",
107121
tag: "index",
108122
expression: AstExpr,
@@ -131,6 +145,7 @@ export type AstFunctionBody = {
131145
}
132146

133147
export type AstExprAnonymousFunction = {
148+
location: span,
134149
kind: "expr",
135150
tag: "function",
136151
attributes: { AstAttribute },
@@ -164,16 +179,24 @@ export type AstExprTableItemGeneral = {
164179
export type AstExprTableItem = AstExprTableItemList | AstExprTableItemRecord | AstExprTableItemGeneral
165180

166181
export type AstExprTable = {
182+
location: span,
167183
kind: "expr",
168184
tag: "table",
169185
openbrace: Token<"{">,
170186
entries: { AstExprTableItem },
171187
closebrace: Token<"}">,
172188
}
173189

174-
export type AstExprUnary = { kind: "expr", tag: "unary", operator: Token<"not" | "-" | "#">, operand: AstExpr }
190+
export type AstExprUnary = {
191+
location: span,
192+
kind: "expr",
193+
tag: "unary",
194+
operator: Token<"not" | "-" | "#">,
195+
operand: AstExpr,
196+
}
175197

176198
export type AstExprBinary = {
199+
location: span,
177200
kind: "expr",
178201
tag: "binary",
179202
lhsoperand: AstExpr,
@@ -182,13 +205,15 @@ export type AstExprBinary = {
182205
}
183206

184207
export type AstExprInterpString = {
208+
location: span,
185209
kind: "expr",
186210
tag: "interpolatedstring",
187211
strings: { Token<string> },
188212
expressions: { AstExpr },
189213
}
190214

191215
export type AstExprTypeAssertion = {
216+
location: span,
192217
kind: "expr",
193218
tag: "cast",
194219
operand: AstExpr,
@@ -205,6 +230,7 @@ export type AstElseIfExpr = {
205230
}
206231

207232
export type AstExprIfElse = {
233+
location: span,
208234
kind: "expr",
209235
tag: "conditional",
210236
ifkeyword: Token<"if">,
@@ -238,6 +264,7 @@ export type AstExpr = { kind: "expr" } & (
238264
)
239265

240266
export type AstStatBlock = {
267+
location: span,
241268
kind: "stat",
242269
tag: "block",
243270
statements: { AstStat },
@@ -251,6 +278,7 @@ export type AstElseIfStat = {
251278
}
252279

253280
export type AstStatIf = {
281+
location: span,
254282
kind: "stat",
255283
tag: "conditional",
256284
ifkeyword: Token<"if">,
@@ -264,6 +292,7 @@ export type AstStatIf = {
264292
}
265293

266294
export type AstStatWhile = {
295+
location: span,
267296
kind: "stat",
268297
tag: "while",
269298
whilekeyword: Token<"while">,
@@ -274,6 +303,7 @@ export type AstStatWhile = {
274303
}
275304

276305
export type AstStatRepeat = {
306+
location: span,
277307
kind: "stat",
278308
tag: "repeat",
279309
repeatkeyword: Token<"repeat">,
@@ -282,20 +312,22 @@ export type AstStatRepeat = {
282312
condition: AstExpr,
283313
}
284314

285-
export type AstStatBreak = Token<"break"> & { kind: "stat", tag: "break" }
315+
export type AstStatBreak = Token<"break"> & { location: span, kind: "stat", tag: "break" }
286316

287-
export type AstStatContinue = Token<"continue"> & { kind: "stat", tag: "continue" }
317+
export type AstStatContinue = Token<"continue"> & { location: span, kind: "stat", tag: "continue" }
288318

289319
export type AstStatReturn = {
320+
location: span,
290321
kind: "stat",
291322
tag: "return",
292323
returnkeyword: Token<"return">,
293324
expressions: Punctuated<AstExpr>,
294325
}
295326

296-
export type AstStatExpr = { kind: "stat", tag: "expression", expression: AstExpr }
327+
export type AstStatExpr = { location: span, kind: "stat", tag: "expression", expression: AstExpr }
297328

298329
export type AstStatLocal = {
330+
location: span,
299331
kind: "stat",
300332
tag: "local",
301333
localkeyword: Token<"local">,
@@ -305,6 +337,7 @@ export type AstStatLocal = {
305337
}
306338

307339
export type AstStatFor = {
340+
location: span,
308341
kind: "stat",
309342
tag: "for",
310343
forkeyword: Token<"for">,
@@ -321,6 +354,7 @@ export type AstStatFor = {
321354
}
322355

323356
export type AstStatForIn = {
357+
location: span,
324358
kind: "stat",
325359
tag: "forin",
326360
forkeyword: Token<"for">,
@@ -333,6 +367,7 @@ export type AstStatForIn = {
333367
}
334368

335369
export type AstStatAssign = {
370+
location: span,
336371
kind: "stat",
337372
tag: "assign",
338373
variables: Punctuated<AstExpr>,
@@ -341,16 +376,18 @@ export type AstStatAssign = {
341376
}
342377

343378
export type AstStatCompoundAssign = {
379+
location: span,
344380
kind: "stat",
345381
tag: "compoundassign",
346382
variable: AstExpr,
347383
operand: Token, -- TODO: enforce token type,
348384
value: AstExpr,
349385
}
350386

351-
export type AstAttribute = Token<"@checked" | "@native" | "@deprecated"> & { kind: "attribute" }
387+
export type AstAttribute = Token<"@checked" | "@native" | "@deprecated"> & { location: span, kind: "attribute" }
352388

353389
export type AstStatFunction = {
390+
location: span,
354391
kind: "stat",
355392
tag: "function",
356393
attributes: { AstAttribute },
@@ -360,6 +397,7 @@ export type AstStatFunction = {
360397
}
361398

362399
export type AstStatLocalFunction = {
400+
location: span,
363401
kind: "stat",
364402
tag: "localfunction",
365403
attributes: { AstAttribute },
@@ -370,6 +408,7 @@ export type AstStatLocalFunction = {
370408
}
371409

372410
export type AstStatTypeAlias = {
411+
location: span,
373412
kind: "stat",
374413
tag: "typealias",
375414
export: Token<"export">?,
@@ -384,6 +423,7 @@ export type AstStatTypeAlias = {
384423
}
385424

386425
export type AstStatTypeFunction = {
426+
location: span,
387427
kind: "stat",
388428
tag: "typefunction",
389429
export: Token<"export">?,
@@ -429,6 +469,7 @@ export type AstGenericTypePack = {
429469
}
430470

431471
export type AstTypeReference = {
472+
location: span,
432473
kind: "type",
433474
tag: "reference",
434475
prefix: Token<string>?,
@@ -439,11 +480,19 @@ export type AstTypeReference = {
439480
closeparameters: Token<">">?,
440481
}
441482

442-
export type AstTypeSingletonBool = Token<"true" | "false"> & { kind: "type", tag: "boolean", value: boolean }
483+
export type AstTypeSingletonBool =
484+
Token<"true" | "false">
485+
& { location: span, kind: "type", tag: "boolean", value: boolean }
443486

444-
export type AstTypeSingletonString = Token<string> & { kind: "type", tag: "string", quotestyle: "single" | "double" }
487+
export type AstTypeSingletonString = Token<string> & {
488+
location: span,
489+
kind: "type",
490+
tag: "string",
491+
quotestyle: "single" | "double",
492+
}
445493

446494
export type AstTypeTypeof = {
495+
location: span,
447496
kind: "type",
448497
tag: "typeof",
449498
typeof: Token<"typeof">,
@@ -453,16 +502,18 @@ export type AstTypeTypeof = {
453502
}
454503

455504
export type AstTypeGroup = {
505+
location: span,
456506
kind: "type",
457507
tag: "group",
458508
openparens: Token<"(">,
459509
type: AstType,
460510
closeparens: Token<")">,
461511
}
462512

463-
export type AstTypeOptional = Token<"?"> & { kind: "type", tag: "optional" }
513+
export type AstTypeOptional = Token<"?"> & { location: span, kind: "type", tag: "optional" }
464514

465515
export type AstTypeUnion = {
516+
location: span,
466517
kind: "type",
467518
tag: "union",
468519
leading: Token<"|">?,
@@ -471,13 +522,15 @@ export type AstTypeUnion = {
471522
}
472523

473524
export type AstTypeIntersection = {
525+
location: span,
474526
kind: "type",
475527
tag: "intersection",
476528
leading: Token<"&">?,
477529
types: Punctuated<AstType, "&">,
478530
}
479531

480532
export type AstTypeArray = {
533+
location: span,
481534
kind: "type",
482535
tag: "array",
483536
openbrace: Token<"{">,
@@ -520,6 +573,7 @@ export type AstTypeTableItemProperty = {
520573
export type AstTypeTableItem = AstTypeTableItemIndexer | AstTypeTableItemStringProperty | AstTypeTableItemProperty
521574

522575
export type AstTypeTable = {
576+
location: span,
523577
kind: "type",
524578
tag: "table",
525579
openbrace: Token<"{">,
@@ -528,12 +582,14 @@ export type AstTypeTable = {
528582
}
529583

530584
export type AstTypeFunctionParameter = {
585+
location: span,
531586
name: Token?,
532587
colon: Token<":">?,
533588
type: AstType,
534589
}
535590

536591
export type AstTypeFunction = {
592+
location: span,
537593
kind: "type",
538594
tag: "function",
539595
opengenerics: Token<"<">?,
@@ -563,6 +619,7 @@ export type AstType = { kind: "type" } & (
563619
)
564620

565621
export type AstTypePackExplicit = {
622+
location: span,
566623
kind: "typepack",
567624
tag: "explicit",
568625
openparens: Token<"(">?,
@@ -571,9 +628,10 @@ export type AstTypePackExplicit = {
571628
closeparens: Token<")">?,
572629
}
573630

574-
export type AstTypePackGeneric = { kind: "typepack", tag: "generic", name: Token, ellipsis: Token<"..."> }
631+
export type AstTypePackGeneric = { location: span, kind: "typepack", tag: "generic", name: Token, ellipsis: Token<"..."> }
575632

576633
export type AstTypePackVariadic = {
634+
location: span,
577635
kind: "typepack",
578636
tag: "variadic",
579637
--- May be nil when present as the vararg annotation in a function body
@@ -583,7 +641,7 @@ export type AstTypePackVariadic = {
583641

584642
export type AstTypePack = { kind: "typepack" } & (AstTypePackExplicit | AstTypePackGeneric | AstTypePackVariadic)
585643

586-
export type AstNode = { location: span? } & (AstExpr | AstStat | AstType | AstTypePack | AstLocal | AstAttribute)
644+
export type AstNode = { location: span } & (AstExpr | AstStat | AstType | AstTypePack | AstLocal | AstAttribute)
587645

588646
export type ParseResult = {
589647
root: AstStatBlock,

0 commit comments

Comments
 (0)