Environments
Environments are a way of specifying when a rule applies. They can be specified as follows:
rule with_env = 'a' -> 'b' in _ 'c';The in keyword starts the environment specification. In an environment, the underscore _ represents the place in the input where the match occurred, and matchers can be specified on either side.
The example above transforms ac to bc, but aa remains unchanged.
Multiple Environments
Multiple environments in which a rule applies can be specified with the or keyword.
rule multi_env = 'a' -> 'b' in _ 'c' or 'c' _;This converts ac to bc and ca to cb.
Exceptions
You can also specify environments in which a rule doesn’t apply using the except keyword.
rule exception = 'a' -> 'b' except _ 'c';This will transform aa to bb, but ac remains unchanged.
Multiple exceptions and environments can be combined into one rule:
rule multi_all = 'a' -> 'b' in 'b' _ or _ 'c' except 'b' _ 'b' or 'c' _ 'c';The in environment(s) must always be before the exception(s).