aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSérgio Queiroz <sqmedeiros@gmail.com>2017-12-29 14:14:29 -0300
committerSérgio Queiroz <sqmedeiros@gmail.com>2017-12-29 14:14:29 -0300
commit08e96cea389b70e7da26b30449f877caf7d33205 (patch)
tree23950eb4b808edc8cffe54e2fe780cc235616ecc
parent19119a91005506ec2133948f3a127e68b6e93f30 (diff)
downloadlpeglabel-08e96cea389b70e7da26b30449f877caf7d33205.tar.gz
lpeglabel-08e96cea389b70e7da26b30449f877caf7d33205.tar.bz2
lpeglabel-08e96cea389b70e7da26b30449f877caf7d33205.zip
Updating documentation for version 1.5
-rw-r--r--README.md133
-rw-r--r--examples/listId2.lua16
-rw-r--r--examples/listId3.lua16
3 files changed, 91 insertions, 74 deletions
diff --git a/README.md b/README.md
index 88b4fc3..c24c06e 100644
--- a/README.md
+++ b/README.md
@@ -129,10 +129,10 @@ function matchPrint(p, s)
129end 129end
130 130
131local p = m.P"a"^0 * m.P"b" + m.P"c" 131local p = m.P"a"^0 * m.P"b" + m.P"c"
132matchPrint(p, "abc") --> r: 3 lab: nil errpos: nil 132matchPrint(p, "abc") --> r: 3 lab: nil errpos: nil
133matchPrint(p, "c") --> r: 2 lab: nil errpos: nil 133matchPrint(p, "c") --> r: 2 lab: nil errpos: nil
134matchPrint(p, "aac") --> r: nil lab: fail errpos: 3 134matchPrint(p, "aac") --> r: nil lab: fail errpos: 3
135matchPrint(p, "xxc") --> r: nil lab: fail errpos: 1 135matchPrint(p, "xxc") --> r: nil lab: fail errpos: 1
136``` 136```
137 137
138 138
@@ -149,20 +149,20 @@ local m = require'lpeglabel'
149local re = require'relabel' 149local re = require'relabel'
150 150
151local terror = { 151local 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
157local id = m.R'az'^1 157local id = m.R'az'^1
158 158
159local g = m.P{ 159local 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
177end 177end
178 178
179print(mymatch(g, "one,two")) 179print(mymatch(g, "one,two")) --> 8
180print(mymatch(g, "one two")) 180print(mymatch(g, "one two")) --> nil Error at line 1 (col 4): expecting EOF before ' two'
181print(mymatch(g, "one,\n two,\nthree,4")) 181print(mymatch(g, "one,\n two,\nthree,4")) --> nil Error at line 3 (col 7): expecting an identifier before '4'
182``` 182```
183 183
184In this example we could think about writing rule <em>List</em> as follows: 184In this example we could think about writing rule <em>List</em> as follows:
@@ -199,20 +199,20 @@ local m = require'lpeglabel'
199local re = require'relabel' 199local re = require'relabel'
200 200
201local terror = { 201local 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
207local id = m.R'az'^1 207local id = m.R'az'^1
208 208
209local g = m.P{ 209local 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
227end 227end
228 228
229print(mymatch(g, "one,two")) 229print(mymatch(g, "one,two")) --> 8
230print(mymatch(g, "one two")) 230print(mymatch(g, "one two")) --> nil Error at line 1 (col 4): expecting ',' before ' two'
231print(mymatch(g, "one,\n two,\nthree,4")) 231print(mymatch(g, "one,\n two,\nthree,4")) --> nil Error at line 3 (col 7): expecting an identifier before '4'
232print(mymatch(g, " 1,2")) 232print(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 = {
307local subject, errors 306local subject, errors
308 307
309function recorderror(pos, lab) 308function 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] })
312end 311end
313 312
314function record (lab) 313function record (lab)
315 return (m.Cp() * m.Cc(lab)) / recorderror 314 return (m.Cp() * m.Cc(lab)) / recorderror
316end 315end
317 316
318function sync (p) 317function sync (p)
319 return (-p * m.P(1))^0 318 return (-p * m.P(1))^0
320end 319end
321 320
322function defaultValue () 321function defaultValue ()
323 return m.Cc"NONE" 322 return m.Cc"NONE"
324end 323end
325 324
326local id = m.R'az'^1 325local id = m.R'az'^1
@@ -338,32 +337,50 @@ local g = m.P{
338} 337}
339 338
340function mymatch (g, s) 339function 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
361end 360end
362 361
363mymatch(g, "one,two") 362mymatch(g, "one,two")
364mymatch(g, "one two three") 363--> Captures (separated by ';'): one; two;
365mymatch(g, "1,\n two, \n3,") 364--> Syntactic errors found: 0
365
366mymatch(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
372mymatch(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
366mymatch(g, "one\n two123, \nthree,") 377mymatch(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
440local function mymatch(g, s) 457local 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
448end 465end
449 466
450local s = [[ 467local s = [[
@@ -455,10 +472,10 @@ repeat
455 n := n - 1 472 n := n - 1
456until (n < 1); 473until (n < 1);
457write f;]] 474write f;]]
458print(mymatch(g, s)) 475print(mymatch(g, s)) --> nil Error at line 6 (col 1): Missing ';' in CmdSeq
459 476
460print(mymatch(g, "a : 2")) 477print(mymatch(g, "a : 2")) --> nil Error at line 1 (col 2): Error matching ':='
461print(mymatch(g, "a := 2; 6")) 478print(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'
2local re = require'relabel' 2local re = require'relabel'
3 3
4local terror = { 4local 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
10local id = m.R'az'^1 10local id = m.R'az'^1
11 11
12local g = m.P{ 12local 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'
2local re = require'relabel' 2local re = require'relabel'
3 3
4local terror = { 4local 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
10local id = m.R'az'^1 10local id = m.R'az'^1
11 11
12local g = m.P{ 12local 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