Patterns: conditions within

OmniMark allows conditions to occur inside patterns.

This find rule illustrates the use of conditions inside patterns. It matches a letter "X" when the switch "include-xes" is active, and matches the letter "Y" whether or not the switch is active.

  find ("X" when include-xes) | "Y"

This example shows how a condition can be embedded in a pattern. It uses a find rule to find a number in parentheses:

  • If the number is greater than zero, it is followed by the indicated number of characters surrounded by another set of parentheses.
  • If the number is zero, it is followed by nothing.

The condition that tests in-codes is not part of the pattern, but determines whether the find rule is to be used.

A condition can, as in this example, be considered to match zero characters.

  find "(" digit+ => number ")"
       ((when number = 0) |
        "(" any {number} => text ")") when in-codes

This example illustrates the importance of properly parenthesizing the conditions of a pattern.

This, for example, is invalid:

  find digit (letter+ => word)? white-space
           when element is term and word is specified

The problem above is that the test to see if the pattern word was specified would occur before the pattern was matched. This is the proper way to write this find rule:

  find (digit (letter+ => word)? white-space when word
        is specified)
        when element is term

Every pattern inside parentheses may be followed by a condition. A condition doesn't always have to be preceded by a pattern, and may appear in parentheses by itself.

When conditions appear inside parentheses after a pattern, first the pattern is tested and, if it succeeds, the condition is attempted. If the condition is false, any pattern variables that were specified in the preceding pattern are unspecified.

When a condition follows a pattern without surrounding parentheses, such as at the head of a rule or in an action, the condition is evaluated before the pattern. In this case the condition is used to determine whether the rule or action containing the pattern is to be evaluated. Because conditions in these circumstances are evaluated first, they may not contain references to any pattern variables specified in the pattern.

Patterns that must match at least one character or one position pattern cannot contain only a condition.