Attribute Macro rust_sitter::leaf

source ·
#[leaf]
Expand description

Defines a field which matches a specific token in the source string. The token can be defined by passing one of two arguments

  • text: a string literal that will be exactly matched
  • pattern: a regular expression that will be matched against the source string

If the resulting token needs to be converted into a richer type at runtime, such as a number, then the transform argument can be used to specify a function that will be called with the token’s text.

Example

Number(
    #[rust_sitter::leaf(pattern = r"\d+", transform = |v| v.parse().unwrap())]
    u32
)