---
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 Expression\n {\n Number(#[adze::leaf(pattern = r\"\\d+\", transform = |v|\n v.parse().unwrap())] i32), #[adze::prec_left(1)]\n Sub(Box<Expression>, #[adze::leaf(text = \"-\")] (),\n Box<Expression>),\n }\n }\n})? .to_token_stream().to_string())"
---
mod grammar {
pub enum Expression {
Number(i32),
Sub(Box<Expression>, (), Box<Expression>),
}
impl ::adze::Extract<Expression> for Expression {
type LeafFn = ();
const GRAMMAR_NAME: &'static str = "test";
#[allow(non_snake_case)]
fn extract(
node: Option<&::adze::pure_parser::ParsedNode>,
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<'a>(
node: &'a ::adze::pure_parser::ParsedNode,
) -> &'a ::adze::pure_parser::ParsedNode {
if (node.kind().starts_with('_') || node.children.len() == 1)
&& node.children.len() > 0
{
return unwrap_hidden_rules(&node.children[0]);
}
node
}
let unwrapped_node = unwrap_hidden_rules(node);
let non_extra_children: Vec<_> = unwrapped_node
.children
.iter()
.filter(|c| !c.is_extra)
.collect();
if unwrapped_node.kind() == stringify!(Expression) && non_extra_children.len() == 1 {
let child_node = non_extra_children[0];
return Self::extract(Some(child_node), source, _last_idx, _leaf_fn);
}
let node = unwrapped_node;
if node.kind() == "Expression_Number" {
return {
let value = <adze::WithLeaf<i32> as ::adze::Extract<_>>::extract(
Some(node),
source,
0,
Some(&|v| v.parse().unwrap()),
);
Expression::Number(value)
};
}
if node.kind() == "Expression_Sub" {
return ::adze::__private::extract_struct_or_variant(
node,
move |cursor, last_idx| {
Expression::Sub(
{
::adze::__private::extract_field::<Box<Expression>, _>(
cursor, source, last_idx, "0", None,
)
},
{
::adze::__private::extract_field::<(), _>(
cursor, source, last_idx, "1", None,
)
},
{
::adze::__private::extract_field::<Box<Expression>, _>(
cursor, source, last_idx, "2", None,
)
},
)
},
);
}
if unwrapped_node.children.is_empty() {
let node = unwrapped_node;
return {
let value = <adze::WithLeaf<i32> as ::adze::Extract<_>>::extract(
Some(node),
source,
0,
Some(&|v| v.parse().unwrap()),
);
Expression::Number(value)
};
}
panic ! ("Could not determine enum variant from tree structure: node kind='{}', symbol={}, child_count={}" , unwrapped_node . kind () , unwrapped_node . symbol , unwrapped_node . children . len ())
}
}
include!(concat!(
env!("OUT_DIR"),
"/grammar_",
"test",
"/parser_",
"test",
".rs"
));
pub fn language() -> &'static ::adze::pure_parser::TSLanguage {
&LANGUAGE
}
pub const LANGUAGE_REF: &'static ::adze::pure_parser::TSLanguage = &LANGUAGE;
#[doc = r" Parse an input string according to the grammar. Returns either any parsing errors that happened, or a"]
#[doc = "[`Expression`]"]
#[doc = r" instance containing the parsed structured data."]
pub fn parse(input: &str) -> core::result::Result<Expression, Vec<::adze::errors::ParseError>> {
::adze::__private::parse::<Expression>(input, || language())
}
}