idea: proper scoping support, completion now works

This commit is contained in:
2023-09-21 23:07:22 -07:00
parent c92a111af7
commit f60715dfb3
25 changed files with 265 additions and 83 deletions

View File

@ -4,6 +4,11 @@ import gay.pizza.pork.ast.*
class Parser(source: TokenSource, attribution: NodeAttribution) :
ParserBase(source, attribution) {
override fun parseArgumentSpec(): ArgumentSpec = guarded(NodeType.ArgumentSpec) {
val symbol = parseSymbol()
ArgumentSpec(symbol, next(TokenType.DotDotDot))
}
override fun parseBlock(): Block = guarded(NodeType.Block) {
expect(TokenType.LeftCurly)
val items = collect(TokenType.RightCurly) {
@ -177,11 +182,15 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
override fun parseForIn(): ForIn = guarded(NodeType.ForIn) {
expect(TokenType.For)
val symbol = parseSymbol()
val forInItem = parseForInItem()
expect(TokenType.In)
val value = parseExpression()
val block = parseBlock()
ForIn(symbol, value, block)
ForIn(forInItem, value, block)
}
override fun parseForInItem(): ForInItem = guarded(NodeType.ForInItem) {
ForInItem(parseSymbol())
}
override fun parseFunctionCall(): FunctionCall = guarded(NodeType.FunctionCall) {
@ -200,12 +209,7 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
val name = parseSymbol()
expect(TokenType.LeftParentheses)
val arguments = collect(TokenType.RightParentheses, TokenType.Comma) {
val symbol = parseSymbol()
var multiple = false
if (next(TokenType.DotDotDot)) {
multiple = true
}
ArgumentSpec(symbol, multiple)
parseArgumentSpec()
}
expect(TokenType.RightParentheses)

View File

@ -30,13 +30,17 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
override fun visitForIn(node: ForIn) {
append("for ")
visit(node.symbol)
visit(node.item)
append(" in ")
visit(node.expression)
append(" ")
visit(node.block)
}
override fun visitForInItem(node: ForInItem) {
node.symbol.visit(this)
}
override fun visitStringLiteral(node: StringLiteral) {
append("\"")
append(StringEscape.escape(node.text))
@ -189,10 +193,7 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
visit(node.symbol)
append("(")
for ((index, argument) in node.arguments.withIndex()) {
visit(argument.symbol)
if (argument.multiple) {
append("...")
}
argument.visit(this)
if (index + 1 != node.arguments.size) {
append(", ")
}
@ -207,6 +208,13 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
}
}
override fun visitArgumentSpec(node: ArgumentSpec) {
node.symbol.visit(this)
if (node.multiple) {
append("...")
}
}
override fun visitBlock(node: Block) {
append("{")
if (node.expressions.isNotEmpty()) {

View File

@ -23,8 +23,8 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
Minus(SingleChar('-'), OperatorFamily, Promotion('-', MinusMinus)),
Multiply(SingleChar('*'), OperatorFamily),
Divide(SingleChar('/'), OperatorFamily),
And(ManyChars("and"), OperatorFamily),
Or(ManyChars("or"), OperatorFamily),
And(ManyChars("and"), KeywordFamily),
Or(ManyChars("or"), KeywordFamily),
Tilde(SingleChar('~'), OperatorFamily),
Ampersand(SingleChar('&'), OperatorFamily),
Pipe(SingleChar('|'), OperatorFamily),