Skip to content

CST: Improve parse API #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions batteries/syntax/visitor.luau
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,15 @@ end

local function visitIndexName(node: luau.AstExprIndexName, visitor: Visitor)
if visitor.visitIndexName(node) then
visitExpression(node.expr, visitor)
visitExpression(node.expression, visitor)
visitToken(node.accessor, visitor)
visitToken(node.index, visitor)
end
end

local function visitIndexExpr(node: luau.AstExprIndexExpr, visitor: Visitor)
if visitor.visitIndexExpr(node) then
visitExpression(node.expr, visitor)
visitExpression(node.expression, visitor)
visitToken(node.openBrackets, visitor)
visitExpression(node.index, visitor)
visitToken(node.closeBrackets, visitor)
Expand Down Expand Up @@ -611,7 +611,7 @@ local function visitTypeTypeof(node: luau.AstTypeTypeof, visitor: Visitor)
if visitor.visitTypeTypeof(node) then
visitToken(node.typeof, visitor)
visitToken(node.openParens, visitor)
visitExpression(node.expr, visitor)
visitExpression(node.expression, visitor)
visitToken(node.closeParens, visitor)
end
end
Expand Down
89 changes: 46 additions & 43 deletions definitions/luau.luau
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ export type MultiLineComment = {
export type Trivia = Whitespace | SingleLineComment | MultiLineComment

export type Token<Kind = string> = {
read leadingTrivia: { Trivia },
read position: Position,
read text: Kind,
read trailingTrivia: { Trivia },
leadingTrivia: { Trivia },
position: Position,
text: Kind,
trailingTrivia: { Trivia },
}

export type Eof = Token<nil> & { tag: "eof" }
export type Eof = Token<""> & { tag: "eof" }

export type Pair<T, Separator> = { node: T, separator: Token<Separator>? }
export type Punctuated<T, Separator = ","> = { Pair<T, Separator> }
Expand All @@ -47,6 +47,7 @@ export type AstLocal = {
name: Token<string>,
colon: Token<":">?,
annotation: AstType?,
shadows: AstLocal?,
}

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

export type AstExprCall = {
tag: "call",
func: AstExpr, -- TODO: stricter?
func: AstExpr,
openParens: Token<"(">?,
arguments: Punctuated<AstExpr>,
closeParens: Token<")">?,
self: boolean,
argLocation: Location,
}

export type AstExprIndexName = {
tag: "indexname",
expr: AstExpr,
expression: AstExpr,
accessor: Token<"." | ":">,
index: Token<string>,
indexLocation: Location,
}

export type AstExprIndexExpr = {
tag: "index",
expr: AstExpr,
expression: AstExpr,
openBrackets: Token<"[">,
index: AstExpr,
closeBrackets: Token<"]">,
Expand All @@ -112,6 +114,7 @@ export type AstFunctionBody = {
generics: Punctuated<AstGenericType>?,
genericPacks: Punctuated<AstGenericTypePack>?,
closeGenerics: Token<">">?,
self: AstLocal?,
openParens: Token<"(">,
parameters: Punctuated<AstLocal>,
vararg: Token<"...">?,
Expand All @@ -121,13 +124,13 @@ export type AstFunctionBody = {
returnSpecifier: Token<":">?,
returnAnnotation: AstTypePack?,
body: AstStatBlock,
["end"]: Token<"end">,
endKeyword: Token<"end">,
}

export type AstExprAnonymousFunction = {
tag: "function",
attributes: { AstAttribute },
["function"]: Token<"function">,
functionKeyword: Token<"function">,
body: AstFunctionBody,
}

Expand Down Expand Up @@ -181,20 +184,20 @@ export type AstExprTypeAssertion = {
}

export type AstExprIfElseIfs = {
["elseif"]: Token<"elseif">,
elseifKeyword: Token<"elseif">,
condition: AstExpr,
["then"]: Token<"then">,
thenKeyword: Token<"then">,
consequent: AstExpr,
}

export type AstExprIfElse = {
tag: "conditional",
["if"]: Token<"if">,
ifKeyword: Token<"if">,
condition: AstExpr,
["then"]: Token<"then">,
thenKeyword: Token<"then">,
consequent: AstExpr,
elseifs: { AstExprIfElseIfs },
["else"]: Token<"else">,
elseKeyword: Token<"else">,
antecedent: AstExpr,
}

Expand Down Expand Up @@ -224,38 +227,38 @@ export type AstStatBlock = {
}

export type AstStatElseIf = {
["elseif"]: Token<"elseif">,
elseifKeyword: Token<"elseif">,
condition: AstExpr,
["then"]: Token<"then">,
thenKeyword: Token<"then">,
consequent: AstStatBlock,
}

export type AstStatIf = {
tag: "conditional",
["if"]: Token<"if">,
ifKeyword: Token<"if">,
condition: AstExpr,
["then"]: Token<"then">,
thenKeyword: Token<"then">,
consequent: AstStatBlock,
elseifs: { AstStatElseIf },
["else"]: Token<"else">, -- TODO: this could be elseif!
antecedent: AstStatBlock,
["end"]: Token<"end">,
elseKeyword: Token<"else">?, -- TODO: this could be elseif!
antecedent: AstStatBlock?,
endKeyword: Token<"end">,
}

export type AstStatWhile = {
tag: "while",
["while"]: Token<"while">,
whileKeyword: Token<"while">,
condition: AstExpr,
["do"]: Token<"do">,
doKeyword: Token<"do">,
body: AstStatBlock,
["end"]: Token<"end">,
endKeyword: Token<"end">,
}

export type AstStatRepeat = {
tag: "repeat",
["repeat"]: Token<"repeat">,
repeatKeyword: Token<"repeat">,
body: AstStatBlock,
["until"]: Token<"until">,
untilKeyword: Token<"until">,
condition: AstExpr,
}

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

export type AstStatReturn = {
tag: "return",
["return"]: Token<"return">,
returnKeyword: Token<"return">,
expressions: Punctuated<AstExpr>,
}

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

export type AstStatLocal = {
tag: "local",
["local"]: Token<"local">,
localKeyword: Token<"local">,
variables: Punctuated<AstLocal>,
equals: Token<"=">?,
values: Punctuated<AstExpr>,
}

export type AstStatFor = {
tag: "for",
["for"]: Token<"for">,
forKeyword: Token<"for">,
variable: AstLocal,
equals: Token<"=">,
from: AstExpr,
toComma: Token<",">,
to: AstExpr,
stepComma: Token<",">?,
step: AstExpr?,
["do"]: Token<"do">,
doKeyword: Token<"do">,
body: AstStatBlock,
["end"]: Token<"end">,
endKeyword: Token<"end">,
}

export type AstStatForIn = {
tag: "forin",
["for"]: Token<"for">,
forKeyword: Token<"for">,
variables: Punctuated<Token<string>>,
["in"]: Token<"in">,
inKeyword: Token<"in">,
values: Punctuated<AstExpr>,
["do"]: Token<"do">,
doKeyword: Token<"do">,
body: AstStatBlock,
["end"]: Token<"end">,
endKeyword: Token<"end">,
}

export type AstStatAssign = {
Expand All @@ -327,23 +330,23 @@ export type AstAttribute = Token<"@checked" | "@native" | "@deprecated"> & { tag
export type AstStatFunction = {
tag: "function",
attributes: { AstAttribute },
["function"]: Token<"function">,
functionKeyword: Token<"function">,
name: AstExpr,
body: AstFunctionBody,
}

export type AstStatLocalFunction = {
tag: "localfunction",
attributes: { AstAttribute },
["local"]: Token<"local">,
["function"]: Token<"function">,
localKeyword: Token<"local">,
functionKeyword: Token<"function">,
name: AstLocal,
body: AstFunctionBody,
}

export type AstStatTypeAlias = {
tag: "typealias",
["export"]: Token<"export">?,
export: Token<"export">?,
typeToken: Token<"type">,
name: Token,
openGenerics: Token<"<">?,
Expand All @@ -356,9 +359,9 @@ export type AstStatTypeAlias = {

export type AstStatTypeFunction = {
tag: "typefunction",
["export"]: Token<"export">?,
export: Token<"export">?,
type: Token<"type">,
["function"]: Token<"function">,
functionKeyword: Token<"function">,
name: Token,
body: AstFunctionBody,
}
Expand Down Expand Up @@ -421,7 +424,7 @@ export type AstTypeTypeof = {
tag: "typeof",
typeof: Token<"typeof">,
openParens: Token<"(">,
expr: AstExpr,
expression: AstExpr,
closeParens: Token<")">,
}

Expand Down
Loading