diff options
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 65 |
1 files changed, 51 insertions, 14 deletions
@@ -30,16 +30,15 @@ An error (a non-ordinary failure), by its turn, is produced | |||
30 | by the throw operator and may be caught by the recovery operator. | 30 | by the throw operator and may be caught by the recovery operator. |
31 | 31 | ||
32 | In LPegLabel, the result of an unsuccessful matching | 32 | In LPegLabel, the result of an unsuccessful matching |
33 | is a triple **nil, lab, sfail**, where **lab** | 33 | is a triple **nil, lab, errpos**, where **lab** |
34 | is the label associated with the failure, and | 34 | is the label associated with the failure, and |
35 | **sfail** is the suffix input being matched when | 35 | **errpos** is the input position being matched when |
36 | **lab** was thrown. | 36 | **lab** was thrown. |
37 | 37 | ||
38 | When **lab** is an ordinary failure and no error was thrown before, | 38 | When **lab** is an ordinary failure and no error was thrown before, |
39 | **sfail** is formed according to the farthest position where an | 39 | **errpos** is the farthest position where an ordinary failure occurred. |
40 | ordinary failure occurred. | ||
41 | In case **lab** is an ordinary failure and an error | 40 | In case **lab** is an ordinary failure and an error |
42 | was thrown before, **sfail** is the farthest suffix | 41 | was thrown before, **errpos** is the farthest input position |
43 | where an ordinary failure occurred after the last error. | 42 | where an ordinary failure occurred after the last error. |
44 | 43 | ||
45 | Below there is a brief summary of the new functions provided by LpegLabel: | 44 | Below there is a brief summary of the new functions provided by LpegLabel: |
@@ -51,12 +50,22 @@ Below there is a brief summary of the new functions provided by LpegLabel: | |||
51 | <tr><td><a href="#f-rec"><code>lpeglabel.Rec (p1, p2, l1 [, l2, ..., ln])</code></a></td> | 50 | <tr><td><a href="#f-rec"><code>lpeglabel.Rec (p1, p2, l1 [, l2, ..., ln])</code></a></td> |
52 | <td>Specifies a recovery pattern <code>p2</code> for <code>p1</code>, | 51 | <td>Specifies a recovery pattern <code>p2</code> for <code>p1</code>, |
53 | when the matching of <code>p1</code> gives one of the labels l1, ..., ln.</td></tr> | 52 | when the matching of <code>p1</code> gives one of the labels l1, ..., ln.</td></tr> |
53 | <tr><td><a href="#f-lc"><code>lpeglabel.Lc (p1, p2, l1, ..., ln)</code></a></td> | ||
54 | <td>Matches <code>p1</code> and tries to match <code>p2</code> | ||
55 | if the matching of <code>p1</code> gives one of l<sub>1</sub>, ..., l<sub>n</sub> | ||
56 | </td></tr> | ||
54 | <tr><td><a href="#re-t"><code>%{l}</code></a></td> | 57 | <tr><td><a href="#re-t"><code>%{l}</code></a></td> |
55 | <td>Syntax of <em>relabel</em> module. Equivalent to <code>lpeglabel.T(l)</code> | 58 | <td>Syntax of <em>relabel</em> module. Equivalent to <code>lpeglabel.T(l)</code> |
56 | </td></tr> | 59 | </td></tr> |
60 | <tr><td><a href="#re-pow"><code>p^l</code></a></td> | ||
61 | <td>Syntax sugar available at <em>relabel</em> for <code>p / %{l}</code> | ||
62 | </td></tr> | ||
57 | <tr><td><a href="#re-rec"><code>p1 //{l1 [, l2, ..., ln} p2</code></a></td> | 63 | <tr><td><a href="#re-rec"><code>p1 //{l1 [, l2, ..., ln} p2</code></a></td> |
58 | <td>Syntax of <em>relabel</em> module. Equivalent to <code>lpeglabel.Rec(p1, p2, l1, ..., ln)</code> | 64 | <td>Syntax of <em>relabel</em> module. Equivalent to <code>lpeglabel.Rec(p1, p2, l1, ..., ln)</code> |
59 | </td></tr> | 65 | </td></tr> |
66 | <tr><td><a href="#re-lc"><code>p1 /{l1, ..., ln} p2</code></a></td> | ||
67 | <td>Syntax of <em>relabel</em> module. Equivalent to <code>lpeg.Lc(p1, p2, l1, ..., ln)</code> | ||
68 | </td></tr> | ||
60 | <tr><td><a href="#re-line"><code>relabel.calcline(subject, i)</code></a></td> | 69 | <tr><td><a href="#re-line"><code>relabel.calcline(subject, i)</code></a></td> |
61 | <td>Calculates line and column information regarding position <i>i</i> of the subject</code> | 70 | <td>Calculates line and column information regarding position <i>i</i> of the subject</code> |
62 | </td></tr> | 71 | </td></tr> |
@@ -75,7 +84,7 @@ Returns a pattern that throws the label `l`. | |||
75 | A label must be an integer between 1 and 255. | 84 | A label must be an integer between 1 and 255. |
76 | 85 | ||
77 | This pattern always causes a failure, whose associated | 86 | This pattern always causes a failure, whose associated |
78 | position will be used to set **sfail**, no matter | 87 | position will be used to set **errpos**, no matter |
79 | whether this is the farthest failure position or not. | 88 | whether this is the farthest failure position or not. |
80 | 89 | ||
81 | 90 | ||
@@ -87,18 +96,45 @@ then the matching of `p2` is tried from the failure position of `p1`. | |||
87 | Otherwise, the result of the matching of `p1` is the pattern's result. | 96 | Otherwise, the result of the matching of `p1` is the pattern's result. |
88 | 97 | ||
89 | 98 | ||
99 | #### <a name="f-lc"></a><code>lpeglabel.Lc(p1, p2, l1, ..., ln)</code> | ||
100 | |||
101 | Returns a pattern equivalent to a *labeled ordered choice*. | ||
102 | If the matching of `p1` gives one of the labels `l1, ..., ln`, | ||
103 | then the matching of `p2` is tried from the same position. Otherwise, | ||
104 | the result of the matching of `p1` is the pattern's result. | ||
105 | |||
106 | <!--- | ||
107 | The labeled ordered choice `lpeg.Lc(p1, p2, 0)` is equivalent to the | ||
108 | regular ordered choice `p1 / p2`. | ||
109 | --> | ||
110 | |||
111 | Although PEG's ordered choice is associative, the labeled ordered choice is not. | ||
112 | When using this function, the user should take care to build a left-associative | ||
113 | labeled ordered choice pattern. | ||
114 | |||
115 | |||
90 | #### <a name="re-t"></a><code>%{l}</code> | 116 | #### <a name="re-t"></a><code>%{l}</code> |
91 | 117 | ||
92 | Syntax of *relabel* module. Equivalent to `lpeg.T(l)`. | 118 | Syntax of *relabel* module. Equivalent to `lpeg.T(l)`. |
93 | 119 | ||
94 | 120 | ||
95 | #### <a name="re-lc"></a><code>p1 //{l1, ..., ln} p2</code> | 121 | #### <a name="re-rec"></a><code>p1 //{l1, ..., ln} p2</code> |
96 | 122 | ||
97 | Syntax of *relabel* module. Equivalent to `lpeglabel.Rec(p1, p2, l1, ..., ln)`. | 123 | Syntax of *relabel* module. Equivalent to `lpeglabel.Rec(p1, p2, l1, ..., ln)`. |
98 | 124 | ||
99 | The `//{}` operator is left-associative. | 125 | The `//{}` operator is left-associative. |
100 | 126 | ||
101 | 127 | ||
128 | #### <a name="re-lc"></a><code>p1 /{l1, ..., ln} p2</code> | ||
129 | |||
130 | Syntax of *relabel* module. Equivalent to `lpeg.Lc(p1, p2, l1, ..., ln)`. | ||
131 | |||
132 | The `/{}` operator is left-associative. | ||
133 | |||
134 | A grammar can use both choice operators (`/` and `/{}`), | ||
135 | but a single choice can not mix them. That is, the parser of `relabel` | ||
136 | module will not recognize a pattern as `p1 / p2 /{l1} p3`. | ||
137 | |||
102 | 138 | ||
103 | #### <a name="re-line"></a><code>relabel.calcline (subject, i)</code> | 139 | #### <a name="re-line"></a><code>relabel.calcline (subject, i)</code> |
104 | 140 | ||
@@ -124,24 +160,25 @@ in the *examples* directory. | |||
124 | This example illustrates the new values returned | 160 | This example illustrates the new values returned |
125 | by the *match* function in case of an unsuccessful | 161 | by the *match* function in case of an unsuccessful |
126 | matching. As no error is thrown, when the matching | 162 | matching. As no error is thrown, when the matching |
127 | fails *sfail* represents the farthest suffix where | 163 | fails *errpos* represents the farthest suffix where |
128 | an ordinary failure occurred. | 164 | an ordinary failure occurred. |
129 | 165 | ||
130 | ```lua | 166 | ```lua |
131 | local m = require'lpeglabel' | 167 | local m = require'lpeglabel' |
132 | 168 | ||
133 | function matchPrint(p, s) | 169 | function matchPrint(p, s) |
134 | local r, lab, sfail = p:match(s) | 170 | local r, lab, errpos = p:match(s) |
135 | print("r: ", r, "lab: ", lab, "sfail: ", sfail) | 171 | print("r: ", r, "lab: ", lab, "errpos: ", errpos) |
136 | end | 172 | end |
137 | 173 | ||
138 | local p = m.P"a"^0 * m.P"b" + m.P"c" | 174 | local p = m.P"a"^0 * m.P"b" + m.P"c" |
139 | matchPrint(p, "abc") --> r: 3 lab: nil sfail: nil | 175 | matchPrint(p, "abc") --> r: 3 lab: nil errpos: nil |
140 | matchPrint(p, "c") --> r: 2 lab: nil sfail: nil | 176 | matchPrint(p, "c") --> r: 2 lab: nil errpos: nil |
141 | matchPrint(p, "aac") --> r: nil lab: 0 sfail: c | 177 | matchPrint(p, "aac") --> r: nil lab: 0 errpos: 3 |
142 | matchPrint(p, "xxc") --> r: nil lab: 0 sfail: xxc | 178 | matchPrint(p, "xxc") --> r: nil lab: 0 errpos: 1 |
143 | ``` | 179 | ``` |
144 | 180 | ||
181 | |||
145 | #### Matching a list of identifiers separated by commas | 182 | #### Matching a list of identifiers separated by commas |
146 | 183 | ||
147 | The following example defines a grammar that matches | 184 | The following example defines a grammar that matches |