Need help to understand LPeg and PEGs
Asked Answered
C

1

7

The following pattern (from this page) matches only strings with balanced parentheses:

b = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }

What does 1- in 1 - lpeg.S"()" mean?

function gsub (s, patt, repl)
  patt = lpeg.P(patt)
  patt = lpeg.Cs((patt / repl + 1)^0)
  return lpeg.match(patt, s)
end

What does the +1 in patt / repl + 1 mean?

And I still not quite get the function of prioritized choice operator / very well from this paper

Any help will be appreciated!

Cuneo answered 18/10, 2013 at 21:44 Comment(0)
B
5

The 1 in 1 - lpeg.S"()" means any character. The whole statement can be read as, match any character while not matching a character in the set "()".

The +1 is the same idea, if repl is a string then patt / repl + 1 matches pattern patt and then replaces it's capture with the string repl or skips a character.

Buckeen answered 19/10, 2013 at 0:35 Comment(1)
Generally, whenever LPEG may accept a pattern as an argument, it may also accept a plain number(or string, or boolean, etc.) and will implicitly convert it to a pattern using lpeg.P(). And lpeg.P(1) means "any character".Cantone

© 2022 - 2024 — McMap. All rights reserved.