Skip to content

Commit 1e13aba

Browse files
committed
Add macro args to readme
1 parent 39508dc commit 1e13aba

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

README.md

+45-6
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,20 @@ out enum{GET,POST} method;
5353

5454
All strings have a defined maximum size, which includes the null-terminator.
5555

56-
Macros in NMFU are simple parse-tree level replacements. They do not support arguments, and look like:
56+
Macros in NMFU are simple parse-tree level replacements. They look like:
5757

5858
```lark
59-
macro_decl: "macro" IDENTIFIER "{" statement* "}"
59+
macro_decl: "macro" IDENTIFIER macro_args "{" statement* "}"
60+
61+
macro_args: "(" macro_arg ("," macro_arg)* ")"
62+
| "(" ")" -> macro_arg_empty
63+
64+
macro_arg: "macro" IDENTIFIER -> macro_macro_arg
65+
| "out" IDENTIFIER -> macro_out_arg
66+
| "match" IDENTIFIER -> macro_match_expr_arg
67+
| "expr" IDENTIFIER -> macro_int_expr_arg
68+
| "hook" IDENTIFIER -> macro_hook_arg
69+
| "loop" IDENTIFIER -> macro_breaktgt_arg
6070
```
6171

6272
For example:
@@ -72,6 +82,31 @@ macro ows() { // optional white space
7282
When macros are "called", or instantiated, all NMFU does is copy the contents of the parse tree from the macro
7383
declaration to the call-site. Note that although macros can call other macros, they cannot recurse.
7484

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+
75110
Hooks (which are callbacks to user code which the parser can call at certain points) are defined with a _hook-declaration_:
76111

77112
```lark
@@ -114,10 +149,10 @@ The next two statements are the _assign-statement_ and _append_statement_. The _
114149
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,
115150
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.
116151

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.
119154

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.
121156

122157
The _break-statement_ is explained along with loops in a later section.
123158

@@ -154,6 +189,8 @@ The _concat-expression_ matches any number of _match-expressions_ in order.
154189

155190
The _regex-expression_ matches a subset of regexes. The quirks of the regex dialect NMFU uses can be found in a later section.
156191

192+
Additionally, the name of a macro argument of type `match` can replace any _match-expression_, including the sub-expressions inside _concat-expressions_.
193+
157194
An _integer-expression_ is anything that can be directly assigned to an output variable, **including strings**:
158195

159196
```lark
@@ -196,6 +233,8 @@ For example:
196233
content_length = [content_length * 10 + ($last - '0')];
197234
```
198235

236+
Additionally, the name of a macro argument of type `expr` can replace any _integer-expression_. Priority versus _output-variable_ names is undefined.
237+
199238
### Block Statements
200239

201240
Block statements are statements which contain a block of other statements:
@@ -440,4 +479,4 @@ There is a vim plugin available which adds syntax highlighting for `.nmfu` files
440479
## License
441480
442481
NMFU is licensed under the GPLv3.
443-
Copyright (C) 2020 Matthew Mirvish.
482+
Copyright (C) 2020-2021 Matthew Mirvish.

0 commit comments

Comments
 (0)