lua-users home
lua-l archive

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


It was thus said that the Great Roberto Ierusalimschy once stated:
> >   I have a pattern that looks like this BNF:
> > 
> > 	TEXT	= 'A' - 'Z' / 'a' - 'z'
> > 	DIGIT	= '0' - '9'
> > 
> > 	pattern	= 1*TEXT [ ';' 1*DIGIT ]
> 
> Maybe like this?
> 
>   local text, sep, digit = string.match(s, "^(%a+)(;?)(%d*)$")
>   if not text or (sep == "") ~= (digit == "") then
>     error("bla bla bla")
>   end
>   return text, tonumber(digit)

  I'll have to test it.  The LPEG pattern I would use would be:

	text    = R("AZ","az")
	digit   = R"09"	
	pattern = C(text^1) * (P";" * (digit^1 / tonumber) + Cc(77))
	-- 77 is the default value if no number is given yada yada ...

but that's hard to express in the context of what I'm trying to do (I mean,
*I'm* comfortable using LPEG for this, but not many other people woule be),
but so far, your suggestion is about the best I've seen.

  -spc