lua-users home
lua-l archive

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


Going back to the original q...

I struggled with this for a while. Currently my approach is to *not* put a P(-1) at the end of my grammar definition (i.e. not require that it get to the end of the text), and instead use a match time capture to verify that the final position == the length of the input text, or printing an error (and inserting it into the result) if not.

Don't know if this is the recommended way, would be happy to hear other suggestions.

e.g. (untested)

local ws = lpeg.S(" \t\n\r\f")
local alpha = R('az', 'AZ')
local word = ws^-1 * alpha^1
local g = lpeg.P { "S";
	S = word^0 * ws^-1 	-- don't require it to end with lpeg.P(-1)
}

local text = " a test sentence "

-- utility match-time function to handle syntax errors:
local function verify(subject, position, captures)
	if position < #subject then
		local errornode = {
			'error:',
			subject,
			string.rep("_", position) .. "^"
		}
		print(unpack(table.concat(errornode, "\n")))
		table.insert(captures, errornode)
		return position, captures
	end
	return position, captures
end

local grammar_pattern = lpeg.Cmt((lpeg.P(g)), verify)

local parse_tree = lpeg.match(grammar_pattern, text)	



On Apr 22, 2009, at 3:06 AM, Luiz Henrique de Figueiredo wrote:

On Wed, Apr 22, 2009 at 9:40 AM, Ico <lua@zevv.nl> wrote:
these files can come from untrusted sources, and I'd like to create a more robust solution to avoid problems caused by unexpected input like
neverending loops.

To get a really tough sandbox, use Luiz' token filtering patch and
filter out all dangerous keywords (or simply detect them as errors).

If there's only one top-level group then you can add "return " before loading the file to force that only expressions are accepted. To avoid infinite loops due to recursive function calls on anonymous functions, just raise errors when
"function" appears the token stream. This is most easily done in C.