compiler: Refactor Language Element Tags
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -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)"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user