---
source: macro/src/lib.rs
expression: "rustfmt_code(&expand_grammar(parse_quote!\n{\n #[adze::grammar(\"test\")] mod grammar\n {\n #[adze::language] pub enum Keyword\n {\n #[adze::leaf(text = \"if\")] If, #[adze::leaf(text = \"else\")] Else,\n #[adze::leaf(text = \"while\")] While,\n }\n }\n})? .to_token_stream().to_string())"
---
mod grammar {
pub enum Keyword {
If,
Else,
While,
}
impl ::adze::Extract<Keyword> for Keyword {
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!(Keyword) && 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() == "Keyword_If" {
return ::adze::__private::extract_struct_or_variant(
node,
move |cursor, last_idx| {
{
::adze::__private::extract_field::<(), _>(
cursor, source, last_idx, "unit", None,
)
};
Keyword::If
},
);
}
if child_node.kind() == "Keyword_Else" {
return ::adze::__private::extract_struct_or_variant(
node,
move |cursor, last_idx| {
{
::adze::__private::extract_field::<(), _>(
cursor, source, last_idx, "unit", None,
)
};
Keyword::Else
},
);
}
if child_node.kind() == "Keyword_While" {
return ::adze::__private::extract_struct_or_variant(
node,
move |cursor, last_idx| {
{
::adze::__private::extract_field::<(), _>(
cursor, source, last_idx, "unit", None,
)
};
Keyword::While
},
);
}
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 = "[`Keyword`]"]
#[doc = r" instance containing the parsed structured data."]
pub fn parse(input: &str) -> core::result::Result<Keyword, Vec<::adze::errors::ParseError>> {
::adze::__private::parse::<Keyword>(input, language)
}
}