mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 13:11:32 +00:00
idea: proper scoping support, completion now works
This commit is contained in:
@ -52,7 +52,6 @@ types:
|
|||||||
type: Expression
|
type: Expression
|
||||||
SetAssignment:
|
SetAssignment:
|
||||||
parent: Expression
|
parent: Expression
|
||||||
namedElementValue: symbol
|
|
||||||
values:
|
values:
|
||||||
- name: symbol
|
- name: symbol
|
||||||
type: Symbol
|
type: Symbol
|
||||||
@ -138,6 +137,8 @@ types:
|
|||||||
- name: arguments
|
- name: arguments
|
||||||
type: List<Expression>
|
type: List<Expression>
|
||||||
ArgumentSpec:
|
ArgumentSpec:
|
||||||
|
parent: Node
|
||||||
|
namedElementValue: symbol
|
||||||
values:
|
values:
|
||||||
- name: symbol
|
- name: symbol
|
||||||
type: Symbol
|
type: Symbol
|
||||||
@ -270,11 +271,17 @@ types:
|
|||||||
type: Expression
|
type: Expression
|
||||||
- name: block
|
- name: block
|
||||||
type: Block
|
type: Block
|
||||||
ForIn:
|
ForInItem:
|
||||||
parent: Expression
|
parent: Node
|
||||||
|
namedElementValue: symbol
|
||||||
values:
|
values:
|
||||||
- name: symbol
|
- name: symbol
|
||||||
type: Symbol
|
type: Symbol
|
||||||
|
ForIn:
|
||||||
|
parent: Expression
|
||||||
|
values:
|
||||||
|
- name: item
|
||||||
|
type: ForInItem
|
||||||
- name: expression
|
- name: expression
|
||||||
type: Expression
|
type: Expression
|
||||||
- name: block
|
- name: block
|
||||||
|
@ -31,6 +31,7 @@ digraph A {
|
|||||||
type_StringLiteral [shape=box,label="StringLiteral"]
|
type_StringLiteral [shape=box,label="StringLiteral"]
|
||||||
type_SymbolReference [shape=box,label="SymbolReference"]
|
type_SymbolReference [shape=box,label="SymbolReference"]
|
||||||
type_While [shape=box,label="While"]
|
type_While [shape=box,label="While"]
|
||||||
|
type_ForInItem [shape=box,label="ForInItem"]
|
||||||
type_ForIn [shape=box,label="ForIn"]
|
type_ForIn [shape=box,label="ForIn"]
|
||||||
type_Break [shape=box,label="Break"]
|
type_Break [shape=box,label="Break"]
|
||||||
type_Continue [shape=box,label="Continue"]
|
type_Continue [shape=box,label="Continue"]
|
||||||
@ -43,6 +44,8 @@ digraph A {
|
|||||||
type_Node -> type_Definition
|
type_Node -> type_Definition
|
||||||
type_Node -> type_Block
|
type_Node -> type_Block
|
||||||
type_Node -> type_CompilationUnit
|
type_Node -> type_CompilationUnit
|
||||||
|
type_Node -> type_ArgumentSpec
|
||||||
|
type_Node -> type_ForInItem
|
||||||
type_Node -> type_Native
|
type_Node -> type_Native
|
||||||
type_Expression -> type_LetAssignment
|
type_Expression -> type_LetAssignment
|
||||||
type_Expression -> type_VarAssignment
|
type_Expression -> type_VarAssignment
|
||||||
@ -105,7 +108,8 @@ digraph A {
|
|||||||
type_SymbolReference -> type_Symbol [style=dotted]
|
type_SymbolReference -> type_Symbol [style=dotted]
|
||||||
type_While -> type_Expression [style=dotted]
|
type_While -> type_Expression [style=dotted]
|
||||||
type_While -> type_Block [style=dotted]
|
type_While -> type_Block [style=dotted]
|
||||||
type_ForIn -> type_Symbol [style=dotted]
|
type_ForInItem -> type_Symbol [style=dotted]
|
||||||
|
type_ForIn -> type_ForInItem [style=dotted]
|
||||||
type_ForIn -> type_Expression [style=dotted]
|
type_ForIn -> type_Expression [style=dotted]
|
||||||
type_ForIn -> type_Block [style=dotted]
|
type_ForIn -> type_Block [style=dotted]
|
||||||
type_Native -> type_Symbol [style=dotted]
|
type_Native -> type_Symbol [style=dotted]
|
||||||
|
@ -6,4 +6,24 @@ import kotlinx.serialization.Serializable
|
|||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@SerialName("argumentSpec")
|
@SerialName("argumentSpec")
|
||||||
class ArgumentSpec(var symbol: Symbol, var multiple: Boolean = false)
|
class ArgumentSpec(val symbol: Symbol, val multiple: Boolean = false) : Node() {
|
||||||
|
override val type: NodeType = NodeType.ArgumentSpec
|
||||||
|
|
||||||
|
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
|
||||||
|
visitor.visitNodes(symbol)
|
||||||
|
|
||||||
|
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
||||||
|
visitor.visitArgumentSpec(this)
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (other !is ArgumentSpec) return false
|
||||||
|
return other.symbol == symbol && other.multiple == multiple
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = symbol.hashCode()
|
||||||
|
result = 31 * result + multiple.hashCode()
|
||||||
|
result = 31 * result + type.hashCode()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,22 +6,22 @@ import kotlinx.serialization.Serializable
|
|||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@SerialName("forIn")
|
@SerialName("forIn")
|
||||||
class ForIn(val symbol: Symbol, val expression: Expression, val block: Block) : Expression() {
|
class ForIn(val item: ForInItem, val expression: Expression, val block: Block) : Expression() {
|
||||||
override val type: NodeType = NodeType.ForIn
|
override val type: NodeType = NodeType.ForIn
|
||||||
|
|
||||||
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
|
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
|
||||||
visitor.visitNodes(symbol, expression, block)
|
visitor.visitNodes(item, expression, block)
|
||||||
|
|
||||||
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
||||||
visitor.visitForIn(this)
|
visitor.visitForIn(this)
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
if (other !is ForIn) return false
|
if (other !is ForIn) return false
|
||||||
return other.symbol == symbol && other.expression == expression && other.block == block
|
return other.item == item && other.expression == expression && other.block == block
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
override fun hashCode(): Int {
|
||||||
var result = symbol.hashCode()
|
var result = item.hashCode()
|
||||||
result = 31 * result + expression.hashCode()
|
result = 31 * result + expression.hashCode()
|
||||||
result = 31 * result + block.hashCode()
|
result = 31 * result + block.hashCode()
|
||||||
result = 31 * result + type.hashCode()
|
result = 31 * result + type.hashCode()
|
||||||
|
28
ast/src/main/kotlin/gay/pizza/pork/ast/ForInItem.kt
Normal file
28
ast/src/main/kotlin/gay/pizza/pork/ast/ForInItem.kt
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// GENERATED CODE FROM PORK AST CODEGEN
|
||||||
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
@SerialName("forInItem")
|
||||||
|
class ForInItem(val symbol: Symbol) : Node() {
|
||||||
|
override val type: NodeType = NodeType.ForInItem
|
||||||
|
|
||||||
|
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
|
||||||
|
visitor.visitNodes(symbol)
|
||||||
|
|
||||||
|
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
||||||
|
visitor.visitForInItem(this)
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (other !is ForInItem) return false
|
||||||
|
return other.symbol == symbol
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = symbol.hashCode()
|
||||||
|
result = 31 * result + type.hashCode()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
@ -10,7 +10,7 @@ class FunctionDefinition(override val modifiers: DefinitionModifiers, override v
|
|||||||
override val type: NodeType = NodeType.FunctionDefinition
|
override val type: NodeType = NodeType.FunctionDefinition
|
||||||
|
|
||||||
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
|
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
|
||||||
visitor.visitAll(listOf(symbol), listOf(block), listOf(native))
|
visitor.visitAll(listOf(symbol), arguments, listOf(block), listOf(native))
|
||||||
|
|
||||||
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
||||||
visitor.visitFunctionDefinition(this)
|
visitor.visitFunctionDefinition(this)
|
||||||
|
@ -2,6 +2,9 @@
|
|||||||
package gay.pizza.pork.ast
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
|
class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
|
||||||
|
override fun visitArgumentSpec(node: ArgumentSpec): Unit =
|
||||||
|
handle(node)
|
||||||
|
|
||||||
override fun visitBlock(node: Block): Unit =
|
override fun visitBlock(node: Block): Unit =
|
||||||
handle(node)
|
handle(node)
|
||||||
|
|
||||||
@ -23,6 +26,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
|
|||||||
override fun visitForIn(node: ForIn): Unit =
|
override fun visitForIn(node: ForIn): Unit =
|
||||||
handle(node)
|
handle(node)
|
||||||
|
|
||||||
|
override fun visitForInItem(node: ForInItem): Unit =
|
||||||
|
handle(node)
|
||||||
|
|
||||||
override fun visitFunctionCall(node: FunctionCall): Unit =
|
override fun visitFunctionCall(node: FunctionCall): Unit =
|
||||||
handle(node)
|
handle(node)
|
||||||
|
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
package gay.pizza.pork.ast
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
interface NodeParser {
|
interface NodeParser {
|
||||||
|
fun parseArgumentSpec(): ArgumentSpec
|
||||||
|
|
||||||
fun parseBlock(): Block
|
fun parseBlock(): Block
|
||||||
|
|
||||||
fun parseExpression(): Expression
|
fun parseExpression(): Expression
|
||||||
@ -22,6 +24,8 @@ interface NodeParser {
|
|||||||
|
|
||||||
fun parseForIn(): ForIn
|
fun parseForIn(): ForIn
|
||||||
|
|
||||||
|
fun parseForInItem(): ForInItem
|
||||||
|
|
||||||
fun parseFunctionCall(): FunctionCall
|
fun parseFunctionCall(): FunctionCall
|
||||||
|
|
||||||
fun parseFunctionDefinition(): FunctionDefinition
|
fun parseFunctionDefinition(): FunctionDefinition
|
||||||
|
@ -15,6 +15,7 @@ fun NodeParser.parse(type: NodeType): Node =
|
|||||||
NodeType.InfixOperation -> parseInfixOperation()
|
NodeType.InfixOperation -> parseInfixOperation()
|
||||||
NodeType.BooleanLiteral -> parseBooleanLiteral()
|
NodeType.BooleanLiteral -> parseBooleanLiteral()
|
||||||
NodeType.FunctionCall -> parseFunctionCall()
|
NodeType.FunctionCall -> parseFunctionCall()
|
||||||
|
NodeType.ArgumentSpec -> parseArgumentSpec()
|
||||||
NodeType.FunctionDefinition -> parseFunctionDefinition()
|
NodeType.FunctionDefinition -> parseFunctionDefinition()
|
||||||
NodeType.LetDefinition -> parseLetDefinition()
|
NodeType.LetDefinition -> parseLetDefinition()
|
||||||
NodeType.If -> parseIf()
|
NodeType.If -> parseIf()
|
||||||
@ -29,6 +30,7 @@ fun NodeParser.parse(type: NodeType): Node =
|
|||||||
NodeType.StringLiteral -> parseStringLiteral()
|
NodeType.StringLiteral -> parseStringLiteral()
|
||||||
NodeType.SymbolReference -> parseSymbolReference()
|
NodeType.SymbolReference -> parseSymbolReference()
|
||||||
NodeType.While -> parseWhile()
|
NodeType.While -> parseWhile()
|
||||||
|
NodeType.ForInItem -> parseForInItem()
|
||||||
NodeType.ForIn -> parseForIn()
|
NodeType.ForIn -> parseForIn()
|
||||||
NodeType.Break -> parseBreak()
|
NodeType.Break -> parseBreak()
|
||||||
NodeType.Continue -> parseContinue()
|
NodeType.Continue -> parseContinue()
|
||||||
|
@ -3,6 +3,7 @@ package gay.pizza.pork.ast
|
|||||||
|
|
||||||
enum class NodeType(val parent: NodeType? = null) {
|
enum class NodeType(val parent: NodeType? = null) {
|
||||||
Node,
|
Node,
|
||||||
|
ArgumentSpec(Node),
|
||||||
Block(Node),
|
Block(Node),
|
||||||
Expression(Node),
|
Expression(Node),
|
||||||
BooleanLiteral(Expression),
|
BooleanLiteral(Expression),
|
||||||
@ -13,6 +14,7 @@ enum class NodeType(val parent: NodeType? = null) {
|
|||||||
Definition(Node),
|
Definition(Node),
|
||||||
DoubleLiteral(Expression),
|
DoubleLiteral(Expression),
|
||||||
ForIn(Expression),
|
ForIn(Expression),
|
||||||
|
ForInItem(Node),
|
||||||
FunctionCall(Expression),
|
FunctionCall(Expression),
|
||||||
FunctionDefinition(Definition),
|
FunctionDefinition(Definition),
|
||||||
If(Expression),
|
If(Expression),
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
package gay.pizza.pork.ast
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
interface NodeVisitor<T> {
|
interface NodeVisitor<T> {
|
||||||
|
fun visitArgumentSpec(node: ArgumentSpec): T
|
||||||
|
|
||||||
fun visitBlock(node: Block): T
|
fun visitBlock(node: Block): T
|
||||||
|
|
||||||
fun visitBooleanLiteral(node: BooleanLiteral): T
|
fun visitBooleanLiteral(node: BooleanLiteral): T
|
||||||
@ -16,6 +18,8 @@ interface NodeVisitor<T> {
|
|||||||
|
|
||||||
fun visitForIn(node: ForIn): T
|
fun visitForIn(node: ForIn): T
|
||||||
|
|
||||||
|
fun visitForInItem(node: ForInItem): T
|
||||||
|
|
||||||
fun visitFunctionCall(node: FunctionCall): T
|
fun visitFunctionCall(node: FunctionCall): T
|
||||||
|
|
||||||
fun visitFunctionDefinition(node: FunctionDefinition): T
|
fun visitFunctionDefinition(node: FunctionDefinition): T
|
||||||
|
@ -12,6 +12,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
|
|||||||
is InfixOperation -> visitInfixOperation(node)
|
is InfixOperation -> visitInfixOperation(node)
|
||||||
is BooleanLiteral -> visitBooleanLiteral(node)
|
is BooleanLiteral -> visitBooleanLiteral(node)
|
||||||
is FunctionCall -> visitFunctionCall(node)
|
is FunctionCall -> visitFunctionCall(node)
|
||||||
|
is ArgumentSpec -> visitArgumentSpec(node)
|
||||||
is FunctionDefinition -> visitFunctionDefinition(node)
|
is FunctionDefinition -> visitFunctionDefinition(node)
|
||||||
is LetDefinition -> visitLetDefinition(node)
|
is LetDefinition -> visitLetDefinition(node)
|
||||||
is If -> visitIf(node)
|
is If -> visitIf(node)
|
||||||
@ -26,6 +27,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
|
|||||||
is StringLiteral -> visitStringLiteral(node)
|
is StringLiteral -> visitStringLiteral(node)
|
||||||
is SymbolReference -> visitSymbolReference(node)
|
is SymbolReference -> visitSymbolReference(node)
|
||||||
is While -> visitWhile(node)
|
is While -> visitWhile(node)
|
||||||
|
is ForInItem -> visitForInItem(node)
|
||||||
is ForIn -> visitForIn(node)
|
is ForIn -> visitForIn(node)
|
||||||
is Break -> visitBreak(node)
|
is Break -> visitBreak(node)
|
||||||
is Continue -> visitContinue(node)
|
is Continue -> visitContinue(node)
|
||||||
|
@ -26,7 +26,7 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : NodeVisitor<Any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
scoped(reuseScope, node = node) {
|
scoped(reuseScope, node = node) {
|
||||||
currentScope.define(node.symbol.id, item ?: None)
|
currentScope.define(node.item.symbol.id, item ?: None)
|
||||||
result = blockFunction.call()
|
result = blockFunction.call()
|
||||||
}
|
}
|
||||||
} catch (_: BreakMarker) {
|
} catch (_: BreakMarker) {
|
||||||
@ -39,6 +39,9 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : NodeVisitor<Any> {
|
|||||||
return result ?: None
|
return result ?: None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun visitForInItem(node: ForInItem): Any =
|
||||||
|
throw RuntimeException("Visiting ForInItem is not supported.")
|
||||||
|
|
||||||
override fun visitStringLiteral(node: StringLiteral): Any = node.text
|
override fun visitStringLiteral(node: StringLiteral): Any = node.text
|
||||||
override fun visitBooleanLiteral(node: BooleanLiteral): Any = node.value
|
override fun visitBooleanLiteral(node: BooleanLiteral): Any = node.value
|
||||||
|
|
||||||
@ -363,6 +366,9 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : NodeVisitor<Any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun visitArgumentSpec(node: ArgumentSpec): Any =
|
||||||
|
throw RuntimeException("Visiting ArgumentSpec is not supported.")
|
||||||
|
|
||||||
override fun visitBlock(node: Block): BlockFunction {
|
override fun visitBlock(node: Block): BlockFunction {
|
||||||
val visitor = this
|
val visitor = this
|
||||||
return object : BlockFunction() {
|
return object : BlockFunction() {
|
||||||
|
@ -4,6 +4,11 @@ import gay.pizza.pork.ast.*
|
|||||||
|
|
||||||
class Parser(source: TokenSource, attribution: NodeAttribution) :
|
class Parser(source: TokenSource, attribution: NodeAttribution) :
|
||||||
ParserBase(source, attribution) {
|
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) {
|
override fun parseBlock(): Block = guarded(NodeType.Block) {
|
||||||
expect(TokenType.LeftCurly)
|
expect(TokenType.LeftCurly)
|
||||||
val items = collect(TokenType.RightCurly) {
|
val items = collect(TokenType.RightCurly) {
|
||||||
@ -177,11 +182,15 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
|
|||||||
|
|
||||||
override fun parseForIn(): ForIn = guarded(NodeType.ForIn) {
|
override fun parseForIn(): ForIn = guarded(NodeType.ForIn) {
|
||||||
expect(TokenType.For)
|
expect(TokenType.For)
|
||||||
val symbol = parseSymbol()
|
val forInItem = parseForInItem()
|
||||||
expect(TokenType.In)
|
expect(TokenType.In)
|
||||||
val value = parseExpression()
|
val value = parseExpression()
|
||||||
val block = parseBlock()
|
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) {
|
override fun parseFunctionCall(): FunctionCall = guarded(NodeType.FunctionCall) {
|
||||||
@ -200,12 +209,7 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
|
|||||||
val name = parseSymbol()
|
val name = parseSymbol()
|
||||||
expect(TokenType.LeftParentheses)
|
expect(TokenType.LeftParentheses)
|
||||||
val arguments = collect(TokenType.RightParentheses, TokenType.Comma) {
|
val arguments = collect(TokenType.RightParentheses, TokenType.Comma) {
|
||||||
val symbol = parseSymbol()
|
parseArgumentSpec()
|
||||||
var multiple = false
|
|
||||||
if (next(TokenType.DotDotDot)) {
|
|
||||||
multiple = true
|
|
||||||
}
|
|
||||||
ArgumentSpec(symbol, multiple)
|
|
||||||
}
|
}
|
||||||
expect(TokenType.RightParentheses)
|
expect(TokenType.RightParentheses)
|
||||||
|
|
||||||
|
@ -30,13 +30,17 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
|
|||||||
|
|
||||||
override fun visitForIn(node: ForIn) {
|
override fun visitForIn(node: ForIn) {
|
||||||
append("for ")
|
append("for ")
|
||||||
visit(node.symbol)
|
visit(node.item)
|
||||||
append(" in ")
|
append(" in ")
|
||||||
visit(node.expression)
|
visit(node.expression)
|
||||||
append(" ")
|
append(" ")
|
||||||
visit(node.block)
|
visit(node.block)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun visitForInItem(node: ForInItem) {
|
||||||
|
node.symbol.visit(this)
|
||||||
|
}
|
||||||
|
|
||||||
override fun visitStringLiteral(node: StringLiteral) {
|
override fun visitStringLiteral(node: StringLiteral) {
|
||||||
append("\"")
|
append("\"")
|
||||||
append(StringEscape.escape(node.text))
|
append(StringEscape.escape(node.text))
|
||||||
@ -189,10 +193,7 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
|
|||||||
visit(node.symbol)
|
visit(node.symbol)
|
||||||
append("(")
|
append("(")
|
||||||
for ((index, argument) in node.arguments.withIndex()) {
|
for ((index, argument) in node.arguments.withIndex()) {
|
||||||
visit(argument.symbol)
|
argument.visit(this)
|
||||||
if (argument.multiple) {
|
|
||||||
append("...")
|
|
||||||
}
|
|
||||||
if (index + 1 != node.arguments.size) {
|
if (index + 1 != node.arguments.size) {
|
||||||
append(", ")
|
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) {
|
override fun visitBlock(node: Block) {
|
||||||
append("{")
|
append("{")
|
||||||
if (node.expressions.isNotEmpty()) {
|
if (node.expressions.isNotEmpty()) {
|
||||||
|
@ -23,8 +23,8 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
|
|||||||
Minus(SingleChar('-'), OperatorFamily, Promotion('-', MinusMinus)),
|
Minus(SingleChar('-'), OperatorFamily, Promotion('-', MinusMinus)),
|
||||||
Multiply(SingleChar('*'), OperatorFamily),
|
Multiply(SingleChar('*'), OperatorFamily),
|
||||||
Divide(SingleChar('/'), OperatorFamily),
|
Divide(SingleChar('/'), OperatorFamily),
|
||||||
And(ManyChars("and"), OperatorFamily),
|
And(ManyChars("and"), KeywordFamily),
|
||||||
Or(ManyChars("or"), OperatorFamily),
|
Or(ManyChars("or"), KeywordFamily),
|
||||||
Tilde(SingleChar('~'), OperatorFamily),
|
Tilde(SingleChar('~'), OperatorFamily),
|
||||||
Ampersand(SingleChar('&'), OperatorFamily),
|
Ampersand(SingleChar('&'), OperatorFamily),
|
||||||
Pipe(SingleChar('|'), OperatorFamily),
|
Pipe(SingleChar('|'), OperatorFamily),
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package gay.pizza.pork.idea
|
||||||
|
|
||||||
|
import com.intellij.lang.refactoring.RefactoringSupportProvider
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import gay.pizza.pork.idea.psi.gen.PorkNamedElement
|
||||||
|
|
||||||
|
class PorkRefactoringSupportProvider : RefactoringSupportProvider() {
|
||||||
|
override fun isMemberInplaceRenameAvailable(element: PsiElement, context: PsiElement?): Boolean {
|
||||||
|
return element is PorkNamedElement
|
||||||
|
}
|
||||||
|
}
|
@ -26,9 +26,6 @@ object PorkElementHelpers {
|
|||||||
|
|
||||||
fun referenceOfElement(element: PorkElement, type: NodeType): PsiReference? {
|
fun referenceOfElement(element: PorkElement, type: NodeType): PsiReference? {
|
||||||
val textRangeOfSymbolInElement = element.childrenOfType<SymbolElement>().firstOrNull()?.textRangeInParent ?: return null
|
val textRangeOfSymbolInElement = element.childrenOfType<SymbolElement>().firstOrNull()?.textRangeInParent ?: return null
|
||||||
if (type == NodeType.FunctionDefinition) {
|
return PorkIdentifierReference(element, textRangeOfSymbolInElement)
|
||||||
return PorkFunctionReference(element, textRangeOfSymbolInElement)
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
package gay.pizza.pork.idea.psi
|
|
||||||
|
|
||||||
import com.intellij.openapi.util.TextRange
|
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
|
||||||
import gay.pizza.pork.idea.psi.gen.FunctionDefinitionElement
|
|
||||||
import gay.pizza.pork.idea.psi.gen.PorkElement
|
|
||||||
|
|
||||||
class PorkFunctionReference(element: PorkElement, textRange: TextRange) : PorkReference(element, textRange) {
|
|
||||||
override fun resolve(): PorkElement? {
|
|
||||||
val functionName = canonicalText
|
|
||||||
val functionDefinitions = findFunctionDefinitions(functionName)
|
|
||||||
if (functionDefinitions.isNotEmpty()) {
|
|
||||||
return functionDefinitions.first()
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getVariants(): Array<Any> {
|
|
||||||
return findFunctionDefinitions().toTypedArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun findFunctionDefinitions(name: String? = null): List<FunctionDefinitionElement> {
|
|
||||||
val foundFunctionDefinitions = mutableListOf<FunctionDefinitionElement>()
|
|
||||||
for (file in getRelevantFiles()) {
|
|
||||||
val fileFunctionDefinitions = PsiTreeUtil.collectElementsOfType(file, FunctionDefinitionElement::class.java)
|
|
||||||
if (name != null) {
|
|
||||||
val fileFoundDefinition = fileFunctionDefinitions.firstOrNull {
|
|
||||||
it.name == name
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fileFoundDefinition != null) {
|
|
||||||
foundFunctionDefinitions.add(fileFoundDefinition)
|
|
||||||
return foundFunctionDefinitions
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
foundFunctionDefinitions.addAll(fileFunctionDefinitions)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return foundFunctionDefinitions
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,90 @@
|
|||||||
|
package gay.pizza.pork.idea.psi
|
||||||
|
|
||||||
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
|
import com.intellij.psi.util.childrenOfType
|
||||||
|
import gay.pizza.pork.idea.psi.gen.*
|
||||||
|
|
||||||
|
class PorkIdentifierReference(element: PorkElement, textRange: TextRange) : PorkReference(element, textRange) {
|
||||||
|
override fun resolve(): PorkElement? {
|
||||||
|
val identifierName = canonicalText
|
||||||
|
val items = findAllCandidates(identifierName)
|
||||||
|
if (items.isNotEmpty()) {
|
||||||
|
return items.first()
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getVariants(): Array<Any> {
|
||||||
|
return findAllCandidates().toTypedArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findAllCandidates(name: String? = null): List<PorkElement> =
|
||||||
|
listOf(findAnyLocals(name), findAnyDefinitions(name)).flatten()
|
||||||
|
|
||||||
|
fun findAnyLocals(name: String? = null): List<PorkElement> {
|
||||||
|
val functionDefinitionElement = PsiTreeUtil.getParentOfType(element, FunctionDefinitionElement::class.java)
|
||||||
|
if (functionDefinitionElement == null) {
|
||||||
|
return emptyList()
|
||||||
|
}
|
||||||
|
val locals = mutableListOf<PorkElement>()
|
||||||
|
|
||||||
|
fun check(localCandidate: PsiElement, upward: Boolean) {
|
||||||
|
if (localCandidate is BlockElement && !upward) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (localCandidate == element) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (localCandidate is ArgumentSpecElement ||
|
||||||
|
localCandidate is LetAssignmentElement ||
|
||||||
|
localCandidate is VarAssignmentElement) {
|
||||||
|
locals.add(localCandidate as PorkElement)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (localCandidate is ForInElement) {
|
||||||
|
val forInItem = localCandidate.childrenOfType<ForInItemElement>().first()
|
||||||
|
locals.add(forInItem)
|
||||||
|
}
|
||||||
|
|
||||||
|
localCandidate.children.forEach { check(it, false) }
|
||||||
|
}
|
||||||
|
|
||||||
|
PsiTreeUtil.treeWalkUp(element, functionDefinitionElement) { _, localCandidate ->
|
||||||
|
if (localCandidate != null) {
|
||||||
|
check(localCandidate, true)
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
val argumentSpecElements = functionDefinitionElement.childrenOfType<ArgumentSpecElement>()
|
||||||
|
locals.addAll(argumentSpecElements)
|
||||||
|
return locals.filter { it.name == name }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findAnyDefinitions(name: String? = null): List<PorkElement> {
|
||||||
|
val foundDefinitions = mutableListOf<PorkNamedElement>()
|
||||||
|
for (file in getRelevantFiles()) {
|
||||||
|
val definitions = PsiTreeUtil.collectElements(file) { element ->
|
||||||
|
element is FunctionDefinitionElement ||
|
||||||
|
element is LetDefinitionElement
|
||||||
|
}.filterIsInstance<PorkNamedElement>()
|
||||||
|
if (name != null) {
|
||||||
|
val fileFoundDefinition = definitions.firstOrNull {
|
||||||
|
it.name == name
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileFoundDefinition != null) {
|
||||||
|
foundDefinitions.add(fileFoundDefinition)
|
||||||
|
return foundDefinitions
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foundDefinitions.addAll(definitions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return foundDefinitions
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
// GENERATED CODE FROM PORK AST CODEGEN
|
||||||
|
package gay.pizza.pork.idea.psi.gen
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import com.intellij.lang.ASTNode
|
||||||
|
import gay.pizza.pork.idea.psi.PorkElementHelpers
|
||||||
|
|
||||||
|
class ArgumentSpecElement(node: ASTNode) : PorkNamedElement(node) {
|
||||||
|
override fun getName(): String? =
|
||||||
|
PorkElementHelpers.nameOfNamedElement(this)
|
||||||
|
|
||||||
|
override fun setName(name: String): PsiElement =
|
||||||
|
PorkElementHelpers.setNameOfNamedElement(this, name)
|
||||||
|
|
||||||
|
override fun getNameIdentifier(): PsiElement? =
|
||||||
|
PorkElementHelpers.nameIdentifierOfNamedElement(this)
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
// GENERATED CODE FROM PORK AST CODEGEN
|
||||||
|
package gay.pizza.pork.idea.psi.gen
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import com.intellij.lang.ASTNode
|
||||||
|
import gay.pizza.pork.idea.psi.PorkElementHelpers
|
||||||
|
|
||||||
|
class ForInItemElement(node: ASTNode) : PorkNamedElement(node) {
|
||||||
|
override fun getName(): String? =
|
||||||
|
PorkElementHelpers.nameOfNamedElement(this)
|
||||||
|
|
||||||
|
override fun setName(name: String): PsiElement =
|
||||||
|
PorkElementHelpers.setNameOfNamedElement(this, name)
|
||||||
|
|
||||||
|
override fun getNameIdentifier(): PsiElement? =
|
||||||
|
PorkElementHelpers.nameIdentifierOfNamedElement(this)
|
||||||
|
}
|
@ -19,6 +19,7 @@ object PorkElementFactory {
|
|||||||
NodeType.InfixOperation -> InfixOperationElement(node)
|
NodeType.InfixOperation -> InfixOperationElement(node)
|
||||||
NodeType.BooleanLiteral -> BooleanLiteralElement(node)
|
NodeType.BooleanLiteral -> BooleanLiteralElement(node)
|
||||||
NodeType.FunctionCall -> FunctionCallElement(node)
|
NodeType.FunctionCall -> FunctionCallElement(node)
|
||||||
|
NodeType.ArgumentSpec -> ArgumentSpecElement(node)
|
||||||
NodeType.FunctionDefinition -> FunctionDefinitionElement(node)
|
NodeType.FunctionDefinition -> FunctionDefinitionElement(node)
|
||||||
NodeType.LetDefinition -> LetDefinitionElement(node)
|
NodeType.LetDefinition -> LetDefinitionElement(node)
|
||||||
NodeType.If -> IfElement(node)
|
NodeType.If -> IfElement(node)
|
||||||
@ -33,6 +34,7 @@ object PorkElementFactory {
|
|||||||
NodeType.StringLiteral -> StringLiteralElement(node)
|
NodeType.StringLiteral -> StringLiteralElement(node)
|
||||||
NodeType.SymbolReference -> SymbolReferenceElement(node)
|
NodeType.SymbolReference -> SymbolReferenceElement(node)
|
||||||
NodeType.While -> WhileElement(node)
|
NodeType.While -> WhileElement(node)
|
||||||
|
NodeType.ForInItem -> ForInItemElement(node)
|
||||||
NodeType.ForIn -> ForInElement(node)
|
NodeType.ForIn -> ForInElement(node)
|
||||||
NodeType.Break -> BreakElement(node)
|
NodeType.Break -> BreakElement(node)
|
||||||
NodeType.Continue -> ContinueElement(node)
|
NodeType.Continue -> ContinueElement(node)
|
||||||
|
@ -1,17 +1,6 @@
|
|||||||
// GENERATED CODE FROM PORK AST CODEGEN
|
// GENERATED CODE FROM PORK AST CODEGEN
|
||||||
package gay.pizza.pork.idea.psi.gen
|
package gay.pizza.pork.idea.psi.gen
|
||||||
|
|
||||||
import com.intellij.psi.PsiElement
|
|
||||||
import com.intellij.lang.ASTNode
|
import com.intellij.lang.ASTNode
|
||||||
import gay.pizza.pork.idea.psi.PorkElementHelpers
|
|
||||||
|
|
||||||
class SetAssignmentElement(node: ASTNode) : PorkNamedElement(node) {
|
class SetAssignmentElement(node: ASTNode) : PorkElement(node)
|
||||||
override fun getName(): String? =
|
|
||||||
PorkElementHelpers.nameOfNamedElement(this)
|
|
||||||
|
|
||||||
override fun setName(name: String): PsiElement =
|
|
||||||
PorkElementHelpers.setNameOfNamedElement(this, name)
|
|
||||||
|
|
||||||
override fun getNameIdentifier(): PsiElement? =
|
|
||||||
PorkElementHelpers.nameIdentifierOfNamedElement(this)
|
|
||||||
}
|
|
||||||
|
@ -20,6 +20,9 @@
|
|||||||
<lang.braceMatcher
|
<lang.braceMatcher
|
||||||
language="Pork"
|
language="Pork"
|
||||||
implementationClass="gay.pizza.pork.idea.PorkBraceMatcher"/>
|
implementationClass="gay.pizza.pork.idea.PorkBraceMatcher"/>
|
||||||
|
<lang.refactoringSupport
|
||||||
|
language="Pork"
|
||||||
|
implementationClass="gay.pizza.pork.idea.PorkRefactoringSupportProvider"/>
|
||||||
<psi.declarationProvider implementation="gay.pizza.pork.idea.PorkSymbolDeclarationProvider"/>
|
<psi.declarationProvider implementation="gay.pizza.pork.idea.PorkSymbolDeclarationProvider"/>
|
||||||
</extensions>
|
</extensions>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user