lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Mon, Jan 30, 2012 at 3:38 AM, Miles Bader <> wrote:
> Laurent FAILLIE <> writes:
>> - LUA is very easy to understand and start with ... but with strings
>> manipulation. Something that is very easy in C or PHP like token parsing
>> is a tedious task in LUA.
> Maybe a bit of a tangent, but Lua + LPEG pretty much eats everything
> else alive when it comes to easy/fast/super-powerful parsing.

"uh, at least I already know regexp syntax"

Balanced parentheses

The following pattern matches only strings with balanced parentheses:

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

My eyes, they burn! Even with syntax highlighting that looks like some
kind of vector math macro package for awk or something.

b = re.compile[[  balanced <- "(" ([^()] / balanced)* ")"  ]]

OK, that's better. People new to LPeg should start with re. In fact,
just hide the main package and tell people it's a low-level interface.

A more verbose table syntax might help (but I should probably know
better than to propose a syntax without implementing it or actually
writing more stuff in it.)

b = t.grammar {
   balanced={ "(",

What, you don't want to blow _G.t on that? Wrap in "do local t=tre .. end"

OK, now something longer:

csv = t.grammar{
{",", t.exp[[field]]}
       }, t.choice{, t.eof}
   field=t.choice{t.exp[[escaped]], t.exp[[nonescaped]]},
       t.choice{ t.range'^"', t.cap('""').as('"') }.star().capture()

I dunno if Kleene would mind if we let his star{} be a prefix as well
as a suffix. Close readers will notice there are a few parts of that
syntax I didn't finesse, but I never was much for late night curry.

OK, that's another experiment in little language syntax done. I have
no idea whether I like the table version better than the re.compile
version. The table certainly is more verbose. Is it any more readable?
My eyes are blurry. Well, lua-mode.el has some clue what to do with
the syntax and kept me closing parens.

I would love to be able to bind string metamethods inside of a table
scope since it would allow "x".star instead of forcing
t.literal"x".star. Uh, wait, I have no idea what that even means when
the strings escape...