The example looks for string literals outside of what it considers comments and preprocessor directives. If you feed it random data it will just end up in various states in the graph. It matches everything and uses captures for subsitution when it's in the right state.
When I need to break on invalid grammar, I use match time captures (Cmt). I think if you have problems with short circuiting parsing you might have the match time capture in the wrong place, but I might be wrong. Do you have an short example?
Trivial example (maybe too trivial?) of error handling:
lpeg = require "lpeg"
P, Cmt, V, S = lpeg.P, lpeg.Cmt, lpeg.V, lpeg.S
function errfunc(match)
error("invalid token: "..tostring(match))
end
P{
"tokens";
space = S" \t",
invalid = (1-V"space")^1/errfunc,
token = P"foo" + P"bar" + P "baz" + V"invalid",
tokens = (V"token" * V"space"^0)^0 * -1
}:match("foo bar baz woops foo")
EDIT: or, to get the position:
lpeg = require "lpeg"
P, Cmt, V, S = lpeg.P, lpeg.Cmt, lpeg.V, lpeg.S
function errfunc(match, pos, cap)
error(string.format("invalid token at position %d: %s",
pos-#cap, cap))
end
p = P{
"tokens";
space = S" \t",
invalid = Cmt((1-V"space")^1, errfunc),
token = P"foo" + P"bar" + P "baz" + V"invalid",
tokens = (V"token" * V"space"^0)^0 * -1
}:match("foo bar baz woops foo")
What you consider alternative / independent journalists could be charlatans by journalistic standards. It's hard to say in the abstract.