@@ -16,17 +16,15 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import Common
|
||||
import P4Lang
|
||||
import P4Runtime
|
||||
import SwiftTreeSitter
|
||||
import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
extension Statement: Compilable {
|
||||
public typealias C = P4Statement
|
||||
extension AST.Statement: Compilable {
|
||||
public typealias C = AST.AnStatement
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Result<P4Statement> {
|
||||
node: SwiftTreeSitter.Node, withContext context: ASTCompilerContext
|
||||
) -> Result<AST.AnStatement> {
|
||||
|
||||
if node.nodeType != "parserStatement" && node.nodeType != "statement" {
|
||||
return Result.Error(
|
||||
@@ -38,11 +36,11 @@ extension Statement: Compilable {
|
||||
let statement = node.child(at: 0)!
|
||||
|
||||
let statementParsers: [String: CompilableStatement.Type] = [
|
||||
"assignmentStatement": ParserAssignmentStatement.self,
|
||||
"expressionStatement": ExpressionStatement.self,
|
||||
"variableDeclaration": VariableDeclarationStatement.self,
|
||||
"conditionalStatement": ConditionalStatement.self, "blockStatement": BlockStatement.self,
|
||||
"return_statement": ReturnStatement.self,
|
||||
"assignmentStatement": AST.ParserAssignmentStatement.self,
|
||||
"expressionStatement": AST.ExpressionStatement.self,
|
||||
"variableDeclaration": AST.VariableDeclarationStatement.self,
|
||||
"conditionalStatement": AST.ConditionalStatement.self, "blockStatement": AST.BlockStatement.self,
|
||||
"return_statement": AST.ReturnStatement.self,
|
||||
]
|
||||
guard let parser = statementParsers[statement.nodeType ?? ""] else {
|
||||
return Result.Error(
|
||||
@@ -63,13 +61,13 @@ extension Statement: Compilable {
|
||||
}
|
||||
}
|
||||
|
||||
extension LocalElements: Compilable {
|
||||
public typealias C = P4Statement
|
||||
extension AST.LocalElements: Compilable {
|
||||
public typealias C = AST.AnStatement
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<P4Statement> {
|
||||
node: Node, withContext context: ASTCompilerContext
|
||||
) -> Result<AST.AnStatement> {
|
||||
let localElementsParsers: [String: CompilableStatement.Type] = [
|
||||
"variableDeclaration": VariableDeclarationStatement.self
|
||||
"variableDeclaration": AST.VariableDeclarationStatement.self
|
||||
]
|
||||
|
||||
guard let parser = localElementsParsers[node.nodeType ?? ""] else {
|
||||
@@ -88,11 +86,11 @@ extension LocalElements: Compilable {
|
||||
}
|
||||
}
|
||||
|
||||
extension ParserState: Compilable {
|
||||
public typealias C = ParserState
|
||||
extension AST.ParserState: Compilable {
|
||||
public typealias C = AST.AnState
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<ParserState> {
|
||||
node: Node, withContext context: ASTCompilerContext
|
||||
) -> Result<AST.AnState> {
|
||||
var walker = Walker(node: node)
|
||||
|
||||
var current_node: Node? = .none
|
||||
@@ -108,7 +106,7 @@ extension ParserState: Compilable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<ParserState>.Error(
|
||||
or: Result<AST.AnState>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing elements in parser state declaration")))
|
||||
@@ -126,12 +124,12 @@ extension ParserState: Compilable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<ParserState>.Error(
|
||||
or: Result<AST.AnState>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing elements in parser state declaration")))
|
||||
|
||||
let maybe_state_identifier = Identifier.Compile(
|
||||
let maybe_state_identifier = AST.Identifier.CompileExpression(
|
||||
node: current_node!, withContext: context)
|
||||
guard case Result.Ok(let state_identifier) = maybe_state_identifier else {
|
||||
return Result.Error(maybe_state_identifier.error()!)
|
||||
@@ -142,13 +140,13 @@ extension ParserState: Compilable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<ParserState>.Error(
|
||||
or: Result<AST.AnState>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration")
|
||||
))
|
||||
|
||||
var errors: (any Errorable)? = .none
|
||||
var parsed_s: [P4Statement] = Array()
|
||||
var parsed_s: [AST.AnStatement] = Array()
|
||||
|
||||
if current_node!.nodeType == "parserStatements" {
|
||||
switch SpecialCompilers.Statements.Compile(
|
||||
@@ -173,14 +171,13 @@ extension ParserState: Compilable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<ParserState>.Error(
|
||||
or: Result<AST.AnState>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing transition statement of state declaration")))
|
||||
|
||||
let updated_context = SpecialCompilers.Statements.effect(statements: parsed_s, context: context)
|
||||
.update(newLexicalContextName: state_identifier).update(newLexicalContextStatements: parsed_s)
|
||||
let updated_context = context.update(withContextName: (state_identifier as! AST.Identifier)).update(withContextStatements: parsed_s)
|
||||
|
||||
return TransitionStatement.Compile(node: current_node!, withContext: updated_context)
|
||||
return AST.TransitionStatement.Compile(node: current_node!, withContext: updated_context)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user