@@ -143,7 +143,9 @@ extension Result: CustomStringConvertible {
|
||||
#externalMacro(module: "Macros", type: "UseErrorResult")
|
||||
@freestanding(codeItem) public macro RequireNodeType<N, T>(node: N, type: String, msg: String) =
|
||||
#externalMacro(module: "Macros", type: "RequireNodeType")
|
||||
@freestanding(codeItem) public macro RequireNodesType<N, T>(nodes: N, type: [String], msg: [String]) =
|
||||
@freestanding(codeItem) public macro RequireNodesType<N, T>(
|
||||
nodes: N, type: [String], msg: [String]
|
||||
) =
|
||||
#externalMacro(module: "Macros", type: "RequireNodesType")
|
||||
@freestanding(codeItem) public macro SkipUnlessNodeType<N, T>(node: N, type: String) =
|
||||
#externalMacro(module: "Macros", type: "SkipUnlessNodeType")
|
||||
|
||||
+29
-16
@@ -26,10 +26,8 @@ public func remove_embedded_quotes(_ from: String) -> String {
|
||||
struct MacroError: Error, CustomStringConvertible {
|
||||
var message: String
|
||||
var description: String {
|
||||
get {
|
||||
return message
|
||||
}
|
||||
}
|
||||
public init(withMessage _message: String) {
|
||||
message = _message
|
||||
}
|
||||
@@ -138,7 +136,9 @@ public struct RequireErrorResult: ExpressionMacro {
|
||||
}
|
||||
|
||||
public struct RequireNodeType: CodeItemMacro {
|
||||
public static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> [CodeBlockItemSyntax] {
|
||||
public static func expansion(
|
||||
of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext
|
||||
) throws -> [CodeBlockItemSyntax] {
|
||||
let arguments = node.arguments.indices
|
||||
var arg_index = arguments.startIndex
|
||||
let node_to_check = node.arguments[arg_index].expression
|
||||
@@ -147,19 +147,24 @@ public struct RequireNodeType: CodeItemMacro {
|
||||
arg_index = arguments.index(after: arg_index)
|
||||
let expected_type_nice_name = node.arguments[arg_index].expression
|
||||
|
||||
let error_message = "Did not find " + remove_embedded_quotes(expected_type_nice_name.description)
|
||||
let error_message =
|
||||
"Did not find " + remove_embedded_quotes(expected_type_nice_name.description)
|
||||
|
||||
return [CodeBlockItemSyntax(
|
||||
return [
|
||||
CodeBlockItemSyntax(
|
||||
"""
|
||||
if \(node_to_check).nodeType != \(expected_type) {
|
||||
return Result.Error(
|
||||
ErrorOnNode(node: \(node_to_check), withError: "\(raw: error_message)"))
|
||||
}
|
||||
""")]
|
||||
""")
|
||||
]
|
||||
}
|
||||
}
|
||||
public struct RequireNodesType: CodeItemMacro {
|
||||
public static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> [CodeBlockItemSyntax] {
|
||||
public static func expansion(
|
||||
of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext
|
||||
) throws -> [CodeBlockItemSyntax] {
|
||||
let arguments = node.arguments.indices
|
||||
var arg_index = arguments.startIndex
|
||||
|
||||
@@ -177,44 +182,52 @@ public struct RequireNodesType: CodeItemMacro {
|
||||
throw MacroError(withMessage: "Node nice names must be in an array")
|
||||
}
|
||||
|
||||
let error_message = "Did not find one of the expected types: " + expected_type_nice_names.elements.map(){ l in
|
||||
let error_message =
|
||||
"Did not find one of the expected types: "
|
||||
+ expected_type_nice_names.elements.map { l in
|
||||
remove_embedded_quotes("\(l.expression)")
|
||||
}.joined(separator: ",")
|
||||
|
||||
|
||||
let ifs = expected_types.elements.map(){ l in
|
||||
let ifs = expected_types.elements.map { l in
|
||||
"\(node_to_check).nodeType != \(l.expression)"
|
||||
}.joined(separator: " && ")
|
||||
|
||||
return [CodeBlockItemSyntax(
|
||||
return [
|
||||
CodeBlockItemSyntax(
|
||||
"""
|
||||
if \(raw: ifs) {
|
||||
return Result.Error(
|
||||
ErrorOnNode(node: \(node_to_check), withError: "\(raw: error_message)"))
|
||||
}
|
||||
""")]
|
||||
""")
|
||||
]
|
||||
}
|
||||
}
|
||||
public struct SkipUnlessNodeType: CodeItemMacro {
|
||||
public static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> [CodeBlockItemSyntax] {
|
||||
public static func expansion(
|
||||
of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext
|
||||
) throws -> [CodeBlockItemSyntax] {
|
||||
let arguments = node.arguments.indices
|
||||
var arg_index = arguments.startIndex
|
||||
let node_to_check = node.arguments[arg_index].expression
|
||||
arg_index = arguments.index(after: arg_index)
|
||||
let expected_type = node.arguments[arg_index].expression
|
||||
|
||||
return [CodeBlockItemSyntax(
|
||||
return [
|
||||
CodeBlockItemSyntax(
|
||||
"""
|
||||
if \(node_to_check).nodeType != \(expected_type) {
|
||||
return Result.Ok(.none)
|
||||
}
|
||||
""")]
|
||||
""")
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
struct P4Macros: CompilerPlugin {
|
||||
var providingMacros: [Macro.Type] = [
|
||||
RequireResult.self, RequireErrorResult.self, UseOkResult.self, UseErrorResult.self, RequireNodeType.self, SkipUnlessNodeType.self, RequireNodesType.self
|
||||
RequireResult.self, RequireErrorResult.self, UseOkResult.self, UseErrorResult.self,
|
||||
RequireNodeType.self, SkipUnlessNodeType.self, RequireNodesType.self,
|
||||
]
|
||||
}
|
||||
|
||||
@@ -33,7 +33,8 @@ extension TypedIdentifier: CompilableExpression {
|
||||
) -> Result<EvaluatableExpression?> {
|
||||
|
||||
let node = node.child(at: 0)!
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(node: node, type: "identifier")
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(
|
||||
node: node, type: "identifier")
|
||||
|
||||
guard
|
||||
case Result.Ok(let type) = scopes.lookup(
|
||||
@@ -52,7 +53,8 @@ extension P4BooleanValue: CompilableExpression {
|
||||
withScopes scopes: LexicalScopes
|
||||
) -> Result<EvaluatableExpression?> {
|
||||
let node = node.child(at: 0)!
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(node: node, type: "booleanLiteralExpression")
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(
|
||||
node: node, type: "booleanLiteralExpression")
|
||||
|
||||
if node.text == "false" {
|
||||
return .Ok(P4BooleanValue(withValue: false))
|
||||
@@ -60,7 +62,8 @@ extension P4BooleanValue: CompilableExpression {
|
||||
return .Ok(P4BooleanValue(withValue: true))
|
||||
}
|
||||
|
||||
return .Error(ErrorOnNode(node: node, withError: "Failed to parse boolean literal: \(node.text!)"))
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Failed to parse boolean literal: \(node.text!)"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +88,8 @@ extension P4StringValue: CompilableExpression {
|
||||
withScopes scopes: LexicalScopes
|
||||
) -> Result<EvaluatableExpression?> {
|
||||
let node = node.child(at: 0)!
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(node: node, type: "string_literal")
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(
|
||||
node: node, type: "string_literal")
|
||||
return .Ok(P4StringValue(withValue: node.text!))
|
||||
}
|
||||
}
|
||||
@@ -95,7 +99,9 @@ struct Expression {
|
||||
node: Node, inTree: MutableTree, withScopes scopes: LexicalScopes
|
||||
) -> Result<EvaluatableExpression> {
|
||||
|
||||
#RequireNodesType<Node, EvaluatableExpression>(nodes: node, type: ["expression", "keysetExpression"], msg: ["expression", "keyset expression"])
|
||||
#RequireNodesType<Node, EvaluatableExpression>(
|
||||
nodes: node, type: ["expression", "keysetExpression"],
|
||||
msg: ["expression", "keyset expression"])
|
||||
|
||||
// If the node is a keyset expression, then dig out the expression:
|
||||
let node =
|
||||
|
||||
@@ -27,7 +27,8 @@ extension ParserAssignmentStatement: CompilableStatement {
|
||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
||||
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "assignmentStatement", msg: "assignment statement")
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||
node: node, type: "assignmentStatement", msg: "assignment statement")
|
||||
|
||||
guard let lvalue_node = node.child(at: 0),
|
||||
lvalue_node.nodeType == "expression"
|
||||
@@ -92,7 +93,8 @@ public struct Parser {
|
||||
guard let parser = localElementsParsers[node.nodeType ?? ""] else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Unparseable statement type (\(node.nodeType ?? "Unknown Statement Type"))"))
|
||||
node: node,
|
||||
withError: "Unparseable statement type (\(node.nodeType ?? "Unknown Statement Type"))"))
|
||||
}
|
||||
|
||||
switch parser.Compile(node: node, inTree: tree, withScopes: scopes) {
|
||||
@@ -126,7 +128,9 @@ public struct Parser {
|
||||
guard let parser = statementParsers[statement.nodeType ?? ""] else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: statement, withError: "Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
|
||||
node: statement,
|
||||
withError:
|
||||
"Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
|
||||
}
|
||||
switch parser.Compile(node: statement, inTree: tree, withScopes: scopes) {
|
||||
case Result.Ok(let (parsed, updatedLexicalScopes)):
|
||||
|
||||
Reference in New Issue
Block a user