diff options
author | Sergio Queiroz <sqmedeiros@gmail.com> | 2017-01-04 14:50:41 -0300 |
---|---|---|
committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2017-01-04 14:50:41 -0300 |
commit | 02b61c3bcbda1b42959ecdf23750647f573f58af (patch) | |
tree | ac8dec513542b5937b962c60fc6d53a1b05af45c | |
parent | e0feb1b3a659411d53b231d24929c4b7b4d92431 (diff) | |
download | lpeglabel-02b61c3bcbda1b42959ecdf23750647f573f58af.tar.gz lpeglabel-02b61c3bcbda1b42959ecdf23750647f573f58af.tar.bz2 lpeglabel-02b61c3bcbda1b42959ecdf23750647f573f58af.zip |
Adding to README an example about farthest failure
-rw-r--r-- | README.md | 23 | ||||
-rw-r--r-- | examples/farthest.lua | 14 |
2 files changed, 37 insertions, 0 deletions
@@ -119,6 +119,29 @@ The code of these and of other examples is available | |||
119 | in the *examples* directory. | 119 | in the *examples* directory. |
120 | 120 | ||
121 | 121 | ||
122 | #### Reporting the farthest failure | ||
123 | |||
124 | This example illustrates the new values returned | ||
125 | by the *match* function in case of an unsuccessful | ||
126 | matching. As no error is thrown, when the matching | ||
127 | fails *sfail* represents the farthest suffix where | ||
128 | an ordinary failure occurred. | ||
129 | |||
130 | ```lua | ||
131 | local m = require'lpeglabel' | ||
132 | |||
133 | function matchPrint(p, s) | ||
134 | local r, lab, sfail = p:match(s) | ||
135 | print("r: ", r, "lab: ", lab, "sfail: ", sfail) | ||
136 | end | ||
137 | |||
138 | local p = m.P"a"^0 * m.P"b" + m.P"c" | ||
139 | matchPrint(p, "abc") --> r: 3 lab: nil sfail: nil | ||
140 | matchPrint(p, "c") --> r: 2 lab: nil sfail: nil | ||
141 | matchPrint(p, "aac") --> r: nil lab: 0 sfail: c | ||
142 | matchPrint(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 | ||
124 | The following example defines a grammar that matches | 147 | The 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 @@ | |||
1 | local m = require'lpeglabel' | ||
2 | |||
3 | function matchPrint(p, s) | ||
4 | local r, lab, sfail = p:match(s) | ||
5 | print("r: ", r, "lab: ", lab, "sfail: ", sfail) | ||
6 | end | ||
7 | |||
8 | local p = m.P"a"^0 * m.P"b" + m.P"c" | ||
9 | matchPrint(p, "abc") --> r: 3 lab: nil sfail: nil | ||
10 | matchPrint(p, "c") --> r: 2 lab: nil sfail: nil | ||
11 | matchPrint(p, "aac") --> r: nil lab: 0 sfail: c | ||
12 | matchPrint(p, "xxc") --> r: nil lab: 0 sfail: xxc | ||
13 | |||
14 | |||