exception/compiler-error.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/text/stringstream"; | ||
| 5 | |||
| 6 | // Own imports | ||
| 7 | import "@/reader/code-loc"; | ||
| 8 | |||
| 9 | public type CompilerErrorType enum { | ||
| 10 | UNRESOLVED_SOFT_ERRORS, | ||
| 11 | SOURCE_FILE_NOT_FOUND, | ||
| 12 | CANT_OPEN_OUTPUT_FILE, | ||
| 13 | WRONG_OUTPUT_TYPE, | ||
| 14 | INTERNAL_ERROR, | ||
| 15 | IO_ERROR, | ||
| 16 | STD_NOT_FOUND, | ||
| 17 | TARGET_NOT_AVAILABLE, | ||
| 18 | OOM, | ||
| 19 | INVALID_FUNCTION, | ||
| 20 | INVALID_MODULE | ||
| 21 | } | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Custom exception for errors, occurring in the general context of the compiler | ||
| 25 | */ | ||
| 26 | ✗ | public type CompilerError struct { | |
| 27 | String errorMessage | ||
| 28 | } | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @param errorType Type of the error | ||
| 32 | * @param message Error message suffix | ||
| 33 | */ | ||
| 34 | 183 | public p CompilerError.ctor(const CompilerErrorType errorType, const string message) { | |
| 35 | 183 | StringStream msg; | |
| 36 | 183 | msg << "[Error|Compiler]:" << endl(); | |
| 37 | 183 | msg << this.getMessagePrefix(errorType) << ": " << message; | |
| 38 | 183 | this.errorMessage = msg.str(); | |
| 39 | 183 | } | |
| 40 | |||
| 41 | /** | ||
| 42 | * @param codeLoc Code location, where the error occurred | ||
| 43 | * @param errorType Type of the error | ||
| 44 | * @param message Error message suffix | ||
| 45 | */ | ||
| 46 | ✗ | public p CompilerError.ctor(const CodeLoc& codeLoc, const CompilerErrorType errorType, const string message) { | |
| 47 | ✗ | StringStream msg; | |
| 48 | ✗ | msg << "[Error|Compiler] " << codeLoc.toPrettyString() << ":" << endl(); | |
| 49 | ✗ | msg << this.getMessagePrefix(errorType) << ": " << message; | |
| 50 | ✗ | this.errorMessage = msg.str(); | |
| 51 | ✗ | } | |
| 52 | |||
| 53 | /** | ||
| 54 | * Get the message for this particular error instance | ||
| 55 | * | ||
| 56 | * @return Error message | ||
| 57 | */ | ||
| 58 | 183 | public f<const String&> CompilerError.getMessage() { | |
| 59 | 183 | return this.errorMessage; | |
| 60 | } | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Get the prefix of the error message for a particular error | ||
| 64 | * | ||
| 65 | * @param errorType Type of the error | ||
| 66 | * @return Prefix string for the error type | ||
| 67 | */ | ||
| 68 | 183 | f<string> CompilerError.getMessagePrefix(const CompilerErrorType errorType) { | |
| 69 | 183 | switch errorType { | |
| 70 | 181 | case CompilerErrorType::UNRESOLVED_SOFT_ERRORS: { return "Unresolved soft errors"; } | |
| 71 | 1 | case CompilerErrorType::SOURCE_FILE_NOT_FOUND: { return "Source file not found"; } | |
| 72 | ✗ | case CompilerErrorType::CANT_OPEN_OUTPUT_FILE: { return "Could not open output file"; } | |
| 73 | ✗ | case CompilerErrorType::WRONG_OUTPUT_TYPE: { return "Wrong type of output file"; } | |
| 74 | ✗ | case CompilerErrorType::INTERNAL_ERROR: { return "Internal compiler error"; } | |
| 75 | 1 | case CompilerErrorType::IO_ERROR: { return "I/O Error"; } | |
| 76 | ✗ | case CompilerErrorType::STD_NOT_FOUND: { return "Std lib not found"; } | |
| 77 | ✗ | case CompilerErrorType::TARGET_NOT_AVAILABLE: { return "Selected target not available"; } | |
| 78 | ✗ | case CompilerErrorType::OOM: { return "An out of memory error occurred"; } | |
| 79 | ✗ | case CompilerErrorType::INVALID_FUNCTION: { return "Invalid function"; } | |
| 80 | ✗ | case CompilerErrorType::INVALID_MODULE: { return "Invalid module"; } | |
| 81 | − | default: { panic(Error("Unknown error")); } // LCOV_EXCL_LINE | |
| 82 | } | ||
| 83 | ✗ | } | |
| 84 |