LPeg grammar oddity
Asked Answered
S

1

5

Part of a Lua application of mine is a search bar, and I'm trying to make it understand boolean expressions. I'm using LPeg, but the current grammar gives a strange result:

> re, yajl = require're', require'yajl'
> querypattern = re.compile[=[
    QUERY       <- ( EXPR / TERM )? S? !. -> {}
    EXPR        <- S? TERM ( (S OPERATOR)? S TERM )+ -> {}
    TERM        <- KEYWORD / ( "(" S? EXPR S? ")" ) -> {}
    KEYWORD     <- ( WORD {":"} )? ( WORD / STRING )
    WORD        <- {[A-Za-z][A-Za-z0-9]*}
    OPERATOR    <- {("AND" / "XOR" / "NOR" / "OR")}
    STRING      <- ('"' {[^"]*} '"' / "'" {[^']*} "'") -> {}
    S           <- %s+
]=]
> = yajl.to_string(lpeg.match(querypattern, "bar foo"))
"bar"
> = yajl.to_string(lpeg.match(querypattern, "name:bar AND foo"))
> = yajl.to_string(lpeg.match(querypattern, "name:bar AND foo"))
"name"
> = yajl.to_string(lpeg.match(querypattern, "name:'bar' AND foo"))
"name"
> = yajl.to_string(lpeg.match(querypattern, "bar AND (name:foo OR place:here)"))
"bar"

It only parses the first token, and I cannot figure out why it does this. As far as I know, a partial match is impossible because of the !. at the end of the starting non-terminal. How can I fix this?

Shoddy answered 13/1, 2012 at 1:38 Comment(0)
W
10

The match is getting the entire string, but the captures are wrong. Note that '->' has a higher precedence than concatenation, so you probably need parentheses around things like this:

  EXPR        <- S? ( TERM ( (S OPERATOR)? S TERM )+ ) -> {}
Waxler answered 13/1, 2012 at 18:1 Comment(1)
Thank you! This was precisely the problem I had. I never realized that the table-capture actually had high precedence.Shoddy

© 2022 - 2024 — McMap. All rights reserved.