|
10 | 10 | </head> |
11 | 11 | <body> |
12 | 12 |
|
13 | | -<!-- $Id: re.html,v 1.14 2010/11/03 17:07:19 roberto Exp $ --> |
| 13 | +<!-- $Id: re.html,v 1.17 2011/01/10 15:08:06 roberto Exp $ --> |
14 | 14 |
|
15 | 15 | <div id="container"> |
16 | 16 |
|
@@ -154,6 +154,13 @@ <h3><code>re.find (subject, pattern [, init])</code></h3> |
154 | 154 | Otherwise, returns nil. |
155 | 155 | </p> |
156 | 156 |
|
| 157 | +<p> |
| 158 | +An optional numeric argument <code>init</code> makes the search |
| 159 | +starts at that position in the subject string. |
| 160 | +As usual in Lua libraries, |
| 161 | +a negative value counts from the end. |
| 162 | +</p> |
| 163 | + |
157 | 164 | <h3><code>re.match (subject, pattern)</code></h3> |
158 | 165 | <p> |
159 | 166 | Matches the given pattern against the given subject. |
@@ -225,13 +232,81 @@ <h3>Lua's long strings</h3> |
225 | 232 | </p> |
226 | 233 | <pre class="example"> |
227 | 234 | c = re.compile([[ |
228 | | - longstring <- ('[' {:eq: '='* :} '[' close) => void |
| 235 | + longstring <- ('[' {:eq: '='* :} '[' close) -> void |
229 | 236 | close <- ']' =eq ']' / . close |
230 | | -]], {void = function () return true end}) |
| 237 | +]], {void = function () end}) |
231 | 238 |
|
232 | 239 | print(c:match'[==[]]===]]]]==]===[]') --> 17 |
233 | 240 | </pre> |
234 | 241 |
|
| 242 | +<h3>Abstract Syntax Trees</h3> |
| 243 | +<p> |
| 244 | +This example shows a simple way to build an |
| 245 | +abstract syntax tree (AST) for a given grammar. |
| 246 | +To keep our example simple, |
| 247 | +let us consider the following grammar |
| 248 | +for lists of names: |
| 249 | +</p> |
| 250 | +<pre class="example"> |
| 251 | +p = re.compile[[ |
| 252 | + listname <- (name s)* |
| 253 | + name <- [a-z][a-z]* |
| 254 | + s <- %s* |
| 255 | +]] |
| 256 | +</pre> |
| 257 | +<p> |
| 258 | +Now, we will add captures to build a corresponding AST. |
| 259 | +As a first step, the pattern will build a table to |
| 260 | +represent each non terminal; |
| 261 | +terminals will be represented by their corresponding strings: |
| 262 | +</p> |
| 263 | +<pre class="example"> |
| 264 | +c = re.compile[[ |
| 265 | + listname <- (name s)* -> {} |
| 266 | + name <- {[a-z][a-z]*} -> {} |
| 267 | + s <- %s* |
| 268 | +]] |
| 269 | +</pre> |
| 270 | +<p> |
| 271 | +Now, a match against <code>"hi hello bye"</code> |
| 272 | +results in the table |
| 273 | +<code>{{"hi"}, {"hello"}, {"bye"}}</code>. |
| 274 | +</p> |
| 275 | +<p> |
| 276 | +For such a simple grammar, |
| 277 | +this AST is more than enough; |
| 278 | +actually, the tables around each single name |
| 279 | +are already overkilling. |
| 280 | +More complex grammars, |
| 281 | +however, may need some more structure. |
| 282 | +Specifically, |
| 283 | +it would be useful if each table had |
| 284 | +a <code>tag</code> field telling what non terminal |
| 285 | +that table represents. |
| 286 | +We can add such a tag using |
| 287 | +<a href="lpeg.html/#cap-g">named group captures</a>: |
| 288 | +</p> |
| 289 | +<pre class="example"> |
| 290 | +x = re.compile[[ |
| 291 | + listname <- ({:tag: '' -> 'list':} (name s)*) -> {} |
| 292 | + name <- ({:tag: '' -> 'id':} {[a-z][a-z]*}) -> {} |
| 293 | + s <- ' '* |
| 294 | +]] |
| 295 | +</pre> |
| 296 | +<p> |
| 297 | +With these group captures, |
| 298 | +a match against <code>"hi hello bye"</code> |
| 299 | +results in the following table: |
| 300 | +</p> |
| 301 | +<pre class="example"> |
| 302 | +{tag="list", |
| 303 | + {tag="id", "hi"}, |
| 304 | + {tag="id", "hello"}, |
| 305 | + {tag="id", "bye"} |
| 306 | +} |
| 307 | +</pre> |
| 308 | + |
| 309 | + |
235 | 310 | <h3>Indented blocks</h3> |
236 | 311 | <p> |
237 | 312 | This example breaks indented blocks into tables, |
@@ -401,7 +476,7 @@ <h2><a name="license">License</a></h2> |
401 | 476 |
|
402 | 477 | <div id="about"> |
403 | 478 | <p><small> |
404 | | -$Id: re.html,v 1.14 2010/11/03 17:07:19 roberto Exp $ |
| 479 | +$Id: re.html,v 1.17 2011/01/10 15:08:06 roberto Exp $ |
405 | 480 | </small></p> |
406 | 481 | </div> <!-- id="about" --> |
407 | 482 |
|
|
0 commit comments