Struct Parser

Source
pub struct Parser {
    pub omit_nonhat_scripts: bool,
    pub name_transformer: Box<dyn Fn(&str) -> Result<CompactString, ()>>,
    pub autofill_generator: Box<dyn Fn(usize) -> Result<CompactString, ()>>,
    pub stmt_replacements: Vec<(CompactString, Box<dyn Fn(Vec<Expr>, Box<BlockInfo>, &LocationRef<'_>) -> Result<Vec<Stmt>, Box<Error>>>)>,
    pub expr_replacements: Vec<(CompactString, Box<dyn Fn(Vec<Expr>, Box<BlockInfo>, &LocationRef<'_>) -> Result<Box<Expr>, Box<Error>>>)>,
}

Fields§

§omit_nonhat_scripts: bool

If true, the parser will skip script blocks that lack a hat block. This is typically desirable since free floating blocks are never automatically executed, and thus are typically not needed for translation efforts. Defaults to true.

§name_transformer: Box<dyn Fn(&str) -> Result<CompactString, ()>>

All symbol names in the program will be passed through this function, allowing easy conversion of Snap! names to, e.g., valid C-like identifiers. The default operation performs no conversion. Note that non-default transform strategies may also require a custom Parser::autofill_generator.

§autofill_generator: Box<dyn Fn(usize) -> Result<CompactString, ()>>

A generator used to produce symbol names for auto-fill closure arguments. The function receives a number that can be used to differentiate different generated arguments. It is expected that multiple calls to this function with the same input will produce the same output symbol name. The default is to produce a string of format %n where n is the input number. Note that, after generation, symbol names are still passed through Parser::name_transformer as usual.

§stmt_replacements: Vec<(CompactString, Box<dyn Fn(Vec<Expr>, Box<BlockInfo>, &LocationRef<'_>) -> Result<Vec<Stmt>, Box<Error>>>)>

A mapping of unknown stmt blocks to functions that replace them with a sequence of zero or more other statements. The mapping function receives as input the arguments list to the original block with replacements already recursively applied, as well as the block info for the original block and its code location. Note that replacements are not further applied to the result of this function.

§expr_replacements: Vec<(CompactString, Box<dyn Fn(Vec<Expr>, Box<BlockInfo>, &LocationRef<'_>) -> Result<Box<Expr>, Box<Error>>>)>

A mapping of unknown expr blocks to functions that replace them with another expression, which could be composed of several sub-expressions. The mapping function receives as input the arguments list to the original block with replacements already recursively applied, as well as the block info for the original block and its code location. Note that replacements are not further applied to the result of this function.

Implementations§

Source§

impl Parser

Source

pub fn parse(&self, xml: &str) -> Result<Project, Box<Error>>

Trait Implementations§

Source§

impl Default for Parser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Parser

§

impl !RefUnwindSafe for Parser

§

impl !Send for Parser

§

impl !Sync for Parser

§

impl Unpin for Parser

§

impl !UnwindSafe for Parser

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.