compiler: Use New Error API to Accumulate Errors

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-04 22:23:52 -04:00
parent e4d6daa8fe
commit 783aac26c7
2 changed files with 36 additions and 29 deletions
+17 -16
View File
@@ -157,7 +157,7 @@ public struct Parser {
sourceLocation: node.toSourceLocation(), withError: "Did not find expected statements")) sourceLocation: node.toSourceLocation(), withError: "Did not find expected statements"))
} }
var parse_errs: [any Errorable] = Array() var errors: (any Errorable)? = .none
var current_context = context var current_context = context
var parsed_s: [EvaluatableStatement] = Array() var parsed_s: [EvaluatableStatement] = Array()
@@ -169,17 +169,18 @@ public struct Parser {
current_context = updated_context current_context = updated_context
parsed_s.append(parsed_statement) parsed_s.append(parsed_statement)
case .Error(let e): case .Error(let e):
parse_errs.append(e) errors = if let errors = errors {
errors.append(error: e)
} else {
e
}
} }
} }
if !parse_errs.isEmpty { if let errors = errors {
return Result.Error( return .Error(errors)
Error(
withMessage: parse_errs.map { err in
return String(err.format())
}.joined(separator: ";")))
} }
return Result.Ok((parsed_s, current_context)) return Result.Ok((parsed_s, current_context))
} }
} }
@@ -242,7 +243,7 @@ public struct Parser {
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 errors: (any Errorable)? = .none
var current_context = context var current_context = context
var parsed_s: [EvaluatableStatement] = Array() var parsed_s: [EvaluatableStatement] = Array()
@@ -254,17 +255,17 @@ public struct Parser {
parsed_s = state_statements parsed_s = state_statements
current_context = updated_context current_context = updated_context
case .Error(let error): case .Error(let error):
parse_errs.append(error) errors = if let errors = errors {
errors.append(error: error)
} else {
error
}
} }
walker.next() walker.next()
} }
if !parse_errs.isEmpty { if let errors = errors {
return Result.Error( return .Error(errors)
Error(
withMessage: parse_errs.map { err in
return String(err.format())
}.joined(separator: ";")))
} }
#MustOr( #MustOr(
+17 -11
View File
@@ -60,7 +60,7 @@ public struct Program {
// Add our FFIs // Add our FFIs
compilation_context = compilation_context.update(newFFIs: ffis) compilation_context = compilation_context.update(newFFIs: ffis)
var errors: [any Errorable] = Array() var errors: (any Errorable)? = .none
// If the caller gave any global instances, add them here. // If the caller gave any global instances, add them here.
if let globalInstances = globalInstances { if let globalInstances = globalInstances {
@@ -90,27 +90,33 @@ public struct Program {
break break
case .Error(let e): case .Error(let e):
found_parser = true found_parser = true
errors.append(e) errors = if let errors = errors {
errors.append(error: e)
} else {
e
}
break break
} }
} }
// If none of the declaration parsers chose to parse, that's an error, too! // If none of the declaration parsers chose to parse, that's an error, too!
if !found_parser { if !found_parser {
errors.append(
ErrorWithLocation( let no_parser_error = ErrorWithLocation(
sourceLocation: specific_declaration_node.toSourceLocation(), sourceLocation: specific_declaration_node.toSourceLocation(),
withError: "Could not find parser for declaration node" withError: "Could not find parser for declaration node"
)) )
errors =
if let errors = errors {
errors.append(error: no_parser_error)
} else {
no_parser_error
}
} }
} }
if !errors.isEmpty { if let errors = errors {
return Result.Error( return .Error(errors)
Error(
withMessage: errors.map { error in
return error.format()
}.joined(separator: ";")))
} }
// Any of the instances that are in the top-level scope should go into the program! // Any of the instances that are in the top-level scope should go into the program!