lua-users home
lua-l archive

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


Below is a little patch that allows you to use bare non-terminal names on
the right-hand-side of a grammar production. This makes LPEG's "re" syntax
a lot lighter and more similar to IETF ABNF or W3C EBNF.

For non-recursive grammars it's currently tempting to write code like the
following for greater brevity...

	local parse = {}

	local function p(pattern)
		return re.compile(pattern, parse)
	end

	parse.ws = p[[ [ 	]+ ]]
	parse.num = p[[ {%d+} ]] / tonumber

	parse.time = p[[ %num ":" %num ":" %num ]] /
		function (H,M,S)
			return H*3600 + M*60 + S
		end

	-- etc.

Patch follows:

--- re.lua~     2008-10-11 20:21:05.000000000 +0000
+++ re.lua      2009-10-08 17:55:37.000000000 +0000
@@ -128,7 +128,8 @@
             + "{~" * m.V"Exp" * "~}" / m.Cs
             + "{" * m.V"Exp" * "}" / m.C
             + m.P"." * m.Cc(any)
-            + "<" * name * ">" / m.V;
+            + "<" * name * ">" / m.V
+            + (name / m.V) * ("" - S * "<-");
   Definition = Identifier * S * '<-' * m.V"Exp";
   Grammar = m.Cf(m.V"Definition" / firstdef * m.Cg(m.V"Definition")^0, adddef) /
                 m.P

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
GERMAN BIGHT HUMBER: SOUTHWEST 5 TO 7. MODERATE OR ROUGH. SQUALLY SHOWERS.
MODERATE OR GOOD.