# Macros
Macros allow you to create polymorphic rules:
# Matches "'Hello?' 'Hello?' 'Hello?'" matchThree[X] -> $X " " $X " " $X inQuotes[X] -> "'" $X "'" main -> matchThree[inQuotes["Hello?"]]
Copied!
1
2
3
4
5
2
3
4
5
Macros are dynamically scoped, which means they see arguments passed to parent macros:
# Matches "Cows oink." and "Cows moo!" sentence[ANIMAL, PUNCTUATION] -> animalGoes[("moo" | "oink" | "baa")] $PUNCTUATION animalGoes[SOUND] -> $ANIMAL " " $SOUND # uses $ANIMAL from its caller main -> sentence["Cows", ("." | "!")]
Copied!
1
2
3
4
5
2
3
4
5
Macros are expanded at compile time and inserted in places they are used. They
are not "real" rules. Therefore, macros cannot be recursive (nearleyc
will
go into an infinite loop trying to expand the macro-loop). They must also be
defined before they are used (except by other macros).