aboutsummaryrefslogtreecommitdiff
path: root/relabel.lua
diff options
context:
space:
mode:
authorUndecidable Robot <undecidabot@gmail.com>2016-05-31 12:34:43 +0800
committerUndecidable Robot <undecidabot@gmail.com>2016-05-31 12:34:43 +0800
commit00aa25d7679154d1e1c05d419f9ed9c53ec87b66 (patch)
tree3050d1cc1f309655c55598fcda6c3cd53a303c6f /relabel.lua
parentcfad25a1c830d86882b97cad6674bd941a3c6354 (diff)
downloadlpeglabel-00aa25d7679154d1e1c05d419f9ed9c53ec87b66.tar.gz
lpeglabel-00aa25d7679154d1e1c05d419f9ed9c53ec87b66.tar.bz2
lpeglabel-00aa25d7679154d1e1c05d419f9ed9c53ec87b66.zip
Cleaning up the code
Diffstat (limited to 'relabel.lua')
-rw-r--r--relabel.lua53
1 files changed, 30 insertions, 23 deletions
diff --git a/relabel.lua b/relabel.lua
index 1198be8..8b12811 100644
--- a/relabel.lua
+++ b/relabel.lua
@@ -87,22 +87,12 @@ local syntaxerrs = {}
87local function expect (pattern, labelname) 87local function expect (pattern, labelname)
88 local label = labels[labelname] 88 local label = labels[labelname]
89 local record = function (input, pos) 89 local record = function (input, pos)
90 tinsert(syntaxerrs, {label, pos}) 90 tinsert(syntaxerrs, { label = label, pos = pos })
91 return true 91 return true
92 end 92 end
93 return pattern + m.Cmt("", record) * m.T(label) 93 return pattern + m.Cmt("", record) * m.T(label)
94end 94end
95 95
96local ignore = m.Cmt(any, function (input, pos)
97 return syntaxerrs[#syntaxerrs][2], dummy
98end)
99
100local pointAtStart = m.Cmt(any, function (input, pos)
101 local ret = syntaxerrs[#syntaxerrs][2]
102 syntaxerrs[#syntaxerrs][2] = pos-1
103 return ret, dummy
104end)
105
106 96
107-- Pre-defined names 97-- Pre-defined names
108local Predef = { nl = m.P"\n" } 98local Predef = { nl = m.P"\n" }
@@ -238,6 +228,7 @@ local function labchoice (...)
238 local p = t[1] 228 local p = t[1]
239 local i = 2 229 local i = 2
240 while i + 1 <= n do 230 while i + 1 <= n do
231 -- t[i] == nil when there are no labels
241 p = t[i] and mm.Lc(p, t[i+1], unpack(t[i])) or mt.__add(p, t[i+1]) 232 p = t[i] and mm.Lc(p, t[i+1], unpack(t[i])) or mt.__add(p, t[i+1])
242 i = i + 2 233 i = i + 2
243 end 234 end
@@ -245,6 +236,29 @@ local function labchoice (...)
245 return p 236 return p
246end 237end
247 238
239-- error recovery
240local skip = m.P { "Skip",
241 Skip = (-m.P"/" * -m.P(name * arrow) * m.V"Ignored")^0 * m.Cc(dummy);
242 Ignored = m.V"Group" + any;
243 Group = "(" * (-m.P")" * m.V"Ignored")^0 * ")"
244 + "{" * (-m.P"}" * m.V"Ignored")^0 * "}"
245 + "[" * (-m.P"]" * m.V"Ignored")^0 * "]"
246 + "'" * (-m.P"'" * m.V"Ignored")^0 * "'"
247 + '"' * (-m.P'"' * m.V"Ignored")^0 * '"';
248}
249
250local ignore = m.Cmt(any, function (input, pos)
251 return syntaxerrs[#syntaxerrs].pos, dummy
252end)
253
254local pointAtStart = m.Cmt(any, function (input, pos)
255 -- like ignore but makes the last syntax error point at the start
256 local ret = syntaxerrs[#syntaxerrs].pos
257 syntaxerrs[#syntaxerrs].pos = pos-1
258 return ret, dummy
259end)
260
261
248local function labify (labelnames) 262local function labify (labelnames)
249 for i, l in ipairs(labelnames) do 263 for i, l in ipairs(labelnames) do
250 labelnames[i] = labels[l] 264 labelnames[i] = labels[l]
@@ -275,21 +289,14 @@ local exp = m.P{ "Exp",
275 + (m.V"RecovSeq" * (S * "/" * m.Lc((m.Ct(m.V"Labels") + m.Cc(nil)) 289 + (m.V"RecovSeq" * (S * "/" * m.Lc((m.Ct(m.V"Labels") + m.Cc(nil))
276 * expect(S * m.V"RecovSeq", 290 * expect(S * m.V"RecovSeq",
277 "ExpPatt1"), 291 "ExpPatt1"),
278 m.Cc(nil) * m.V"Skip", 292 m.Cc(nil) * skip,
279 unpack(labelset3)) 293 unpack(labelset3))
280 )^0 294 )^0
281 ) / labchoice); 295 ) / labchoice);
282 Labels = m.P"{" * expect(S * m.V"Label", "ExpLab1") 296 Labels = m.P"{" * expect(S * m.V"Label", "ExpLab1")
283 * (S * "," * expect(S * m.V"Label", "ExpLab2"))^0 297 * (S * "," * expect(S * m.V"Label", "ExpLab2"))^0
284 * expect(S * "}", "MisClose7"); 298 * expect(S * "}", "MisClose7");
285 Skip = (-m.P"/" * -m.P(name * arrow) * m.V"Ignored")^0 * m.Cc(dummy); 299 RecovSeq = m.Lc(m.V"Seq", skip, unpack(labelset1));
286 Ignored = m.V"Group" + any;
287 Group = "(" * (-m.P")" * m.V"Ignored")^0 * ")"
288 + "{" * (-m.P"}" * m.V"Ignored")^0 * "}"
289 + "[" * (-m.P"]" * m.V"Ignored")^0 * "]"
290 + "'" * (-m.P"'" * m.V"Ignored")^0 * "'"
291 + '"' * (-m.P'"' * m.V"Ignored")^0 * '"';
292 RecovSeq = m.Lc(m.V"Seq", m.V"Skip", unpack(labelset1));
293 Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix" * (S * m.V"Prefix")^0, mt.__mul); 300 Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix" * (S * m.V"Prefix")^0, mt.__mul);
294 Prefix = "&" * expect(S * m.V"Prefix", "ExpPatt2") / mt.__len 301 Prefix = "&" * expect(S * m.V"Prefix", "ExpPatt2") / mt.__len
295 + "!" * expect(S * m.V"Prefix", "ExpPatt3") / mt.__unm 302 + "!" * expect(S * m.V"Prefix", "ExpPatt3") / mt.__unm
@@ -376,13 +383,13 @@ local function compile (p, defs)
376 local lines = splitlines(p) 383 local lines = splitlines(p)
377 local errors = {} 384 local errors = {}
378 for i, err in ipairs(syntaxerrs) do 385 for i, err in ipairs(syntaxerrs) do
379 local line, col = lineno(p, err[2]) 386 local line, col = lineno(p, err.pos)
380 tinsert(errors, "L" .. line .. ":C" .. col .. ": " .. errmsgs[err[1]]) 387 tinsert(errors, "L" .. line .. ":C" .. col .. ": " .. errmsgs[err.label])
381 tinsert(errors, lines[line]) 388 tinsert(errors, lines[line])
382 tinsert(errors, rep(" ", col-1) .. "^") 389 tinsert(errors, rep(" ", col-1) .. "^")
383 end 390 end
384 syntaxerrs = {} 391 syntaxerrs = {}
385 error("\n" .. concat(errors, "\n")) 392 error("syntax error(s) in pattern\n" .. concat(errors, "\n"))
386 end 393 end
387 return cp 394 return cp
388end 395end