aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergio Queiroz <sqmedeiros@gmail.com>2017-07-06 11:41:14 -0300
committerSergio Queiroz <sqmedeiros@gmail.com>2017-07-06 11:41:14 -0300
commit976b38458e0bba58ca748e96b53afd9ee74a1d1d (patch)
tree55f910858b7573b90e6491a3d88a66b56d5d4db7
parent8ee42c29131e1c7de48575d6d8a9b24ea6977cbd (diff)
downloadlpeglabel-976b38458e0bba58ca748e96b53afd9ee74a1d1d.tar.gz
lpeglabel-976b38458e0bba58ca748e96b53afd9ee74a1d1d.tar.bz2
lpeglabel-976b38458e0bba58ca748e96b53afd9ee74a1d1d.zip
Fixing the documentation of setlabels and an example of error recovery
-rw-r--r--README.md71
1 files changed, 32 insertions, 39 deletions
diff --git a/README.md b/README.md
index ddad3c8..31cd43e 100644
--- a/README.md
+++ b/README.md
@@ -108,8 +108,8 @@ Returns line and column information regarding position <i>i</i> of the subject.
108#### <a name="re-setl"></a><code>relabel.setlabels (tlabel)</code> 108#### <a name="re-setl"></a><code>relabel.setlabels (tlabel)</code>
109 109
110Allows to specicify a table with labels. They keys of 110Allows to specicify a table with labels. They keys of
111`tlabel` must be integers between 1 and 255, 111`tlabel` must be strings and the associated values must
112and the associated values should be strings. 112be integers between 1 and 255.
113 113
114 114
115### Examples 115### Examples
@@ -279,7 +279,7 @@ local g = m.P{
279 "S", 279 "S",
280 S = m.V"Id" * m.V"List", 280 S = m.V"Id" * m.V"List",
281 List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", 281 List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List",
282 Id = m.V"Sp" * id + m.T(errId), 282 Id = m.V"Sp" * m.C(id) + m.T(errId),
283 Comma = m.V"Sp" * "," + m.T(errComma), 283 Comma = m.V"Sp" * "," + m.T(errComma),
284 Sp = m.S" \n\t"^0, 284 Sp = m.S" \n\t"^0,
285} 285}
@@ -287,64 +287,57 @@ local g = m.P{
287local subject, errors 287local subject, errors
288 288
289function recorderror(pos, lab) 289function recorderror(pos, lab)
290 local line, col = re.calcline(subject, pos) 290 local line, col = re.calcline(subject, pos)
291 table.insert(errors, { line = line, col = col, msg = terror[lab] }) 291 table.insert(errors, { line = line, col = col, msg = terror[lab] })
292end 292end
293 293
294function record (lab) 294function record (lab)
295 return (m.Cp() * m.Cc(lab)) / recorderror 295 return (m.Cp() * m.Cc(lab)) / recorderror
296end 296end
297 297
298function sync (p) 298function sync (p)
299 return (-p * m.P(1))^0 299 return (-p * m.P(1))^0
300end
301
302function defaultValue ()
303 return m.Cc"NONE"
300end 304end
301 305
302local grec = m.P{ 306local grec = m.P{
303 "S", 307 "S",
304 S = m.Rec(m.Rec(g, m.V"ErrComma", errComma), m.V"ErrId", errId), 308 S = m.Rec(m.Rec(g, m.V"ErrComma", errComma), m.V"ErrId", errId),
305 ErrComma = record(errComma) * sync(id), 309 ErrComma = record(errComma) * sync(id),
306 ErrId = record(errId) * sync(m.P",") 310 ErrId = record(errId) * sync(m.P",") * defaultValue(),
307} 311}
308 312
309 313
310function mymatch (g, s) 314function mymatch (g, s)
311 errors = {} 315 errors = {}
312 subject = s 316 subject = s
313 local r, e, sfail = g:match(s) 317 io.write("Input: ", s, "\n")
314 if #errors > 0 then 318 local r = { g:match(s) }
315 local out = {} 319 io.write("Captures (separated by ';'): ")
320 for k, v in pairs(r) do
321 io.write(v .. "; ")
322 end
323 io.write("\nSyntactic errors found: " .. #errors)
324 if #errors > 0 then
325 io.write("\n")
326 local out = {}
316 for i, err in ipairs(errors) do 327 for i, err in ipairs(errors) do
317 local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg 328 local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg
318 table.insert(out, msg) 329 table.insert(out, msg)
319 end 330 end
320 return nil, table.concat(out, "\n") .. "\n" 331 io.write(table.concat(out, "\n"))
321 end 332 end
322 return r 333 print("\n")
334 return r
323end 335end
324 336
325print(mymatch(grec, "one,two")) 337mymatch(grec, "one,two")
326-- Captures (separated by ';'): one; two; 338mymatch(grec, "one two three")
327-- Syntactic errors found: 0 339mymatch(grec, "1,\n two, \n3,")
328 340mymatch(grec, "one\n two123, \nthree,")
329print(mymatch(grec, "one two three"))
330-- Captures (separated by ';'): one; two; three;
331-- Syntactic errors found: 2
332-- Error at line 1 (col 4): expecting ','
333-- Error at line 1 (col 8): expecting ','
334
335print(mymatch(grec, "1,\n two, \n3,"))
336-- Captures (separated by ';'): NONE; two; NONE; NONE;
337-- Syntactic errors found: 3
338-- Error at line 1 (col 1): expecting an identifier
339-- Error at line 2 (col 6): expecting an identifier
340-- Error at line 3 (col 2): expecting an identifier
341
342print(mymatch(grec, "one\n two123, \nthree,"))
343-- Captures (separated by ';'): one; two; three; NONE;
344-- Syntactic errors found: 3
345-- Error at line 2 (col 1): expecting ','
346-- Error at line 2 (col 5): expecting ','
347-- Error at line 3 (col 6): expecting an identifier
348``` 341```
349 342
350##### *relabel* syntax 343##### *relabel* syntax