syncat_stylesheet/ast/
helper.rs1use tree_sitter::TreeCursor;
2
3macro_rules! extras {
4 ($tree:ident, $context:literal) => {
5 while $tree.node().is_extra() || !$tree.node().is_named() {
6 if !$tree.goto_next_sibling() {
7 return Err($crate::Error::invalid(
8 concat!("extras(", $context, ")"),
9 "no more children",
10 ));
11 }
12 }
13 };
14}
15
16#[macro_export]
17macro_rules! children {
18 ($tree:ident, $name:literal) => {{
19 extras!($tree, $name);
20 match $tree.node().kind() {
21 $name => $tree.goto_first_child(),
22 name => {
23 return Err($crate::Error::invalid(
24 concat!("children(", $name, ")"),
25 name,
26 ));
27 }
28 }
29 }};
30}
31
32#[macro_export]
33macro_rules! text {
34 ($tree:ident, $source:expr, $name:literal) => {{
35 extras!($tree, $name);
36 match $tree.node().kind() {
37 $name => $tree.node().utf8_text($source),
38 name => return Err($crate::Error::invalid(concat!("text(", $name, ")"), name)),
39 }
40 }};
41}
42
43pub(super) trait FromSource: Sized {
44 fn from_source(tree: &mut TreeCursor, source: &[u8]) -> Result<Self, crate::Error>;
45}