Rule Combinators

Simultaneous Rules

Writing multiple rule specifications inside curly braces will make them run simultaneously, like so:

rule simultaneous = {
  'a' -> 'b';
  'b' -> 'c';
}

This will transform a into b and b into c, without applying the second sub-rule to the already transformed parts of the input.

If a multiple sub-rules of a simultaneous rule would modify the same part of the input, the one listed first takes priority. This can become important when matching large sections or when constructing compound rules with sequential rules inside simultaneous rules.

Sequential Rules

Using the keyword then between rules creates a sequential rule which runs all the sub-rules inside it one after the other.

rule sequence = {
  'a' -> 'b';
  then
  'b' -> 'c';
}

This is equivalent to one of the first examples in this guid and will transform ab into cc.

Fallback Rules

Finally, using the else keywords creates a fallback rule which runs the second rule only if the first produced no matches.

rule fallback = {
  'a' -> 'b';
  else
  'b' -> 'c';
}

This will transform ab to bb and bc to cc.

Chaining a third rule with else means it only gets run when the first two find amatch, and so on.

Combinator Precedence

These combinators can be arbitrarily combined and nested. To group rules within them, you can use curly braces. The precedence order of the combinators is as follows:

  1. Simultaneous
  2. Sequential
  3. Fallback

This means that

rule precedence = {
  'a' -> 'b';
  then
  'b' -> 'c';
  'c' -> 'd';
  else
  'd' -> 'e';
}

is equivalent to

rule grouping = {
  {
    'a' -> 'b';
    then {
      'b' -> 'c';
      'c' -> 'd';
    }
  }
  else
  'd' -> 'e';
}