Skip to main content

php_rs_parser/
version.rs

1use std::fmt;
2
3/// The PHP language version the parser should target.
4///
5/// When a [`PhpVersion`] is supplied via [`crate::parse_versioned`], the parser
6/// emits [`crate::diagnostics::ParseError::VersionTooLow`] for any syntax that
7/// requires a higher version.  The AST is still produced (the parser recovers),
8/// so callers can always inspect what was parsed.
9///
10/// Ordering is meaningful: `Php82 > Php81`, etc.
11///
12/// Defaults to [`PhpVersion::Php84`] (the latest supported version).
13#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
14pub enum PhpVersion {
15    /// PHP 8.0 — match, named arguments, constructor promotion, union types, nullsafe `?->`, throw expression.
16    Php80,
17    /// PHP 8.1 — enums, `readonly` properties/parameters, intersection types, first-class callables, `never`.
18    Php81,
19    /// PHP 8.2 — `readonly class`.
20    Php82,
21    /// PHP 8.3 — typed class/enum constants.
22    Php83,
23    /// PHP 8.4 — `abstract readonly class`, property hooks, asymmetric visibility.
24    Php84,
25    /// PHP 8.5 — pipe operator (`|>`), `clone` with property overrides, `final` promoted properties,
26    ///           asymmetric visibility on static properties.
27    #[default]
28    Php85,
29}
30
31impl fmt::Display for PhpVersion {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            PhpVersion::Php80 => write!(f, "8.0"),
35            PhpVersion::Php81 => write!(f, "8.1"),
36            PhpVersion::Php82 => write!(f, "8.2"),
37            PhpVersion::Php83 => write!(f, "8.3"),
38            PhpVersion::Php84 => write!(f, "8.4"),
39            PhpVersion::Php85 => write!(f, "8.5"),
40        }
41    }
42}