|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from enum import Enum |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | + |
| 7 | +class ErrorCategory(str, Enum): |
| 8 | + PARSER = "parser" |
| 9 | + SEMANTIC = "semantic" |
| 10 | + CODEGEN = "codegen" |
| 11 | + IMPORT = "import" |
| 12 | + |
| 13 | + |
| 14 | +class CompileTimeError: |
| 15 | + code: str = "FS-COMP-0000" |
| 16 | + category: ErrorCategory = ErrorCategory.PARSER |
| 17 | + message_template: str = "{message}" |
| 18 | + |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + *, |
| 22 | + source_file: Optional[str] = None, |
| 23 | + line: int = 0, |
| 24 | + column: int = 0, |
| 25 | + snippet: Optional[str] = None, |
| 26 | + **context: object, |
| 27 | + ): |
| 28 | + self.source_file = source_file |
| 29 | + self.line = line |
| 30 | + self.column = column |
| 31 | + self.snippet = snippet |
| 32 | + self.message = self._render(context) |
| 33 | + |
| 34 | + def _render(self, context: dict[str, object]) -> str: |
| 35 | + try: |
| 36 | + return self.message_template.format(**context) |
| 37 | + except Exception: |
| 38 | + # Fallback to avoid formatter crashes hiding the actual compiler error. |
| 39 | + return self.message_template |
| 40 | + |
| 41 | + def to_log_string(self) -> str: |
| 42 | + header = self.message |
| 43 | + if ( |
| 44 | + self.source_file |
| 45 | + and self.line > 0 |
| 46 | + and self.column >= 0 |
| 47 | + and self.snippet is not None |
| 48 | + ): |
| 49 | + return ( |
| 50 | + header |
| 51 | + + f"\n> {self.snippet.rstrip()}\n" |
| 52 | + + " " * (self.column + 1) |
| 53 | + + "^" |
| 54 | + + f"\n({self.source_file}:{self.line}:{self.column})" |
| 55 | + ) |
| 56 | + return header |
| 57 | + |
| 58 | + |
| 59 | +class ParserError(CompileTimeError): |
| 60 | + code = "FS-PARSE-0001" |
| 61 | + category = ErrorCategory.PARSER |
| 62 | + message_template = "{message}" |
| 63 | + |
| 64 | + |
| 65 | +class UnexpectedTokenError(CompileTimeError): |
| 66 | + code = "FS-PARSE-0002" |
| 67 | + category = ErrorCategory.PARSER |
| 68 | + message_template = "Expected {expected} but got {actual}" |
| 69 | + |
| 70 | + |
| 71 | +class UndefinedIdentifierError(CompileTimeError): |
| 72 | + code = "FS-PARSE-0003" |
| 73 | + category = ErrorCategory.PARSER |
| 74 | + message_template = "Variable '{identifier}' not defined" |
| 75 | + |
| 76 | + |
| 77 | +class SemanticError(CompileTimeError): |
| 78 | + code = "FS-SEM-0001" |
| 79 | + category = ErrorCategory.SEMANTIC |
| 80 | + message_template = "{message}" |
| 81 | + |
| 82 | + |
| 83 | +class TypeError(CompileTimeError): |
| 84 | + code = "FS-SEM-0002" |
| 85 | + category = ErrorCategory.SEMANTIC |
| 86 | + message_template = "{detail}" |
| 87 | + |
| 88 | + |
| 89 | +class CodegenError(CompileTimeError): |
| 90 | + code = "FS-CGEN-0001" |
| 91 | + category = ErrorCategory.CODEGEN |
| 92 | + message_template = "{message}" |
| 93 | + |
| 94 | + |
| 95 | +class ImportNotFoundError(CompileTimeError): |
| 96 | + code = "FS-IMP-0001" |
| 97 | + category = ErrorCategory.IMPORT |
| 98 | + message_template = "Module not found: {module}" |
| 99 | + |
| 100 | + |
| 101 | +class CyclicImportError(CompileTimeError): |
| 102 | + code = "FS-IMP-0002" |
| 103 | + category = ErrorCategory.IMPORT |
| 104 | + message_template = "Cyclic import detected: {cycle}" |
| 105 | + |
| 106 | + |
| 107 | +# Additional Parser Errors for specific syntax issues |
| 108 | +class ExpectedTokenError(CompileTimeError): |
| 109 | + code = "FS-PARSE-0010" |
| 110 | + category = ErrorCategory.PARSER |
| 111 | + message_template = "Expected {expected}" |
| 112 | + |
| 113 | + |
| 114 | +class MissingIdentifierError(CompileTimeError): |
| 115 | + code = "FS-PARSE-0011" |
| 116 | + category = ErrorCategory.PARSER |
| 117 | + message_template = "Expected identifier" |
| 118 | + |
| 119 | + |
| 120 | +class InvalidExpressionError(CompileTimeError): |
| 121 | + code = "FS-PARSE-0012" |
| 122 | + category = ErrorCategory.PARSER |
| 123 | + message_template = "{detail}" |
| 124 | + |
| 125 | + |
| 126 | +class InvalidArrayAccessError(CompileTimeError): |
| 127 | + code = "FS-PARSE-0013" |
| 128 | + category = ErrorCategory.PARSER |
| 129 | + message_template = "{detail}" |
| 130 | + |
| 131 | + |
| 132 | +class InvalidFieldAccessError(CompileTimeError): |
| 133 | + code = "FS-PARSE-0014" |
| 134 | + category = ErrorCategory.PARSER |
| 135 | + message_template = "{detail}" |
| 136 | + |
| 137 | + |
| 138 | +# Type System Errors |
| 139 | +class InvalidTypeError(CompileTimeError): |
| 140 | + code = "FS-SEM-0010" |
| 141 | + category = ErrorCategory.SEMANTIC |
| 142 | + message_template = "{detail}" |
| 143 | + |
| 144 | + |
| 145 | +class FieldNotFoundError(CompileTimeError): |
| 146 | + code = "FS-SEM-0011" |
| 147 | + category = ErrorCategory.SEMANTIC |
| 148 | + message_template = "Type '{type_name}' has no field '{field_name}'" |
| 149 | + |
| 150 | + |
| 151 | +class MethodNotFoundError(CompileTimeError): |
| 152 | + code = "FS-SEM-0012" |
| 153 | + category = ErrorCategory.SEMANTIC |
| 154 | + message_template = "Type '{type_name}' has no method '{method_name}'" |
| 155 | + |
| 156 | + |
| 157 | +class ConstructorNotFoundError(CompileTimeError): |
| 158 | + code = "FS-SEM-0013" |
| 159 | + category = ErrorCategory.SEMANTIC |
| 160 | + message_template = "No constructor defined for type '{type_name}'" |
| 161 | + |
| 162 | + |
| 163 | +class InvalidOperatorError(CompileTimeError): |
| 164 | + code = "FS-SEM-0014" |
| 165 | + category = ErrorCategory.SEMANTIC |
| 166 | + message_template = "Operator '{operator}' is not valid for type '{type_name}'" |
| 167 | + |
| 168 | + |
| 169 | +class ControlFlowError(CompileTimeError): |
| 170 | + code = "FS-SEM-0015" |
| 171 | + category = ErrorCategory.SEMANTIC |
| 172 | + message_template = "{statement} statement not within a loop" |
| 173 | + |
| 174 | + |
| 175 | +class InvalidSuperError(CompileTimeError): |
| 176 | + code = "FS-SEM-0016" |
| 177 | + category = ErrorCategory.SEMANTIC |
| 178 | + message_template = "{detail}" |
0 commit comments