From 3d4d26d07bc75e5c159b453e3f3874989e332990 Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Mon, 4 May 2026 10:34:33 -0400 Subject: [PATCH] Make Formatter Happy Signed-off-by: Will Hawkins --- Sources/Common/DataTypes.swift | 3 +- Sources/Common/Result.swift | 2 +- Sources/Common/Support.swift | 4 +- Sources/P4Compiler/Common.swift | 29 +++-- Sources/P4Compiler/Declarations.swift | 122 ++++++++++++------ Sources/P4Compiler/Expression.swift | 175 +++++++++++++++----------- Sources/P4Compiler/Parser.swift | 36 ++++-- Sources/P4Compiler/Program.swift | 3 +- Sources/P4Compiler/Statement.swift | 46 ++++--- Sources/P4Lang/Expressions.swift | 4 +- 10 files changed, 279 insertions(+), 145 deletions(-) diff --git a/Sources/Common/DataTypes.swift b/Sources/Common/DataTypes.swift index 6b27f95..62b96bc 100644 --- a/Sources/Common/DataTypes.swift +++ b/Sources/Common/DataTypes.swift @@ -304,7 +304,8 @@ public class P4StructValue: P4DataValue { } public init(withType type: P4Struct, andInitializers initializers: [P4Value?]) { - let values: [P4Value?] = zip(0..(input: T, block: (T) -> U) -> U { @freestanding(expression) public macro RequireOkResult(_: Result) -> Bool = #externalMacro(module: "Macros", type: "RequireResult") -@freestanding(expression) public macro RequireErrorResult(_: any Errorable, _: Result) -> Bool = +@freestanding(expression) public macro RequireErrorResult( + _: any Errorable, _: Result +) -> Bool = #externalMacro(module: "Macros", type: "RequireErrorResult") @freestanding(expression) public macro UseOkResult(_: Result) -> T = #externalMacro(module: "Macros", type: "UseOkResult") diff --git a/Sources/P4Compiler/Common.swift b/Sources/P4Compiler/Common.swift index 999bf90..9e0944a 100644 --- a/Sources/P4Compiler/Common.swift +++ b/Sources/P4Compiler/Common.swift @@ -102,7 +102,8 @@ extension ParameterList: Compilable { result: current_node, thing: walker.getNext(), or: Result<(ParameterList, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing '(' in parameter list component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing '(' in parameter list component"))) walker.next() #MustOr( @@ -132,7 +133,8 @@ extension Direction: Compilable { guard let parsed_direction = directions[direction_node.text!] else { return .Error( ErrorWithLocation( - sourceLocation: direction_node.toSourceLocation(), withError: "\(direction_node.text!) is not a valid direction")) + sourceLocation: direction_node.toSourceLocation(), + withError: "\(direction_node.text!) is not a valid direction")) } return .Ok((parsed_direction, context)) @@ -155,7 +157,8 @@ extension Parameter: Compilable { result: current_node, thing: walker.getNext(), or: Result<(Parameter, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing parameter declaration component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing parameter declaration component"))) // Annotation? if current_node!.nodeType == "annotations" { @@ -170,7 +173,8 @@ extension Parameter: Compilable { result: current_node, thing: walker.getNext(), or: Result<(Parameter, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing parameter declaration component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing parameter declaration component"))) var direction: Direction? = .none // Direction? @@ -189,12 +193,14 @@ extension Parameter: Compilable { result: current_node, thing: walker.getNext(), or: Result<(Parameter, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing parameter declaration component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing parameter declaration component"))) if current_node!.nodeType != "typeRef" { return Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Did not find type name for parameter declaration")) + sourceLocation: node.toSourceLocation(), + withError: "Did not find type name for parameter declaration")) } guard @@ -209,12 +215,14 @@ extension Parameter: Compilable { result: current_node, thing: walker.getNext(), or: Result<(Parameter, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing parameter declaration component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing parameter declaration component"))) if current_node!.nodeType != "identifier" { return Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Did not find identifier for parameter statement")) + sourceLocation: node.toSourceLocation(), + withError: "Did not find identifier for parameter statement")) } guard @@ -318,7 +326,8 @@ extension ArgumentList: Compilable { result: current_node, thing: walker.getNext(), or: Result<(ArgumentList, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing '(' in argument list component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing '(' in argument list component"))) walker.next() @@ -377,4 +386,4 @@ extension Node { public func toSourceLocation() -> SourceLocation { return SourceLocation(self.range.location, self.range.length) } -} \ No newline at end of file +} diff --git a/Sources/P4Compiler/Declarations.swift b/Sources/P4Compiler/Declarations.swift index bccb794..ce060ea 100644 --- a/Sources/P4Compiler/Declarations.swift +++ b/Sources/P4Compiler/Declarations.swift @@ -61,7 +61,8 @@ extension FunctionDeclaration: CompilableDeclaration { result: current_node, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: function_declaration_node.toSourceLocation(), withError: "Missing function declaration component"))) + sourceLocation: function_declaration_node.toSourceLocation(), + withError: "Missing function declaration component"))) let maybe_function_type = Types.CompileType(type: current_node!, withContext: context) guard case .Ok(let function_type) = maybe_function_type else { @@ -73,7 +74,8 @@ extension FunctionDeclaration: CompilableDeclaration { result: current_node, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: function_declaration_node.toSourceLocation(), withError: "Missing function declaration component"))) + sourceLocation: function_declaration_node.toSourceLocation(), + withError: "Missing function declaration component"))) let maybe_function_name = Identifier.Compile(node: current_node!, withContext: context) guard case .Ok(let function_name) = maybe_function_name else { @@ -85,7 +87,8 @@ extension FunctionDeclaration: CompilableDeclaration { result: current_node, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: function_declaration_node.toSourceLocation(), withError: "Missing function declaration component"))) + sourceLocation: function_declaration_node.toSourceLocation(), + withError: "Missing function declaration component"))) let maybe_function_parameters = ParameterList.Compile(node: current_node!, withContext: context) guard case .Ok((let function_parameters, let updated_context)) = maybe_function_parameters @@ -121,7 +124,8 @@ extension FunctionDeclaration: CompilableDeclaration { if !context.extern_context { return Result.Error( ErrorWithLocation( - sourceLocation: function_declaration_node.toSourceLocation(), withError: "Missing function declaration component")) + sourceLocation: function_declaration_node.toSourceLocation(), + withError: "Missing function declaration component")) } } @@ -166,7 +170,8 @@ extension P4Struct: CompilableDeclaration { result: currentNode, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: struct_declaration_node.toSourceLocation(), withError: "Missing function declaration component"))) + sourceLocation: struct_declaration_node.toSourceLocation(), + withError: "Missing function declaration component"))) // The name of the struct type. let maybe_struct_identifier = Identifier.Compile( @@ -182,14 +187,16 @@ extension P4Struct: CompilableDeclaration { result: currentNode, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: struct_declaration_node.toSourceLocation(), withError: "Missing function declaration component"))) + sourceLocation: struct_declaration_node.toSourceLocation(), + withError: "Missing function declaration component"))) // If there are no fields, it will be a "}" if currentNode!.nodeType == "}" { let struc = Declaration( TypedIdentifier( id: struct_identifier, - withType: P4QualifiedType(P4Struct(withName: struct_identifier, andFields: P4StructFields([]))))) + withType: P4QualifiedType( + P4Struct(withName: struct_identifier, andFields: P4StructFields([]))))) return Result.Ok( ( struc, @@ -265,11 +272,14 @@ extension P4Lang.Parser: CompilableDeclaration { result: current_node, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: parser_node.toSourceLocation(), withError: "Missing elements of parser declaration"))) + sourceLocation: parser_node.toSourceLocation(), + withError: "Missing elements of parser declaration"))) if current_node!.nodeType != "parserType" { return .Error( - ErrorWithLocation(sourceLocation: current_node!.toSourceLocation(), withError: "Missing type for parser declaration")) + ErrorWithLocation( + sourceLocation: current_node!.toSourceLocation(), + withError: "Missing type for parser declaration")) } let type_node = current_node @@ -286,12 +296,14 @@ extension P4Lang.Parser: CompilableDeclaration { result: type_node_child, thing: type_node_walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: parser_node.toSourceLocation(), withError: "Missing elements of parser type in parser declaration"))) + sourceLocation: parser_node.toSourceLocation(), + withError: "Missing elements of parser type in parser declaration"))) if type_node_child!.nodeType == "annotations" { return .Error( ErrorWithLocation( - sourceLocation: type_node_child!.toSourceLocation(), withError: "Annotations in parser type are not yet handled.")) + sourceLocation: type_node_child!.toSourceLocation(), + withError: "Annotations in parser type are not yet handled.")) // Will increment indexes here. } @@ -300,7 +312,8 @@ extension P4Lang.Parser: CompilableDeclaration { result: type_node_child, thing: type_node_walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: type_node_child!.toSourceLocation(), withError: "Missing name in parser type declaration"))) + sourceLocation: type_node_child!.toSourceLocation(), + withError: "Missing name in parser type declaration"))) switch Identifier.Compile(node: type_node_child!, withContext: current_context) { case .Ok(let id): parser_name = id @@ -313,7 +326,8 @@ extension P4Lang.Parser: CompilableDeclaration { result: type_node_child, thing: type_node_walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: type_node_child!.toSourceLocation(), withError: "Missing parser parameters"))) + sourceLocation: type_node_child!.toSourceLocation(), + withError: "Missing parser parameters"))) switch ParameterList.Compile(node: type_node_child!, withContext: current_context) { case .Ok(let (parsed_parameter_list, updated_context)): @@ -336,18 +350,22 @@ extension P4Lang.Parser: CompilableDeclaration { result: current_node, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: parser_node.toSourceLocation(), withError: "Missing parser declaration component"))) + sourceLocation: parser_node.toSourceLocation(), + withError: "Missing parser declaration component"))) walker.next() #MustOr( result: current_node, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: parser_node.toSourceLocation(), withError: "Missing elements of parser declaration"))) + sourceLocation: parser_node.toSourceLocation(), + withError: "Missing elements of parser declaration"))) if current_node!.nodeType == "parserLocalElements" { return .Error( - ErrorWithLocation(sourceLocation: current_node!.toSourceLocation(), withError: "Parser Local Elements are not yet handled.")) + ErrorWithLocation( + sourceLocation: current_node!.toSourceLocation(), + withError: "Parser Local Elements are not yet handled.")) // Will increment indexes here. } @@ -355,7 +373,8 @@ extension P4Lang.Parser: CompilableDeclaration { result: current_node, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: parser_node.toSourceLocation(), withError: "Missing body of parser declaration"))) + sourceLocation: parser_node.toSourceLocation(), + withError: "Missing body of parser declaration"))) if current_node!.nodeType != "parserStates" { return .Error(Error(withMessage: "Missing parser states in parser declaration")) @@ -400,7 +419,8 @@ extension Control: CompilableDeclaration { result: current_node, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing control declaration component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing control declaration component"))) guard case .Ok(let control_name) = Identifier.Compile( @@ -415,7 +435,8 @@ extension Control: CompilableDeclaration { result: current_node, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing control declaration component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing control declaration component"))) let maybe_control_parameters = ParameterList.Compile( node: current_node!, withContext: local_context) @@ -440,7 +461,8 @@ extension Control: CompilableDeclaration { result: current_node, thing: walker.getNext(), or: Result<(Declaration, CompilerContext)?>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing control declaration component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing control declaration component"))) var actions: [Action] = Array() var tables: [Table] = Array() @@ -490,7 +512,9 @@ extension Control: CompilableDeclaration { // But, that is handled by the compiler. } else { return .Error( - ErrorWithLocation(sourceLocation: current_node.toSourceLocation(), withError: "Uknown node type in control declaration")) + ErrorWithLocation( + sourceLocation: current_node.toSourceLocation(), + withError: "Uknown node type in control declaration")) } return .Ok(()) } @@ -506,13 +530,17 @@ extension Control: CompilableDeclaration { /// IDEA: Add a "compilation context" for the error message into the `CompilationContext` // that can be retrieved to make the error messages nicer. return .Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "More than one table in control declaration")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "More than one table in control declaration")) } // Check to make sure that there is an apply. guard let apply = apply else { return .Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Missing apply in control declaration")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "Missing apply in control declaration" + )) } let declared_control = @@ -556,7 +584,9 @@ extension Action: Compilable { #MustOr( result: current_node, thing: walker.getNext(), or: Result<(P4Lang.Action, CompilerContext)>.Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component" + )) ) guard @@ -571,7 +601,9 @@ extension Action: Compilable { #MustOr( result: current_node, thing: walker.getNext(), or: Result<(P4Lang.Action, CompilerContext)>.Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component" + )) ) let maybe_action_parameters = ParameterList.Compile( @@ -586,7 +618,9 @@ extension Action: Compilable { #MustOr( result: current_node, thing: walker.getNext(), or: Result<(P4Lang.Action, CompilerContext)>.Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component" + )) ) // Add the parameters into scope. @@ -633,7 +667,8 @@ extension TableKeyEntry: Compilable { result: current_node, thing: walker.getNext(), or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing table key entry declaration component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing table key entry declaration component"))) let maybe_keyset_expression = KeysetExpression.compile( node: current_node!, withContext: current_context) @@ -648,7 +683,8 @@ extension TableKeyEntry: Compilable { result: current_node, thing: walker.getNext(), or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing table key entry declaration component"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing table key entry declaration component"))) let maybe_match_type = TableKeyMatchType.Compile( node: current_node!, withContext: current_context) @@ -671,7 +707,10 @@ extension TableKeyMatchType: Compilable { if node.text! == "exact" { return .Ok((TableKeyMatchType.Exact, context)) } - return .Error(ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "\(node.text!) is not a valid match type)")) + return .Error( + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "\(node.text!) is not a valid match type)")) } } @@ -697,7 +736,8 @@ extension TableKeys: Compilable { result: current_node, thing: walker.getNext(), or: Result<(P4Lang.TableKeys, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing table keys declaration component in control declaration")) + sourceLocation: node.toSourceLocation(), + withError: "Missing table keys declaration component in control declaration")) ) let (keys, errors) = walker.try_map(n: node.childCount - 1, onlyNamed: true) { current_node in @@ -757,7 +797,9 @@ extension TableActionsProperty: Compilable { return .Ok(TypedIdentifier(id: listed_action, withType: P4QualifiedType(maybe_action))) } return .Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "\(listed_action) does not name an action")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "\(listed_action) does not name an action")) case .Error(let e): return .Error(e) } case .Error(let e): return .Error(e) @@ -809,7 +851,9 @@ extension TablePropertyList: Compilable { } } else { errors.append( - ErrorWithLocation(sourceLocation: child.toSourceLocation(), withError: "Uknown node type in control declaration")) + ErrorWithLocation( + sourceLocation: child.toSourceLocation(), + withError: "Uknown node type in control declaration")) } } @@ -827,14 +871,18 @@ extension TablePropertyList: Compilable { if keys.count > 1 { // Todo: Make this error message better. return .Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "More than one key set in table property list")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "More than one key set in table property list")) } // There should be only one table actions! if actions.count > 1 { // Todo: Make this error message better. return .Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "More than one actions in table property list")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "More than one actions in table property list")) } if actions.isEmpty { actions.append(TableActionsProperty()) @@ -866,7 +914,8 @@ extension Table: Compilable { result: current_node, thing: walker.getNext(), or: Result<(P4Lang.Table, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component"))) + sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component") + )) guard case .Ok(let table_name) = Identifier.Compile( @@ -883,7 +932,8 @@ extension Table: Compilable { result: current_node, thing: walker.getNext(), or: Result<(P4Lang.Table, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component"))) + sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component") + )) let maybe_table_property_list = TablePropertyList.Compile( node: current_node!, withContext: current_context) diff --git a/Sources/P4Compiler/Expression.swift b/Sources/P4Compiler/Expression.swift index 01fb3ee..5937a18 100644 --- a/Sources/P4Compiler/Expression.swift +++ b/Sources/P4Compiler/Expression.swift @@ -46,7 +46,9 @@ extension TypedIdentifier: CompilableExpression { case Result.Ok(let type) = context.instances.lookup( identifier: Common.Identifier(name: node.text!)) else { - return .Error(ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Cannot find \(node.text!) in scope")) + return .Error( + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "Cannot find \(node.text!) in scope")) } return .Ok(TypedIdentifier(name: node.text!, withType: type)) @@ -88,7 +90,9 @@ extension P4BooleanValue: CompilableExpression { } return .Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Failed to parse boolean literal: \(node.text!)")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "Failed to parse boolean literal: \(node.text!)")) } } @@ -101,7 +105,10 @@ extension P4IntValue: CompilableExpression { if let parsed_int = Int(node.text!) { return .Ok(P4Value(P4IntValue(withValue: parsed_int))) } else { - return .Error(ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Failed to parse integer: \(node.text!)")) + return .Error( + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "Failed to parse integer: \(node.text!)")) } } } @@ -237,13 +244,18 @@ extension SelectExpression: CompilableExpression { guard let selector_node = node.child(at: 2), selector_node.nodeType == "expression" else { - return .Error(ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Could not find selector expression")) + return .Error( + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "Could not find selector expression")) } guard let select_body_node = node.child(at: 5), select_body_node.nodeType == "selectBody" else { - return .Error(ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Could not find select expression body")) + return .Error( + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "Could not find select expression body")) } let maybe_selector = Expression.Compile(node: selector_node, withContext: context) @@ -263,12 +275,14 @@ extension SelectExpression: CompilableExpression { let maybe_parsed_cse = SelectCaseExpression.compile( node: current_node, withContext: context.update(newExpectation: selector.type())) switch maybe_parsed_cse { - case .Ok(let parsed_cse): sces.append(parsed_cse as! SelectCaseExpression) - case .Error(let e): sces_errors = if let sces_errors = sces_errors { - sces_errors.append(error: Error(withMessage: "\(maybe_parsed_cse.error()!)")) - } else { - e - } + case .Ok(let parsed_cse): sces.append(parsed_cse as! SelectCaseExpression) + case .Error(let e): + sces_errors = + if let sces_errors = sces_errors { + sces_errors.append(error: Error(withMessage: "\(maybe_parsed_cse.error()!)")) + } else { + e + } } } @@ -310,13 +324,17 @@ extension SelectCaseExpression: CompilableExpression { guard let maybe_keysetexpression = maybe_keysetexpression else { return Result.Error( - ErrorWithLocation(sourceLocation: keysetexpression_node.toSourceLocation(), withError: "Missing expected keyset expression")) + ErrorWithLocation( + sourceLocation: keysetexpression_node.toSourceLocation(), + withError: "Missing expected keyset expression")) } let keysetexpression = maybe_keysetexpression as! KeysetExpression if case .Error(let e) = keysetexpression.compatible(type: context.expected_type!) { - return .Error(ErrorWithLocation(sourceLocation: keysetexpression_node.toSourceLocation(), withError: e.msg())) + return .Error( + ErrorWithLocation( + sourceLocation: keysetexpression_node.toSourceLocation(), withError: e.msg())) } let maybe_parsed_targetstate = Identifier.Compile( @@ -349,7 +367,8 @@ extension BinaryOperatorExpression: CompilableExpression { result: current_node, thing: walker.getNext(), or: Result.Error( 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 // swift-format-ignore @@ -361,7 +380,8 @@ extension BinaryOperatorExpression: CompilableExpression { result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing LHS for binary operator expression"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing LHS for binary operator expression"))) let left_hand_side_raw = current_node! @@ -370,14 +390,16 @@ extension BinaryOperatorExpression: CompilableExpression { result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing binary operator for binary operator expression"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing binary operator for binary operator expression"))) walker.next() #MustOr( result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing RHS for binary operator expression"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing RHS for binary operator expression"))) let right_hand_side_raw = current_node! @@ -391,52 +413,55 @@ extension BinaryOperatorExpression: CompilableExpression { return Result.Error(maybe_right_hand_side.error()!) } - let evaluators: [String: (String, P4QualifiedType, BinaryOperatorChecker?, BinaryOperatorEvaluator)] = [ - "binaryEqualOperatorExpression": ( - "Binary Equal", P4QualifiedType(P4Boolean()), Optional.none, - binary_equal_operator_evaluator - ), - "binaryLessThanOperatorExpression": ( - "Binary Less Than", P4QualifiedType(P4Boolean()), Optional.none, - binary_lt_operator_evaluator - ), - "binaryLessThanEqualOperatorExpression": ( - "Binary Less Than Or Equal", P4QualifiedType(P4Boolean()), Optional.none, - binary_lte_operator_evaluator - ), - "binaryGreaterThanOperatorExpression": ( - "Binary Greater Than", P4QualifiedType(P4Boolean()), Optional.none, - binary_gt_operator_evaluator - ), - "binaryGreaterThanEqualOperatorExpression": ( - "Binary Greater Than Or Equal", P4QualifiedType(P4Boolean()), Optional.none, - binary_gte_operator_evaluator - ), - "binaryAndOperatorExpression": ( - "Binary Or", P4QualifiedType(P4Boolean()), binary_and_or_operator_checker, - binary_and_operator_evaluator - ), - "binaryOrOperatorExpression": ( - "Binary And", P4QualifiedType(P4Boolean()), binary_and_or_operator_checker, - binary_or_operator_evaluator - ), - "binaryAddOperatorExpression": ( - "Binary Add", P4QualifiedType(P4Int()), binary_int_math_operator_checker, - binary_add_operator_evaluator - ), - "binarySubtractOperatorExpression": ( - "Binary Subtract", P4QualifiedType(P4Int()), binary_int_math_operator_checker, - binary_subtract_operator_evaluator - ), - "binaryMultiplyOperatorExpression": ( - "Binary Multiply", P4QualifiedType(P4Int()), binary_int_math_operator_checker, - binary_multiply_operator_evaluator - ), - "binaryDivideOperatorExpression": ( - "Binary Divide", P4QualifiedType(P4Int()), binary_int_math_operator_checker, - binary_divide_operator_evaluator - ), - ] + let evaluators: + [String: (String, P4QualifiedType, BinaryOperatorChecker?, BinaryOperatorEvaluator)] = [ + "binaryEqualOperatorExpression": ( + "Binary Equal", P4QualifiedType(P4Boolean()), Optional.none, + binary_equal_operator_evaluator + ), + "binaryLessThanOperatorExpression": ( + "Binary Less Than", P4QualifiedType(P4Boolean()), Optional.none, + binary_lt_operator_evaluator + ), + "binaryLessThanEqualOperatorExpression": ( + "Binary Less Than Or Equal", P4QualifiedType(P4Boolean()), + Optional.none, + binary_lte_operator_evaluator + ), + "binaryGreaterThanOperatorExpression": ( + "Binary Greater Than", P4QualifiedType(P4Boolean()), Optional.none, + binary_gt_operator_evaluator + ), + "binaryGreaterThanEqualOperatorExpression": ( + "Binary Greater Than Or Equal", P4QualifiedType(P4Boolean()), + Optional.none, + binary_gte_operator_evaluator + ), + "binaryAndOperatorExpression": ( + "Binary Or", P4QualifiedType(P4Boolean()), binary_and_or_operator_checker, + binary_and_operator_evaluator + ), + "binaryOrOperatorExpression": ( + "Binary And", P4QualifiedType(P4Boolean()), binary_and_or_operator_checker, + binary_or_operator_evaluator + ), + "binaryAddOperatorExpression": ( + "Binary Add", P4QualifiedType(P4Int()), binary_int_math_operator_checker, + binary_add_operator_evaluator + ), + "binarySubtractOperatorExpression": ( + "Binary Subtract", P4QualifiedType(P4Int()), binary_int_math_operator_checker, + binary_subtract_operator_evaluator + ), + "binaryMultiplyOperatorExpression": ( + "Binary Multiply", P4QualifiedType(P4Int()), binary_int_math_operator_checker, + binary_multiply_operator_evaluator + ), + "binaryDivideOperatorExpression": ( + "Binary Divide", P4QualifiedType(P4Int()), binary_int_math_operator_checker, + binary_divide_operator_evaluator + ), + ] guard let selected_evaluator = evaluators[binary_operator_expression_node.nodeType!] else { return Result.Error( @@ -486,7 +511,8 @@ extension ArrayAccessExpression: CompilableExpression { result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing [ for array access expression"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing [ for array access expression"))) walker.next() @@ -494,7 +520,8 @@ extension ArrayAccessExpression: CompilableExpression { result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing indexor expression for array access expression"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing indexor expression for array access expression"))) #RequireNodeType( node: current_node!, type: "expression", @@ -559,14 +586,16 @@ extension FieldAccessExpression: CompilableExpression { result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing . for field access expression"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing . for field access expression"))) walker.next() #MustOr( result: current_node, thing: walker.getNext(), or: Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing field name for field access expression"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing field name for field access expression"))) #RequireNodeType( node: current_node!, type: "identifier", @@ -679,7 +708,9 @@ extension FunctionCall: CompilableExpression { Result<(FunctionDeclaration?, Declaration?)>.Ok((callee, .none)) // What we found is actually a function declaration default: Result<(FunctionDeclaration?, Declaration?)>.Error( - ErrorWithLocation(sourceLocation: current_node!.toSourceLocation(), withError: "\(callee_name) is not a function")) + ErrorWithLocation( + sourceLocation: current_node!.toSourceLocation(), + withError: "\(callee_name) is not a function")) } case .Error(let e): Result<(FunctionDeclaration?, Declaration?)>.Error(e) } @@ -692,7 +723,10 @@ extension FunctionCall: CompilableExpression { switch callee.identifier.type.baseType() { case is FunctionDeclaration: Result.Ok((.none, callee)) default: - .Error(ErrorWithLocation(sourceLocation: current_node!.toSourceLocation(), withError: "\(callee_name) is not a function")) + .Error( + ErrorWithLocation( + sourceLocation: current_node!.toSourceLocation(), + withError: "\(callee_name) is not a function")) } default: .Error(e) } @@ -747,7 +781,8 @@ extension FunctionCall: CompilableExpression { default: Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Unexpected error occurred calling function named (\(callee_name))" + sourceLocation: node.toSourceLocation(), + withError: "Unexpected error occurred calling function named (\(callee_name))" )) } } diff --git a/Sources/P4Compiler/Parser.swift b/Sources/P4Compiler/Parser.swift index 03cdf14..4744b7b 100644 --- a/Sources/P4Compiler/Parser.swift +++ b/Sources/P4Compiler/Parser.swift @@ -52,7 +52,10 @@ public struct Parser { node: Node, withContext context: CompilerContext ) -> Result<(EvaluatableStatement, CompilerContext)> { if node.nodeType != "parserStatement" && node.nodeType != "statement" { - return Result.Error(ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Missing expected parser statement")) + return Result.Error( + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "Missing expected parser statement") + ) } let statement = node.child(at: 0)! @@ -76,7 +79,9 @@ public struct Parser { return .Ok((parsed, updated_context)) case Result.Error(let e): return .Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Failed to parse a statement element: \(e)")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "Failed to parse a statement element: \(e)")) } } } @@ -95,7 +100,9 @@ public struct Parser { tse_node.nodeType! == "transitionSelectionExpression" else { return .Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Could not find transition select expression")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "Could not find transition select expression")) } guard let next_node = tse_node.child(at: 0) else { @@ -145,7 +152,9 @@ public struct Parser { node: Node, withContext context: CompilerContext ) -> Result<([EvaluatableStatement], CompilerContext)> { if node.nodeType != "statements" && node.nodeType != "parserStatements" { - return Result.Error(ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Did not find expected statements")) + return Result.Error( + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "Did not find expected statements")) } var parse_errs: [any Errorable] = Array() @@ -187,19 +196,23 @@ public struct Parser { node_type == "parserState" else { return Result.Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Did not find a parser state declaration")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "Did not find a parser state declaration")) } #MustOr( result: current_node, thing: walker.getNext(), or: Result<(InstantiatedParserState, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing elements in parser state declaration"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing elements in parser state declaration"))) if current_node!.nodeType == "annotations" { return Result.Error( ErrorWithLocation( - sourceLocation: current_node!.toSourceLocation(), withError: "Annotations in parser state are not yet handled.")) + sourceLocation: current_node!.toSourceLocation(), + withError: "Annotations in parser state are not yet handled.")) // Would increment here. } @@ -210,7 +223,8 @@ public struct Parser { result: current_node, thing: walker.getNext(), or: Result<(InstantiatedParserState, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing elements in parser state declaration"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing elements in parser state declaration"))) let maybe_state_identifier = Identifier.Compile( node: current_node!, withContext: context) @@ -225,7 +239,8 @@ public struct Parser { result: current_node, thing: walker.getNext(), or: Result<(InstantiatedParserState, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration"))) + sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration") + )) var parse_errs: [any Errorable] = Array() var current_context = context @@ -256,7 +271,8 @@ public struct Parser { result: current_node, thing: walker.getNext(), or: Result<(InstantiatedParserState, CompilerContext)>.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Missing transition statement of state declaration"))) + sourceLocation: node.toSourceLocation(), + withError: "Missing transition statement of state declaration"))) return TransitionStatement.Compile( node: current_node!, forState: state_identifier, withStatements: parsed_s, diff --git a/Sources/P4Compiler/Program.swift b/Sources/P4Compiler/Program.swift index 1da4424..343e5b7 100644 --- a/Sources/P4Compiler/Program.swift +++ b/Sources/P4Compiler/Program.swift @@ -99,7 +99,8 @@ public struct Program { if !found_parser { errors.append( ErrorWithLocation( - sourceLocation: specific_declaration_node.toSourceLocation(), withError: "Could not find parser for declaration node" + sourceLocation: specific_declaration_node.toSourceLocation(), + withError: "Could not find parser for declaration node" )) } } diff --git a/Sources/P4Compiler/Statement.swift b/Sources/P4Compiler/Statement.swift index 1f96f99..fda1d63 100644 --- a/Sources/P4Compiler/Statement.swift +++ b/Sources/P4Compiler/Statement.swift @@ -35,11 +35,14 @@ extension BlockStatement: CompilableStatement { #MustOr( result: current_node, thing: walker.getNext(), or: Result<(EvaluatableStatement, CompilerContext)>.Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Malformed block statement"))) + 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")) + ErrorWithLocation( + sourceLocation: current_node!.toSourceLocation(), + withError: "Missing { on block statement")) } var statements: [EvaluatableStatement] = Array() @@ -50,7 +53,8 @@ extension BlockStatement: CompilableStatement { #MustOr( result: current_node, thing: walker.getNext(), or: Result<(EvaluatableStatement, CompilerContext)>.Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Malformed block statement"))) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "Malformed block statement"))) if current_node!.nodeType == "statements" { switch Parser.Statements.Compile( @@ -73,11 +77,14 @@ extension BlockStatement: CompilableStatement { #MustOr( result: current_node, thing: walker.getNext(), or: Result<(EvaluatableStatement, CompilerContext)>.Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Malformed block statement"))) + 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")) + ErrorWithLocation( + sourceLocation: current_node!.toSourceLocation(), + withError: "Missing } on block statement")) } return .Ok((BlockStatement(statements), current_context)) @@ -97,7 +104,9 @@ extension ConditionalStatement: CompilableStatement { condition_expression.nodeType == "expression" else { return Result.Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Did not find condition for conditional statement")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "Did not find condition for conditional statement")) } let maybe_thens = node.child(at: 4) @@ -106,7 +115,8 @@ extension ConditionalStatement: CompilableStatement { else { return Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Did not find then statement block for conditional statement")) + sourceLocation: node.toSourceLocation(), + withError: "Did not find then statement block for conditional statement")) } guard @@ -166,7 +176,8 @@ extension VariableDeclarationStatement: CompilableStatement { else { return Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Did not find type name for variable declaration statement")) + sourceLocation: node.toSourceLocation(), + withError: "Did not find type name for variable declaration statement")) } let maybe_variablename = node.child(at: 1) @@ -175,7 +186,8 @@ extension VariableDeclarationStatement: CompilableStatement { else { return Result.Error( ErrorWithLocation( - sourceLocation: node.toSourceLocation(), withError: "Did not find identifier name for variable declaration statement")) + sourceLocation: node.toSourceLocation(), + withError: "Did not find identifier name for variable declaration statement")) } let maybe_rvalue = node.childCount > 3 ? node.child(at: 3) : .none @@ -205,7 +217,8 @@ extension VariableDeclarationStatement: CompilableStatement { withError: "initial value for declaration statement is not an expression")) } - let maybe_parsed_rvalue = Expression.Compile(node: initializer_expression, withContext: context) + let maybe_parsed_rvalue = Expression.Compile( + node: initializer_expression, withContext: context) guard case .Ok(let parsed_rvalue) = maybe_parsed_rvalue else { @@ -230,8 +243,9 @@ extension VariableDeclarationStatement: CompilableStatement { } guard let initializer = initializer else { - return Result.Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "No initializer for declaration")) + return Result.Error( + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), withError: "No initializer for declaration")) } return Result.Ok( @@ -275,14 +289,18 @@ extension ParserAssignmentStatement: CompilableStatement { lvalue_node.nodeType == "expression" else { return Result.Error( - ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Missing lvalue in assignment statement")) + 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")) + ErrorWithLocation( + sourceLocation: node.toSourceLocation(), + withError: "Missing rvalue in assignment statement")) } let maybe_parsed_rvalue = Expression.Compile( diff --git a/Sources/P4Lang/Expressions.swift b/Sources/P4Lang/Expressions.swift index 91def56..cf58d86 100644 --- a/Sources/P4Lang/Expressions.swift +++ b/Sources/P4Lang/Expressions.swift @@ -84,7 +84,9 @@ public struct SelectExpression { } } -public typealias NamedBinaryOperatorEvaluator = (String, P4QualifiedType, (P4Value, P4Value) -> P4DataValue) +public typealias NamedBinaryOperatorEvaluator = ( + String, P4QualifiedType, (P4Value, P4Value) -> P4DataValue +) public typealias BinaryOperatorEvaluator = (P4Value, P4Value) -> P4DataValue public struct BinaryOperatorExpression {