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
+30 -30
View File
@@ -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<CST.AnDeclaration> {
) -> Result<CST.Categories.Declaration> {
// 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<CST.AnDeclaration>.Type] = [
let declaration_compilers: [String: any Parsable<CST.Categories.Declaration>.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<CST.AnDeclaration> {
) -> Common.Result<CST.Categories.Declaration> {
let function_declaration_node = node
#RequireNodeType<Node, CST.AnDeclaration>(
#RequireNodeType<Node, CST.Categories.Declaration>(
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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration> {
) -> Result<CST.Categories.Declaration> {
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")
var walker = Walker(node: parser_node)
var current_node: Node? = .none
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<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")
var walker = Walker(node: node)
@@ -393,7 +393,7 @@ extension CST.Control: Parsable {
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration>.Error(
or: Result<CST.Categories.Declaration>.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<CST.AnDeclaration> {
) -> Result<CST.Categories.Declaration> {
let extern_declaration_node = node
#RequireNodeType<Node, CST.ExternDeclaration>(
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<CST.AnExpression> {
) -> Common.Result<CST.Categories.Expression> {
let argument_node = node
#RequireNodeType<Node, CST.AnExpression>(
#RequireNodeType<Node, CST.Categories.Expression>(
node: argument_node, type: "argument", nice_type_name: "argument")
let expression_node = node.child(at: 0)!
+41 -41
View File
@@ -22,9 +22,9 @@ import TreeSitterP4
extension CST.Identifier: ParsableExpression {
public static func ParseExpression(
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")
/// 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<CST.AnExpression> {
) -> Result<CST.Categories.Expression> {
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")
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<CST.AnExpression> {
) -> Result<CST.Categories.Expression> {
let node = node.child(at: 0)!
#RequireNodesType<Node, CST.AnExpression>(
#RequireNodesType<Node, CST.Categories.Expression>(
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<CST.AnExpression> {
) -> Result<CST.Categories.Expression> {
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")
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<CST.AnExpression> {
#RequireNodeType<Node, CST.AnExpression>(
) -> Result<CST.Categories.Expression> {
#RequireNodeType<Node, CST.Categories.Expression>(
node: node, type: "expression", nice_type_name: "expression")
let expression_node = node.child(at: 0)!
#RequireNodesType<Node, CST.AnExpression>(
#RequireNodesType<Node, CST.Categories.Expression>(
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<CST.AnExpression> {
) -> Result<CST.Categories.Expression> {
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"],
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<CST.AnExpression> {
) -> Result<CST.Categories.Expression> {
#RequireNodeType<Node, (CST.SelectExpression, CSTCompilerContext)>(
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<CST.AnExpression> {
) -> Result<CST.Categories.Expression> {
#RequireNodeType<Node, CST.AnExpression>(
#RequireNodeType<Node, CST.Categories.Expression>(
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<CST.AnExpression> {
) -> Result<CST.Categories.Expression> {
let expression = node.child(at: 0)!
#RequireNodeType<Node, CST.AnExpression>(
#RequireNodeType<Node, CST.Categories.Expression>(
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<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Malformed binary operator expression"
)))
/// TODO: This macro cannot handle new lines in the arrays
// swift-format-ignore
#RequireNodesType<Node, CST.AnExpression>(
#RequireNodesType<Node, CST.Categories.Expression>(
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<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.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<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.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<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.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<CST.AnExpression> {
) -> Result<CST.Categories.Expression> {
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")
let array_access_expression_node = expression
@@ -419,11 +419,11 @@ extension CST.ArrayAccessExpression: ParsableExpression {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Malformed array access expression")))
#RequireNodeType<Node, CST.AnExpression>(
#RequireNodeType<Node, CST.Categories.Expression>(
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<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.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<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing indexor expression for array access expression")))
#RequireNodeType<Node, CST.AnExpression>(
#RequireNodeType<Node, CST.Categories.Expression>(
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<CST.AnExpression> {
) -> Result<CST.Categories.Expression> {
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")
let field_access_expression_node = expression
@@ -485,11 +485,11 @@ extension CST.FieldAccessExpression: ParsableExpression {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Malformed field access expression")))
#RequireNodeType<Node, CST.AnExpression>(
#RequireNodeType<Node, CST.Categories.Expression>(
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<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.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<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing field name for field access expression")))
#RequireNodeType<Node, CST.AnExpression>(
#RequireNodeType<Node, CST.Categories.Expression>(
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<CST.AnExpression> {
) -> Result<CST.Categories.Expression> {
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")
var walker = Walker(node: expression)
@@ -550,7 +550,7 @@ extension CST.FunctionCall: ParsableExpression {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.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<CST.AnExpression>.Error(
or: Result<CST.Categories.Expression>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing function call component")))
+8 -8
View File
@@ -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<CST.Statement> {
) -> Result<CST.Categories.Statement> {
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<CST.AnState> {
) -> Result<CST.Categories.State> {
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<CST.AnState>.Error(
or: Result<CST.Categories.State>.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<CST.AnState>.Error(
or: Result<CST.Categories.State>.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<CST.AnState>.Error(
or: Result<CST.Categories.State>.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<CST.AnState>.Error(
or: Result<CST.Categories.State>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing transition statement of state declaration")))
+1 -1
View File
@@ -24,7 +24,7 @@ extension CST.Program: Parsable {
node: Node, withContext context: CSTCompilerContext
) -> Common.Result<CST.Program> {
var statements: [CST.Statement] = Array()
var statements: [CST.Categories.Statement] = Array()
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
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<CST.Statement>? =
let optional_elss: Result<CST.Categories.Statement>? =
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<CST.AnState> {
) -> Result<CST.Categories.State> {
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<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)"))
}
}
}