diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-06-17 16:25:25 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-06-17 16:25:25 -0300 |
| commit | 80ec9f932aa01d445e86c699523265359055e1bd (patch) | |
| tree | c32ee93fba8e2af7c114ee177b7fe2c8e353650a /lpeg.html | |
| parent | 73a96935337d78d0e1015d7ccce6cf7470c192c4 (diff) | |
| download | lpeg-master.tar.gz lpeg-master.tar.bz2 lpeg-master.zip | |
Diffstat (limited to '')
| -rw-r--r-- | lpeg.html | 30 |
1 files changed, 21 insertions, 9 deletions
| @@ -53,19 +53,22 @@ | |||
| 53 | <h2><a name="intro">Introduction</a></h2> | 53 | <h2><a name="intro">Introduction</a></h2> |
| 54 | 54 | ||
| 55 | <p> | 55 | <p> |
| 56 | <em>LPeg</em> is a new pattern-matching library for Lua, | 56 | <em>LPeg</em> is a pattern-matching library for Lua, |
| 57 | based on | 57 | based on |
| 58 | <a href="//bford.info/packrat/"> | 58 | <a href="//bford.info/packrat/"> |
| 59 | Parsing Expression Grammars</a> (PEGs). | 59 | Parsing Expression Grammars</a> (PEGs). |
| 60 | This text is a reference manual for the library. | 60 | This text is a reference manual for the library. |
| 61 | For those starting with LPeg, | ||
| 62 | <a href="https://www.inf.puc-rio.br/~roberto/docs/lpeg-primer.pdf"> | ||
| 63 | Mastering LPeg</a> presents a good tutorial. | ||
| 61 | For a more formal treatment of LPeg, | 64 | For a more formal treatment of LPeg, |
| 62 | as well as some discussion about its implementation, | 65 | as well as some discussion about its implementation, |
| 63 | see | 66 | see |
| 64 | <a href="//www.inf.puc-rio.br/~roberto/docs/peg.pdf"> | 67 | <a href="//www.inf.puc-rio.br/~roberto/docs/peg.pdf"> |
| 65 | A Text Pattern-Matching Tool based on Parsing Expression Grammars</a>. | 68 | A Text Pattern-Matching Tool based on Parsing Expression Grammars</a>. |
| 66 | (You may also be interested in my | 69 | You may also be interested in my |
| 67 | <a href="//vimeo.com/1485123">talk about LPeg</a> | 70 | <a href="//vimeo.com/1485123">talk about LPeg</a> |
| 68 | given at the III Lua Workshop.) | 71 | given at the III Lua Workshop. |
| 69 | </p> | 72 | </p> |
| 70 | 73 | ||
| 71 | <p> | 74 | <p> |
| @@ -664,13 +667,22 @@ produces values for each match. | |||
| 664 | 667 | ||
| 665 | <p> | 668 | <p> |
| 666 | Usually, | 669 | Usually, |
| 667 | LPeg does not specify when (and if) it evaluates its captures. | 670 | LPeg does not specify when, if, or how many times it evaluates its captures. |
| 668 | (As an example, | ||
| 669 | consider the pattern <code>lpeg.P"a" / func / 0</code>. | ||
| 670 | Because the "division" by 0 instructs LPeg to throw away the | ||
| 671 | results from the pattern, | ||
| 672 | it is not specified whether LPeg will call <code>func</code>.) | ||
| 673 | Therefore, captures should avoid side effects. | 671 | Therefore, captures should avoid side effects. |
| 672 | As an example, | ||
| 673 | LPeg may or may not call <code>func</code> in the pattern | ||
| 674 | <code>lpeg.P"a" / func / 0</code>, | ||
| 675 | given that the <a href="#cap-num">"division" by 0</a> | ||
| 676 | instructs LPeg to throw away the | ||
| 677 | results from the pattern. | ||
| 678 | Similarly, a capture nested inside a <a href="#cap-g">named group</a> | ||
| 679 | may be evaluated only when that group is referred in a | ||
| 680 | <a href="#cap-b">back capture</a>; | ||
| 681 | if there are multiple back captures, | ||
| 682 | the group may be evaluated multiple times. | ||
| 683 | </p> | ||
| 684 | |||
| 685 | <p> | ||
| 674 | Moreover, | 686 | Moreover, |
| 675 | captures cannot affect the way a pattern matches a subject. | 687 | captures cannot affect the way a pattern matches a subject. |
| 676 | The only exception to this rule is the | 688 | The only exception to this rule is the |
