Skip to main content

Pattern

Enum Pattern 

Source
#[non_exhaustive]
pub enum Pattern<K> { Token(K), Group { open: K, close: K, body: Vec<Pattern<K>>, }, Bind { name: Symbol, fragment: Fragment<K>, }, Repeat { body: Vec<Pattern<K>>, separator: Option<K>, kleene: Kleene, }, }
Expand description

One element of a rule’s pattern — the left-hand side of a macro_rules! arm.

A pattern is a sequence of elements matched against an invocation’s token trees from left to right. Literal tokens and groups must appear exactly; a Bind captures a tree under a name the template can substitute; a Repeat matches its body any number of times. The whole pattern must consume the whole invocation.

Patterns are plain data: a language’s front end parses its own macro syntax (say $x:ident or $($e:expr),*) and lowers it into these elements. Macro::new validates and compiles them.

Literal tokens compare by kind alone. Spans never matter to a match, and neither do hygiene contexts: a literal else in a pattern matches an else token whichever expansion introduced it.

The enum is #[non_exhaustive] so that new kinds of pattern element can be added in a minor release. Constructing any variant is unaffected; code that matches on a Pattern needs a wildcard arm.

§Examples

The pattern $name:tt = ( $($arg:tt),* ) with char tokens:

use macro_lang::{Fragment, Kleene, Pattern};
use intern_lang::Interner;

let mut names = Interner::new();
let pattern: Vec<Pattern<char>> = vec![
    Pattern::Bind { name: names.intern("name"), fragment: Fragment::Tree },
    Pattern::Token('='),
    Pattern::Group {
        open: '(',
        close: ')',
        body: vec![Pattern::Repeat {
            body: vec![Pattern::Bind { name: names.intern("arg"), fragment: Fragment::Tree }],
            separator: Some(','),
            kleene: Kleene::ZeroOrMore,
        }],
    },
];
assert_eq!(pattern.len(), 3);

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Token(K)

Matches a single token whose kind equals this one.

§

Group

Matches a delimited group with these delimiter kinds whose contents match body exactly.

Fields

§open: K

The kind of the opening delimiter.

§close: K

The kind of the closing delimiter.

§body: Vec<Pattern<K>>

The pattern the group’s contents must match, in full.

§

Bind

Captures one tree under name — a metavariable, $name:fragment.

Fields

§name: Symbol

The metavariable name the template refers to. Unique within a rule.

§fragment: Fragment<K>

What the captured tree may be.

§

Repeat

Matches body repeatedly — $( ... ) sep kleene.

Every metavariable bound inside the body captures one tree per repetition, and a template must expand it inside a matching Template::Repeat.

Fields

§body: Vec<Pattern<K>>

The pattern matched on each repetition. It must consume at least one tree; a body that can match nothing is rejected, since it could repeat forever.

§separator: Option<K>

A token kind required between repetitions, such as ,. Not allowed with Kleene::ZeroOrOne, which never repeats.

§kleene: Kleene

How many repetitions are allowed.

Trait Implementations§

Source§

impl<K: Clone> Clone for Pattern<K>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug> Debug for Pattern<K>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K> Forget for Pattern<K>

§

impl<K> Freeze for Pattern<K>
where K: Freeze, Vec<Pattern<K>>: Freeze, Fragment<K>: Freeze, Option<K>: Freeze,

§

impl<K> Move for Pattern<K>

§

impl<K> RefUnwindSafe for Pattern<K>

§

impl<K> Send for Pattern<K>
where K: Send, Vec<Pattern<K>>: Send, Fragment<K>: Send, Option<K>: Send,

§

impl<K> Sync for Pattern<K>
where K: Sync, Vec<Pattern<K>>: Sync, Fragment<K>: Sync, Option<K>: Sync,

§

impl<K> Unpin for Pattern<K>
where K: Unpin, Vec<Pattern<K>>: Unpin, Fragment<K>: Unpin, Option<K>: Unpin,

§

impl<K> UnsafeUnpin for Pattern<K>

§

impl<K> UnwindSafe for Pattern<K>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.