Skip to main content

php_rs_parser/
diagnostics.rs

1use php_ast::Span;
2use php_lexer::TokenKind;
3use std::borrow::Cow;
4use thiserror::Error;
5
6/// A parse error or diagnostic emitted during parsing.
7///
8/// The parser recovers from all errors and always produces a complete AST,
9/// so errors are informational rather than fatal. Each variant carries a
10/// [`Span`] identifying the source location.
11#[derive(Debug, Clone, Error)]
12pub enum ParseError {
13    /// A specific token was expected but a different one was found.
14    #[error("expected {expected}, found {found}")]
15    Expected {
16        expected: Cow<'static, str>,
17        found: TokenKind,
18        span: Span,
19    },
20
21    /// An expression was expected but not found (e.g. empty parentheses).
22    #[error("expected expression")]
23    ExpectedExpression { span: Span },
24
25    /// A statement was expected but not found.
26    #[error("expected statement")]
27    ExpectedStatement { span: Span },
28
29    /// PHP source must start with `<?php` or `<?`.
30    #[error("expected opening PHP tag")]
31    ExpectedOpenTag { span: Span },
32
33    /// A string literal was opened but never closed.
34    #[error("unterminated string literal")]
35    UnterminatedString { span: Span },
36
37    /// A required token was missing after another construct.
38    #[error("expected {expected} after {after}")]
39    ExpectedAfter {
40        expected: Cow<'static, str>,
41        after: Cow<'static, str>,
42        span: Span,
43    },
44
45    /// A delimiter (parenthesis, bracket, brace) was opened but never closed.
46    #[error("unclosed {delimiter} opened at {opened_at:?}")]
47    UnclosedDelimiter {
48        delimiter: Cow<'static, str>,
49        opened_at: Span,
50        span: Span,
51    },
52
53    /// A construct that is syntactically valid but semantically forbidden
54    /// (e.g. `(unset)` cast, deprecated syntax).
55    #[error("{message}")]
56    Forbidden {
57        message: Cow<'static, str>,
58        span: Span,
59    },
60
61    /// Syntax that requires a newer PHP version than the targeted one.
62    /// Emitted by [`crate::parse_versioned`] when the source uses features
63    /// unavailable in the specified [`crate::PhpVersion`].
64    #[error("'{feature}' requires PHP {required} or higher (targeting PHP {used})")]
65    VersionTooLow {
66        feature: Cow<'static, str>,
67        required: Cow<'static, str>,
68        used: Cow<'static, str>,
69        span: Span,
70    },
71}
72
73impl ParseError {
74    pub fn span(&self) -> Span {
75        match self {
76            ParseError::Expected { span, .. }
77            | ParseError::ExpectedExpression { span }
78            | ParseError::ExpectedStatement { span }
79            | ParseError::ExpectedOpenTag { span }
80            | ParseError::UnterminatedString { span }
81            | ParseError::ExpectedAfter { span, .. }
82            | ParseError::UnclosedDelimiter { span, .. }
83            | ParseError::Forbidden { span, .. }
84            | ParseError::VersionTooLow { span, .. } => *span,
85        }
86    }
87}