pub struct Macro<K> { /* private fields */ }Expand description
A validated, compiled macro: a name and an ordered list of rules.
Building a Macro does all the work that does not depend on an invocation:
it checks every rule (see MacroError) and compiles each pattern into a
matching automaton and each template into a flat program. A macro is
immutable once built and can be expanded any number of times, by any number
of Expanders.
When expanded, the rules are tried in order and the first rule whose pattern
matches the invocation is used, exactly like the arms of macro_rules!.
§Examples
A two-rule macro: () expands to 0, anything else is passed through.
use macro_lang::{Fragment, Kleene, Macro, Pattern, Rule, Template};
use intern_lang::Interner;
use token_lang::Span;
let mut names = Interner::new();
let t = names.intern("t");
let mac: Macro<char> = Macro::new(names.intern("or_zero"), vec![
Rule { pattern: vec![], template: vec![Template::token('0', Span::new(0, 1))] },
Rule {
pattern: vec![Pattern::Repeat {
body: vec![Pattern::Bind { name: t, fragment: Fragment::Tree }],
separator: None,
kleene: Kleene::OneOrMore,
}],
template: vec![Template::Repeat { body: vec![Template::Var(t)], separator: None }],
},
])?;
assert_eq!(mac.name(), names.intern("or_zero"));Implementations§
Source§impl<K> Macro<K>
impl<K> Macro<K>
Sourcepub fn new(name: Symbol, rules: Vec<Rule<K>>) -> Result<Self, MacroError>
pub fn new(name: Symbol, rules: Vec<Rule<K>>) -> Result<Self, MacroError>
Validates and compiles a macro from its rules.
§Parameters
name— the macro’s name. It is recorded in theOriginof every context the macro’s expansions mint, so diagnostics can say which macro introduced a token.rules— the arms, in the order they are tried.
§Errors
Returns the first problem found, checking rules in order:
MacroError::NoRulesifrulesis empty.MacroError::DuplicateBindingif a pattern binds a name twice.MacroError::EmptyRepetitionif a pattern repetition can match nothing.MacroError::OptionalSeparatorif a?repetition has a separator.MacroError::UnboundVariableif a template uses a name the pattern does not bind.MacroError::StillRepeatingif a template uses a repeating metavariable outside enough repetitions.MacroError::NoRepeatingVariableif a template repetition has nothing to repeat over.
§Examples
use macro_lang::{Fragment, Kleene, Macro, MacroError, Pattern, Rule, Template};
use intern_lang::Interner;
let mut names = Interner::new();
let x = names.intern("x");
// `$($x:tt)*` captured `x` inside a repetition, so the template cannot
// use it bare.
let err = Macro::<char>::new(names.intern("m"), vec![Rule {
pattern: vec![Pattern::Repeat {
body: vec![Pattern::Bind { name: x, fragment: Fragment::Tree }],
separator: None,
kleene: Kleene::ZeroOrMore,
}],
template: vec![Template::Var(x)],
}]).unwrap_err();
assert_eq!(err, MacroError::StillRepeating { rule: 0, name: x });
assert_eq!(Macro::<char>::new(x, vec![]).unwrap_err(), MacroError::NoRules);Sourcepub const fn name(&self) -> Symbol
pub const fn name(&self) -> Symbol
Returns the macro’s name.
§Examples
use macro_lang::{Macro, Rule};
use intern_lang::Interner;
let mut names = Interner::new();
let empty = names.intern("empty");
let mac: Macro<char> = Macro::new(empty, vec![Rule { pattern: vec![], template: vec![] }])?;
assert_eq!(names.resolve(mac.name()), Some("empty"));Trait Implementations§
Auto Trait Implementations§
impl<K> Forget for Macro<K>
impl<K> Freeze for Macro<K>
impl<K> Move for Macro<K>
impl<K> RefUnwindSafe for Macro<K>where
Vec<CompiledRule<K>>: RefUnwindSafe,
impl<K> Send for Macro<K>
impl<K> Sync for Macro<K>
impl<K> Unpin for Macro<K>
impl<K> UnsafeUnpin for Macro<K>where
Vec<CompiledRule<K>>: UnsafeUnpin,
impl<K> UnwindSafe for Macro<K>where
Vec<CompiledRule<K>>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more