diff options
-rw-r--r-- | lpeglabel.html | 72 |
1 files changed, 38 insertions, 34 deletions
diff --git a/lpeglabel.html b/lpeglabel.html index b38e900..30e6636 100644 --- a/lpeglabel.html +++ b/lpeglabel.html | |||
@@ -10,14 +10,12 @@ | |||
10 | </head> | 10 | </head> |
11 | <body> | 11 | <body> |
12 | 12 | ||
13 | <!-- $Id: lpeg.html,v 1.71 2013/04/11 19:17:41 roberto Exp $ --> | ||
14 | |||
15 | <div id="container"> | 13 | <div id="container"> |
16 | 14 | ||
17 | <div id="product"> | 15 | <div id="product"> |
18 | <div id="product_logo"> | 16 | <div id="product_logo"> |
19 | <a href="https://github.com/sqmedeiros/lpeglabel"> | 17 | <a href="https://github.com/sqmedeiros/lpeglabel"> |
20 | <img alt="LPeg logo" src="lpeg-128.gif"/></a> | 18 | <img alt="LPegLabel logo" src="lpeglabel-logo.gif" width="128"/></a> |
21 | 19 | ||
22 | </div> | 20 | </div> |
23 | <div id="product_name"><big><strong>LPegLabel</strong></big></div> | 21 | <div id="product_name"><big><strong>LPegLabel</strong></big></div> |
@@ -86,14 +84,13 @@ provided by LpegLabel: | |||
86 | if the matching of <code>p1</code> gives one of l<sub>1</sub>, ..., l<sub>n</sub> | 84 | if the matching of <code>p1</code> gives one of l<sub>1</sub>, ..., l<sub>n</sub> |
87 | </td></tr> | 85 | </td></tr> |
88 | <tr><td><a href="#re-t"><code>%{l<sub>1</sub>, ..., l<sub>n</sub>}</code></a></td> | 86 | <tr><td><a href="#re-t"><code>%{l<sub>1</sub>, ..., l<sub>n</sub>}</code></a></td> |
89 | <td>Syntax of <em>re</em> module. Equivalent to <code>lpeg.T(l<sub>1</sub>, ..., l<sub>n</sub>)</code> | 87 | <td>Syntax of <em>relabel</em> module. Equivalent to <code>lpeg.T(l<sub>1</sub>, ..., l<sub>n</sub>)</code> |
90 | </td></tr> | 88 | </td></tr> |
91 | <tr><td><a href="#re-lc"><code>p1 /{l<sub>1</sub>, ..., l<sub>n</sub>} p2</code></a></td> | 89 | <tr><td><a href="#re-lc"><code>p1 /{l<sub>1</sub>, ..., l<sub>n</sub>} p2</code></a></td> |
92 | <td>Syntax of <em>re</em> module. Equivalent to <code>lpeg.Lc(p1, p2, l<sub>1</sub>, ..., l<sub>n</sub>)</code> | 90 | <td>Syntax of <em>relabel</em> module. Equivalent to <code>lpeg.Lc(p1, p2, l<sub>1</sub>, ..., l<sub>n</sub>)</code> |
93 | </td></tr> | 91 | </td></tr> |
94 | <tr><td><a href="#re-setl"><code>re.setlabels (tlabel)</code></a></td> | 92 | <tr><td><a href="#re-setl"><code>relabel.setlabels (tlabel)</code></a></td> |
95 | <td>Allows to specicify a table with labels. They keys of | 93 | <td>Allows to specicify a table with mnemonic labels. |
96 | <code>tlabel</code> must be integers, and the associated values usually are strings. | ||
97 | </td></tr> | 94 | </td></tr> |
98 | </tbody></table> | 95 | </tbody></table> |
99 | 96 | ||
@@ -136,13 +133,13 @@ labeled ordered choice pattern. | |||
136 | 133 | ||
137 | <h3><a name="re-t"></a><code>%{l<sub>1</sub>, ..., l<sub>n</sub>}</code></h3> | 134 | <h3><a name="re-t"></a><code>%{l<sub>1</sub>, ..., l<sub>n</sub>}</code></h3> |
138 | <p> | 135 | <p> |
139 | Syntax of <em>re</em> module. Equivalent to <code>lpeg.T(l<sub>1</sub>, ..., l<sub>n</sub>)</code>. | 136 | Syntax of <em>relabel</em> module. Equivalent to <code>lpeg.T(l<sub>1</sub>, ..., l<sub>n</sub>)</code>. |
140 | </p> | 137 | </p> |
141 | 138 | ||
142 | 139 | ||
143 | <h3><a name="re-lc"></a><code>p1 /{l<sub>1</sub>, ..., l<sub>n</sub>} p2</code></h3> | 140 | <h3><a name="re-lc"></a><code>p1 /{l<sub>1</sub>, ..., l<sub>n</sub>} p2</code></h3> |
144 | <p> | 141 | <p> |
145 | Syntax of <em>re</em> module. Equivalent to <code>lpeg.Lc(p1, p2, l<sub>1</sub>, ..., l<sub>n</sub>)</code>. | 142 | Syntax of <em>relabel</em> module. Equivalent to <code>lpeg.Lc(p1, p2, l<sub>1</sub>, ..., l<sub>n</sub>)</code>. |
146 | </p> | 143 | </p> |
147 | 144 | ||
148 | <p> | 145 | <p> |
@@ -152,11 +149,19 @@ The <code>/{}</code> operator is left-associative. | |||
152 | <p> | 149 | <p> |
153 | A grammar can use both choice operators (<code>/</code> and <code>/{}</code>), | 150 | A grammar can use both choice operators (<code>/</code> and <code>/{}</code>), |
154 | but a single choice can not mix them. That is, the parser | 151 | but a single choice can not mix them. That is, the parser |
155 | of <code>re</code> module will not recognize a pattern as | 152 | of <code>relabel</code> module will not recognize a pattern as |
156 | <code>p1 / p2 /{l<sub>1</sub>} p3</code>. | 153 | <code>p1 / p2 /{l<sub>1</sub>} p3</code>. |
157 | </p> | 154 | </p> |
158 | 155 | ||
159 | 156 | ||
157 | <h3><a name="re-setl"></a><code>relabel.setlabels (tlabel)</code></h3> | ||
158 | |||
159 | <p>Allows to specicify a table with labels. They keys of | ||
160 | <code>tlabel</code> must be integers between <code>0</code> and <code>31</code>, | ||
161 | and the associated values should be strings. | ||
162 | </p> | ||
163 | |||
164 | |||
160 | 165 | ||
161 | <h2><a name="ex">Some Examples</a></h2> | 166 | <h2><a name="ex">Some Examples</a></h2> |
162 | 167 | ||
@@ -181,11 +186,11 @@ function mymatch (g, s) | |||
181 | local r, e = g:match(s) | 186 | local r, e = g:match(s) |
182 | if not r then | 187 | if not r then |
183 | if e == 1 then | 188 | if e == 1 then |
184 | return r, "Error: expecting an identifier" | 189 | return "Error: expecting an identifier" |
185 | elseif e == 2 then | 190 | elseif e == 2 then |
186 | return r, "Error: expecting ','" | 191 | return "Error: expecting ','" |
187 | else | 192 | else |
188 | return r, "Error" | 193 | return "Error" |
189 | end | 194 | end |
190 | end | 195 | end |
191 | return r | 196 | return r |
@@ -201,9 +206,8 @@ In this example we could think about writing rule <em>List</em> as follows: | |||
201 | List = m.P(("," + m.T(2)) * m.V"Id")^0 | 206 | List = m.P(("," + m.T(2)) * m.V"Id")^0 |
202 | </pre> | 207 | </pre> |
203 | but this would give us an expression that when matching | 208 | but this would give us an expression that when matching |
204 | the end of input would result in a failure with a label | 209 | the end of input would result in a failure whose associated |
205 | different from 0, that is equivalent to the regular failure, | 210 | label would be <em>2</em>. |
206 | and the result of the repetition would be this label. | ||
207 | </p> | 211 | </p> |
208 | 212 | ||
209 | <p> | 213 | <p> |
@@ -235,7 +239,7 @@ local g = m.P{ | |||
235 | function mymatch (g, s) | 239 | function mymatch (g, s) |
236 | local r, e = g:match(s) | 240 | local r, e = g:match(s) |
237 | if not r then | 241 | if not r then |
238 | return r, terror[e] | 242 | return terror[e] |
239 | end | 243 | end |
240 | return r | 244 | return r |
241 | end | 245 | end |
@@ -245,14 +249,14 @@ print(mymatch(g, "a b")) | |||
245 | print(mymatch(g, ", b")) | 249 | print(mymatch(g, ", b")) |
246 | </pre> | 250 | </pre> |
247 | 251 | ||
248 | <h3>Throwing a label using the <em>re</em> module</h3> | 252 | <h3>Throwing a label using the <em>relabel</em> module</h3> |
249 | 253 | ||
250 | <p> | 254 | <p> |
251 | We can also rewrite the previous example using the <em>re</em> module | 255 | We can also rewrite the previous example using the <em>relabel</em> module |
252 | as follows: | 256 | as follows: |
253 | </p> | 257 | </p> |
254 | <pre class="example"> | 258 | <pre class="example"> |
255 | local re = require 're' | 259 | local re = require 'relabel' |
256 | 260 | ||
257 | local g = re.compile[[ | 261 | local g = re.compile[[ |
258 | S <- Id List | 262 | S <- Id List |
@@ -264,11 +268,11 @@ function mymatch (g, s) | |||
264 | local r, e = g:match(s) | 268 | local r, e = g:match(s) |
265 | if not r then | 269 | if not r then |
266 | if e == 1 then | 270 | if e == 1 then |
267 | return r, "Error: expecting an identifier" | 271 | return "Error: expecting an identifier" |
268 | elseif e == 2 then | 272 | elseif e == 2 then |
269 | return r, "Error: expecting ','" | 273 | return "Error: expecting ','" |
270 | else | 274 | else |
271 | return r, "Error" | 275 | return "Error" |
272 | end | 276 | end |
273 | end | 277 | end |
274 | return r | 278 | return r |
@@ -280,12 +284,12 @@ print(mymatch(g, ", b")) | |||
280 | </pre> | 284 | </pre> |
281 | 285 | ||
282 | <p> | 286 | <p> |
283 | Another way to describe the previous example using the <em>re</em> module | 287 | Another way to describe the previous example using the <em>relabel</em> module |
284 | is by using a table with the description the errors (<em>terror</em>) and | 288 | is by using a table with the description of the errors (<em>terror</em>) and |
285 | another table that associates a name to a given label (<em>tlabel</em>): | 289 | another table that associates a name to a given label (<em>tlabels</em>): |
286 | </p> | 290 | </p> |
287 | <pre class="example"> | 291 | <pre class="example"> |
288 | local re = require 're' | 292 | local re = require 'relabel' |
289 | 293 | ||
290 | local errUndef, errId, errComma = 0, 1, 2 | 294 | local errUndef, errId, errComma = 0, 1, 2 |
291 | 295 | ||
@@ -310,7 +314,7 @@ local g = re.compile[[ | |||
310 | function mymatch (g, s) | 314 | function mymatch (g, s) |
311 | local r, e = g:match(s) | 315 | local r, e = g:match(s) |
312 | if not r then | 316 | if not r then |
313 | return r, terror[e] | 317 | return terror[e] |
314 | end | 318 | end |
315 | return r | 319 | return r |
316 | end | 320 | end |
@@ -369,7 +373,7 @@ a left-associative order as we did in the previous example that | |||
369 | used <code>Lc</code>: | 373 | used <code>Lc</code>: |
370 | </p> | 374 | </p> |
371 | <pre class="example"> | 375 | <pre class="example"> |
372 | local re = require're' | 376 | local re = require'relabel' |
373 | 377 | ||
374 | local terror = {} | 378 | local terror = {} |
375 | 379 | ||
@@ -409,11 +413,11 @@ print(p:match(",b")) | |||
409 | As a more complex example, below we have the grammar | 413 | As a more complex example, below we have the grammar |
410 | for the Tiny language, as described in | 414 | for the Tiny language, as described in |
411 | <a href="http://arxiv.org/abs/1405.6646">this</a> paper. | 415 | <a href="http://arxiv.org/abs/1405.6646">this</a> paper. |
412 | The example below can also how the line where the syntactic | 416 | The example below can also show the line where the syntactic |
413 | error probably happened. | 417 | error probably happened. |
414 | </p> | 418 | </p> |
415 | <pre class="example"> | 419 | <pre class="example"> |
416 | local re = require 're' | 420 | local re = require 'relabel' |
417 | 421 | ||
418 | local terror = {} | 422 | local terror = {} |
419 | 423 | ||
@@ -523,8 +527,8 @@ local g = re.compile([[ | |||
523 | 527 | ||
524 | <h2><a name="download"></a>Download</h2> | 528 | <h2><a name="download"></a>Download</h2> |
525 | 529 | ||
526 | <p>LPeg | 530 | <p>LPegLabel |
527 | <a href="http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-0.12.tar.gz">source code</a>.</p> | 531 | <a href="https://github.com/sqmedeiros/lpeglabel/archive/master.zip">source code</a>.</p> |
528 | 532 | ||
529 | 533 | ||
530 | <h2><a name="license">License</a></h2> | 534 | <h2><a name="license">License</a></h2> |