Diagnostics class for error reporting in parser. Formats parse errors with source location context and raises directly, without going through DiagnosticContext. Parameters ---------- source : Source The source code.
| 259 | |
| 260 | |
| 261 | class Diagnostics: |
| 262 | """Diagnostics class for error reporting in parser. |
| 263 | |
| 264 | Formats parse errors with source location context and raises directly, |
| 265 | without going through DiagnosticContext. |
| 266 | |
| 267 | Parameters |
| 268 | ---------- |
| 269 | source : Source |
| 270 | The source code. |
| 271 | """ |
| 272 | |
| 273 | source: Source |
| 274 | |
| 275 | def __init__(self, source: Source): |
| 276 | self.source = source |
| 277 | |
| 278 | def error(self, node: doc.AST, message: str) -> None: |
| 279 | """Emit a diagnostic error by raising with source location context. |
| 280 | |
| 281 | Parameters |
| 282 | ---------- |
| 283 | node : doc.AST |
| 284 | The node with diagnostic error. |
| 285 | |
| 286 | message : str |
| 287 | The diagnostic message. |
| 288 | """ |
| 289 | lineno = getattr(node, "lineno", 1) |
| 290 | col_offset = getattr(node, "col_offset", self.source.start_column) |
| 291 | end_lineno = getattr(node, "end_lineno", lineno) |
| 292 | end_col_offset = getattr(node, "end_col_offset", col_offset) |
| 293 | lineno += self.source.start_line - 1 |
| 294 | end_lineno += self.source.start_line - 1 |
| 295 | col_offset += self.source.start_column + 1 |
| 296 | end_col_offset += self.source.start_column + 1 |
| 297 | |
| 298 | source_lines = self.source.full_source.splitlines(keepends=True) |
| 299 | snippet = _format_source_snippet( |
| 300 | source_lines, lineno, col_offset, end_lineno, end_col_offset |
| 301 | ) |
| 302 | |
| 303 | location = f"{self.source.source_name}:{lineno}:{col_offset}" |
| 304 | formatted = f"error: {message}\n --> {location}\n{snippet}" |
| 305 | raise DiagnosticError(formatted) |
no outgoing calls
no test coverage detected
searching dependent graphs…