[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Transform (cond ? then : else) to ifthenelse(cond, then, else) with lpeg
- From: Christophe Jorssen <jorssen.leraincy@...>
- Date: Mon, 20 Feb 2012 18:21:17 +0100
Hello all,
I'm trying to convert a string like
'a?(b?c:d):e'
to another string
'ifthenelse(a,ifthenelse(b,c,d),e)'
using the lpeg lua parser. I'm slowly learning how to use lpeg but I
still can't find a suitable solution to do this with capture
substitutions. Any ideas?
Here is what I did so far.
local lpeg = require("lpeg")
local S, P, R = lpeg.S, lpeg.P, lpeg.R
local C, Cc, Ct = lpeg.C, lpeg.Cc, lpeg.Ct
local Cf, Cg, Cs = lpeg.Cf, lpeg.Cg, lpeg.Cs
local V = lpeg.V
local thenop = P("?")
local elseop = P(":")
local openpar = P("(")
local closepar = P(")")
local digit = R("09")
local letter = R("az") + R("AZ")
local parser =
P({
"F",
F = V("E") * (thenop * V("E") * elseop * V("E"))^0,
E = (letter + digit)^1 + (openpar * V("F") * closepar)
}) -- * -1 -- Is it needed?
print(lpeg.match(parser,"a?(b?c:d):e"))
print(lpeg.match(parser,"a"))
Many thanks in advance.
PS: This is a question I asked a few days ago here
http://stackoverflow.com/questions/9330477/transform-cond-then-else-to-ifthenelsecond-then-else-with-lpeg
but got no answer (yet).
Best regards
--
Christophe