SyntaxInterpreter

Trait SyntaxInterpreter 

Source
pub trait SyntaxInterpreter {
    type ContextData;
    type Error;

    // Provided methods
    fn interpret_literal<'a>(
        &mut self,
        el: SyntaxElement<'a>,
        _lit: &'a str,
        _ctx: Option<&mut Self::ContextData>,
        _out: &mut String,
    ) -> SyntaxInterpreterResult<'a, (), Self::Error> { ... }
    fn interpret_bool<'a>(
        &mut self,
        el: SyntaxElement<'a>,
        _maybe_bool: MaybeBool,
        _ctx: Option<&mut Self::ContextData>,
        _out: &mut String,
    ) -> SyntaxInterpreterResult<'a, (), Self::Error> { ... }
    fn interpret_enum_substitutions<'a>(
        &mut self,
        el: SyntaxElement<'a>,
        _enum_subs: EnumSubstitutions<'a>,
        _ctx: Option<&mut Self::ContextData>,
        _out: &mut String,
    ) -> SyntaxInterpreterResult<'a, (), Self::Error> { ... }
    fn interpret_trivial_substitution<'a>(
        &mut self,
        el: SyntaxElement<'a>,
        _member_to_sub: &'a str,
        _ctx: Option<&mut Self::ContextData>,
        _out: &mut String,
    ) -> SyntaxInterpreterResult<'a, (), Self::Error> { ... }
    fn interpret_delegate_substitution<'a>(
        &mut self,
        el: SyntaxElement<'a>,
        _member_to_sub: &'a str,
        _ctx: Option<&mut Self::ContextData>,
        _out: &mut String,
    ) -> SyntaxInterpreterResult<'a, (), Self::Error> { ... }
    fn interpret_json_substitution<'a>(
        &mut self,
        el: SyntaxElement<'a>,
        _member_to_sub: &'a str,
        _ctx: Option<&mut Self::ContextData>,
        _out: &mut String,
    ) -> SyntaxInterpreterResult<'a, (), Self::Error> { ... }
    fn interpret_vec_substitution<'a>(
        &mut self,
        el: SyntaxElement<'a>,
        _member_to_sub: &'a str,
        _delim: &'a str,
        _ctx: Option<&mut Self::ContextData>,
        _out: &mut String,
    ) -> SyntaxInterpreterResult<'a, (), Self::Error> { ... }
    fn interpret_optional_context_enter<'a>(
        &mut self,
        el: SyntaxElement<'a>,
        _out: &mut String,
    ) -> SyntaxInterpreterResult<'a, Self::ContextData, Self::Error> { ... }
    fn interpret_optional_context_exit<'a>(
        &mut self,
        _el: SyntaxElement<'a>,
        _ctx: Self::ContextData,
        _out: &mut String,
    ) -> SyntaxInterpreterResult<'a, (), Self::Error> { ... }
    fn interpret_syntax<'a>(
        &mut self,
        syntax: &'a str,
        ctx: Option<&mut Self::ContextData>,
        out: &mut String,
    ) -> SyntaxInterpreterResult<'a, (), Self::Error> { ... }
}
Expand description

Implement interpret_ methods and use the provided Self::interpret_syntax to generate the whole interpreter body from the syntax.

It’s not recommended to use Self::interpret_syntax directly though, use Generator that allows to generate some code around the interpreter body.

By default all methods return SyntaxInterpreterError::Unexpected error.

The SyntaxElement<’_> parameter is included into each method only to make errors more informative.

Required Associated Types§

Source

type ContextData

A data that is passed between optional and non-optional contexts. Effectively acts as a stack frame that can be used to decide how to render items in SyntaxElement::Optional after processing the optional scope. For example, in Rust this can help to decide which syntax to use for the if statement.

Set it to () if you don’t need it.

Source

type Error

A type used in SyntaxInterpreterError::Custom variant.

Provided Methods§

Source

fn interpret_literal<'a>( &mut self, el: SyntaxElement<'a>, _lit: &'a str, _ctx: Option<&mut Self::ContextData>, _out: &mut String, ) -> SyntaxInterpreterResult<'a, (), Self::Error>

Source

fn interpret_bool<'a>( &mut self, el: SyntaxElement<'a>, _maybe_bool: MaybeBool, _ctx: Option<&mut Self::ContextData>, _out: &mut String, ) -> SyntaxInterpreterResult<'a, (), Self::Error>

Source

fn interpret_enum_substitutions<'a>( &mut self, el: SyntaxElement<'a>, _enum_subs: EnumSubstitutions<'a>, _ctx: Option<&mut Self::ContextData>, _out: &mut String, ) -> SyntaxInterpreterResult<'a, (), Self::Error>

Source

fn interpret_trivial_substitution<'a>( &mut self, el: SyntaxElement<'a>, _member_to_sub: &'a str, _ctx: Option<&mut Self::ContextData>, _out: &mut String, ) -> SyntaxInterpreterResult<'a, (), Self::Error>

Source

fn interpret_delegate_substitution<'a>( &mut self, el: SyntaxElement<'a>, _member_to_sub: &'a str, _ctx: Option<&mut Self::ContextData>, _out: &mut String, ) -> SyntaxInterpreterResult<'a, (), Self::Error>

Source

fn interpret_json_substitution<'a>( &mut self, el: SyntaxElement<'a>, _member_to_sub: &'a str, _ctx: Option<&mut Self::ContextData>, _out: &mut String, ) -> SyntaxInterpreterResult<'a, (), Self::Error>

Source

fn interpret_vec_substitution<'a>( &mut self, el: SyntaxElement<'a>, _member_to_sub: &'a str, _delim: &'a str, _ctx: Option<&mut Self::ContextData>, _out: &mut String, ) -> SyntaxInterpreterResult<'a, (), Self::Error>

Source

fn interpret_optional_context_enter<'a>( &mut self, el: SyntaxElement<'a>, _out: &mut String, ) -> SyntaxInterpreterResult<'a, Self::ContextData, Self::Error>

Invoked before diving into the optional context. Should return an instance of Self::ContextData that can be mutated by the interpret calls. Then Self::ContextData will be consumed consumed by the matching Self::interpret_optional_context_exit.

This Self::ContextData may be used to decide how to adjust the surrounding code depending on the contents of the opitonal context.

If you don’t need Self::ContextData set it to ().

Source

fn interpret_optional_context_exit<'a>( &mut self, _el: SyntaxElement<'a>, _ctx: Self::ContextData, _out: &mut String, ) -> SyntaxInterpreterResult<'a, (), Self::Error>

Invoked right after the optional context was fully processed. Consumes Self::ContextData tha created by the matching interprert_optional_context_enter.

Source

fn interpret_syntax<'a>( &mut self, syntax: &'a str, ctx: Option<&mut Self::ContextData>, out: &mut String, ) -> SyntaxInterpreterResult<'a, (), Self::Error>

Generates the full body from syntax

Implementors§