module base.match

Pattern capturing for Op argument parsing.

There is only one basic buildings block for assembling patterns: Type. It can match ValueStreams and EventStreams depending on its metatype argument and can take an optional type name to match as an argument.

In addition to this primitive, the following modifiers are available: Repeat, Sequence, Choice, and Optional. They can be used directly, but there is also a number of shorthands for assembling patterns quickly:

To perform the actual matching, call the :match method on a pattern and pass a sequence of Results. The method will either return the captured Results (or a table structuring them)

Any ambiguous pattern can be set to ‘recall mode’ by invoking it. Recalling patterns will memorize the first Result they match, and only match further Results of the same type. For example

arg = (val.num / val.str)!
pattern = arg + arg

…will match either two numbers or two strings, but not one number and one string. Recalling works on Choice and Type patterns. Type patterns without a type (val! and evt!) always behave like this.

On Sequence patterns, a special method :named exists. It takes a sequence of keys that are used instead of integers when constructing the capture table:

pattern = (val.str + val.num):named('key', 'value')
-- returns { {key='a', value=1}, {key='b', value=2}, ...}