---
source: macro/src/lib.rs
expression: "rustfmt_code(&expand_grammar(parse_quote!\n{\n #[adze::grammar(\"test\")] mod grammar\n {\n pub struct Number\n {\n #[adze::leaf(pattern = r\"\\d+\", transform = |v|\n v.parse().unwrap())] value: u32\n } #[adze::language] pub enum Expr\n { Numbers(#[adze::repeat(non_empty = true)] Vec<Number>) }\n }\n})? .to_token_stream().to_string())"
---
mod grammar {
pub struct Number {
value: u32,
}
impl ::adze::Extract<Number> for Number {
type LeafFn = ();
#[allow(non_snake_case)]
fn extract(
node: Option<::adze::tree_sitter::Node>,
source: &[u8],
last_idx: usize,
_leaf_fn: Option<&Self::LeafFn>,
) -> Self {
let node = node.expect("Extract called with None node for struct");
::adze::__private::extract_struct_or_variant(node, move |cursor, last_idx| Number {
value: {
::adze::__private::extract_field::<adze::WithLeaf<u32>, _>(
cursor,
source,
last_idx,
"value",
Some(&|v| v.parse().unwrap()),
)
},
})
}
}
pub enum Expr {
Numbers(Vec<Number>),
}
impl ::adze::Extract<Expr> for Expr {
type LeafFn = ();
#[allow(non_snake_case)]
fn extract(
node: Option<::adze::tree_sitter::Node>,
source: &[u8],
_last_idx: usize,
_leaf_fn: Option<&Self::LeafFn>,
) -> Self {
let node = node.expect("Extract called with None node for enum");
fn unwrap_hidden_rules(node: ::adze::tree_sitter::Node) -> ::adze::tree_sitter::Node {
if (node.kind().starts_with('_') || node.child_count() == 1)
&& node.child_count() > 0
{
if let Some(child) = node.child(0) {
return unwrap_hidden_rules(child);
}
}
node
}
let unwrapped_node = unwrap_hidden_rules(node);
let mut cursor = unwrapped_node.walk();
let non_extra_children: Vec<_> = unwrapped_node
.children(&mut cursor)
.filter(|c| !c.is_extra())
.collect();
if unwrapped_node.kind() == stringify!(Expr) && non_extra_children.len() == 1 {
let child = non_extra_children[0];
return Self::extract(Some(child), source, _last_idx, _leaf_fn);
}
let child_node = unwrapped_node;
if child_node.kind() == "Expr_Numbers" {
return ::adze::__private::extract_struct_or_variant(
node,
move |cursor, last_idx| {
Expr::Numbers({
::adze::__private::extract_field::<Vec<Number>, _>(
cursor, source, last_idx, "0", None,
)
})
},
);
}
panic ! ("Could not determine enum variant from tree structure: node kind='{}', child_count={}" , unwrapped_node . kind () , unwrapped_node . child_count ())
}
}
unsafe extern "C" {
fn tree_sitter_test() -> ::adze::tree_sitter::Language;
}
pub fn language() -> ::adze::tree_sitter::Language {
unsafe { tree_sitter_test() }
}
#[doc = r" Parse an input string according to the grammar. Returns either any parsing errors that happened, or a"]
#[doc = "[`Expr`]"]
#[doc = r" instance containing the parsed structured data."]
pub fn parse(input: &str) -> core::result::Result<Expr, Vec<::adze::errors::ParseError>> {
::adze::__private::parse::<Expr>(input, language)
}
}