diff options
author | Undecidable Robot <undecidabot@gmail.com> | 2016-05-16 18:32:41 +0800 |
---|---|---|
committer | Undecidable Robot <undecidabot@gmail.com> | 2016-05-16 18:32:41 +0800 |
commit | a782cf72be84a10a95be301fce6e432b6e747814 (patch) | |
tree | 78f365e76898426f57beea31f354c37b542928a2 | |
parent | 08296c5f604ad40428e642f09fd9b4b479e329cc (diff) | |
download | lpeglabel-a782cf72be84a10a95be301fce6e432b6e747814.tar.gz lpeglabel-a782cf72be84a10a95be301fce6e432b6e747814.tar.bz2 lpeglabel-a782cf72be84a10a95be301fce6e432b6e747814.zip |
Unifying reporting of syntactic and semantic error messages
-rw-r--r-- | relabel.lua | 29 | ||||
-rw-r--r-- | 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) | |||
42 | local Predef = { nl = m.P"\n" } | 42 | local Predef = { nl = m.P"\n" } |
43 | local tlabels = {} | 43 | local tlabels = {} |
44 | 44 | ||
45 | local function adderror(message) | ||
46 | tinsert(errors, {message}) | ||
47 | end | ||
48 | |||
45 | 49 | ||
46 | local mem | 50 | local mem |
47 | local fmem | 51 | local fmem |
@@ -89,7 +93,10 @@ local I = m.P(function (s,i) print(i, s:sub(1, i-1)); return i end) | |||
89 | 93 | ||
90 | local function getdef (id, defs) | 94 | local function getdef (id, defs) |
91 | local c = defs and defs[id] | 95 | local c = defs and defs[id] |
92 | if not c then error("undefined name: " .. id) end | 96 | if not c then |
97 | adderror("undefined name: " .. id) | ||
98 | return nil | ||
99 | end | ||
93 | return c | 100 | return c |
94 | end | 101 | end |
95 | 102 | ||
@@ -128,7 +135,10 @@ local String = "'" * m.C((any - "'")^0) * ("'" + throw(31)) + | |||
128 | 135 | ||
129 | local defined = "%" * Def / function (c,Defs) | 136 | local defined = "%" * Def / function (c,Defs) |
130 | local cat = Defs and Defs[c] or Predef[c] | 137 | local cat = Defs and Defs[c] or Predef[c] |
131 | if not cat then error ("name '" .. c .. "' undefined") end | 138 | if not cat then |
139 | adderror ("name '" .. c .. "' undefined") | ||
140 | return mm.P"" | ||
141 | end | ||
132 | return cat | 142 | return cat |
133 | end | 143 | end |
134 | 144 | ||
@@ -145,7 +155,7 @@ local Class = | |||
145 | 155 | ||
146 | local function adddef (t, k, exp) | 156 | local function adddef (t, k, exp) |
147 | if t[k] then | 157 | if t[k] then |
148 | error("'"..k.."' already defined as a rule") | 158 | adderror("'"..k.."' already defined as a rule") |
149 | else | 159 | else |
150 | t[k] = exp | 160 | t[k] = exp |
151 | end | 161 | end |
@@ -157,7 +167,8 @@ local function firstdef (n, r) return adddef({n}, n, r) end | |||
157 | 167 | ||
158 | local function NT (n, b) | 168 | local function NT (n, b) |
159 | if not b then | 169 | if not b then |
160 | error("rule '"..n.."' used outside a grammar") | 170 | adderror("rule '"..n.."' used outside a grammar") |
171 | return mm.P"" | ||
161 | else return mm.V(n) | 172 | else return mm.V(n) |
162 | end | 173 | end |
163 | end | 174 | end |
@@ -276,10 +287,14 @@ local function compile (p, defs) | |||
276 | if #errors > 0 then | 287 | if #errors > 0 then |
277 | local errmsg = "" | 288 | local errmsg = "" |
278 | for i, err in ipairs(errors) do | 289 | for i, err in ipairs(errors) do |
279 | local line, col = lineno(p, err[2]) | 290 | if #err == 1 then |
280 | errmsg = errmsg .. "Line" .. line .. ", Col " .. col .. ": " .. errorMessages[err[1]] .. "\n" | 291 | errmsg = errmsg .. err[1] .. "\n" |
292 | else | ||
293 | local line, col = lineno(p, err[2]) | ||
294 | errmsg = errmsg .. "Line" .. line .. ", Col " .. col .. ": " .. errorMessages[err[1]] .. "\n" | ||
295 | end | ||
281 | end | 296 | end |
282 | error(errmsg, 3) | 297 | error(errmsg) |
283 | end | 298 | end |
284 | return cp | 299 | return cp |
285 | end | 300 | 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 = { | |||
99 | [[{||}]], | 99 | [[{||}]], |
100 | [[{|@|}]], | 100 | [[{|@|}]], |
101 | [['p' {| 'q' / 'r' }]], | 101 | [['p' {| 'q' / 'r' }]], |
102 | -- 71-73 | 102 | -- 71-75 |
103 | [['a'/{1}'b'/'c']], | 103 | [['a'/{1}'b'/'c']], |
104 | [[x <- {:x:}]], | 104 | [[x <- {:x:}]], |
105 | [[&'p'/&/!/'p'^'q']], | 105 | [[&'p'/&/!/'p'^'q']], |
@@ -107,7 +107,21 @@ local patterns = { | |||
107 | A <- 'a' (B 'b' | 107 | A <- 'a' (B 'b' |
108 | B <- 'x' / ! | 108 | B <- 'x' / ! |
109 | C <- 'c' | 109 | C <- 'c' |
110 | ]] | 110 | ]], |
111 | [[ | ||
112 | A <- %nosuch %def | ||
113 | A <- 'A again' | ||
114 | A <- 'and again' | ||
115 | ]], | ||
116 | -- 76 - 79 | ||
117 | [[names not in grammar]], | ||
118 | [[ | ||
119 | A <- %nosuch %def | ||
120 | A <- 'A again' | ||
121 | A <- 'and again' | ||
122 | ]], | ||
123 | [[ A <- %nosuch ('error' ]], | ||
124 | [[A <- Unknown Rules]] | ||
111 | } | 125 | } |
112 | 126 | ||
113 | for i, patt in ipairs(patterns) do | 127 | for i, patt in ipairs(patterns) do |