exception/parser-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 ParserErrorType enum { | ||
| 10 | PARSING_FAILED, | ||
| 11 | NUMBER_OUT_OF_RANGE, | ||
| 12 | INVALID_QUALIFIER_COMBINATION, | ||
| 13 | INVALID_CHAR_LITERAL, | ||
| 14 | INVALID_ATTR_VALUE_TYPE, | ||
| 15 | RESERVED_KEYWORD, | ||
| 16 | RESERVED_TYPENAME | ||
| 17 | } | ||
| 18 | |||
| 19 | /** | ||
| 20 | * Custom exception for errors, occurring while parsing | ||
| 21 | */ | ||
| 22 | ✗ | public type ParserError struct { | |
| 23 | String errorMessage | ||
| 24 | } | ||
| 25 | |||
| 26 | /** | ||
| 27 | * Constructor: Used in case that the exact code position where the error occurred is known | ||
| 28 | * | ||
| 29 | * @param codeLoc Code location where the error occurred | ||
| 30 | * @param errorType Type of the error | ||
| 31 | * @param message Error message suffix | ||
| 32 | */ | ||
| 33 | 12 | public p ParserError.ctor(const CodeLoc& codeLoc, const ParserErrorType errorType, const string message) { | |
| 34 | 12 | StringStream msg; | |
| 35 | 12 | msg << "[Error|Parser] " << codeLoc.toPrettyString() << ": " << this.getMessagePrefix(errorType) << ": " << message; | |
| 36 | 12 | this.errorMessage = msg.str(); | |
| 37 | 12 | } | |
| 38 | |||
| 39 | /** | ||
| 40 | * Get the message for this particular error instance | ||
| 41 | * | ||
| 42 | * @return Error message | ||
| 43 | */ | ||
| 44 | 12 | public f<const String&> ParserError.getMessage() { | |
| 45 | 12 | return this.errorMessage; | |
| 46 | } | ||
| 47 | |||
| 48 | /** | ||
| 49 | * Get the prefix of the error message for a particular error | ||
| 50 | * | ||
| 51 | * @param errorType Type of the error | ||
| 52 | * @return Prefix string for the error type | ||
| 53 | */ | ||
| 54 | 12 | f<string> ParserError.getMessagePrefix(const ParserErrorType errorType) { | |
| 55 | 12 | switch errorType { | |
| 56 | 5 | case ParserErrorType::PARSING_FAILED: { return "Parsing failed"; } | |
| 57 | 1 | case ParserErrorType::NUMBER_OUT_OF_RANGE: { return "Number is out of range"; } | |
| 58 | 1 | case ParserErrorType::INVALID_QUALIFIER_COMBINATION: { return "Invalid qualifier combination"; } | |
| 59 | 1 | case ParserErrorType::INVALID_CHAR_LITERAL: { return "Invalid char literal"; } | |
| 60 | 1 | case ParserErrorType::INVALID_ATTR_VALUE_TYPE: { return "Invalid attribute value type"; } | |
| 61 | 2 | case ParserErrorType::RESERVED_KEYWORD: { return "Usage of reserved keyword"; } | |
| 62 | 1 | case ParserErrorType::RESERVED_TYPENAME: { return "Usage of reserved typename"; } | |
| 63 | − | default: { panic(Error("Unknown error")); } // LCOV_EXCL_LINE | |
| 64 | } | ||
| 65 | ✗ | } | |
| 66 |