LPeg 기반 파서를 쓰고 있습니다. 구문 분석 오류가 nil, errmsg
을 반환하도록하려면 어떻게해야합니까?어떻게 LPeg과 구문 분석 오류 신호를 보낼 수 있습니까?
error()
을 사용할 수 있지만 정상적인 오류가 발생하는 것을 알고있는 한 nil, errmsg
이 아닙니다.
코드는 pretty long이지만, 관련 부분은 이것이다 :
local eof = lpeg.P(-1)
local nl = (lpeg.P "\r")^-1 * lpeg.P "\n" + lpeg.P "\\n" + eof -- \r for winblows compat
local nlnoeof = (lpeg.P "\r")^-1 * lpeg.P "\n" + lpeg.P "\\n"
local ws = lpeg.S(" \t")
local inlineComment = lpeg.P("`") * (1 - (lpeg.S("`") + nl * nl))^0 * lpeg.P("`")
local wsc = ws + inlineComment -- comments count as whitespace
local backslashEscaped
= lpeg.P("\\ ")/" " -- escaped spaces
+ lpeg.P("\\\\")/"\\" -- escaped escape character
+ lpeg.P("\\#")/"#"
+ lpeg.P("\\>")/">"
+ lpeg.P("\\`")/"`"
+ lpeg.P("\\n") -- \\n newlines count as backslash escaped
+ lpeg.P("\\") * lpeg.P(function(_, i)
error("Unknown backslash escape at position " .. i) -- this error() is what I wanna get rid of.
end)
local Line = lpeg.C((wsc + (backslashEscaped + 1 - nl))^0)/function(x) return x end * nl * lpeg.Cp()
내가 잘못된 탈출있을 때 nil, errmsg
을 반환 Line:match(...)
를 원한다.
달성하려는 목표는 무엇입니까? 최소 예입니까? '돌아 오려고 '했습니까? – Jakuje
'error()'는'nil, errmsg' 대신에 오류를 생성합니다. 백 슬래시 이스케이프에 오류가있는 경우'Line : match()'및'Data : match()'를 사용하여'nil, errmsg'를 반환합니다. – SoniEx2
@Jakuje 실은, 유효하지 않은 이스케이프가있는 경우'Line : match()'만'nil, errmsg'를 돌려 줄 필요가 있습니다. – SoniEx2