From f96350f89d0eeb983d77babdd9af67b9c6c43e08 Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Fri, 6 Mar 2026 08:03:07 -0500 Subject: [PATCH] Make Formatter Happy Signed-off-by: Will Hawkins --- Sources/Common/Support.swift | 4 +- Sources/Macros/Macros.swift | 91 ++++++++++++++++------------- Sources/P4Compiler/Compiler.swift | 12 ++-- Sources/P4Compiler/Expression.swift | 16 +++-- Sources/P4Compiler/Parser.swift | 10 +++- 5 files changed, 79 insertions(+), 54 deletions(-) diff --git a/Sources/Common/Support.swift b/Sources/Common/Support.swift index 9557db6..6c70117 100644 --- a/Sources/Common/Support.swift +++ b/Sources/Common/Support.swift @@ -143,7 +143,9 @@ extension Result: CustomStringConvertible { #externalMacro(module: "Macros", type: "UseErrorResult") @freestanding(codeItem) public macro RequireNodeType(node: N, type: String, msg: String) = #externalMacro(module: "Macros", type: "RequireNodeType") -@freestanding(codeItem) public macro RequireNodesType(nodes: N, type: [String], msg: [String]) = +@freestanding(codeItem) public macro RequireNodesType( + nodes: N, type: [String], msg: [String] +) = #externalMacro(module: "Macros", type: "RequireNodesType") @freestanding(codeItem) public macro SkipUnlessNodeType(node: N, type: String) = #externalMacro(module: "Macros", type: "SkipUnlessNodeType") diff --git a/Sources/Macros/Macros.swift b/Sources/Macros/Macros.swift index 2efe56b..6ef008d 100644 --- a/Sources/Macros/Macros.swift +++ b/Sources/Macros/Macros.swift @@ -24,15 +24,13 @@ 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 - } + var message: String + var description: String { + return message + } + public init(withMessage _message: String) { + message = _message + } } public struct UseOkResult: ExpressionMacro { @@ -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( - """ - if \(node_to_check).nodeType != \(expected_type) { - return Result.Error( - ErrorOnNode(node: \(node_to_check), withError: "\(raw: error_message)")) - } - """)] + 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 - remove_embedded_quotes("\(l.expression)") - }.joined(separator: ",") + 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( - """ - if \(raw: ifs) { - return Result.Error( - ErrorOnNode(node: \(node_to_check), withError: "\(raw: error_message)")) - } - """)] + 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( - """ - if \(node_to_check).nodeType != \(expected_type) { - return Result.Ok(.none) - } - """)] + 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, ] } diff --git a/Sources/P4Compiler/Compiler.swift b/Sources/P4Compiler/Compiler.swift index 6419e71..e259c51 100644 --- a/Sources/P4Compiler/Compiler.swift +++ b/Sources/P4Compiler/Compiler.swift @@ -25,13 +25,13 @@ import TreeSitterP4 let p4lang = Language(tree_sitter_p4()) public func ConfigureP4Parser() -> Result { - let p = SwiftTreeSitter.Parser.init() + let p = SwiftTreeSitter.Parser.init() - do { - try p.setLanguage(p4lang) - } catch { - return Result.Error(Error(withMessage: "Could not configure the P4 parser")) - } + do { + try p.setLanguage(p4lang) + } catch { + return Result.Error(Error(withMessage: "Could not configure the P4 parser")) + } return .Ok(p) } diff --git a/Sources/P4Compiler/Expression.swift b/Sources/P4Compiler/Expression.swift index 8186d0f..f03191e 100644 --- a/Sources/P4Compiler/Expression.swift +++ b/Sources/P4Compiler/Expression.swift @@ -33,7 +33,8 @@ extension TypedIdentifier: CompilableExpression { ) -> Result { let node = node.child(at: 0)! - #SkipUnlessNodeType(node: node, type: "identifier") + #SkipUnlessNodeType( + node: node, type: "identifier") guard case Result.Ok(let type) = scopes.lookup( @@ -52,7 +53,8 @@ extension P4BooleanValue: CompilableExpression { withScopes scopes: LexicalScopes ) -> Result { let node = node.child(at: 0)! - #SkipUnlessNodeType(node: node, type: "booleanLiteralExpression") + #SkipUnlessNodeType( + 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 { let node = node.child(at: 0)! - #SkipUnlessNodeType(node: node, type: "string_literal") + #SkipUnlessNodeType( + 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 { - #RequireNodesType(nodes: node, type: ["expression", "keysetExpression"], msg: ["expression", "keyset expression"]) + #RequireNodesType( + nodes: node, type: ["expression", "keysetExpression"], + msg: ["expression", "keyset expression"]) // If the node is a keyset expression, then dig out the expression: let node = diff --git a/Sources/P4Compiler/Parser.swift b/Sources/P4Compiler/Parser.swift index 3a5d5b4..34d5e7e 100644 --- a/Sources/P4Compiler/Parser.swift +++ b/Sources/P4Compiler/Parser.swift @@ -27,7 +27,8 @@ extension ParserAssignmentStatement: CompilableStatement { node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes ) -> Result<(EvaluatableStatement, LexicalScopes)> { - #RequireNodeType(node: node, type: "assignmentStatement", msg: "assignment statement") + #RequireNodeType( + 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)):