Enum Expression

Source
pub enum Expression {
    Parsed(Box<ExpressionAst>),
    Unparsed(String),
}
Expand description

An expression that may be parsed or unparsed

This is the core type for representing expressions in the IR. It gracefully handles both structured and unstructured forms.

§Learning: Enums for Polymorphism

Instead of inheritance (like in C++), Rust uses enums to represent “one of several types”. This is more explicit and type-safe.

§Learning: Box for Recursion

The Parsed variant contains Box<ExpressionAst> instead of ExpressionAst directly. Why? Because ExpressionAst itself contains Expression values (recursion!).

Without Box, the type would have infinite size. Box provides indirection through a heap pointer, breaking the cycle.

§Example

use roup::ir::{Expression, ParserConfig};

let config = ParserConfig::default();

// Simple expression gets parsed
let simple = Expression::new("42", &config);
assert!(simple.is_parsed());

// Complex expression falls back to string
let complex = Expression::new("sizeof(struct foo)", &config);
// May or may not be parsed depending on parser capability

// With parsing disabled, always unparsed
let config_no_parse = ParserConfig::string_only(roup::ir::Language::C);
let expr = Expression::new("N * 2", &config_no_parse);
assert!(!expr.is_parsed());
assert_eq!(expr.as_str(), "N * 2");

Variants§

§

Parsed(Box<ExpressionAst>)

Expression was successfully parsed into structured form

The compiler can analyze the AST structure for optimization, validation, or transformation.

§

Unparsed(String)

Expression kept as raw string

This happens when:

  • Expression parsing is disabled
  • Expression is too complex for the parser
  • Parser doesn’t support this language construct yet

The compiler must parse this string according to the source language.

Implementations§

Source§

impl Expression

Source

pub fn new(raw: impl Into<String>, config: &ParserConfig) -> Self

Create a new expression, attempting to parse if enabled

§Example
use roup::ir::{Expression, ParserConfig, Language};

let config = ParserConfig::default();
let expr = Expression::new("100", &config);
assert_eq!(expr.as_str(), "100");
Source

pub fn unparsed(raw: impl Into<String>) -> Self

Create an unparsed expression directly

Useful when you know parsing will fail or you want to bypass it.

Source

pub fn as_str(&self) -> &str

Get the raw string representation

This always works, whether the expression is parsed or not. The original source is always preserved.

Source

pub const fn is_parsed(&self) -> bool

Check if expression was successfully parsed

Source

pub fn as_ast(&self) -> Option<&ExpressionAst>

Get the parsed AST if available

Trait Implementations§

Source§

impl Clone for Expression

Source§

fn clone(&self) -> Expression

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Expression

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Expression

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Expression> for ClauseItem

Source§

fn from(expr: Expression) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Expression

Source§

fn eq(&self, other: &Expression) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Expression

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.