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.