[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [LPeg] Switch matcher
- From: "Soni L." <fakedme@...>
- Date: Sun, 16 Aug 2015 12:16:53 -0300
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.