Alternation
An alternation matches one of several alternatives.
Syntax
Section titled “Syntax”let Alternation = ('|'? Alternatives)?;
let Alternatives = Intersection ('|' Intersection)*;See Intersection.
Note that an alternation may have a leading pipe. Also note that an alternative may not be empty,
i.e. | | is not allowed. Use an empty string instead, e.g. 'foo' | '' | 'bar'.
Example
Section titled “Example”| 'hello'| 'pomsky'+Support
Section titled “Support”Alternation is supported in all flavors.
Behavior
Section titled “Behavior”Alternatives are matched consecutively. The first alternative that matches is used.
Compilation
Section titled “Compilation”Compiled to an alternation. The example above would be compiled to hello|(?:pomsky)+.
Alternations are subject to advanced optimizations:
- Merging of single-character alternations:
'a' | ['bc']becomes[a-c]. - Prefix merging:
'world' | 'wow'becomeswo(?:rld|w). - Empty alternatives: If the first or last alternative is empty, it is replaced with a
?or??quantifier.
These alternatives are sometimes combined. There is no guarantee regarding in which order the optimizations are applied.
Issues
Section titled “Issues”Merging of alternatives is currently not supported if they are not adjacent.
History
Section titled “History”- Many optimizations added in Pomsky 0.12
- Support for leading pipes added in Pomsky 0.6
- Initial implementation in Pomsky 0.1