// p4rse, Copyright 2026, Will Hawkins // // This file is part of p4rse. // // This file is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . import Common import SwiftTreeSitter import TreeSitterExtensions import TreeSitterP4 extension AST.BlockStatement: Compilable { public typealias C = AST.BlockStatement public static func Compile( node: Node, withContext context: ASTCompilerContext ) -> Result { /* #RequireNodeType( node: node, type: "blockStatement", nice_type_name: "block statement") var walker = Walker(node: node) var current_node: Node? = .none #MustOr( result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Malformed block statement"))) if current_node!.nodeType != "{" { return Result.Error( ErrorWithLocation( sourceLocation: current_node!.toSourceLocation(), withError: "Missing { on block statement")) } var statements: [AST.AnStatement] = Array() var parse_err: (any Errorable)? = .none walker.next() #MustOr( result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Malformed block statement"))) if current_node!.nodeType == "statements" { switch SpecialCompilers.Statements.Compile( node: current_node!, withContext: context) { case .Ok(let parsed_statements): statements = parsed_statements case .Error(let error): parse_err = error } walker.next() } if let err = parse_err { return .Error(err) } #MustOr( result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Malformed block statement"))) if current_node!.nodeType != "}" { return Result.Error( ErrorWithLocation( sourceLocation: current_node!.toSourceLocation(), withError: "Missing } on block statement")) } return .Ok(AST.BlockStatement(statements)) */ return .Ok(AST.BlockStatement([])) } } @deriveCompilableStatement extension AST.BlockStatement: CompilableStatement {} extension AST.ConditionalStatement: Compilable { public typealias C = AST.ConditionalStatement public static func Compile( node: Node, withContext context: ASTCompilerContext ) -> Result { #RequireNodeType( node: node, type: "conditionalStatement", nice_type_name: "conditional statement") let maybe_condition_expression = node.child(at: 2) guard let condition_expression = maybe_condition_expression, condition_expression.nodeType == "expression" else { return Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Did not find condition for conditional statement")) } let maybe_thens = node.child(at: 4) guard let thens = maybe_thens, thens.nodeType == "statement" else { return Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Did not find then statement block for conditional statement")) } guard case .Ok(let condition) = AST.Expression.Compile( node: condition_expression, withContext: context) else { return Result.Error( Error(withMessage: "Could not parse a conditional expression in a conditional statement")) } guard case .Ok(let thenns) = AST.Statement.Compile( node: thens, withContext: context) else { return Result.Error( Error( withMessage: "Could not parse the then block in a conditional statement")) } let optional_elss: Result? = if let elss = node.child(at: 6) { .some( AST.Statement.Compile( node: elss, withContext: context)) } else { .none } if let parsed_elss = optional_elss { guard case .Ok(let elss) = parsed_elss else { return Result.Error( Error( withMessage: "Could not parse the else block in a conditional statement")) } return .Ok( AST.ConditionalStatement(condition: condition, withThen: thenns, andElse: elss)) } return .Ok(AST.ConditionalStatement(condition: condition, withThen: thenns)) } } @deriveCompilableStatement extension AST.ConditionalStatement: CompilableStatement {} extension AST.VariableDeclarationStatement: Compilable { public typealias C = AST.VariableDeclarationStatement public static func Compile( node: Node, withContext context: ASTCompilerContext ) -> Result { #RequireNodeType( node: node, type: "variableDeclaration", nice_type_name: "variable declaration statement") let maybe_typeref = node.child(at: 0) guard let typeref = maybe_typeref, typeref.nodeType == "typeRef" else { return Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Did not find type name for variable declaration statement")) } let maybe_variablename = node.child(at: 1) guard let variablename = maybe_variablename, variablename.nodeType == "identifier" else { return Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Did not find identifier name for variable declaration statement")) } let maybe_rvalue = node.childCount > 3 ? node.child(at: 3) : .none guard case .Ok(let parsed_variablename) = AST.Identifier.CompileExpression( node: variablename, withContext: context) else { return Result.Error( Error(withMessage: "Could not parse variable name")) } guard case .Ok(let declaration_p4_type) = AST.Types.CompileType(type: typeref, withContext: context) else { return Result.Error( Error(withMessage: "Could not parse a P4 type from \(typeref.text!)")) } var initializer: AST.AnExpression? = .none // If there is an initializer, it must be an expression. if let initializer_expression = maybe_rvalue { guard initializer_expression.nodeType == "expression" else { return Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "initial value for declaration statement is not an expression")) } let maybe_parsed_rvalue = AST.Expression.Compile( node: initializer_expression, withContext: context) guard case .Ok(let parsed_rvalue) = maybe_parsed_rvalue else { return .Error(maybe_parsed_rvalue.error()!) } initializer = parsed_rvalue } return Result.Ok( AST.VariableDeclarationStatement( identifier: parsed_variablename as! AST.Identifier, withType: declaration_p4_type, withInitializer: initializer), ) } } @deriveCompilableStatement extension AST.VariableDeclarationStatement: CompilableStatement {} extension AST.ExpressionStatement: Compilable { public typealias C = AST.ExpressionStatement public static func Compile( node: Node, withContext context: ASTCompilerContext ) -> Result { #RequireNodeType( node: node, type: "expressionStatement", nice_type_name: "expression statement") let expression_node = node.child(at: 0)! return switch AST.Expression.Compile(node: expression_node, withContext: context) { case .Ok(let expression): .Ok(AST.ExpressionStatement(expression)) case .Error(let e): .Error(e) } } } @deriveCompilableStatement extension AST.ExpressionStatement: CompilableStatement {} extension AST.ParserAssignmentStatement: Compilable { public typealias C = AST.ParserAssignmentStatement public static func Compile( node: Node, withContext context: ASTCompilerContext ) -> Result { #RequireNodeType( node: node, type: "assignmentStatement", nice_type_name: "assignment statement") guard let lvalue_node = node.child(at: 0), lvalue_node.nodeType == "expression" else { return Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing lvalue in assignment statement")) } guard let rvalue_node = node.child(at: 2), rvalue_node.nodeType == "expression" else { return Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing rvalue in assignment statement")) } let maybe_parsed_rvalue = AST.Expression.Compile( node: rvalue_node, withContext: context) guard case Result.Ok(let rvalue) = maybe_parsed_rvalue else { return Result.Error(maybe_parsed_rvalue.error()!) } let maybe_parsed_lvalue = AST.Expression.Compile(node: lvalue_node, withContext: context) guard case .Ok(let lvalue) = maybe_parsed_lvalue else { return Result.Error(maybe_parsed_lvalue.error()!) } return Result.Ok( AST.ParserAssignmentStatement( withLValue: lvalue, withValue: rvalue )) } } @deriveCompilableStatement extension AST.ParserAssignmentStatement: CompilableStatement {} extension AST.ReturnStatement: Compilable { public typealias C = AST.ReturnStatement public static func Compile( node: SwiftTreeSitter.Node, withContext context: ASTCompilerContext ) -> Result { #RequireNodeType( node: node, type: "return_statement", nice_type_name: "return statement") let expression_node = node.child(at: 1)! return switch AST.Expression.Compile(node: expression_node, withContext: context) { case .Ok(let result): .Ok(AST.ReturnStatement(result)) case .Error(let e): .Error(e) } } } @deriveCompilableStatement extension AST.ReturnStatement: CompilableStatement {} extension AST.ApplyStatement: Compilable { public typealias C = AST.ApplyStatement public static func Compile( node: SwiftTreeSitter.Node, withContext context: ASTCompilerContext ) -> Result { #RequireNodeType( node: node, type: "apply_statement", nice_type_name: "apply statement") let expression_node = node.child(at: 1)! return switch AST.BlockStatement.Compile(node: expression_node, withContext: context) { case .Ok(let statement): .Ok(AST.ApplyStatement(statement)) case .Error(let e): .Error(e) } } } @deriveCompilableStatement extension AST.ApplyStatement: CompilableStatement {} extension AST.Instantiation: Compilable { public typealias C = AST.Instantiation public static func Compile( node: SwiftTreeSitter.Node, withContext context: ASTCompilerContext ) -> Result { let expression = node #RequireNodeType( node: expression, type: "instantiation", nice_type_name: "instantiation statement") var walker = Walker(node: expression) var current_node: Node? = .none #MustOr( result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing function call component"))) let maybe_instantiated_type_name = AST.Identifier.CompileExpression( node: current_node!, withContext: context) guard case .Ok(let instantiated_type_name) = maybe_instantiated_type_name else { return Result.Error(maybe_instantiated_type_name.error()!) } walker.next() #MustOr( result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing instantiation component"))) let maybe_argument_list = AST.ArgumentList.Compile(node: current_node!, withContext: context) guard case .Ok(let arguments) = maybe_argument_list else { return .Error(maybe_argument_list.error()!) } walker.next() #MustOr( result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( sourceLocation: node.toSourceLocation(), withError: "Missing instantiation name"))) let name = AST.Identifier.CompileExpression(node: current_node!, withContext: context) guard case .Ok(let name) = name else { return .Error(name.error()!) } return .Ok( AST.Instantiation( named: name as! AST.Identifier, withType: instantiated_type_name as! AST.Identifier, withArguments: arguments)) } } @deriveCompilableStatement extension AST.Instantiation: CompilableStatement {}