adze-macro 0.8.0

Procedural macros for Rust Sitter
Documentation
---
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(2)]\n            Compare(Box<Expression>, #[adze::leaf(text = \"==\")] (),\n            Box<Expression>),\n        }\n    }\n})? .to_token_stream().to_string())"
---
mod grammar {
    pub enum Expression {
        Number(i32),
        Compare(Box<Expression>, (), Box<Expression>),
    }
    impl ::adze::Extract<Expression> for Expression {
        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!(Expression) && 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() == "Expression_Number" {
                return ::adze::__private::extract_struct_or_variant(
                    node,
                    move |cursor, last_idx| {
                        Expression::Number({
                            ::adze::__private::extract_field::<i32, _>(
                                cursor, source, last_idx, "0", None,
                            )
                        })
                    },
                );
            }
            if child_node.kind() == "Expression_Compare" {
                return ::adze::__private::extract_struct_or_variant(
                    node,
                    move |cursor, last_idx| {
                        Expression::Compare(
                            {
                                ::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,
                                )
                            },
                        )
                    },
                );
            }
            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 = "[`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)
    }
}