diff options
author | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-29 14:14:29 -0300 |
---|---|---|
committer | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-29 14:14:29 -0300 |
commit | 08e96cea389b70e7da26b30449f877caf7d33205 (patch) | |
tree | 23950eb4b808edc8cffe54e2fe780cc235616ecc | |
parent | 19119a91005506ec2133948f3a127e68b6e93f30 (diff) | |
download | lpeglabel-08e96cea389b70e7da26b30449f877caf7d33205.tar.gz lpeglabel-08e96cea389b70e7da26b30449f877caf7d33205.tar.bz2 lpeglabel-08e96cea389b70e7da26b30449f877caf7d33205.zip |
Updating documentation for version 1.5
-rw-r--r-- | README.md | 133 | ||||
-rw-r--r-- | examples/listId2.lua | 16 | ||||
-rw-r--r-- | examples/listId3.lua | 16 |
3 files changed, 91 insertions, 74 deletions
@@ -129,10 +129,10 @@ function matchPrint(p, s) | |||
129 | end | 129 | end |
130 | 130 | ||
131 | local p = m.P"a"^0 * m.P"b" + m.P"c" | 131 | local p = m.P"a"^0 * m.P"b" + m.P"c" |
132 | matchPrint(p, "abc") --> r: 3 lab: nil errpos: nil | 132 | matchPrint(p, "abc") --> r: 3 lab: nil errpos: nil |
133 | matchPrint(p, "c") --> r: 2 lab: nil errpos: nil | 133 | matchPrint(p, "c") --> r: 2 lab: nil errpos: nil |
134 | matchPrint(p, "aac") --> r: nil lab: fail errpos: 3 | 134 | matchPrint(p, "aac") --> r: nil lab: fail errpos: 3 |
135 | matchPrint(p, "xxc") --> r: nil lab: fail errpos: 1 | 135 | matchPrint(p, "xxc") --> r: nil lab: fail errpos: 1 |
136 | ``` | 136 | ``` |
137 | 137 | ||
138 | 138 | ||
@@ -149,20 +149,20 @@ local m = require'lpeglabel' | |||
149 | local re = require'relabel' | 149 | local re = require'relabel' |
150 | 150 | ||
151 | local terror = { | 151 | local terror = { |
152 | ErrId = "expecting an identifier", | 152 | ErrId = "expecting an identifier", |
153 | ErrEnd = "expecting EOF", | 153 | ErrEnd = "expecting EOF", |
154 | fail = "undefined" | 154 | fail = "undefined" |
155 | } | 155 | } |
156 | 156 | ||
157 | local id = m.R'az'^1 | 157 | local id = m.R'az'^1 |
158 | 158 | ||
159 | local g = m.P{ | 159 | local g = m.P{ |
160 | 'S', | 160 | 'S', |
161 | S = m.V'List' * (-m.P(1) + m.T'ErrEnd'), | 161 | S = m.V'List' * (-m.P(1) + m.T'ErrEnd'), |
162 | List = m.V'Id' * (m.V'Comma' * (m.V'Id' + m.T'ErrId'))^0, | 162 | List = m.V'Id' * (m.V'Comma' * (m.V'Id' + m.T'ErrId'))^0, |
163 | Id = m.V'Sp' * id, | 163 | Id = m.V'Sp' * id, |
164 | Comma = m.V'Sp' * ',', | 164 | Comma = m.V'Sp' * ',', |
165 | Sp = m.S' \n\t'^0, | 165 | Sp = m.S' \n\t'^0, |
166 | } | 166 | } |
167 | 167 | ||
168 | 168 | ||
@@ -176,9 +176,9 @@ function mymatch (g, s) | |||
176 | return r | 176 | return r |
177 | end | 177 | end |
178 | 178 | ||
179 | print(mymatch(g, "one,two")) | 179 | print(mymatch(g, "one,two")) --> 8 |
180 | print(mymatch(g, "one two")) | 180 | print(mymatch(g, "one two")) --> nil Error at line 1 (col 4): expecting EOF before ' two' |
181 | print(mymatch(g, "one,\n two,\nthree,4")) | 181 | print(mymatch(g, "one,\n two,\nthree,4")) --> nil Error at line 3 (col 7): expecting an identifier before '4' |
182 | ``` | 182 | ``` |
183 | 183 | ||
184 | In this example we could think about writing rule <em>List</em> as follows: | 184 | In this example we could think about writing rule <em>List</em> as follows: |
@@ -199,20 +199,20 @@ local m = require'lpeglabel' | |||
199 | local re = require'relabel' | 199 | local re = require'relabel' |
200 | 200 | ||
201 | local terror = { | 201 | local terror = { |
202 | ErrId = "expecting an identifier", | 202 | ErrId = "expecting an identifier", |
203 | ErrComma = "expecting ','", | 203 | ErrComma = "expecting ','", |
204 | fail = "undefined" | 204 | fail = "undefined" |
205 | } | 205 | } |
206 | 206 | ||
207 | local id = m.R'az'^1 | 207 | local id = m.R'az'^1 |
208 | 208 | ||
209 | local g = m.P{ | 209 | local g = m.P{ |
210 | 'S', | 210 | 'S', |
211 | S = m.V'List', | 211 | S = m.V'List', |
212 | List = m.V'Id' * (#m.P(1) * m.V'Comma' * (m.V'Id' + m.T'ErrId'))^0, | 212 | List = m.V'Id' * (#m.P(1) * m.V'Comma' * (m.V'Id' + m.T'ErrId'))^0, |
213 | Id = m.V'Sp' * id, | 213 | Id = m.V'Sp' * id, |
214 | Comma = m.V'Sp' * ',' + m.T'ErrComma', | 214 | Comma = m.V'Sp' * ',' + m.T'ErrComma', |
215 | Sp = m.S' \n\t'^0, | 215 | Sp = m.S' \n\t'^0, |
216 | } | 216 | } |
217 | 217 | ||
218 | 218 | ||
@@ -226,11 +226,10 @@ function mymatch (g, s) | |||
226 | return r | 226 | return r |
227 | end | 227 | end |
228 | 228 | ||
229 | print(mymatch(g, "one,two")) | 229 | print(mymatch(g, "one,two")) --> 8 |
230 | print(mymatch(g, "one two")) | 230 | print(mymatch(g, "one two")) --> nil Error at line 1 (col 4): expecting ',' before ' two' |
231 | print(mymatch(g, "one,\n two,\nthree,4")) | 231 | print(mymatch(g, "one,\n two,\nthree,4")) --> nil Error at line 3 (col 7): expecting an identifier before '4' |
232 | print(mymatch(g, " 1,2")) | 232 | print(mymatch(g, " 1,2")) --> nil Error at line 1 (col 2): undefined before '1,2' |
233 | |||
234 | ``` | 233 | ``` |
235 | 234 | ||
236 | 235 | ||
@@ -307,20 +306,20 @@ local terror = { | |||
307 | local subject, errors | 306 | local subject, errors |
308 | 307 | ||
309 | function recorderror(pos, lab) | 308 | function recorderror(pos, lab) |
310 | local line, col = re.calcline(subject, pos) | 309 | local line, col = re.calcline(subject, pos) |
311 | table.insert(errors, { line = line, col = col, msg = terror[lab] }) | 310 | table.insert(errors, { line = line, col = col, msg = terror[lab] }) |
312 | end | 311 | end |
313 | 312 | ||
314 | function record (lab) | 313 | function record (lab) |
315 | return (m.Cp() * m.Cc(lab)) / recorderror | 314 | return (m.Cp() * m.Cc(lab)) / recorderror |
316 | end | 315 | end |
317 | 316 | ||
318 | function sync (p) | 317 | function sync (p) |
319 | return (-p * m.P(1))^0 | 318 | return (-p * m.P(1))^0 |
320 | end | 319 | end |
321 | 320 | ||
322 | function defaultValue () | 321 | function defaultValue () |
323 | return m.Cc"NONE" | 322 | return m.Cc"NONE" |
324 | end | 323 | end |
325 | 324 | ||
326 | local id = m.R'az'^1 | 325 | local id = m.R'az'^1 |
@@ -338,32 +337,50 @@ local g = m.P{ | |||
338 | } | 337 | } |
339 | 338 | ||
340 | function mymatch (g, s) | 339 | function mymatch (g, s) |
341 | errors = {} | 340 | errors = {} |
342 | subject = s | 341 | subject = s |
343 | io.write("Input: ", s, "\n") | 342 | io.write("Input: ", s, "\n") |
344 | local r = { g:match(s) } | 343 | local r = { g:match(s) } |
345 | io.write("Captures (separated by ';'): ") | 344 | io.write("Captures (separated by ';'): ") |
346 | for k, v in pairs(r) do | 345 | for k, v in pairs(r) do |
347 | io.write(v .. "; ") | 346 | io.write(v .. "; ") |
348 | end | 347 | end |
349 | io.write("\nSyntactic errors found: " .. #errors) | 348 | io.write("\nSyntactic errors found: " .. #errors) |
350 | if #errors > 0 then | 349 | if #errors > 0 then |
351 | io.write("\n") | 350 | io.write("\n") |
352 | local out = {} | 351 | local out = {} |
353 | for i, err in ipairs(errors) do | 352 | for i, err in ipairs(errors) do |
354 | local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg | 353 | local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg |
355 | table.insert(out, msg) | 354 | table.insert(out, msg) |
356 | end | 355 | end |
357 | io.write(table.concat(out, "\n")) | 356 | io.write(table.concat(out, "\n")) |
358 | end | 357 | end |
359 | print("\n") | 358 | print("\n") |
360 | return r | 359 | return r |
361 | end | 360 | end |
362 | 361 | ||
363 | mymatch(g, "one,two") | 362 | mymatch(g, "one,two") |
364 | mymatch(g, "one two three") | 363 | --> Captures (separated by ';'): one; two; |
365 | mymatch(g, "1,\n two, \n3,") | 364 | --> Syntactic errors found: 0 |
365 | |||
366 | mymatch(g, "one two three") | ||
367 | --> Captures (separated by ';'): one; two; three; | ||
368 | --> Syntactic errors found: 2 | ||
369 | --> Error at line 1 (col 4): expecting ',' | ||
370 | --> Error at line 1 (col 8): expecting ',' | ||
371 | |||
372 | mymatch(g, "1,\n two, \n3,") | ||
373 | --> Captures (separated by ';'): NONE; | ||
374 | --> Syntactic errors found: 1 | ||
375 | --> Error at line 1 (col 2): expecting a list of identifiers | ||
376 | |||
366 | mymatch(g, "one\n two123, \nthree,") | 377 | mymatch(g, "one\n two123, \nthree,") |
378 | --> Captures (separated by ';'): one; two; three; NONE; | ||
379 | --> Syntactic errors found: 3 | ||
380 | --> Error at line 2 (col 1): expecting ',' | ||
381 | --> Error at line 2 (col 5): expecting ',' | ||
382 | --> Error at line 3 (col 6): expecting an identifier | ||
383 | |||
367 | ``` | 384 | ``` |
368 | 385 | ||
369 | ##### *relabel* syntax | 386 | ##### *relabel* syntax |
@@ -438,13 +455,13 @@ g = re.compile([[ | |||
438 | 455 | ||
439 | 456 | ||
440 | local function mymatch(g, s) | 457 | local function mymatch(g, s) |
441 | local r, e, pos = g:match(s) | 458 | local r, e, pos = g:match(s) |
442 | if not r then | 459 | if not r then |
443 | local line, col = re.calcline(s, pos) | 460 | local line, col = re.calcline(s, pos) |
444 | local msg = "Error at line " .. line .. " (col " .. col .. "): " | 461 | local msg = "Error at line " .. line .. " (col " .. col .. "): " |
445 | return r, msg .. terror[e] | 462 | return r, msg .. terror[e] |
446 | end | 463 | end |
447 | return r | 464 | return r |
448 | end | 465 | end |
449 | 466 | ||
450 | local s = [[ | 467 | local s = [[ |
@@ -455,10 +472,10 @@ repeat | |||
455 | n := n - 1 | 472 | n := n - 1 |
456 | until (n < 1); | 473 | until (n < 1); |
457 | write f;]] | 474 | write f;]] |
458 | print(mymatch(g, s)) | 475 | print(mymatch(g, s)) --> nil Error at line 6 (col 1): Missing ';' in CmdSeq |
459 | 476 | ||
460 | print(mymatch(g, "a : 2")) | 477 | print(mymatch(g, "a : 2")) --> nil Error at line 1 (col 2): Error matching ':=' |
461 | print(mymatch(g, "a := 2; 6")) | 478 | print(mymatch(g, "a := 2; 6")) --> nil Error at line 1 (col 8): Error, expecting EOF |
462 | ``` | 479 | ``` |
463 | 480 | ||
464 | ### Caveats | 481 | ### Caveats |
diff --git a/examples/listId2.lua b/examples/listId2.lua index dc30ce5..11a181a 100644 --- a/examples/listId2.lua +++ b/examples/listId2.lua | |||
@@ -2,20 +2,20 @@ local m = require'lpeglabel' | |||
2 | local re = require'relabel' | 2 | local re = require'relabel' |
3 | 3 | ||
4 | local terror = { | 4 | local terror = { |
5 | ErrId = "expecting an identifier", | 5 | ErrId = "expecting an identifier", |
6 | ErrEnd = "expecting EOF", | 6 | ErrEnd = "expecting EOF", |
7 | fail = "undefined" | 7 | fail = "undefined" |
8 | } | 8 | } |
9 | 9 | ||
10 | local id = m.R'az'^1 | 10 | local id = m.R'az'^1 |
11 | 11 | ||
12 | local g = m.P{ | 12 | local g = m.P{ |
13 | 'S', | 13 | 'S', |
14 | S = m.V'List' * (-m.P(1) + m.T'ErrEnd'), | 14 | S = m.V'List' * (-m.P(1) + m.T'ErrEnd'), |
15 | List = m.V'Id' * (m.V'Comma' * (m.V'Id' + m.T'ErrId'))^0, | 15 | List = m.V'Id' * (m.V'Comma' * (m.V'Id' + m.T'ErrId'))^0, |
16 | Id = m.V'Sp' * id, | 16 | Id = m.V'Sp' * id, |
17 | Comma = m.V'Sp' * ',', | 17 | Comma = m.V'Sp' * ',', |
18 | Sp = m.S' \n\t'^0, | 18 | Sp = m.S' \n\t'^0, |
19 | } | 19 | } |
20 | 20 | ||
21 | 21 | ||
diff --git a/examples/listId3.lua b/examples/listId3.lua index 03da97d..84793ce 100644 --- a/examples/listId3.lua +++ b/examples/listId3.lua | |||
@@ -2,20 +2,20 @@ local m = require'lpeglabel' | |||
2 | local re = require'relabel' | 2 | local re = require'relabel' |
3 | 3 | ||
4 | local terror = { | 4 | local terror = { |
5 | ErrId = "expecting an identifier", | 5 | ErrId = "expecting an identifier", |
6 | ErrComma = "expecting ','", | 6 | ErrComma = "expecting ','", |
7 | fail = "undefined" | 7 | fail = "undefined" |
8 | } | 8 | } |
9 | 9 | ||
10 | local id = m.R'az'^1 | 10 | local id = m.R'az'^1 |
11 | 11 | ||
12 | local g = m.P{ | 12 | local g = m.P{ |
13 | 'S', | 13 | 'S', |
14 | S = m.V'List', | 14 | S = m.V'List', |
15 | List = m.V'Id' * (#m.P(1) * m.V'Comma' * (m.V'Id' + m.T'ErrId'))^0, | 15 | List = m.V'Id' * (#m.P(1) * m.V'Comma' * (m.V'Id' + m.T'ErrId'))^0, |
16 | Id = m.V'Sp' * id, | 16 | Id = m.V'Sp' * id, |
17 | Comma = m.V'Sp' * ',' + m.T'ErrComma', | 17 | Comma = m.V'Sp' * ',' + m.T'ErrComma', |
18 | Sp = m.S' \n\t'^0, | 18 | Sp = m.S' \n\t'^0, |
19 | } | 19 | } |
20 | 20 | ||
21 | 21 | ||