neutralts/
macros.rs

1// Inherit, macro: new_child_parse
2// -------------------------------
3// Inheritance is implemented with this macro. It is also used for the inheritance
4// of the application itself.
5//
6//
7// Block level scope example:
8//    {:code; <--------------------------.
9//        {:* block *:}                  |<---- Block
10//        {:param; name >> value :} <----|----- Set "name" for this block and its children
11//        {:param; name :} <-------------|----- "name" has the value "value".
12//        {:code;                        |
13//            {:* child block *:}        |
14//            {:param; name :} <---------|----- "name" has the value "value".
15//        :}                             |
16//    :} <-------------------------------ยท
17//    {:param; name :} <----------------------- outside block, no value or a previous value if any.
18//
19//
20// "include" has a block scope, then:
21//    {:code;
22//        {:* include for set "snippet-name" *:}
23//        {:include; snippet.ntpl :}
24//        {:snippet; snippet-name :} {:* Ok, "snippet-name" is set *:}
25//    :}
26//    {:snippet; snippet-name :} {:* Ko, "snippet-name" is not set *:}
27//
28// The modifier scope (+) adds the scope to the current level to make it possible to do this:
29//    {:+bool; something >>
30//        {:include; snippet.ntpl :}
31//    :}
32//    {:snippet; snippet-name :} {:* Ok, "snippet-name" is set *:}
33//
34#[macro_use]
35mod macros {
36    macro_rules! new_child_parse {
37        ($self:expr, $source:expr, $scope:expr) => {{
38            let mut child_inherit = $self.inherit.clone();
39            let shared = &mut $self.shared;
40
41            //  "bif.alias" is used and not "bif.name" because in "var" or "unprintable"
42            // its name is an empty string and could have more meanings.
43            child_inherit.alias = $self.alias.clone();
44
45            if !$self.file_path.is_empty() {
46                child_inherit.current_file = $self.file_path.clone();
47            }
48
49            if !$self.dir.is_empty() {
50                child_inherit.current_dir = $self.dir.clone();
51            }
52
53            // Create a new version of the schema if mod_scope
54            // This is necessary because indirections are used,
55            // and create_block_schema takes care of that.
56            if $scope {
57                $self.inherit.create_block_schema(shared);
58            }
59
60            let mut block = $crate::block_parser::BlockParser::new(shared, child_inherit);
61            let code = block.parse($source, $self.only);
62
63            // Update this block with the data generated in the child
64            if $scope {
65                block.update_indir(&$self.inherit.indir);
66            }
67
68            code
69        }};
70    }
71}