compiler: Refactor Language Element Tags

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-06-15 21:16:52 -04:00
parent d7022725ed
commit d22776b018
10 changed files with 207 additions and 228 deletions
+1 -1
View File
@@ -449,7 +449,7 @@ public enum DeriveParsableStatement: MemberMacro {
""" """
public static func ParseStatement( public static func ParseStatement(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.Statement> { ) -> Result<CST.Categories.Statement> {
return switch Parse(node: node, withContext: context) { return switch Parse(node: node, withContext: context) {
case .Ok(let res): .Ok(res) case .Ok(let res): .Ok(res)
case .Error(let e): .Error(e) case .Error(let e): .Error(e)
+70 -66
View File
@@ -17,31 +17,34 @@
import Common import Common
extension P4Value: CST.AnExpression {} extension P4Value: CST.Categories.Expression {}
public struct CST { public struct CST {
public protocol AnExpression {} public struct Categories {
public protocol Expression {}
public protocol Statement {} public protocol Statement {}
public protocol AnState {} public protocol State {}
public protocol Declaration: Categories.Statement {}
}
struct Expression {} struct Expression {}
public struct Statements { public struct Statements {
public let statements: [Statement] public let statements: [Categories.Statement]
public init(_ s: [Statement]) { public init(_ s: [Categories.Statement]) {
self.statements = s self.statements = s
} }
} }
public struct VariableDeclarationStatement: Statement { public struct VariableDeclarationStatement: Categories.Statement {
public var initializer: AnExpression? public var initializer: Categories.Expression?
public var identifier: CST.Identifier public var identifier: CST.Identifier
public var tipe: CST.Tipe public var tipe: CST.Tipe
public init( 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.identifier = identifier
self.initializer = initializer self.initializer = initializer
@@ -49,20 +52,20 @@ public struct CST {
} }
} }
public struct ConditionalStatement: Statement { public struct ConditionalStatement: Categories.Statement {
public var condition: AnExpression public var condition: Categories.Expression
public var thenn: Statement public var thenn: Categories.Statement
public var elss: 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.condition = condition
self.thenn = thenn self.thenn = thenn
self.elss = .none self.elss = .none
} }
public init( public init(
condition: AnExpression, withThen thenn: Statement, condition: Categories.Expression, withThen thenn: Categories.Statement,
andElse elss: Statement andElse elss: Categories.Statement
) { ) {
self.condition = condition self.condition = condition
self.thenn = thenn self.thenn = thenn
@@ -70,7 +73,7 @@ public struct CST {
} }
} }
public struct BlockStatement: Statement { public struct BlockStatement: Categories.Statement {
public var statements: Statements public var statements: Statements
public init(_ statements: Statements) { public init(_ statements: Statements) {
@@ -79,15 +82,15 @@ public struct CST {
} }
public struct ReturnStatement: Statement { public struct ReturnStatement: Categories.Statement {
public let value: AnExpression public let value: Categories.Expression
public init(_ value: AnExpression) { public init(_ value: Categories.Expression) {
self.value = value self.value = value
} }
} }
public struct ApplyStatement: Statement { public struct ApplyStatement: Categories.Statement {
public let body: CST.BlockStatement? public let body: CST.BlockStatement?
public init() { self.body = .none } public init() { self.body = .none }
@@ -223,11 +226,10 @@ public struct CST {
} }
} }
public protocol AnDeclaration: Statement {}
public struct Declaration {} public struct Declaration {}
public struct Control: CustomStringConvertible, AnDeclaration { public struct Control: CustomStringConvertible, Categories.Declaration {
public var description: String { public var description: String {
return "Control named \(self._name) \(self.parameters) \(self.actions) \(self.table)" return "Control named \(self._name) \(self.parameters) \(self.actions) \(self.table)"
} }
@@ -272,15 +274,15 @@ public struct CST {
} }
public struct ExternDeclaration: AnDeclaration { public struct ExternDeclaration: Categories.Declaration {
public let declaration: CST.AnDeclaration public let declaration: CST.Categories.Declaration
public init(_ declaration: CST.AnDeclaration) { public init(_ declaration: CST.Categories.Declaration) {
self.declaration = declaration self.declaration = declaration
} }
} }
public struct FunctionDeclaration: AnDeclaration { public struct FunctionDeclaration: Categories.Declaration {
public var description: String { public var description: String {
return "Function named \(self.name) that returns \(self.tipe) with parameters \(self.params)" 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 fields: [CST.VariableDeclarationStatement]
public let identifier: CST.Identifier public let identifier: CST.Identifier
public init(_ id: CST.Identifier, _ fields: [CST.VariableDeclarationStatement]) { 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 let name: CST.Identifier
public var tipe: CST.Identifier public var tipe: CST.Identifier
public let arguments: CST.ArgumentList public let arguments: CST.ArgumentList
@@ -326,34 +328,34 @@ public struct CST {
} }
} }
public struct ExpressionStatement: Statement { public struct ExpressionStatement: Categories.Statement {
public let expression: AnExpression public let expression: Categories.Expression
public init(_ expr: AnExpression) { public init(_ expr: Categories.Expression) {
self.expression = expr self.expression = expr
} }
} }
public struct Identifier: AnExpression { public struct Identifier: Categories.Expression {
public let id: Common.Identifier public let id: Common.Identifier
public init(_ id: Common.Identifier) { public init(_ id: Common.Identifier) {
self.id = id self.id = id
} }
} }
public struct Literal: AnExpression { public struct Literal: Categories.Expression {
public let literal: P4Value public let literal: P4Value
public init(_ literal: P4Value) { public init(_ literal: P4Value) {
self.literal = literal self.literal = literal
} }
} }
public enum KeysetExpression: AnExpression { public enum KeysetExpression: Categories.Expression {
case Default 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 key: CST.KeysetExpression
public let next_state_identifier: CST.Identifier public let next_state_identifier: CST.Identifier
@@ -363,12 +365,12 @@ public struct CST {
} }
} }
public struct SelectExpression: AnExpression { public struct SelectExpression: Categories.Expression {
public let selector: AnExpression public let selector: Categories.Expression
public let case_expressions: [CST.SelectCaseExpression] public let case_expressions: [CST.SelectCaseExpression]
public init( public init(
withSelector selector: AnExpression, withSelector selector: Categories.Expression,
withSelectCaseExpressions sces: [CST.SelectCaseExpression] withSelectCaseExpressions sces: [CST.SelectCaseExpression]
) { ) {
self.selector = selector self.selector = selector
@@ -397,15 +399,15 @@ public struct CST {
case Or case Or
} }
public struct BinaryOperatorExpression: AnExpression { public struct BinaryOperatorExpression: Categories.Expression {
public let left: AnExpression public let left: Categories.Expression
public let right: AnExpression public let right: Categories.Expression
public let type: BinaryOperatorExpressionType public let type: BinaryOperatorExpressionType
public init( public init(
withType tipe: BinaryOperatorExpressionType, withType tipe: BinaryOperatorExpressionType,
withLhs lhs: AnExpression, withLhs lhs: Categories.Expression,
withRhs rhs: AnExpression withRhs rhs: Categories.Expression
) { ) {
self.type = tipe self.type = tipe
self.left = lhs self.left = lhs
@@ -413,30 +415,30 @@ public struct CST {
} }
} }
public struct ArrayAccessExpression: AnExpression { public struct ArrayAccessExpression: Categories.Expression {
public let indexor: AnExpression public let indexor: Categories.Expression
public let name: AnExpression public let name: Categories.Expression
public init( public init(
withName name: AnExpression, withName name: Categories.Expression,
withIndexor indexor: AnExpression withIndexor indexor: Categories.Expression
) { ) {
self.name = name self.name = name
self.indexor = indexor self.indexor = indexor
} }
} }
public struct FieldAccessExpression: AnExpression { public struct FieldAccessExpression: Categories.Expression {
public let field: Identifier 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.strct = strct
self.field = field self.field = field
} }
} }
public struct FunctionCall: AnExpression { public struct FunctionCall: Categories.Expression {
public let callee: Identifier public let callee: Identifier
public let arguments: ArgumentList public let arguments: ArgumentList
@@ -450,12 +452,12 @@ public struct CST {
public struct LocalElement {} public struct LocalElement {}
public struct ParserAssignmentStatement: Statement { public struct ParserAssignmentStatement: Categories.Statement {
public let lvalue: AnExpression public let lvalue: Categories.Expression
public let value: AnExpression public let value: Categories.Expression
public init( public init(
withLValue lvalue: AnExpression, withValue value: AnExpression withLValue lvalue: Categories.Expression, withValue value: Categories.Expression
) { ) {
self.lvalue = lvalue self.lvalue = lvalue
self.value = value self.value = value
@@ -491,7 +493,7 @@ public struct CST {
/// Only defined to define Compilable extension. /// Only defined to define Compilable extension.
public struct TransitionStatement {} public struct TransitionStatement {}
public class ParserStateDirectTransition: ParserState, AnState { public class ParserStateDirectTransition: ParserState, Categories.State {
public let next_state_identifier: Identifier? public let next_state_identifier: Identifier?
public init( public init(
@@ -503,7 +505,7 @@ public struct CST {
} }
} }
public class ParserStateNoTransition: ParserState, AnState { public class ParserStateNoTransition: ParserState, Categories.State {
/// Construct a ParserState /// Construct a ParserState
public init( public init(
name: Identifier, withStatements stmts: CST.Statements? = .none 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 public let te: SelectExpression
@@ -526,17 +528,17 @@ public struct CST {
} }
public struct ParserStates { public struct ParserStates {
public var states: [AnState] = Array() public var states: [Categories.State] = Array()
public func count() -> Int { public func count() -> Int {
return states.count return states.count
} }
public init(_ states: [AnState] = Array()) { public init(_ states: [Categories.State] = Array()) {
self.states = states self.states = states
} }
public func append(state: AnState) -> ParserStates { public func append(state: Categories.State) -> ParserStates {
var new_states = self.states var new_states = self.states
new_states.append(state) new_states.append(state)
return ParserStates(new_states) return ParserStates(new_states)
@@ -546,7 +548,7 @@ public struct CST {
/// A P4 Parser /// A P4 Parser
/// ///
/// Note: A Parser is a type /// Note: A Parser is a type
public struct Parser: AnDeclaration { public struct Parser: Categories.Declaration {
public var states: ParserStates public var states: ParserStates
public var name: Identifier public var name: Identifier
@@ -632,9 +634,9 @@ public struct CST {
public struct Argument { public struct Argument {
public let index: Int 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.argument = argument
self.index = index self.index = index
} }
@@ -646,6 +648,8 @@ public struct CST {
self.statements = stmts self.statements = stmts
} }
} }
public struct Statement {}
} }
public struct CSTCompilerContext { public struct CSTCompilerContext {
+30 -30
View File
@@ -21,10 +21,10 @@ import TreeSitterExtensions
import TreeSitterP4 import TreeSitterP4
extension CST.Declaration: Parsable { extension CST.Declaration: Parsable {
public typealias C = CST.AnDeclaration public typealias C = CST.Categories.Declaration
public static func Parse( public static func Parse(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.AnDeclaration> { ) -> Result<CST.Categories.Declaration> {
// Be kind to our user -- if we are at a declaration node, dive into it! // Be kind to our user -- if we are at a declaration node, dive into it!
let declaration_node = let declaration_node =
@@ -34,7 +34,7 @@ extension CST.Declaration: Parsable {
node node
} }
let declaration_compilers: [String: any Parsable<CST.AnDeclaration>.Type] = [ let declaration_compilers: [String: any Parsable<CST.Categories.Declaration>.Type] = [
"function_declaration": CST.FunctionDeclaration.self, "function_declaration": CST.FunctionDeclaration.self,
"control_declaration": CST.Control.self, "control_declaration": CST.Control.self,
//"type_declaration": AST.P4Struct.self, //"type_declaration": AST.P4Struct.self,
@@ -61,12 +61,12 @@ extension CST.Declaration: Parsable {
extension CST.Declaration: ParsableStatement {} extension CST.Declaration: ParsableStatement {}
extension CST.FunctionDeclaration: Parsable { extension CST.FunctionDeclaration: Parsable {
public typealias C = CST.AnDeclaration public typealias C = CST.Categories.Declaration
public static func Parse( public static func Parse(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Common.Result<CST.AnDeclaration> { ) -> Common.Result<CST.Categories.Declaration> {
let function_declaration_node = node let function_declaration_node = node
#RequireNodeType<Node, CST.AnDeclaration>( #RequireNodeType<Node, CST.Categories.Declaration>(
node: function_declaration_node, type: "function_declaration", node: function_declaration_node, type: "function_declaration",
nice_type_name: "Function Declaration") nice_type_name: "Function Declaration")
@@ -75,7 +75,7 @@ extension CST.FunctionDeclaration: Parsable {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: function_declaration_node.toSourceLocation(), sourceLocation: function_declaration_node.toSourceLocation(),
withError: "Missing function declaration component"))) withError: "Missing function declaration component")))
@@ -88,7 +88,7 @@ extension CST.FunctionDeclaration: Parsable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: function_declaration_node.toSourceLocation(), sourceLocation: function_declaration_node.toSourceLocation(),
withError: "Missing function declaration component"))) withError: "Missing function declaration component")))
@@ -102,7 +102,7 @@ extension CST.FunctionDeclaration: Parsable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: function_declaration_node.toSourceLocation(), sourceLocation: function_declaration_node.toSourceLocation(),
withError: "Missing function declaration component"))) withError: "Missing function declaration component")))
@@ -227,19 +227,19 @@ extension CST.StructDeclaration: Parsable {
} }
extension CST.Parser: Parsable { extension CST.Parser: Parsable {
public typealias C = CST.AnDeclaration public typealias C = CST.Categories.Declaration
public static func Parse( public static func Parse(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.AnDeclaration> { ) -> Result<CST.Categories.Declaration> {
let parser_node = node let parser_node = node
#RequireNodeType<Node, Result<CST.AnDeclaration>>( #RequireNodeType<Node, Result<CST.Categories.Declaration>>(
node: parser_node, type: "parserDeclaration", nice_type_name: "parser declaration") node: parser_node, type: "parserDeclaration", nice_type_name: "parser declaration")
var walker = Walker(node: parser_node) var walker = Walker(node: parser_node)
var current_node: Node? = .none var current_node: Node? = .none
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: parser_node.toSourceLocation(), sourceLocation: parser_node.toSourceLocation(),
withError: "Missing elements of parser declaration"))) withError: "Missing elements of parser declaration")))
@@ -263,7 +263,7 @@ extension CST.Parser: Parsable {
#MustOr( #MustOr(
result: type_node_child, thing: type_node_walker.getNext(), result: type_node_child, thing: type_node_walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: parser_node.toSourceLocation(), sourceLocation: parser_node.toSourceLocation(),
withError: "Missing elements of parser type in parser declaration"))) withError: "Missing elements of parser type in parser declaration")))
@@ -279,7 +279,7 @@ extension CST.Parser: Parsable {
type_node_walker.next() type_node_walker.next()
#MustOr( #MustOr(
result: type_node_child, thing: type_node_walker.getNext(), result: type_node_child, thing: type_node_walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: type_node_child!.toSourceLocation(), sourceLocation: type_node_child!.toSourceLocation(),
withError: "Missing name in parser type declaration"))) withError: "Missing name in parser type declaration")))
@@ -293,7 +293,7 @@ extension CST.Parser: Parsable {
type_node_walker.next() type_node_walker.next()
#MustOr( #MustOr(
result: type_node_child, thing: type_node_walker.getNext(), result: type_node_child, thing: type_node_walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: type_node_child!.toSourceLocation(), sourceLocation: type_node_child!.toSourceLocation(),
withError: "Missing parser parameters"))) withError: "Missing parser parameters")))
@@ -309,7 +309,7 @@ extension CST.Parser: Parsable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: parser_node.toSourceLocation(), sourceLocation: parser_node.toSourceLocation(),
withError: "Missing parser declaration component"))) withError: "Missing parser declaration component")))
@@ -317,7 +317,7 @@ extension CST.Parser: Parsable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: parser_node.toSourceLocation(), sourceLocation: parser_node.toSourceLocation(),
withError: "Missing elements of parser declaration"))) withError: "Missing elements of parser declaration")))
@@ -332,7 +332,7 @@ extension CST.Parser: Parsable {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: parser_node.toSourceLocation(), sourceLocation: parser_node.toSourceLocation(),
withError: "Missing body of parser declaration"))) withError: "Missing body of parser declaration")))
@@ -379,12 +379,12 @@ extension CST.Parser: Parsable {
} }
extension CST.Control: Parsable { extension CST.Control: Parsable {
public typealias C = CST.AnDeclaration public typealias C = CST.Categories.Declaration
public static func Parse( public static func Parse(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Common.Result<CST.AnDeclaration> { ) -> Common.Result<CST.Categories.Declaration> {
#RequireNodeType<Node, Result<CST.AnDeclaration>>( #RequireNodeType<Node, Result<CST.Categories.Declaration>>(
node: node, type: "control_declaration", nice_type_name: "control declaration") node: node, type: "control_declaration", nice_type_name: "control declaration")
var walker = Walker(node: node) var walker = Walker(node: node)
@@ -393,7 +393,7 @@ extension CST.Control: Parsable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing control declaration component"))) withError: "Missing control declaration component")))
@@ -409,7 +409,7 @@ extension CST.Control: Parsable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing control declaration component"))) withError: "Missing control declaration component")))
@@ -426,7 +426,7 @@ extension CST.Control: Parsable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error( or: Result<CST.Categories.Declaration>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing control declaration component"))) withError: "Missing control declaration component")))
@@ -873,10 +873,10 @@ extension CST.Table: Parsable {
} }
extension CST.ExternDeclaration: Parsable { extension CST.ExternDeclaration: Parsable {
public typealias C = CST.AnDeclaration public typealias C = CST.Categories.Declaration
public static func Parse( public static func Parse(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Result<CST.AnDeclaration> { ) -> Result<CST.Categories.Declaration> {
let extern_declaration_node = node let extern_declaration_node = node
#RequireNodeType<Node, CST.ExternDeclaration>( #RequireNodeType<Node, CST.ExternDeclaration>(
node: extern_declaration_node, type: "extern_declaration", node: extern_declaration_node, type: "extern_declaration",
@@ -1223,12 +1223,12 @@ extension CST.ArgumentList: Parsable {
} }
extension CST.Argument: Parsable { extension CST.Argument: Parsable {
public typealias C = CST.AnExpression public typealias C = CST.Categories.Expression
public static func Parse( public static func Parse(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Common.Result<CST.AnExpression> { ) -> Common.Result<CST.Categories.Expression> {
let argument_node = node let argument_node = node
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: argument_node, type: "argument", nice_type_name: "argument") node: argument_node, type: "argument", nice_type_name: "argument")
let expression_node = node.child(at: 0)! let expression_node = node.child(at: 0)!
+41 -41
View File
@@ -22,9 +22,9 @@ import TreeSitterP4
extension CST.Identifier: ParsableExpression { extension CST.Identifier: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: node, type: "identifier", nice_type_name: "Identifier") node: node, type: "identifier", nice_type_name: "Identifier")
/// TODO: If there is a value here, then we can make this a compile-time constant! /// 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 { extension P4BooleanValue: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
let node = node.child(at: 0)! let node = node.child(at: 0)!
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: node, type: "booleanLiteralExpression", nice_type_name: "Boolean Literal Expression") node: node, type: "booleanLiteralExpression", nice_type_name: "Boolean Literal Expression")
if node.text == "false" { if node.text == "false" {
@@ -56,10 +56,10 @@ extension P4BooleanValue: ParsableExpression {
extension P4IntValue: ParsableExpression { extension P4IntValue: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
let node = node.child(at: 0)! let node = node.child(at: 0)!
#RequireNodesType<Node, CST.AnExpression>( #RequireNodesType<Node, CST.Categories.Expression>(
nodes: node, type: ["integer", "integer_elaborated"], nodes: node, type: ["integer", "integer_elaborated"],
nice_type_names: ["Integer", "Elaborated Integer"]) nice_type_names: ["Integer", "Elaborated Integer"])
@@ -106,9 +106,9 @@ extension P4IntValue: ParsableExpression {
extension P4StringValue: ParsableExpression { extension P4StringValue: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: SwiftTreeSitter.Node, withContext scopes: CSTCompilerContext node: SwiftTreeSitter.Node, withContext scopes: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
let node = node.child(at: 0)! let node = node.child(at: 0)!
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: node, type: "string_literal", nice_type_name: "String Literal") node: node, type: "string_literal", nice_type_name: "String Literal")
return .Ok(CST.Literal(P4Value(P4StringValue(withValue: node.text!)))) return .Ok(CST.Literal(P4Value(P4StringValue(withValue: node.text!))))
} }
@@ -117,12 +117,12 @@ extension P4StringValue: ParsableExpression {
extension CST.Expression: Parsable { extension CST.Expression: Parsable {
public static func Parse( public static func Parse(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: node, type: "expression", nice_type_name: "expression") node: node, type: "expression", nice_type_name: "expression")
let expression_node = node.child(at: 0)! let expression_node = node.child(at: 0)!
#RequireNodesType<Node, CST.AnExpression>( #RequireNodesType<Node, CST.Categories.Expression>(
nodes: expression_node, type: ["grouped_expression", "simple_expression"], nodes: expression_node, type: ["grouped_expression", "simple_expression"],
nice_type_names: ["grouped expression", "simple expression"]) nice_type_names: ["grouped expression", "simple expression"])
@@ -154,10 +154,10 @@ extension CST.Expression: Parsable {
extension CST.KeysetExpression: ParsableExpression { extension CST.KeysetExpression: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
let keyset_expression_node = node.child(at: 0)! let keyset_expression_node = node.child(at: 0)!
#RequireNodesType<Node, CST.AnExpression>( #RequireNodesType<Node, CST.Categories.Expression>(
nodes: keyset_expression_node, type: ["expression", "default_keyset"], nodes: keyset_expression_node, type: ["expression", "default_keyset"],
nice_type_names: ["expression", "default keyset"]) nice_type_names: ["expression", "default keyset"])
@@ -180,7 +180,7 @@ extension CST.KeysetExpression: ParsableExpression {
extension CST.SelectExpression: ParsableExpression { extension CST.SelectExpression: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
#RequireNodeType<Node, (CST.SelectExpression, CSTCompilerContext)>( #RequireNodeType<Node, (CST.SelectExpression, CSTCompilerContext)>(
node: node, type: "selectExpression", nice_type_name: "parser select expression") node: node, type: "selectExpression", nice_type_name: "parser select expression")
@@ -242,9 +242,9 @@ extension CST.SelectExpression: ParsableExpression {
extension CST.SelectCaseExpression: ParsableExpression { extension CST.SelectCaseExpression: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: node, type: "selectCase", nice_type_name: "Select Case") node: node, type: "selectCase", nice_type_name: "Select Case")
guard let keysetexpression_node = node.child(at: 0), guard let keysetexpression_node = node.child(at: 0),
@@ -283,10 +283,10 @@ extension CST.SelectCaseExpression: ParsableExpression {
extension CST.BinaryOperatorExpression: ParsableExpression { extension CST.BinaryOperatorExpression: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
let expression = node.child(at: 0)! let expression = node.child(at: 0)!
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: expression, type: "binaryOperatorExpression", node: expression, type: "binaryOperatorExpression",
nice_type_name: "Binary Operator Expression") nice_type_name: "Binary Operator Expression")
let binary_operator_expression_node = expression.child(at: 0)! let binary_operator_expression_node = expression.child(at: 0)!
@@ -295,20 +295,20 @@ extension CST.BinaryOperatorExpression: ParsableExpression {
var current_node: Node? = .none var current_node: Node? = .none
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Malformed binary operator expression" sourceLocation: node.toSourceLocation(), withError: "Malformed binary operator expression"
))) )))
/// TODO: This macro cannot handle new lines in the arrays /// TODO: This macro cannot handle new lines in the arrays
// swift-format-ignore // swift-format-ignore
#RequireNodesType<Node, CST.AnExpression>( #RequireNodesType<Node, CST.Categories.Expression>(
nodes: binary_operator_expression_node, nodes: binary_operator_expression_node,
type: ["binaryEqualOperatorExpression", "binaryLessThanOperatorExpression", "binaryLessThanEqualOperatorExpression", "binaryGreaterThanOperatorExpression", "binaryGreaterThanEqualOperatorExpression", "binaryAndOperatorExpression", "binaryOrOperatorExpression", "binaryAddOperatorExpression", "binarySubtractOperatorExpression", "binaryMultiplyOperatorExpression", "binaryDivideOperatorExpression"], 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"]) 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( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing LHS for binary operator expression"))) withError: "Missing LHS for binary operator expression")))
@@ -318,7 +318,7 @@ extension CST.BinaryOperatorExpression: ParsableExpression {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing binary operator for binary operator expression"))) withError: "Missing binary operator for binary operator expression")))
@@ -326,7 +326,7 @@ extension CST.BinaryOperatorExpression: ParsableExpression {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing RHS for binary operator expression"))) withError: "Missing RHS for binary operator expression")))
@@ -407,10 +407,10 @@ extension CST.BinaryOperatorExpression: ParsableExpression {
extension CST.ArrayAccessExpression: ParsableExpression { extension CST.ArrayAccessExpression: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
let expression = node.child(at: 0)! let expression = node.child(at: 0)!
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: expression, type: "arrayAccessExpression", nice_type_name: "Array Access Expression") node: expression, type: "arrayAccessExpression", nice_type_name: "Array Access Expression")
let array_access_expression_node = expression let array_access_expression_node = expression
@@ -419,11 +419,11 @@ extension CST.ArrayAccessExpression: ParsableExpression {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Malformed array access expression"))) sourceLocation: node.toSourceLocation(), withError: "Malformed array access expression")))
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: current_node!, type: "expression", node: current_node!, type: "expression",
nice_type_name: "array identifier expression") nice_type_name: "array identifier expression")
let array_access_identifier_node = current_node! let array_access_identifier_node = current_node!
@@ -431,7 +431,7 @@ extension CST.ArrayAccessExpression: ParsableExpression {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing [ for array access expression"))) withError: "Missing [ for array access expression")))
@@ -440,12 +440,12 @@ extension CST.ArrayAccessExpression: ParsableExpression {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing indexor expression for array access expression"))) withError: "Missing indexor expression for array access expression")))
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: current_node!, type: "expression", node: current_node!, type: "expression",
nice_type_name: "array indexor expression") nice_type_name: "array indexor expression")
@@ -472,10 +472,10 @@ extension CST.ArrayAccessExpression: ParsableExpression {
extension CST.FieldAccessExpression: ParsableExpression { extension CST.FieldAccessExpression: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
let expression = node.child(at: 0)! let expression = node.child(at: 0)!
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: expression, type: "fieldAccessExpression", nice_type_name: "Array Access Expression") node: expression, type: "fieldAccessExpression", nice_type_name: "Array Access Expression")
let field_access_expression_node = expression let field_access_expression_node = expression
@@ -485,11 +485,11 @@ extension CST.FieldAccessExpression: ParsableExpression {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Malformed field access expression"))) sourceLocation: node.toSourceLocation(), withError: "Malformed field access expression")))
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: current_node!, type: "expression", node: current_node!, type: "expression",
nice_type_name: "struct identifier expression") nice_type_name: "struct identifier expression")
let struct_identifier_node = current_node! let struct_identifier_node = current_node!
@@ -497,7 +497,7 @@ extension CST.FieldAccessExpression: ParsableExpression {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing . for field access expression"))) withError: "Missing . for field access expression")))
@@ -505,12 +505,12 @@ extension CST.FieldAccessExpression: ParsableExpression {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing field name for field access expression"))) withError: "Missing field name for field access expression")))
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: current_node!, type: "identifier", node: current_node!, type: "identifier",
nice_type_name: "field name") nice_type_name: "field name")
@@ -539,10 +539,10 @@ extension CST.FieldAccessExpression: ParsableExpression {
extension CST.FunctionCall: ParsableExpression { extension CST.FunctionCall: ParsableExpression {
public static func ParseExpression( public static func ParseExpression(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> { ) -> Result<CST.Categories.Expression> {
let expression = node.child(at: 0)! let expression = node.child(at: 0)!
#RequireNodeType<Node, CST.AnExpression>( #RequireNodeType<Node, CST.Categories.Expression>(
node: expression, type: "function_call", nice_type_name: "Function Call") node: expression, type: "function_call", nice_type_name: "Function Call")
var walker = Walker(node: expression) var walker = Walker(node: expression)
@@ -550,7 +550,7 @@ extension CST.FunctionCall: ParsableExpression {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing function call component"))) sourceLocation: node.toSourceLocation(), withError: "Missing function call component")))
@@ -563,7 +563,7 @@ extension CST.FunctionCall: ParsableExpression {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error( or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing function call component"))) sourceLocation: node.toSourceLocation(), withError: "Missing function call component")))
+8 -8
View File
@@ -21,10 +21,10 @@ import TreeSitterExtensions
import TreeSitterP4 import TreeSitterP4
extension CST.LocalElements: Parsable { extension CST.LocalElements: Parsable {
public typealias C = CST.Statement public typealias C = CST.Categories.Statement
public static func Parse( public static func Parse(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.Statement> { ) -> Result<CST.Categories.Statement> {
let localElementsParsers: [String: ParsableStatement.Type] = [ let localElementsParsers: [String: ParsableStatement.Type] = [
"variableDeclaration": CST.VariableDeclarationStatement.self "variableDeclaration": CST.VariableDeclarationStatement.self
] ]
@@ -46,10 +46,10 @@ extension CST.LocalElements: Parsable {
} }
extension CST.ParserState: Parsable { extension CST.ParserState: Parsable {
public typealias C = CST.AnState public typealias C = CST.Categories.State
public static func Parse( public static func Parse(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.AnState> { ) -> Result<CST.Categories.State> {
var walker = Walker(node: node) var walker = Walker(node: node)
var current_node: Node? = .none var current_node: Node? = .none
@@ -65,7 +65,7 @@ extension CST.ParserState: Parsable {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnState>.Error( or: Result<CST.Categories.State>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing elements in parser state declaration"))) withError: "Missing elements in parser state declaration")))
@@ -83,7 +83,7 @@ extension CST.ParserState: Parsable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnState>.Error( or: Result<CST.Categories.State>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing elements in parser state declaration"))) withError: "Missing elements in parser state declaration")))
@@ -99,7 +99,7 @@ extension CST.ParserState: Parsable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnState>.Error( or: Result<CST.Categories.State>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration") sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration")
)) ))
@@ -130,7 +130,7 @@ extension CST.ParserState: Parsable {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<CST.AnState>.Error( or: Result<CST.Categories.State>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing transition statement of state declaration"))) withError: "Missing transition statement of state declaration")))
+1 -1
View File
@@ -24,7 +24,7 @@ extension CST.Program: Parsable {
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Common.Result<CST.Program> { ) -> Common.Result<CST.Program> {
var statements: [CST.Statement] = Array() var statements: [CST.Categories.Statement] = Array()
var errors: (any Errorable)? = .none var errors: (any Errorable)? = .none
@@ -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 <https://www.gnu.org/licenses/>.
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<CST.Statement> {
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)"))
}
}
}
+49 -7
View File
@@ -132,7 +132,7 @@ extension CST.ConditionalStatement: Parsable {
} }
guard guard
case .Ok(let thenns) = SpecialParsers.Statement.Parse( case .Ok(let thenns) = CST.Statement.Parse(
node: thens, withContext: context) node: thens, withContext: context)
else { else {
return Result.Error( return Result.Error(
@@ -141,10 +141,10 @@ extension CST.ConditionalStatement: Parsable {
"Could not parse the then block in a conditional statement")) "Could not parse the then block in a conditional statement"))
} }
let optional_elss: Result<CST.Statement>? = let optional_elss: Result<CST.Categories.Statement>? =
if let elss = node.child(at: 6) { if let elss = node.child(at: 6) {
.some( .some(
SpecialParsers.Statement.Parse( CST.Statement.Parse(
node: elss, withContext: context)) node: elss, withContext: context))
} else { } else {
.none .none
@@ -215,7 +215,7 @@ extension CST.VariableDeclarationStatement: Parsable {
Error(withMessage: "Could not parse a P4 type from \(typeref.text!)")) 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 there is an initializer, it must be an expression.
if let initializer_expression = maybe_rvalue { if let initializer_expression = maybe_rvalue {
@@ -431,10 +431,10 @@ extension CST.Statements: Parsable {
} }
var errors: (any Errorable)? = .none var errors: (any Errorable)? = .none
var parsed_s: [CST.Statement] = Array() var parsed_s: [CST.Categories.Statement] = Array()
node.enumerateNamedChildren { node in node.enumerateNamedChildren { node in
switch SpecialParsers.Statement.Parse( switch CST.Statement.Parse(
node: node, withContext: context) node: node, withContext: context)
{ {
case .Ok(let parsed_statement): case .Ok(let parsed_statement):
@@ -460,7 +460,7 @@ extension CST.Statements: Parsable {
extension CST.TransitionStatement: Parsable { extension CST.TransitionStatement: Parsable {
public static func Parse( public static func Parse(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.AnState> { ) -> Result<CST.Categories.State> {
guard let state_identifier = context.lexical_context_name else { guard let state_identifier = context.lexical_context_name else {
return .Error( 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<CST.Categories.Statement> {
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)"))
}
}
}
+3 -3
View File
@@ -29,7 +29,7 @@ public protocol CompilableValue {
public protocol MaybeParsableType { public protocol MaybeParsableType {
static func MaybeParseType( static func MaybeParseType(
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
) -> Result<CST.Tipe?> ) -> Result<CST.Tipe>
} }
public protocol ParsableType { public protocol ParsableType {
@@ -40,7 +40,7 @@ public protocol ParsableType {
public protocol ParsableExpression { public protocol ParsableExpression {
static func ParseExpression( static func ParseExpression(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.AnExpression> ) -> Result<CST.Categories.Expression>
} }
public protocol Parsable<C> { public protocol Parsable<C> {
@@ -53,7 +53,7 @@ public protocol Parsable<C> {
public protocol ParsableStatement { public protocol ParsableStatement {
static func ParseStatement( static func ParseStatement(
node: Node, withContext context: CSTCompilerContext node: Node, withContext context: CSTCompilerContext
) -> Result<CST.Statement> ) -> Result<CST.Categories.Statement>
} }
public protocol CSTVisitor<T> { public protocol CSTVisitor<T> {
+3 -3
View File
@@ -21,7 +21,7 @@ public struct CSTVisitorDriver {
public init() {} public init() {}
public func visit<T>( public func visit<T>(
expression: any CST.AnExpression, visitor: any CSTVisitor<T>, context: T expression: any CST.Categories.Expression, visitor: any CSTVisitor<T>, context: T
) -> Result<T> { ) -> Result<T> {
return switch expression { return switch expression {
case let e as CST.BinaryOperatorExpression: case let e as CST.BinaryOperatorExpression:
@@ -32,7 +32,7 @@ public struct CSTVisitorDriver {
} }
public func visit<T>( public func visit<T>(
state: any CST.AnState, visitor: any CSTVisitor<T>, context: T state: any CST.Categories.State, visitor: any CSTVisitor<T>, context: T
) -> Result<T> { ) -> Result<T> {
return switch state { return switch state {
case let s as CST.ParserStateDirectTransition: case let s as CST.ParserStateDirectTransition:
@@ -46,7 +46,7 @@ public struct CSTVisitorDriver {
} }
public func visit<T>( public func visit<T>(
statement: any CST.Statement, visitor: any CSTVisitor<T>, context: T statement: any CST.Categories.Statement, visitor: any CSTVisitor<T>, context: T
) -> Result<T> { ) -> Result<T> {
return switch statement { return switch statement {
case let s as CST.Parser: visitor.visit(node: s, driver: self, context: context) case let s as CST.Parser: visitor.visit(node: s, driver: self, context: context)