cexpr::expr

Struct IdentifierParser

Source
pub struct IdentifierParser<'ident> { /* private fields */ }
Expand description

Expression parser/evaluator that supports identifiers.

Implementations§

Source§

impl<'ident> IdentifierParser<'ident>

Source

pub fn new(identifiers: &HashMap<Vec<u8>, EvalResult>) -> IdentifierParser<'_>

Create a new IdentifierParser with a set of known identifiers. When a known identifier is encountered during parsing, it is substituted for the value specified.

Source

pub fn expr<'a>(&self, input: &'a [Token]) -> CResult<'a, EvalResult>

Parse and evaluate an expression of a list of tokens.

Returns an error if the input is not a valid expression or if the token stream contains comments, keywords or unknown identifiers.

Source

pub fn macro_definition<'a>( &self, input: &'a [Token], ) -> CResult<'a, (&'a [u8], EvalResult)>

Parse and evaluate a macro definition from a list of tokens.

Returns the identifier for the macro and its replacement evaluated as an expression. The input should not include #define.

Returns an error if the replacement is not a valid expression, if called on most function-like macros, or if the token stream contains comments, keywords or unknown identifiers.

N.B. This is intended to fail on function-like macros, but if it the macro takes a single argument, the argument name is defined as an identifier, and the macro otherwise parses as an expression, it will return a result even on function-like macros.

// will evaluate into IDENTIFIER
#define DELETE(IDENTIFIER)
// will evaluate into IDENTIFIER-3
#define NEGATIVE_THREE(IDENTIFIER)  -3

Trait Implementations§

Source§

impl<'ident> Debug for IdentifierParser<'ident>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'ident> Freeze for IdentifierParser<'ident>

§

impl<'ident> RefUnwindSafe for IdentifierParser<'ident>

§

impl<'ident> Send for IdentifierParser<'ident>

§

impl<'ident> Sync for IdentifierParser<'ident>

§

impl<'ident> Unpin for IdentifierParser<'ident>

§

impl<'ident> UnwindSafe for IdentifierParser<'ident>

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> 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, 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.