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::Php85`] (the latest supported version).
13#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
14pub enum PhpVersion {
15    /// PHP 7.4 — arrow functions, typed properties, spread in array expressions, numeric literal separator.
16    Php74,
17    /// PHP 8.0 — match, named arguments, constructor promotion, union types, nullsafe `?->`, throw expression, `mixed`/`false`/`null` types.
18    Php80,
19    /// PHP 8.1 — enums, `readonly` properties/parameters, intersection types, first-class callables, `never` type.
20    Php81,
21    /// PHP 8.2 — `readonly class`, DNF types, `true` type.
22    Php82,
23    /// PHP 8.3 — typed class/enum constants.
24    Php83,
25    /// PHP 8.4 — `abstract readonly class`, property hooks, asymmetric visibility.
26    Php84,
27    /// PHP 8.5 — pipe operator (`|>`), `clone` with property overrides, `final` promoted properties,
28    ///           asymmetric visibility on static properties.
29    #[default]
30    Php85,
31}
32
33impl fmt::Display for PhpVersion {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            PhpVersion::Php74 => write!(f, "7.4"),
37            PhpVersion::Php80 => write!(f, "8.0"),
38            PhpVersion::Php81 => write!(f, "8.1"),
39            PhpVersion::Php82 => write!(f, "8.2"),
40            PhpVersion::Php83 => write!(f, "8.3"),
41            PhpVersion::Php84 => write!(f, "8.4"),
42            PhpVersion::Php85 => write!(f, "8.5"),
43        }
44    }
45}