aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergio Queiroz <sqmedeiros@gmail.com>2017-01-04 14:50:41 -0300
committerSergio Queiroz <sqmedeiros@gmail.com>2017-01-04 14:50:41 -0300
commit02b61c3bcbda1b42959ecdf23750647f573f58af (patch)
treeac8dec513542b5937b962c60fc6d53a1b05af45c
parente0feb1b3a659411d53b231d24929c4b7b4d92431 (diff)
downloadlpeglabel-02b61c3bcbda1b42959ecdf23750647f573f58af.tar.gz
lpeglabel-02b61c3bcbda1b42959ecdf23750647f573f58af.tar.bz2
lpeglabel-02b61c3bcbda1b42959ecdf23750647f573f58af.zip
Adding to README an example about farthest failure
-rw-r--r--README.md23
-rw-r--r--examples/farthest.lua14
2 files changed, 37 insertions, 0 deletions
diff --git a/README.md b/README.md
index 907802f..ddad3c8 100644
--- a/README.md
+++ b/README.md
@@ -119,6 +119,29 @@ The code of these and of other examples is available
119in the *examples* directory. 119in the *examples* directory.
120 120
121 121
122#### Reporting the farthest failure
123
124This example illustrates the new values returned
125by the *match* function in case of an unsuccessful
126matching. As no error is thrown, when the matching
127fails *sfail* represents the farthest suffix where
128an ordinary failure occurred.
129
130```lua
131local m = require'lpeglabel'
132
133function matchPrint(p, s)
134 local r, lab, sfail = p:match(s)
135 print("r: ", r, "lab: ", lab, "sfail: ", sfail)
136end
137
138local p = m.P"a"^0 * m.P"b" + m.P"c"
139matchPrint(p, "abc") --> r: 3 lab: nil sfail: nil
140matchPrint(p, "c") --> r: 2 lab: nil sfail: nil
141matchPrint(p, "aac") --> r: nil lab: 0 sfail: c
142matchPrint(p, "xxc") --> r: nil lab: 0 sfail: xxc
143```
144
122#### Matching a list of identifiers separated by commas 145#### Matching a list of identifiers separated by commas
123 146
124The following example defines a grammar that matches 147The following example defines a grammar that matches
diff --git a/examples/farthest.lua b/examples/farthest.lua
new file mode 100644
index 0000000..692390f
--- /dev/null
+++ b/examples/farthest.lua
@@ -0,0 +1,14 @@
1local m = require'lpeglabel'
2
3function matchPrint(p, s)
4 local r, lab, sfail = p:match(s)
5 print("r: ", r, "lab: ", lab, "sfail: ", sfail)
6end
7
8local p = m.P"a"^0 * m.P"b" + m.P"c"
9matchPrint(p, "abc") --> r: 3 lab: nil sfail: nil
10matchPrint(p, "c") --> r: 2 lab: nil sfail: nil
11matchPrint(p, "aac") --> r: nil lab: 0 sfail: c
12matchPrint(p, "xxc") --> r: nil lab: 0 sfail: xxc
13
14