Skip to main content

mago_codex/
misc.rs

1use serde::Deserialize;
2use serde::Serialize;
3
4use mago_atom::Atom;
5use mago_span::Span;
6
7/// Represents a PHP variable identifier (e.g., `$foo`, `$this`).
8/// Wraps a `Atom` which holds the interned name (including '$').
9#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
10pub struct VariableIdentifier(
11    /// The atom for the variable name (e.g., "$foo").
12    pub Atom,
13);
14
15/// Identifies the target of an expression, distinguishing simple variables from property accesses.
16#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
17pub enum ExpressionIdentifier {
18    /// A simple variable identifier.
19    ///
20    /// * `VariableIdentifier` - The identifier for the variable (e.g., `$foo`).
21    Variable(VariableIdentifier),
22    /// An instance property access (e.g., `$this->prop`, `$user->name`).
23    ///
24    /// * `VariableIdentifier` - The identifier for the object variable (e.g., `$this`, `$user`).
25    /// * `Span` - The source code location covering the property name part (e.g., `prop` or `name`).
26    /// * `Atom` - The name of the property being accessed (e.g., `prop`, `name`).
27    InstanceProperty(VariableIdentifier, Span, Atom),
28}
29
30/// Identifies the scope where a generic template parameter (`@template`) is defined.
31#[derive(PartialEq, Eq, Hash, Clone, Copy, Serialize, Deserialize, PartialOrd, Ord, Debug)]
32pub enum GenericParent {
33    /// The template is defined on a class, interface, trait, or enum.
34    /// * `Atom` - The fully qualified name (FQCN) of the class-like structure.
35    ClassLike(Atom),
36    /// The template is defined on a function or method.
37    /// * `(Atom, Atom)` - A tuple representing the function/method.
38    ///   - `.0`: The FQCN of the class if it's a method, or the FQN of the function if global/namespaced.
39    ///   - `.1`: The method name if it's a method, or `Atom::empty()` if it's a function.
40    FunctionLike((Atom, Atom)),
41}
42
43impl std::fmt::Display for GenericParent {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        match self {
46            GenericParent::ClassLike(id) => write!(f, "{id}"),
47            GenericParent::FunctionLike(id) => {
48                let part1 = id.0;
49                let part2 = id.1;
50
51                if part1.is_empty() { write!(f, "{part2}()") } else { write!(f, "{part1}::{part2}()") }
52            }
53        }
54    }
55}