I've posted about a duplication macro before. Here's a revised grammar:
- Under identifiers (first two augment an existing production):
- identifier → duplication-marker
- implicit-parameter-name → $ ( duplication-marker )
- duplication-marker → $$ decimal-digits
- Under keywords that begin with a number sign (
- Under declarations, but in the generic section along with top-level code and code blocks:
- duplication-directive → #dup ( duplication-amount ; balanced-tokensopt )
- duplication-amount → decimal-literal | duplication-marker
A directive can duplicate one term, or multiple terms if the right separator is in the token sequence at the top level.
// Both of these initialize to zero through five. let myArray1 = [ #dup(6; $$0) ] let myArray2 = [ #dup(3; 2 * $$0, 2 * $$0 + 1) ]
A duplication directive normally can appear anywhere a term for a comma-separated list can appear. But they can also duplicate statements (which are semicolon-separated) or protocols/classes (which are ampersand-separated). I'll make up a complete list, including if protocols/classes will stay included, later.
A few days ago, I realized: but what about compiler errors whenever one happens within a duplication directive. There can be three main kinds:
- The directive resolves to a null token sequence, but it's in a context where the number of items must be positive, and there are no non-directive and/or non-empty directive sibling terms.
- There's a structural error in the token sequence that applies to every iteration.
- The structure is valid, but certain values of duplication markers end up out of range in particular iterations.
Right now, I guess the compiler tracks an error's module & file, line, and column. WIth this, or any other macro, it will also have to track the application iteration indices. (Indices, not index, because directives can be nested.) How hard would it be to add this possibility for error detection and reporting?