You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -72,6 +82,31 @@ macro ows() { // optional white space
72
82
When macros are "called", or instantiated, all NMFU does is copy the contents of the parse tree from the macro
73
83
declaration to the call-site. Note that although macros can call other macros, they cannot recurse.
74
84
85
+
Macros can also take arguments, which are similarly treated as parse-tree level replacements, with the added restriction
86
+
that their types _are_ checked. For example:
87
+
88
+
```
89
+
macro read_number(out target, match delimit) {
90
+
target = 0;
91
+
foreach {
92
+
/\d+/;
93
+
} do {
94
+
target = [target * 10 + ($last - '0')];
95
+
}
96
+
97
+
delimit;
98
+
}
99
+
```
100
+
101
+
There are 6 types of arguments:
102
+
103
+
-`macro`: a reference to another macro
104
+
-`hook`: a reference to a hook
105
+
-`out`: a reference to an _output-variable_
106
+
-`match`: an arbitrary _match-expression_
107
+
-`expr`: an arbitrary _integer-expression_
108
+
-`loop`: an arbitrary named _loop-statement_, for use in _break-statements_.
109
+
75
110
Hooks (which are callbacks to user code which the parser can call at certain points) are defined with a _hook-declaration_:
76
111
77
112
```lark
@@ -114,10 +149,10 @@ The next two statements are the _assign-statement_ and _append_statement_. The _
114
149
and assigns its result into the named _output-variable_. The _append-statement_ instead appends whatever is matched by the _match-expression_ into the named _output-variable_ which must by a string type. Additionally,
115
150
if the argument to an _append-statement_ is a _math-expression_, then the result of evaluating the expression will be treated as a character code and appended to the string.
116
151
117
-
The _call-stmt_ instantiates a macro or calls a hook. Note that there is currently no valid way to pass parameters to either, and as such the expressions provided
118
-
will be ignored, although may in future be used as C-style macro arguments or passed to the underlying hook function.
152
+
The _call-stmt_ instantiates a macro or calls a hook. Note that there is currently no valid way to pass parameters to a hook, and as such the expressions provided
153
+
in that case will be ignored. Macro arguments are always parsed as generic expressions and then interpreted according to the type given to them at declaration.
119
154
120
-
If a hook and macro have the same name, the macro will take priority.
155
+
If a hook and macro have the same name, the macro will take priority. Priority is undefined if a macro argument and global hook or macro share a name.
121
156
122
157
The _break-statement_ is explained along with loops in a later section.
123
158
@@ -154,6 +189,8 @@ The _concat-expression_ matches any number of _match-expressions_ in order.
154
189
155
190
The _regex-expression_ matches a subset of regexes. The quirks of the regex dialect NMFU uses can be found in a later section.
156
191
192
+
Additionally, the name of a macro argument of type `match` can replace any _match-expression_, including the sub-expressions inside _concat-expressions_.
193
+
157
194
An _integer-expression_ is anything that can be directly assigned to an output variable, **including strings**:
0 commit comments