compiler, runtime: Refactor Errors

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-04 10:34:07 -04:00
parent b49ec104e9
commit 168d48fa7c
16 changed files with 456 additions and 304 deletions
+81 -80
View File
@@ -60,8 +60,8 @@ extension FunctionDeclaration: CompilableDeclaration {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: function_declaration_node, withError: "Missing function declaration component")))
ErrorWithLocation(
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 {
@@ -72,8 +72,8 @@ extension FunctionDeclaration: CompilableDeclaration {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: function_declaration_node, withError: "Missing function declaration component")))
ErrorWithLocation(
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 {
@@ -84,8 +84,8 @@ extension FunctionDeclaration: CompilableDeclaration {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: function_declaration_node, withError: "Missing function declaration component")))
ErrorWithLocation(
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
@@ -120,8 +120,8 @@ extension FunctionDeclaration: CompilableDeclaration {
if !context.extern_context {
return Result.Error(
ErrorOnNode(
node: function_declaration_node, withError: "Missing function declaration component"))
ErrorWithLocation(
sourceLocation: function_declaration_node.toSourceLocation(), withError: "Missing function declaration component"))
}
}
@@ -165,8 +165,8 @@ extension P4Struct: CompilableDeclaration {
#MustOr(
result: currentNode, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: struct_declaration_node, withError: "Missing function declaration component")))
ErrorWithLocation(
sourceLocation: struct_declaration_node.toSourceLocation(), withError: "Missing function declaration component")))
// The name of the struct type.
let maybe_struct_identifier = Identifier.Compile(
@@ -181,8 +181,8 @@ extension P4Struct: CompilableDeclaration {
#MustOr(
result: currentNode, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: struct_declaration_node, withError: "Missing function declaration component")))
ErrorWithLocation(
sourceLocation: struct_declaration_node.toSourceLocation(), withError: "Missing function declaration component")))
// If there are no fields, it will be a "}"
if currentNode!.nodeType == "}" {
@@ -201,7 +201,7 @@ extension P4Struct: CompilableDeclaration {
))
}
var parse_errs: [Error] = Array()
var parse_errs: (any Errorable)? = .none
var current_context = context
var parsed_fields: [P4StructFieldIdentifier] = Array()
@@ -217,18 +217,19 @@ extension P4Struct: CompilableDeclaration {
id: variable_declaration.identifier, withType: variable_declaration.initializer.type()
))
current_context = updated_context
case .Error(let e): parse_errs.append(e)
case .Error(let e):
parse_errs =
if let e = parse_errs {
parse_errs?.append(error: e)
} else {
e
}
}
}
}
if !parse_errs.isEmpty {
return .Error(
Error(
withMessage: "Error(s) parsing select cases: "
+ (parse_errs.map { error in
return "\(error.msg)"
}.joined(separator: ";"))))
if let parse_errs = parse_errs {
return .Error(ErrorWithLabel("Error(s) parsing select cases", parse_errs))
}
let declared_struct = Declaration(
@@ -263,12 +264,12 @@ extension P4Lang.Parser: CompilableDeclaration {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: parser_node, withError: "Missing elements of parser declaration")))
ErrorWithLocation(
sourceLocation: parser_node.toSourceLocation(), withError: "Missing elements of parser declaration")))
if current_node!.nodeType != "parserType" {
return .Error(
ErrorOnNode(node: current_node!, withError: "Missing type for parser declaration"))
ErrorWithLocation(sourceLocation: current_node!.toSourceLocation(), withError: "Missing type for parser declaration"))
}
let type_node = current_node
@@ -284,13 +285,13 @@ extension P4Lang.Parser: CompilableDeclaration {
#MustOr(
result: type_node_child, thing: type_node_walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: parser_node, withError: "Missing elements of parser type in parser declaration")))
ErrorWithLocation(
sourceLocation: parser_node.toSourceLocation(), withError: "Missing elements of parser type in parser declaration")))
if type_node_child!.nodeType == "annotations" {
return .Error(
ErrorOnNode(
node: type_node_child!, withError: "Annotations in parser type are not yet handled."))
ErrorWithLocation(
sourceLocation: type_node_child!.toSourceLocation(), withError: "Annotations in parser type are not yet handled."))
// Will increment indexes here.
}
@@ -298,8 +299,8 @@ extension P4Lang.Parser: CompilableDeclaration {
#MustOr(
result: type_node_child, thing: type_node_walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: type_node_child!, withError: "Missing name in parser type declaration")))
ErrorWithLocation(
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
@@ -311,8 +312,8 @@ extension P4Lang.Parser: CompilableDeclaration {
#MustOr(
result: type_node_child, thing: type_node_walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: type_node_child!, withError: "Missing parser parameters")))
ErrorWithLocation(
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)):
@@ -334,27 +335,27 @@ extension P4Lang.Parser: CompilableDeclaration {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: parser_node, withError: "Missing parser declaration component")))
ErrorWithLocation(
sourceLocation: parser_node.toSourceLocation(), withError: "Missing parser declaration component")))
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: parser_node, withError: "Missing elements of parser declaration")))
ErrorWithLocation(
sourceLocation: parser_node.toSourceLocation(), withError: "Missing elements of parser declaration")))
if current_node!.nodeType == "parserLocalElements" {
return .Error(
ErrorOnNode(node: current_node!, 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.
}
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: parser_node, withError: "Missing body of parser declaration")))
ErrorWithLocation(
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"))
@@ -398,8 +399,8 @@ extension Control: CompilableDeclaration {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: node, withError: "Missing control declaration component")))
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing control declaration component")))
guard
case .Ok(let control_name) = Identifier.Compile(
@@ -413,8 +414,8 @@ extension Control: CompilableDeclaration {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: node, withError: "Missing control declaration component")))
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing control declaration component")))
let maybe_control_parameters = ParameterList.Compile(
node: current_node!, withContext: local_context)
@@ -438,8 +439,8 @@ extension Control: CompilableDeclaration {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(Declaration, CompilerContext)?>.Error(
ErrorOnNode(
node: node, withError: "Missing control declaration component")))
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing control declaration component")))
var actions: [Action] = Array()
var tables: [Table] = Array()
@@ -489,7 +490,7 @@ extension Control: CompilableDeclaration {
// But, that is handled by the compiler.
} else {
return .Error(
ErrorOnNode(node: current_node, withError: "Uknown node type in control declaration"))
ErrorWithLocation(sourceLocation: current_node.toSourceLocation(), withError: "Uknown node type in control declaration"))
}
return .Ok(())
}
@@ -505,13 +506,13 @@ 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(
ErrorOnNode(node: node, 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(
ErrorOnNode(node: node, withError: "Missing apply in control declaration"))
ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Missing apply in control declaration"))
}
let declared_control =
@@ -555,7 +556,7 @@ extension Action: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Action, CompilerContext)>.Error(
ErrorOnNode(node: node, withError: "Missing action declaration component"))
ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"))
)
guard
@@ -570,7 +571,7 @@ extension Action: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Action, CompilerContext)>.Error(
ErrorOnNode(node: node, withError: "Missing action declaration component"))
ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"))
)
let maybe_action_parameters = ParameterList.Compile(
@@ -585,7 +586,7 @@ extension Action: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Action, CompilerContext)>.Error(
ErrorOnNode(node: node, withError: "Missing action declaration component"))
ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"))
)
// Add the parameters into scope.
@@ -631,8 +632,8 @@ extension TableKeyEntry: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error(
ErrorOnNode(
node: node, withError: "Missing table key entry declaration component")))
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing table key entry declaration component")))
let maybe_keyset_expression = KeysetExpression.compile(
node: current_node!, withContext: current_context)
@@ -646,8 +647,8 @@ extension TableKeyEntry: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error(
ErrorOnNode(
node: node, withError: "Missing table key entry declaration component")))
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing table key entry declaration component")))
let maybe_match_type = TableKeyMatchType.Compile(
node: current_node!, withContext: current_context)
@@ -670,7 +671,7 @@ extension TableKeyMatchType: Compilable {
if node.text! == "exact" {
return .Ok((TableKeyMatchType.Exact, context))
}
return .Error(ErrorOnNode(node: node, withError: "\(node.text!) is not a valid match type)"))
return .Error(ErrorWithLocation(sourceLocation: node.toSourceLocation(), withError: "\(node.text!) is not a valid match type)"))
}
}
@@ -695,8 +696,8 @@ extension TableKeys: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.TableKeys, CompilerContext)>.Error(
ErrorOnNode(
node: node, withError: "Missing table keys declaration component in control declaration"))
ErrorWithLocation(
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
@@ -708,11 +709,11 @@ extension TableKeys: Compilable {
if !errors.isEmpty {
return .Error(
ErrorOnNode(
node: node,
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Error(s) parsing table key: "
+ (errors.map { error in
return "\(error.msg)"
return "\(error.msg())"
}.joined(separator: ";"))))
}
@@ -741,8 +742,8 @@ extension TableActionsProperty: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(TableActionsProperty, CompilerContext)>.Error(
ErrorOnNode(
node: node,
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing table actions declaration component in control declaration"))
)
@@ -756,7 +757,7 @@ extension TableActionsProperty: Compilable {
return .Ok(TypedIdentifier(id: listed_action, withType: P4QualifiedType(maybe_action)))
}
return .Error(
ErrorOnNode(node: node, 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)
@@ -765,11 +766,11 @@ extension TableActionsProperty: Compilable {
if !errors.isEmpty {
return .Error(
ErrorOnNode(
node: node,
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Error(s) parsing table actions: "
+ (errors.map { error in
return "\(error.msg)"
return "\(error.msg())"
}.joined(separator: ";"))))
}
@@ -789,7 +790,7 @@ extension TablePropertyList: Compilable {
var keys: [TableKeys] = Array()
var actions: [TableActionsProperty] = Array()
var errors: [Error] = Array()
var errors: [any Errorable] = Array()
node.enumerateNamedChildren { child in
if child.nodeType == "table_keys" {
@@ -808,17 +809,17 @@ extension TablePropertyList: Compilable {
}
} else {
errors.append(
ErrorOnNode(node: child, withError: "Uknown node type in control declaration"))
ErrorWithLocation(sourceLocation: child.toSourceLocation(), withError: "Uknown node type in control declaration"))
}
}
if !errors.isEmpty {
return .Error(
ErrorOnNode(
node: node,
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Error(s) parsing property list: "
+ (errors.map { error in
return "\(error.msg)"
return "\(error.msg())"
}.joined(separator: ";"))))
}
@@ -826,14 +827,14 @@ extension TablePropertyList: Compilable {
if keys.count > 1 {
// Todo: Make this error message better.
return .Error(
ErrorOnNode(node: node, 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(
ErrorOnNode(node: node, 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())
@@ -864,8 +865,8 @@ extension Table: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Table, CompilerContext)>.Error(
ErrorOnNode(
node: node, withError: "Missing table declaration component")))
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component")))
guard
case .Ok(let table_name) = Identifier.Compile(
@@ -881,8 +882,8 @@ extension Table: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Table, CompilerContext)>.Error(
ErrorOnNode(
node: node, withError: "Missing table declaration component")))
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component")))
let maybe_table_property_list = TablePropertyList.Compile(
node: current_node!, withContext: current_context)
@@ -928,8 +929,8 @@ extension ExternDeclaration: CompilableDeclaration {
guard let found_ffi = found_ffi else {
return .Error(
ErrorOnNode(
node: declarationed_node,
ErrorWithLocation(
sourceLocation: declarationed_node.toSourceLocation(),
withError:
"Could not find a foreign function that matches the extern declaration (\(declared))"))
}