conjure_cp_essence_parser/parser/
macros.rs

1/// Get the i-th named child of a node, or return a syntax error with a message if it doesn't exist.
2#[macro_export]
3macro_rules! named_child {
4    ($node:ident) => {
5        named_child!($node, 0, "Missing sub-expression")
6    };
7    ($node:ident, $i:literal) => {
8        named_child!($node, $i, format!("Missing sub-expression #{}", $i + 1))
9    };
10    ($node:ident, $i:literal, $msg:expr) => {
11        $node
12            .named_child($i)
13            .ok_or(EssenceParseError::syntax_error(
14                format!("{} in expression of kind '{}'", $msg, $node.kind()),
15                Some($node.range()),
16            ))?
17    };
18}
19
20/// Get the i-th child of a node, or return a syntax error with a message if it doesn't exist.
21#[macro_export]
22macro_rules! child {
23    ($node:ident) => {
24        child!($node, 0, "Missing sub-expression")
25    };
26    ($node:ident, $i:literal) => {
27        child!($node, $i, format!("Missing sub-expression #{}", $i + 1))
28    };
29    ($node:ident, $i:literal, $msg:expr) => {
30        $node.child($i).ok_or(EssenceParseError::syntax_error(
31            format!("{} in expression of kind '{}'", $msg, $node.kind()),
32            Some($node.range()),
33        ))?
34    };
35}
36
37/// Get the named field of a node, or return a syntax error with a message if it doesn't exist.
38#[macro_export]
39macro_rules! field {
40    ($node:ident, $name:expr) => {
41        $node
42            .child_by_field_name($name)
43            .ok_or(EssenceParseError::syntax_error(
44                format!(
45                    "Missing field '{}' in expression of kind '{}'",
46                    $name,
47                    $node.kind()
48                ),
49                Some($node.range()),
50            ))?
51    };
52}