diff --git a/Sources/P4Compiler/Parser.swift b/Sources/P4Compiler/Parser.swift index 4744b7b..109d1e4 100644 --- a/Sources/P4Compiler/Parser.swift +++ b/Sources/P4Compiler/Parser.swift @@ -157,7 +157,7 @@ public struct Parser { 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 parsed_s: [EvaluatableStatement] = Array() @@ -169,17 +169,18 @@ public struct Parser { current_context = updated_context parsed_s.append(parsed_statement) case .Error(let e): - parse_errs.append(e) + errors = if let errors = errors { + errors.append(error: e) + } else { + e + } } } - if !parse_errs.isEmpty { - return Result.Error( - Error( - withMessage: parse_errs.map { err in - return String(err.format()) - }.joined(separator: ";"))) + if let errors = errors { + return .Error(errors) } + return Result.Ok((parsed_s, current_context)) } } @@ -242,7 +243,7 @@ public struct Parser { 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 parsed_s: [EvaluatableStatement] = Array() @@ -254,17 +255,17 @@ public struct Parser { parsed_s = state_statements current_context = updated_context case .Error(let error): - parse_errs.append(error) + errors = if let errors = errors { + errors.append(error: error) + } else { + error + } } walker.next() } - if !parse_errs.isEmpty { - return Result.Error( - Error( - withMessage: parse_errs.map { err in - return String(err.format()) - }.joined(separator: ";"))) + if let errors = errors { + return .Error(errors) } #MustOr( diff --git a/Sources/P4Compiler/Program.swift b/Sources/P4Compiler/Program.swift index 343e5b7..8c3ac63 100644 --- a/Sources/P4Compiler/Program.swift +++ b/Sources/P4Compiler/Program.swift @@ -60,7 +60,7 @@ public struct Program { // Add our 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 let globalInstances = globalInstances { @@ -90,27 +90,33 @@ public struct Program { break case .Error(let e): found_parser = true - errors.append(e) + errors = if let errors = errors { + errors.append(error: e) + } else { + e + } break } } // If none of the declaration parsers chose to parse, that's an error, too! if !found_parser { - errors.append( - ErrorWithLocation( - sourceLocation: specific_declaration_node.toSourceLocation(), - withError: "Could not find parser for declaration node" - )) + + let no_parser_error = ErrorWithLocation( + sourceLocation: specific_declaration_node.toSourceLocation(), + 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 { - return Result.Error( - Error( - withMessage: errors.map { error in - return error.format() - }.joined(separator: ";"))) + if let errors = errors { + return .Error(errors) } // Any of the instances that are in the top-level scope should go into the program!