diff --git a/Sources/Macros/Macros.swift b/Sources/Macros/Macros.swift index 34b03c4..ee870d1 100644 --- a/Sources/Macros/Macros.swift +++ b/Sources/Macros/Macros.swift @@ -449,7 +449,7 @@ public enum DeriveParsableStatement: MemberMacro { """ public static func ParseStatement( node: Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { return switch Parse(node: node, withContext: context) { case .Ok(let res): .Ok(res) case .Error(let e): .Error(e) diff --git a/Sources/P4Parser/CST.swift b/Sources/P4Parser/CST.swift index 30ed8f6..6e4797a 100644 --- a/Sources/P4Parser/CST.swift +++ b/Sources/P4Parser/CST.swift @@ -17,31 +17,34 @@ import Common -extension P4Value: CST.AnExpression {} +extension P4Value: CST.Categories.Expression {} public struct CST { - public protocol AnExpression {} - public protocol Statement {} - public protocol AnState {} + public struct Categories { + public protocol Expression {} + public protocol Statement {} + public protocol State {} + public protocol Declaration: Categories.Statement {} + } struct Expression {} public struct Statements { - public let statements: [Statement] + public let statements: [Categories.Statement] - public init(_ s: [Statement]) { + public init(_ s: [Categories.Statement]) { self.statements = s } } - public struct VariableDeclarationStatement: Statement { - public var initializer: AnExpression? + public struct VariableDeclarationStatement: Categories.Statement { + public var initializer: Categories.Expression? public var identifier: CST.Identifier public var tipe: CST.Tipe public init( - identifier: Identifier, withType tipe: CST.Tipe, withInitializer initializer: AnExpression? + identifier: Identifier, withType tipe: CST.Tipe, withInitializer initializer: Categories.Expression? ) { self.identifier = identifier self.initializer = initializer @@ -49,20 +52,20 @@ public struct CST { } } - public struct ConditionalStatement: Statement { - public var condition: AnExpression - public var thenn: Statement - public var elss: Statement? + public struct ConditionalStatement: Categories.Statement { + public var condition: Categories.Expression + public var thenn: Categories.Statement + public var elss: Categories.Statement? - public init(condition: AnExpression, withThen thenn: Statement) { + public init(condition: Categories.Expression, withThen thenn: Categories.Statement) { self.condition = condition self.thenn = thenn self.elss = .none } public init( - condition: AnExpression, withThen thenn: Statement, - andElse elss: Statement + condition: Categories.Expression, withThen thenn: Categories.Statement, + andElse elss: Categories.Statement ) { self.condition = condition self.thenn = thenn @@ -70,7 +73,7 @@ public struct CST { } } - public struct BlockStatement: Statement { + public struct BlockStatement: Categories.Statement { public var statements: Statements public init(_ statements: Statements) { @@ -79,15 +82,15 @@ public struct CST { } - public struct ReturnStatement: Statement { - public let value: AnExpression + public struct ReturnStatement: Categories.Statement { + public let value: Categories.Expression - public init(_ value: AnExpression) { + public init(_ value: Categories.Expression) { self.value = value } } - public struct ApplyStatement: Statement { + public struct ApplyStatement: Categories.Statement { public let body: CST.BlockStatement? public init() { self.body = .none } @@ -223,11 +226,10 @@ public struct CST { } } - public protocol AnDeclaration: Statement {} public struct Declaration {} - public struct Control: CustomStringConvertible, AnDeclaration { + public struct Control: CustomStringConvertible, Categories.Declaration { public var description: String { return "Control named \(self._name) \(self.parameters) \(self.actions) \(self.table)" } @@ -272,15 +274,15 @@ public struct CST { } - public struct ExternDeclaration: AnDeclaration { - public let declaration: CST.AnDeclaration + public struct ExternDeclaration: Categories.Declaration { + public let declaration: CST.Categories.Declaration - public init(_ declaration: CST.AnDeclaration) { + public init(_ declaration: CST.Categories.Declaration) { self.declaration = declaration } } - public struct FunctionDeclaration: AnDeclaration { + public struct FunctionDeclaration: Categories.Declaration { public var description: String { return "Function named \(self.name) that returns \(self.tipe) with parameters \(self.params)" } @@ -302,7 +304,7 @@ public struct CST { } } - public struct StructDeclaration: AnDeclaration { + public struct StructDeclaration: Categories.Declaration { public let fields: [CST.VariableDeclarationStatement] public let identifier: CST.Identifier public init(_ id: CST.Identifier, _ fields: [CST.VariableDeclarationStatement]) { @@ -311,7 +313,7 @@ public struct CST { } } - public struct Instantiation: Statement { + public struct Instantiation: Categories.Statement { public let name: CST.Identifier public var tipe: CST.Identifier public let arguments: CST.ArgumentList @@ -326,34 +328,34 @@ public struct CST { } } - public struct ExpressionStatement: Statement { - public let expression: AnExpression + public struct ExpressionStatement: Categories.Statement { + public let expression: Categories.Expression - public init(_ expr: AnExpression) { + public init(_ expr: Categories.Expression) { self.expression = expr } } - public struct Identifier: AnExpression { + public struct Identifier: Categories.Expression { public let id: Common.Identifier public init(_ id: Common.Identifier) { self.id = id } } - public struct Literal: AnExpression { + public struct Literal: Categories.Expression { public let literal: P4Value public init(_ literal: P4Value) { self.literal = literal } } - public enum KeysetExpression: AnExpression { + public enum KeysetExpression: Categories.Expression { case Default - case Value(AnExpression) + case Value(Categories.Expression) } - public struct SelectCaseExpression: AnExpression { + public struct SelectCaseExpression: Categories.Expression { public let key: CST.KeysetExpression public let next_state_identifier: CST.Identifier @@ -363,12 +365,12 @@ public struct CST { } } - public struct SelectExpression: AnExpression { - public let selector: AnExpression + public struct SelectExpression: Categories.Expression { + public let selector: Categories.Expression public let case_expressions: [CST.SelectCaseExpression] public init( - withSelector selector: AnExpression, + withSelector selector: Categories.Expression, withSelectCaseExpressions sces: [CST.SelectCaseExpression] ) { self.selector = selector @@ -397,15 +399,15 @@ public struct CST { case Or } - public struct BinaryOperatorExpression: AnExpression { - public let left: AnExpression - public let right: AnExpression + public struct BinaryOperatorExpression: Categories.Expression { + public let left: Categories.Expression + public let right: Categories.Expression public let type: BinaryOperatorExpressionType public init( withType tipe: BinaryOperatorExpressionType, - withLhs lhs: AnExpression, - withRhs rhs: AnExpression + withLhs lhs: Categories.Expression, + withRhs rhs: Categories.Expression ) { self.type = tipe self.left = lhs @@ -413,30 +415,30 @@ public struct CST { } } - public struct ArrayAccessExpression: AnExpression { - public let indexor: AnExpression - public let name: AnExpression + public struct ArrayAccessExpression: Categories.Expression { + public let indexor: Categories.Expression + public let name: Categories.Expression public init( - withName name: AnExpression, - withIndexor indexor: AnExpression + withName name: Categories.Expression, + withIndexor indexor: Categories.Expression ) { self.name = name self.indexor = indexor } } - public struct FieldAccessExpression: AnExpression { + public struct FieldAccessExpression: Categories.Expression { public let field: Identifier - public let strct: AnExpression + public let strct: Categories.Expression - public init(withStruct strct: AnExpression, withField field: Identifier) { + public init(withStruct strct: Categories.Expression, withField field: Identifier) { self.strct = strct self.field = field } } - public struct FunctionCall: AnExpression { + public struct FunctionCall: Categories.Expression { public let callee: Identifier public let arguments: ArgumentList @@ -450,12 +452,12 @@ public struct CST { public struct LocalElement {} - public struct ParserAssignmentStatement: Statement { - public let lvalue: AnExpression - public let value: AnExpression + public struct ParserAssignmentStatement: Categories.Statement { + public let lvalue: Categories.Expression + public let value: Categories.Expression public init( - withLValue lvalue: AnExpression, withValue value: AnExpression + withLValue lvalue: Categories.Expression, withValue value: Categories.Expression ) { self.lvalue = lvalue self.value = value @@ -491,7 +493,7 @@ public struct CST { /// Only defined to define Compilable extension. public struct TransitionStatement {} - public class ParserStateDirectTransition: ParserState, AnState { + public class ParserStateDirectTransition: ParserState, Categories.State { public let next_state_identifier: Identifier? public init( @@ -503,7 +505,7 @@ public struct CST { } } - public class ParserStateNoTransition: ParserState, AnState { + public class ParserStateNoTransition: ParserState, Categories.State { /// Construct a ParserState public init( name: Identifier, withStatements stmts: CST.Statements? = .none @@ -512,7 +514,7 @@ public struct CST { } } - public class ParserStateSelectTransition: ParserState, AnState { + public class ParserStateSelectTransition: ParserState, Categories.State { public let te: SelectExpression @@ -526,17 +528,17 @@ public struct CST { } public struct ParserStates { - public var states: [AnState] = Array() + public var states: [Categories.State] = Array() public func count() -> Int { return states.count } - public init(_ states: [AnState] = Array()) { + public init(_ states: [Categories.State] = Array()) { self.states = states } - public func append(state: AnState) -> ParserStates { + public func append(state: Categories.State) -> ParserStates { var new_states = self.states new_states.append(state) return ParserStates(new_states) @@ -546,7 +548,7 @@ public struct CST { /// A P4 Parser /// /// Note: A Parser is a type - public struct Parser: AnDeclaration { + public struct Parser: Categories.Declaration { public var states: ParserStates public var name: Identifier @@ -632,9 +634,9 @@ public struct CST { public struct Argument { public let index: Int - public let argument: CST.AnExpression + public let argument: Categories.Expression - public init(_ argument: CST.AnExpression, atIndex index: Int) { + public init(_ argument: Categories.Expression, atIndex index: Int) { self.argument = argument self.index = index } @@ -646,6 +648,8 @@ public struct CST { self.statements = stmts } } + + public struct Statement {} } public struct CSTCompilerContext { diff --git a/Sources/P4Parser/Parsers/Declarations.swift b/Sources/P4Parser/Parsers/Declarations.swift index 2ea3b4a..10297be 100644 --- a/Sources/P4Parser/Parsers/Declarations.swift +++ b/Sources/P4Parser/Parsers/Declarations.swift @@ -21,10 +21,10 @@ import TreeSitterExtensions import TreeSitterP4 extension CST.Declaration: Parsable { - public typealias C = CST.AnDeclaration + public typealias C = CST.Categories.Declaration public static func Parse( node: Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { // Be kind to our user -- if we are at a declaration node, dive into it! let declaration_node = @@ -34,7 +34,7 @@ extension CST.Declaration: Parsable { node } - let declaration_compilers: [String: any Parsable.Type] = [ + let declaration_compilers: [String: any Parsable.Type] = [ "function_declaration": CST.FunctionDeclaration.self, "control_declaration": CST.Control.self, //"type_declaration": AST.P4Struct.self, @@ -61,12 +61,12 @@ extension CST.Declaration: Parsable { extension CST.Declaration: ParsableStatement {} extension CST.FunctionDeclaration: Parsable { - public typealias C = CST.AnDeclaration + public typealias C = CST.Categories.Declaration public static func Parse( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Common.Result { + ) -> Common.Result { let function_declaration_node = node - #RequireNodeType( + #RequireNodeType( node: function_declaration_node, type: "function_declaration", nice_type_name: "Function Declaration") @@ -75,7 +75,7 @@ extension CST.FunctionDeclaration: Parsable { #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: function_declaration_node.toSourceLocation(), withError: "Missing function declaration component"))) @@ -88,7 +88,7 @@ extension CST.FunctionDeclaration: Parsable { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: function_declaration_node.toSourceLocation(), withError: "Missing function declaration component"))) @@ -102,7 +102,7 @@ extension CST.FunctionDeclaration: Parsable { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: function_declaration_node.toSourceLocation(), withError: "Missing function declaration component"))) @@ -227,19 +227,19 @@ extension CST.StructDeclaration: Parsable { } extension CST.Parser: Parsable { - public typealias C = CST.AnDeclaration + public typealias C = CST.Categories.Declaration public static func Parse( node: Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { let parser_node = node - #RequireNodeType>( + #RequireNodeType>( node: parser_node, type: "parserDeclaration", nice_type_name: "parser declaration") var walker = Walker(node: parser_node) var current_node: Node? = .none #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: parser_node.toSourceLocation(), withError: "Missing elements of parser declaration"))) @@ -263,7 +263,7 @@ extension CST.Parser: Parsable { #MustOr( result: type_node_child, thing: type_node_walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: parser_node.toSourceLocation(), withError: "Missing elements of parser type in parser declaration"))) @@ -279,7 +279,7 @@ extension CST.Parser: Parsable { type_node_walker.next() #MustOr( result: type_node_child, thing: type_node_walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: type_node_child!.toSourceLocation(), withError: "Missing name in parser type declaration"))) @@ -293,7 +293,7 @@ extension CST.Parser: Parsable { type_node_walker.next() #MustOr( result: type_node_child, thing: type_node_walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: type_node_child!.toSourceLocation(), withError: "Missing parser parameters"))) @@ -309,7 +309,7 @@ extension CST.Parser: Parsable { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: parser_node.toSourceLocation(), withError: "Missing parser declaration component"))) @@ -317,7 +317,7 @@ extension CST.Parser: Parsable { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: parser_node.toSourceLocation(), withError: "Missing elements of parser declaration"))) @@ -332,7 +332,7 @@ extension CST.Parser: Parsable { #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: parser_node.toSourceLocation(), withError: "Missing body of parser declaration"))) @@ -379,12 +379,12 @@ extension CST.Parser: Parsable { } extension CST.Control: Parsable { - public typealias C = CST.AnDeclaration + public typealias C = CST.Categories.Declaration public static func Parse( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Common.Result { + ) -> Common.Result { - #RequireNodeType>( + #RequireNodeType>( node: node, type: "control_declaration", nice_type_name: "control declaration") var walker = Walker(node: node) @@ -393,7 +393,7 @@ extension CST.Control: Parsable { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing control declaration component"))) @@ -409,7 +409,7 @@ extension CST.Control: Parsable { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing control declaration component"))) @@ -426,7 +426,7 @@ extension CST.Control: Parsable { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing control declaration component"))) @@ -873,10 +873,10 @@ extension CST.Table: Parsable { } extension CST.ExternDeclaration: Parsable { - public typealias C = CST.AnDeclaration + public typealias C = CST.Categories.Declaration public static func Parse( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { let extern_declaration_node = node #RequireNodeType( node: extern_declaration_node, type: "extern_declaration", @@ -1223,12 +1223,12 @@ extension CST.ArgumentList: Parsable { } extension CST.Argument: Parsable { - public typealias C = CST.AnExpression + public typealias C = CST.Categories.Expression public static func Parse( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Common.Result { + ) -> Common.Result { let argument_node = node - #RequireNodeType( + #RequireNodeType( node: argument_node, type: "argument", nice_type_name: "argument") let expression_node = node.child(at: 0)! diff --git a/Sources/P4Parser/Parsers/Expression.swift b/Sources/P4Parser/Parsers/Expression.swift index 9119f9b..4ba42ee 100644 --- a/Sources/P4Parser/Parsers/Expression.swift +++ b/Sources/P4Parser/Parsers/Expression.swift @@ -22,9 +22,9 @@ import TreeSitterP4 extension CST.Identifier: ParsableExpression { public static func ParseExpression( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { - #RequireNodeType( + #RequireNodeType( node: node, type: "identifier", nice_type_name: "Identifier") /// TODO: If there is a value here, then we can make this a compile-time constant! @@ -35,9 +35,9 @@ extension CST.Identifier: ParsableExpression { extension P4BooleanValue: ParsableExpression { public static func ParseExpression( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { let node = node.child(at: 0)! - #RequireNodeType( + #RequireNodeType( node: node, type: "booleanLiteralExpression", nice_type_name: "Boolean Literal Expression") if node.text == "false" { @@ -56,10 +56,10 @@ extension P4BooleanValue: ParsableExpression { extension P4IntValue: ParsableExpression { public static func ParseExpression( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { let node = node.child(at: 0)! - #RequireNodesType( + #RequireNodesType( nodes: node, type: ["integer", "integer_elaborated"], nice_type_names: ["Integer", "Elaborated Integer"]) @@ -106,9 +106,9 @@ extension P4IntValue: ParsableExpression { extension P4StringValue: ParsableExpression { public static func ParseExpression( node: SwiftTreeSitter.Node, withContext scopes: CSTCompilerContext - ) -> Result { + ) -> Result { let node = node.child(at: 0)! - #RequireNodeType( + #RequireNodeType( node: node, type: "string_literal", nice_type_name: "String Literal") return .Ok(CST.Literal(P4Value(P4StringValue(withValue: node.text!)))) } @@ -117,12 +117,12 @@ extension P4StringValue: ParsableExpression { extension CST.Expression: Parsable { public static func Parse( node: Node, withContext context: CSTCompilerContext - ) -> Result { - #RequireNodeType( + ) -> Result { + #RequireNodeType( node: node, type: "expression", nice_type_name: "expression") let expression_node = node.child(at: 0)! - #RequireNodesType( + #RequireNodesType( nodes: expression_node, type: ["grouped_expression", "simple_expression"], nice_type_names: ["grouped expression", "simple expression"]) @@ -154,10 +154,10 @@ extension CST.Expression: Parsable { extension CST.KeysetExpression: ParsableExpression { public static func ParseExpression( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { let keyset_expression_node = node.child(at: 0)! - #RequireNodesType( + #RequireNodesType( nodes: keyset_expression_node, type: ["expression", "default_keyset"], nice_type_names: ["expression", "default keyset"]) @@ -180,7 +180,7 @@ extension CST.KeysetExpression: ParsableExpression { extension CST.SelectExpression: ParsableExpression { public static func ParseExpression( node: Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { #RequireNodeType( node: node, type: "selectExpression", nice_type_name: "parser select expression") @@ -242,9 +242,9 @@ extension CST.SelectExpression: ParsableExpression { extension CST.SelectCaseExpression: ParsableExpression { public static func ParseExpression( node: Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { - #RequireNodeType( + #RequireNodeType( node: node, type: "selectCase", nice_type_name: "Select Case") guard let keysetexpression_node = node.child(at: 0), @@ -283,10 +283,10 @@ extension CST.SelectCaseExpression: ParsableExpression { extension CST.BinaryOperatorExpression: ParsableExpression { public static func ParseExpression( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { let expression = node.child(at: 0)! - #RequireNodeType( + #RequireNodeType( node: expression, type: "binaryOperatorExpression", nice_type_name: "Binary Operator Expression") let binary_operator_expression_node = expression.child(at: 0)! @@ -295,20 +295,20 @@ extension CST.BinaryOperatorExpression: ParsableExpression { var current_node: Node? = .none #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Malformed binary operator expression" ))) /// TODO: This macro cannot handle new lines in the arrays // swift-format-ignore - #RequireNodesType( + #RequireNodesType( nodes: binary_operator_expression_node, type: ["binaryEqualOperatorExpression", "binaryLessThanOperatorExpression", "binaryLessThanEqualOperatorExpression", "binaryGreaterThanOperatorExpression", "binaryGreaterThanEqualOperatorExpression", "binaryAndOperatorExpression", "binaryOrOperatorExpression", "binaryAddOperatorExpression", "binarySubtractOperatorExpression", "binaryMultiplyOperatorExpression", "binaryDivideOperatorExpression"], nice_type_names: [ "binary equal operator", "binary less than operator", "binary less than or equal to operator", "binary greater than operator", "binary greater than or equal to operator", "binary and operator", "binary or operator", "binary add operator", "binary subtract operator", "binary multiply operator", "binary divide operator"]) #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing LHS for binary operator expression"))) @@ -318,7 +318,7 @@ extension CST.BinaryOperatorExpression: ParsableExpression { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing binary operator for binary operator expression"))) @@ -326,7 +326,7 @@ extension CST.BinaryOperatorExpression: ParsableExpression { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing RHS for binary operator expression"))) @@ -407,10 +407,10 @@ extension CST.BinaryOperatorExpression: ParsableExpression { extension CST.ArrayAccessExpression: ParsableExpression { public static func ParseExpression( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { let expression = node.child(at: 0)! - #RequireNodeType( + #RequireNodeType( node: expression, type: "arrayAccessExpression", nice_type_name: "Array Access Expression") let array_access_expression_node = expression @@ -419,11 +419,11 @@ extension CST.ArrayAccessExpression: ParsableExpression { #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Malformed array access expression"))) - #RequireNodeType( + #RequireNodeType( node: current_node!, type: "expression", nice_type_name: "array identifier expression") let array_access_identifier_node = current_node! @@ -431,7 +431,7 @@ extension CST.ArrayAccessExpression: ParsableExpression { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing [ for array access expression"))) @@ -440,12 +440,12 @@ extension CST.ArrayAccessExpression: ParsableExpression { #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing indexor expression for array access expression"))) - #RequireNodeType( + #RequireNodeType( node: current_node!, type: "expression", nice_type_name: "array indexor expression") @@ -472,10 +472,10 @@ extension CST.ArrayAccessExpression: ParsableExpression { extension CST.FieldAccessExpression: ParsableExpression { public static func ParseExpression( node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { let expression = node.child(at: 0)! - #RequireNodeType( + #RequireNodeType( node: expression, type: "fieldAccessExpression", nice_type_name: "Array Access Expression") let field_access_expression_node = expression @@ -485,11 +485,11 @@ extension CST.FieldAccessExpression: ParsableExpression { #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Malformed field access expression"))) - #RequireNodeType( + #RequireNodeType( node: current_node!, type: "expression", nice_type_name: "struct identifier expression") let struct_identifier_node = current_node! @@ -497,7 +497,7 @@ extension CST.FieldAccessExpression: ParsableExpression { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing . for field access expression"))) @@ -505,12 +505,12 @@ extension CST.FieldAccessExpression: ParsableExpression { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing field name for field access expression"))) - #RequireNodeType( + #RequireNodeType( node: current_node!, type: "identifier", nice_type_name: "field name") @@ -539,10 +539,10 @@ extension CST.FieldAccessExpression: ParsableExpression { extension CST.FunctionCall: ParsableExpression { public static func ParseExpression( node: Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { let expression = node.child(at: 0)! - #RequireNodeType( + #RequireNodeType( node: expression, type: "function_call", nice_type_name: "Function Call") var walker = Walker(node: expression) @@ -550,7 +550,7 @@ extension CST.FunctionCall: ParsableExpression { #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing function call component"))) @@ -563,7 +563,7 @@ extension CST.FunctionCall: ParsableExpression { #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing function call component"))) diff --git a/Sources/P4Parser/Parsers/Parser.swift b/Sources/P4Parser/Parsers/Parser.swift index 42c902d..55f3cf0 100644 --- a/Sources/P4Parser/Parsers/Parser.swift +++ b/Sources/P4Parser/Parsers/Parser.swift @@ -21,10 +21,10 @@ import TreeSitterExtensions import TreeSitterP4 extension CST.LocalElements: Parsable { - public typealias C = CST.Statement + public typealias C = CST.Categories.Statement public static func Parse( node: Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { let localElementsParsers: [String: ParsableStatement.Type] = [ "variableDeclaration": CST.VariableDeclarationStatement.self ] @@ -46,10 +46,10 @@ extension CST.LocalElements: Parsable { } extension CST.ParserState: Parsable { - public typealias C = CST.AnState + public typealias C = CST.Categories.State public static func Parse( node: Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { var walker = Walker(node: node) var current_node: Node? = .none @@ -65,7 +65,7 @@ extension CST.ParserState: Parsable { #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing elements in parser state declaration"))) @@ -83,7 +83,7 @@ extension CST.ParserState: Parsable { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing elements in parser state declaration"))) @@ -99,7 +99,7 @@ extension CST.ParserState: Parsable { walker.next() #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration") )) @@ -130,7 +130,7 @@ extension CST.ParserState: Parsable { #MustOr( result: current_node, thing: walker.getNext(), - or: Result.Error( + or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing transition statement of state declaration"))) diff --git a/Sources/P4Parser/Parsers/Program.swift b/Sources/P4Parser/Parsers/Program.swift index 6ecfda9..246f0b4 100644 --- a/Sources/P4Parser/Parsers/Program.swift +++ b/Sources/P4Parser/Parsers/Program.swift @@ -24,7 +24,7 @@ extension CST.Program: Parsable { node: Node, withContext context: CSTCompilerContext ) -> Common.Result { - var statements: [CST.Statement] = Array() + var statements: [CST.Categories.Statement] = Array() var errors: (any Errorable)? = .none diff --git a/Sources/P4Parser/Parsers/SpecialParsers.swift b/Sources/P4Parser/Parsers/SpecialParsers.swift deleted file mode 100644 index fd41477..0000000 --- a/Sources/P4Parser/Parsers/SpecialParsers.swift +++ /dev/null @@ -1,67 +0,0 @@ -// p4rse, Copyright 2026, Will Hawkins -// -// This file is part of p4rse. -// -// This file is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import Common -import SwiftTreeSitter -import TreeSitterExtensions -import TreeSitterP4 - -public struct SpecialParsers { - public struct Statement {} -} - -extension SpecialParsers.Statement: Parsable { - public typealias C = CST.Statement - public static func Parse( - node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext - ) -> Result { - - if node.nodeType != "parserStatement" && node.nodeType != "statement" { - return Result.Error( - ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing expected parser statement") - ) - } - - let statement = node.child(at: 0)! - - let statementParsers: [String: ParsableStatement.Type] = [ - "assignmentStatement": CST.ParserAssignmentStatement.self, - "expressionStatement": CST.ExpressionStatement.self, - "variableDeclaration": CST.VariableDeclarationStatement.self, - "conditionalStatement": CST.ConditionalStatement.self, - "blockStatement": CST.BlockStatement.self, - "return_statement": CST.ReturnStatement.self, - ] - guard let parser = statementParsers[statement.nodeType ?? ""] else { - return Result.Error( - ErrorWithLocation( - sourceLocation: statement.toSourceLocation(), - withError: - "Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))")) - } - switch parser.ParseStatement(node: statement, withContext: context) { - case Result.Ok(let parsed): - return .Ok(parsed) - case Result.Error(let e): - return .Error( - ErrorWithLocation( - sourceLocation: node.toSourceLocation(), - withError: "Failed to parse a statement element: \(e)")) - } - } -} diff --git a/Sources/P4Parser/Parsers/Statement.swift b/Sources/P4Parser/Parsers/Statement.swift index 8c8cd19..498ffcd 100644 --- a/Sources/P4Parser/Parsers/Statement.swift +++ b/Sources/P4Parser/Parsers/Statement.swift @@ -132,7 +132,7 @@ extension CST.ConditionalStatement: Parsable { } guard - case .Ok(let thenns) = SpecialParsers.Statement.Parse( + case .Ok(let thenns) = CST.Statement.Parse( node: thens, withContext: context) else { return Result.Error( @@ -141,10 +141,10 @@ extension CST.ConditionalStatement: Parsable { "Could not parse the then block in a conditional statement")) } - let optional_elss: Result? = + let optional_elss: Result? = if let elss = node.child(at: 6) { .some( - SpecialParsers.Statement.Parse( + CST.Statement.Parse( node: elss, withContext: context)) } else { .none @@ -215,7 +215,7 @@ extension CST.VariableDeclarationStatement: Parsable { Error(withMessage: "Could not parse a P4 type from \(typeref.text!)")) } - var initializer: CST.AnExpression? = .none + var initializer: CST.Categories.Expression? = .none // If there is an initializer, it must be an expression. if let initializer_expression = maybe_rvalue { @@ -431,10 +431,10 @@ extension CST.Statements: Parsable { } var errors: (any Errorable)? = .none - var parsed_s: [CST.Statement] = Array() + var parsed_s: [CST.Categories.Statement] = Array() node.enumerateNamedChildren { node in - switch SpecialParsers.Statement.Parse( + switch CST.Statement.Parse( node: node, withContext: context) { case .Ok(let parsed_statement): @@ -460,7 +460,7 @@ extension CST.Statements: Parsable { extension CST.TransitionStatement: Parsable { public static func Parse( node: Node, withContext context: CSTCompilerContext - ) -> Result { + ) -> Result { guard let state_identifier = context.lexical_context_name else { return .Error( @@ -522,3 +522,45 @@ extension CST.TransitionStatement: Parsable { } } } + +extension CST.Statement: Parsable { + public typealias C = CST.Categories.Statement + public static func Parse( + node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext + ) -> Result { + + if node.nodeType != "parserStatement" && node.nodeType != "statement" { + return Result.Error( + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "Missing expected parser statement") + ) + } + + let statement = node.child(at: 0)! + + let statementParsers: [String: ParsableStatement.Type] = [ + "assignmentStatement": CST.ParserAssignmentStatement.self, + "expressionStatement": CST.ExpressionStatement.self, + "variableDeclaration": CST.VariableDeclarationStatement.self, + "conditionalStatement": CST.ConditionalStatement.self, + "blockStatement": CST.BlockStatement.self, + "return_statement": CST.ReturnStatement.self, + ] + guard let parser = statementParsers[statement.nodeType ?? ""] else { + return Result.Error( + ErrorWithLocation( + sourceLocation: statement.toSourceLocation(), + withError: + "Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))")) + } + switch parser.ParseStatement(node: statement, withContext: context) { + case Result.Ok(let parsed): + return .Ok(parsed) + case Result.Error(let e): + return .Error( + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "Failed to parse a statement element: \(e)")) + } + } +} diff --git a/Sources/P4Parser/Protocols.swift b/Sources/P4Parser/Protocols.swift index c181e53..ae09052 100644 --- a/Sources/P4Parser/Protocols.swift +++ b/Sources/P4Parser/Protocols.swift @@ -29,7 +29,7 @@ public protocol CompilableValue { public protocol MaybeParsableType { static func MaybeParseType( type: SwiftTreeSitter.Node, withContext: CSTCompilerContext - ) -> Result + ) -> Result } public protocol ParsableType { @@ -40,7 +40,7 @@ public protocol ParsableType { public protocol ParsableExpression { static func ParseExpression( node: Node, withContext context: CSTCompilerContext - ) -> Result + ) -> Result } public protocol Parsable { @@ -53,7 +53,7 @@ public protocol Parsable { public protocol ParsableStatement { static func ParseStatement( node: Node, withContext context: CSTCompilerContext - ) -> Result + ) -> Result } public protocol CSTVisitor { diff --git a/Sources/P4Parser/Visitor.swift b/Sources/P4Parser/Visitor.swift index 1f66bdf..a4b10a3 100644 --- a/Sources/P4Parser/Visitor.swift +++ b/Sources/P4Parser/Visitor.swift @@ -21,7 +21,7 @@ public struct CSTVisitorDriver { public init() {} public func visit( - expression: any CST.AnExpression, visitor: any CSTVisitor, context: T + expression: any CST.Categories.Expression, visitor: any CSTVisitor, context: T ) -> Result { return switch expression { case let e as CST.BinaryOperatorExpression: @@ -32,7 +32,7 @@ public struct CSTVisitorDriver { } public func visit( - state: any CST.AnState, visitor: any CSTVisitor, context: T + state: any CST.Categories.State, visitor: any CSTVisitor, context: T ) -> Result { return switch state { case let s as CST.ParserStateDirectTransition: @@ -46,7 +46,7 @@ public struct CSTVisitorDriver { } public func visit( - statement: any CST.Statement, visitor: any CSTVisitor, context: T + statement: any CST.Categories.Statement, visitor: any CSTVisitor, context: T ) -> Result { return switch statement { case let s as CST.Parser: visitor.visit(node: s, driver: self, context: context)