lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello!

Would it be possible to add a "switch matcher" to LPeg?

It should be constructed as `pattern | {cases}`.

Here's how it works:

You match the pattern `pattern`. You use the result of that match to index into `{cases}`. `{cases}` is a key-value map from strings (match results) to patterns. If the key doesn't exist, the match fails, otherwise we attempt to match the indexed pattern (match the value we got from `{cases}`).

A default case can be done with `pattern - (pattern | {cases})`.

This is faster than using alternations: O(1) vs O(n).

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.