exception/lexer-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 LexerErrorType enum { | ||
| 10 | TOKENIZING_FAILED | ||
| 11 | } | ||
| 12 | |||
| 13 | /** | ||
| 14 | * Custom exception for errors, occurring while lexing | ||
| 15 | */ | ||
| 16 | ✗ | public type LexerError struct { | |
| 17 | String errorMessage | ||
| 18 | } | ||
| 19 | |||
| 20 | /** | ||
| 21 | * Constructor: Used in case that the exact code position where the error occurred is known | ||
| 22 | * | ||
| 23 | * @param codeLoc Code location where the error occurred | ||
| 24 | * @param errorType Type of the error | ||
| 25 | * @param message Error message suffix | ||
| 26 | */ | ||
| 27 | 1 | public p LexerError.ctor(const CodeLoc& codeLoc, const LexerErrorType errorType, const string message) { | |
| 28 | 1 | StringStream msg; | |
| 29 | 1 | msg << "[Error|Lexer] " << codeLoc.toPrettyString() << ": " << this.getMessagePrefix(errorType) << ": " << message; | |
| 30 | 1 | this.errorMessage = msg.str(); | |
| 31 | 1 | } | |
| 32 | |||
| 33 | /** | ||
| 34 | * Get the message for this particular error instance | ||
| 35 | * | ||
| 36 | * @return Error message | ||
| 37 | */ | ||
| 38 | 1 | public f<const String&> LexerError.getMessage() { | |
| 39 | 1 | return this.errorMessage; | |
| 40 | } | ||
| 41 | |||
| 42 | /** | ||
| 43 | * Get the prefix of the error message for a particular error | ||
| 44 | * | ||
| 45 | * @param errorType Type of the error | ||
| 46 | * @return Prefix string for the error type | ||
| 47 | */ | ||
| 48 | 1 | f<string> LexerError.getMessagePrefix(const LexerErrorType errorType) { | |
| 49 | 1 | switch errorType { | |
| 50 | 1 | case LexerErrorType::TOKENIZING_FAILED: { return "Tokenizing failed"; } | |
| 51 | − | default: { panic(Error("Unknown error")); } // LCOV_EXCL_LINE | |
| 52 | } | ||
| 53 | ✗ | } | |
| 54 |