How can I write a LPeg pattern that never matches anything?

174 Views Asked by At

Is there a way to create an LPeg pattern that always fails to match anything? I'm not talking about matching the empty string, I'm talking about a pattern that always fails so if you put it in an ordered choice it always will fall back to the second option.

The reason for this is that I am writing a small parser with LPEG and I wish I could write

 operators = empty_pattern + "==" + "~=" + "<=" + ">=" + "<" + ">"

instead of

 operators = lpeg.P("==") + "~=" + "<=" + ">=" + "<" + ">"
1

There are 1 best solutions below

1
On BEST ANSWER

lpeg.P( false ) is the simplest way.

If the argument is a boolean, the result is a pattern that always succeeds or always fails (according to the boolean value), without consuming any input.