From a782cf72be84a10a95be301fce6e432b6e747814 Mon Sep 17 00:00:00 2001 From: Undecidable Robot Date: Mon, 16 May 2016 18:32:41 +0800 Subject: Unifying reporting of syntactic and semantic error messages --- relabel.lua | 29 ++++++++++++++++++++++------- testerrors.lua | 18 ++++++++++++++++-- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/relabel.lua b/relabel.lua index 0655418..0a0ed8e 100644 --- a/relabel.lua +++ b/relabel.lua @@ -42,6 +42,10 @@ end) local Predef = { nl = m.P"\n" } local tlabels = {} +local function adderror(message) + tinsert(errors, {message}) +end + local mem local fmem @@ -89,7 +93,10 @@ local I = m.P(function (s,i) print(i, s:sub(1, i-1)); return i end) local function getdef (id, defs) local c = defs and defs[id] - if not c then error("undefined name: " .. id) end + if not c then + adderror("undefined name: " .. id) + return nil + end return c end @@ -128,7 +135,10 @@ local String = "'" * m.C((any - "'")^0) * ("'" + throw(31)) + local defined = "%" * Def / function (c,Defs) local cat = Defs and Defs[c] or Predef[c] - if not cat then error ("name '" .. c .. "' undefined") end + if not cat then + adderror ("name '" .. c .. "' undefined") + return mm.P"" + end return cat end @@ -145,7 +155,7 @@ local Class = local function adddef (t, k, exp) if t[k] then - error("'"..k.."' already defined as a rule") + adderror("'"..k.."' already defined as a rule") else t[k] = exp end @@ -157,7 +167,8 @@ local function firstdef (n, r) return adddef({n}, n, r) end local function NT (n, b) if not b then - error("rule '"..n.."' used outside a grammar") + adderror("rule '"..n.."' used outside a grammar") + return mm.P"" else return mm.V(n) end end @@ -276,10 +287,14 @@ local function compile (p, defs) if #errors > 0 then local errmsg = "" for i, err in ipairs(errors) do - local line, col = lineno(p, err[2]) - errmsg = errmsg .. "Line" .. line .. ", Col " .. col .. ": " .. errorMessages[err[1]] .. "\n" + if #err == 1 then + errmsg = errmsg .. err[1] .. "\n" + else + local line, col = lineno(p, err[2]) + errmsg = errmsg .. "Line" .. line .. ", Col " .. col .. ": " .. errorMessages[err[1]] .. "\n" + end end - error(errmsg, 3) + error(errmsg) end return cp end diff --git a/testerrors.lua b/testerrors.lua index dff35df..ab1c665 100644 --- a/testerrors.lua +++ b/testerrors.lua @@ -99,7 +99,7 @@ local patterns = { [[{||}]], [[{|@|}]], [['p' {| 'q' / 'r' }]], - -- 71-73 + -- 71-75 [['a'/{1}'b'/'c']], [[x <- {:x:}]], [[&'p'/&/!/'p'^'q']], @@ -107,7 +107,21 @@ local patterns = { A <- 'a' (B 'b' B <- 'x' / ! C <- 'c' - ]] + ]], + [[ + A <- %nosuch %def + A <- 'A again' + A <- 'and again' + ]], + -- 76 - 79 + [[names not in grammar]], + [[ + A <- %nosuch %def + A <- 'A again' + A <- 'and again' + ]], + [[ A <- %nosuch ('error' ]], + [[A <- Unknown Rules]] } for i, patt in ipairs(patterns) do -- cgit v1.2.3-55-g6feb