Make Formatter Happy

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-06 21:40:37 -05:00
parent eff19df968
commit 81b9345c7f
8 changed files with 83 additions and 76 deletions
-2
View File
@@ -72,7 +72,6 @@ public class Variable: TypedIdentifier {
}
}
/// The type for a P4 struct
public struct P4Struct: P4Type {
@@ -247,7 +246,6 @@ public class P4StringValue: P4Value {
}
}
public class Packet {
public init() {}
}
+3 -1
View File
@@ -141,7 +141,9 @@ extension Result: CustomStringConvertible {
#externalMacro(module: "Macros", type: "UseOkResult")
@freestanding(expression) public macro UseErrorResult<T>(_: Result<T>) -> Error =
#externalMacro(module: "Macros", type: "UseErrorResult")
@freestanding(codeItem) public macro RequireNodeType<N, T>(node: N, type: String, nice_type_name: String) =
@freestanding(codeItem) public macro RequireNodeType<N, T>(
node: N, type: String, nice_type_name: String
) =
#externalMacro(module: "Macros", type: "RequireNodeType")
@freestanding(codeItem) public macro RequireNodesType<N, T>(
nodes: N, type: [String], nice_type_names: [String]
+37 -37
View File
@@ -184,8 +184,9 @@ extension SelectExpression: CompilableExpression {
var kses: [KeysetExpression] = Array()
var kses_errors: [Error] = Array()
select_body_node.enumerateNamedChildren() { current_node in
let maybe_parsed_kse = KeysetExpression.compile(node: current_node, inTree: tree, withScopes: scopes)
select_body_node.enumerateNamedChildren { current_node in
let maybe_parsed_kse = KeysetExpression.compile(
node: current_node, inTree: tree, withScopes: scopes)
if case .Ok(let parsed_kse) = maybe_parsed_kse {
kses.append(parsed_kse as! KeysetExpression)
} else {
@@ -202,8 +203,8 @@ extension SelectExpression: CompilableExpression {
}.joined(separator: ";\n"))))
}
return .Ok(
SelectExpression(withSelector: selector, withKeysetExpressions: kses),
)
SelectExpression(withSelector: selector, withKeysetExpressions: kses),
)
}
}
@@ -211,38 +212,37 @@ extension KeysetExpression: CompilableExpression {
static func compile(
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<EvaluatableExpression?> {
if node.nodeType != "selectCase" {
return Result.Error(Error(withMessage: "Expected select case not found"))
}
guard let keysetexpression_node = node.child(at: 0),
keysetexpression_node.nodeType == "keysetExpression"
else {
return Result.Error(Error(withMessage: "Missing keyset expression in select case"))
}
guard let targetstate_node = node.child(at: 2),
targetstate_node.nodeType == "identifier"
else {
return Result.Error(Error(withMessage: "Missing target state in select case"))
}
let maybe_parsed_keysetexpression = Expression.Compile(
node: keysetexpression_node, inTree: tree, withScopes: scopes)
guard case Result.Ok(let keysetexpression) = maybe_parsed_keysetexpression else {
return Result.Error(maybe_parsed_keysetexpression.error()!)
}
let maybe_parsed_targetstate = Identifier.Compile(
node: targetstate_node, inTree: tree, withScopes: scopes)
guard case .Ok(let targetstate) = maybe_parsed_targetstate else {
return Result.Error(maybe_parsed_targetstate.error()!)
}
return .Ok(
KeysetExpression(
withKey: keysetexpression, withNextState: targetstate)
)
if node.nodeType != "selectCase" {
return Result.Error(Error(withMessage: "Expected select case not found"))
}
}
guard let keysetexpression_node = node.child(at: 0),
keysetexpression_node.nodeType == "keysetExpression"
else {
return Result.Error(Error(withMessage: "Missing keyset expression in select case"))
}
guard let targetstate_node = node.child(at: 2),
targetstate_node.nodeType == "identifier"
else {
return Result.Error(Error(withMessage: "Missing target state in select case"))
}
let maybe_parsed_keysetexpression = Expression.Compile(
node: keysetexpression_node, inTree: tree, withScopes: scopes)
guard case Result.Ok(let keysetexpression) = maybe_parsed_keysetexpression else {
return Result.Error(maybe_parsed_keysetexpression.error()!)
}
let maybe_parsed_targetstate = Identifier.Compile(
node: targetstate_node, inTree: tree, withScopes: scopes)
guard case .Ok(let targetstate) = maybe_parsed_targetstate else {
return Result.Error(maybe_parsed_targetstate.error()!)
}
return .Ok(
KeysetExpression(
withKey: keysetexpression, withNextState: targetstate)
)
}
}
+13 -5
View File
@@ -147,15 +147,22 @@ public struct Parser {
node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes
) -> Result<(ParserTransitionStatement, LexicalScopes)> {
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement")
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement"
)
guard let tse_node = node.child(at: 1),
tse_node.nodeType! == "transitionSelectionExpression" else {
return .Error(ErrorOnNode(node: node, withError: "Could not find transition select expression"))
tse_node.nodeType! == "transitionSelectionExpression"
else {
return .Error(
ErrorOnNode(node: node, withError: "Could not find transition select expression"))
}
guard let next_node = tse_node.child(at: 0) else {
return .Error(ErrorOnNode(node: node, withError: "Could not find the next token in a transition selection expression"))
return .Error(
ErrorOnNode(
node: node,
withError: "Could not find the next token in a transition selection expression"))
}
// If the next node is an identifier, we have the simple form ...
@@ -179,7 +186,8 @@ public struct Parser {
switch SelectExpression.compile(node: next_node, inTree: tree, withScopes: scopes)
{
case .Ok(let tse):
.Ok((ParserTransitionStatement(withTransitionExpression: tse! as! SelectExpression), scopes))
.Ok(
(ParserTransitionStatement(withTransitionExpression: tse! as! SelectExpression), scopes))
case .Error(let e): .Error(e)
}
}
+8 -4
View File
@@ -26,7 +26,8 @@ extension BlockStatement: CompilableStatement {
public static func Compile(
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "blockStatement", nice_type_name: "block statement")
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
node: node, type: "blockStatement", nice_type_name: "block statement")
var currentChildIdx = 0
var currentChildIdxSafe = 1
@@ -91,7 +92,8 @@ extension ConditionalStatement: CompilableStatement {
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "conditionalStatement", nice_type_name: "conditional statement")
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
node: node, type: "conditionalStatement", nice_type_name: "conditional statement")
let maybe_condition_expression = node.child(at: 2)
guard let condition_expression = maybe_condition_expression,
@@ -158,7 +160,8 @@ extension VariableDeclarationStatement: CompilableStatement {
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "variableDeclaration", nice_type_name: "variable declaration statement")
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
node: node, type: "variableDeclaration", nice_type_name: "variable declaration statement")
let maybe_typeref = node.child(at: 0)
guard let typeref = maybe_typeref,
@@ -235,7 +238,8 @@ extension ExpressionStatement: CompilableStatement {
public static func Compile(
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "expressionStatement", nice_type_name: "expression statement")
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
node: node, type: "expressionStatement", nice_type_name: "expression statement")
let _ = node.child(at: 0)
+12 -12
View File
@@ -51,27 +51,27 @@ extension SelectExpression: EvaluatableExpression {
}
extension P4StringValue: EvaluatableExpression {
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
return .Ok(self)
}
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
return .Ok(self)
}
}
extension P4BooleanValue: EvaluatableExpression {
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
return .Ok(self)
}
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
return .Ok(self)
}
}
extension P4StructValue: EvaluatableExpression {
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
return .Ok(self)
}
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
return .Ok(self)
}
}
extension P4IntValue: EvaluatableExpression {
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
return .Ok(self)
}
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
return .Ok(self)
}
}
// Variables are evaluatable because they can be looked up by identifiers.
+6 -11
View File
@@ -46,9 +46,7 @@ public struct ParserStateDirectTransition: ParserStateInstance {
}
public var description: String {
get {
return "Instance of \(currrent_state)"
}
return "Instance of \(currrent_state)"
}
public let currrent_state: ParserState
@@ -99,9 +97,7 @@ public struct ParserStateNoTransition: ParserStateInstance {
}
public var description: String {
get {
return "Instance of \(currrent_state)"
}
return "Instance of \(currrent_state)"
}
public let currrent_state: ParserState
@@ -134,9 +130,7 @@ public struct ParserStateSelectTransition: ParserStateInstance {
}
public var description: String {
get {
return "Instance of \(currrent_state)"
}
return "Instance of \(currrent_state)"
}
public func execute(
@@ -203,7 +197,7 @@ extension ParserStates: Compilable {
public typealias ToCompile = ParserStates
public typealias Compiled = (ParserStateInstance, [ParserStateInstance])
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
var compiled_states:[ParserStateInstance] = Array()
var compiled_states: [ParserStateInstance] = Array()
compiled_states.append(ParserStateNoTransition(currrent_state: accept))
compiled_states.append(ParserStateNoTransition(currrent_state: reject))
@@ -252,7 +246,8 @@ extension ParserInstance: Compilable {
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
return switch ParserStates.compile(parser.states) {
case .Ok(let (start_state, states)): Result.Ok(ParserInstance(start: start_state, states: states))
case .Ok(let (start_state, states)):
Result.Ok(ParserInstance(start: start_state, states: states))
case .Error(let e): Result.Error(e)
}
}