module base.match

Pattern capturing for Op argument parsing.

There is only one basic buildings block for assembling patterns: Type. It can match SigStreams and EvtStreams 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 RTNodes. The method will either return the captured RTNodes (or a table structuring them)

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

arg = (sig.num / sig.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.

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 = (sig.str + sig.num):named('key', 'value')
pattern:match(...)
-- returns { {key='a', value=1}, {key='b', value=2}, ...}

index

functions

tables

details

functions

tables