| 1 |
|
| 2 |
|
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<title>grammar</title> |
| 6 |
<link rel="stylesheet" href="doc.css" type="text/css"/> |
| 7 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> |
| 8 |
</head> |
| 9 |
<body> |
| 10 |
|
| 11 |
<div id="container"> |
| 12 |
|
| 13 |
<div id="product"> |
| 14 |
<p/> |
| 15 |
<div id="product_logo"> |
| 16 |
<a href="http://www.lua.org"> <img alt="Lua logo" src="lua.gif"/> </a> |
| 17 |
</div> |
| 18 |
<div id="product_name"><big><strong>grammar</strong></big></div> |
| 19 |
<div id="product_description"> LPeg grammar manipulation </div> |
| 20 |
<br/> |
| 21 |
|
| 22 |
<div id="product_description"> <small><em> <b>Version:</b> 0.1.2 </em></small></div> |
| 23 |
<div id="product_description"> <small><em><b>Generated:</b> November 26, 2007 </em></small> </div> |
| 24 |
|
| 25 |
</div> <!-- id="product" --> |
| 26 |
|
| 27 |
<div id="main"> |
| 28 |
|
| 29 |
<div id="navigation"> |
| 30 |
<h1><a href="index.html">Modules</a></h1> |
| 31 |
<ul> |
| 32 |
<li><a href="grammar.html">grammar</a></li> |
| 33 |
<li><a href="parser.html">parser</a></li> |
| 34 |
<li><a href="scanner.html">scanner</a></li> |
| 35 |
</ul> |
| 36 |
<br/> |
| 37 |
<h1>grammar</h1> |
| 38 |
<ul> |
| 39 |
|
| 40 |
<!-- |
| 41 |
<li><a href="#section_Description"> Description </a> |
| 42 |
|
| 43 |
</li> |
| 44 |
--> |
| 45 |
<li><a href="#section_Description"> Description </a> |
| 46 |
</li> |
| 47 |
|
| 48 |
<!-- |
| 49 |
<li><a href="#section_Dependencies"> Dependencies </a> |
| 50 |
|
| 51 |
</li> |
| 52 |
--> |
| 53 |
<li><a href="#section_Dependencies"> Dependencies </a> |
| 54 |
</li> |
| 55 |
|
| 56 |
<!-- |
| 57 |
<li><a href="#section_Operations"> Operations </a> |
| 58 |
|
| 59 |
<ul> |
| 60 |
|
| 61 |
<li><a href="#section_Operations_Piping"> Piping </a></li> |
| 62 |
|
| 63 |
<li><a href="#section_Operations_Completing"> Completing </a></li> |
| 64 |
|
| 65 |
<li><a href="#section_Operations_Applying"> Applying </a></li> |
| 66 |
|
| 67 |
<li><a href="#section_Operations_Example"> Example </a></li> |
| 68 |
|
| 69 |
</ul> |
| 70 |
|
| 71 |
</li> |
| 72 |
--> |
| 73 |
<li><a href="#section_Operations"> Operations </a> |
| 74 |
<ul> |
| 75 |
<li><a href="#section_Piping"> Piping </a></li> |
| 76 |
<li><a href="#section_Completing"> Completing </a></li> |
| 77 |
<li><a href="#section_Applying"> Applying </a></li> |
| 78 |
<li><a href="#section_Example"> Example </a></li> |
| 79 |
</ul> |
| 80 |
</li> |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
<li><a href="#functions"> Functions </a> |
| 85 |
<ul> |
| 86 |
|
| 87 |
<li><code><a href="#function_anyOf"> anyOf </a></code></li> |
| 88 |
|
| 89 |
<li><code><a href="#function_apply"> apply </a></code></li> |
| 90 |
|
| 91 |
<li><code><a href="#function_C"> C </a></code></li> |
| 92 |
|
| 93 |
<li><code><a href="#function_complete"> complete </a></code></li> |
| 94 |
|
| 95 |
<li><code><a href="#function_copy"> copy </a></code></li> |
| 96 |
|
| 97 |
<li><code><a href="#function_Ct"> Ct </a></code></li> |
| 98 |
|
| 99 |
<li><code><a href="#function_listOf"> listOf </a></code></li> |
| 100 |
|
| 101 |
<li><code><a href="#function_pipe"> pipe </a></code></li> |
| 102 |
|
| 103 |
</ul> |
| 104 |
</li> |
| 105 |
|
| 106 |
|
| 107 |
</ul> |
| 108 |
</div> <!-- id="navigation" --> |
| 109 |
|
| 110 |
<div id="content"> |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
<p/> |
| 115 |
|
| 116 |
|
| 117 |
<p/> |
| 118 |
<h1><a name="section_Description"></a>Description</h1> |
| 119 |
<p/> |
| 120 |
This module defines a handful of operations which can be applied to |
| 121 |
<a href="http://www.inf.puc-rio.br/~roberto/lpeg.html">LPeg</a> patterns and grammars in |
| 122 |
general. |
| 123 |
<p/> |
| 124 |
<h1><a name="section_Dependencies"></a>Dependencies</h1> |
| 125 |
<ul> |
| 126 |
<li><a href="http://www.inf.puc-rio.br/~roberto/lpeg.html">LPeg</a>.</li></ul> |
| 127 |
<p/> |
| 128 |
<h1><a name="section_Operations"></a>Operations</h1> |
| 129 |
<p/> |
| 130 |
<h2><a name="section_Piping"></a>Piping</h2> |
| 131 |
<p/> |
| 132 |
Pattern matching dissociates the notion of <em>matching</em> from the notion of |
| 133 |
<em>capturing</em>: matching checks if a given string follows a certain pattern, |
| 134 |
and capturing generates values according to the match made. This division |
| 135 |
allows interesting possibilities: |
| 136 |
<ul> |
| 137 |
<li>different problems can be solved by applying different captures to the same grammar;</li> |
| 138 |
<li>captures may be defined separately;</li> |
| 139 |
<li>captures may be done on top of other captures.</li></ul> |
| 140 |
<p/> |
| 141 |
Accounting for the first and second bullets, the grammar given in |
| 142 |
<a href="parser.html">parser</a> has no captures, enabling the user to reuse it to solve any |
| 143 |
problems that require a Lua grammar. One good example is documentation |
| 144 |
generation, described in a little more detail <a href="#section_Example">below</a>. |
| 145 |
<p/> |
| 146 |
The third bullet depicts a more interesting idea: a capture might take the |
| 147 |
result of another capture as input, doing a further transformation of the |
| 148 |
original data. This capture chaining, with the latter ones using the former's |
| 149 |
output as its input, is very similar to <a href="http://en.wikipedia.org/wiki/Pipeline_%28Unix%29">Unix pipelines</a>, |
| 150 |
so this mechanism was named <strong>piping</strong>. |
| 151 |
<p/> |
| 152 |
<h2><a name="section_Completing"></a>Completing</h2> |
| 153 |
<p/> |
| 154 |
With piping, several levels of captures can be chained together up to the |
| 155 |
most appropriate for the task at hand. Yet some levels might require extra rules, and modifications to existing ones, to ensure proper matching. |
| 156 |
<p/> |
| 157 |
To avoid manual copying, the new grammar should redefine only the necessary |
| 158 |
rules, copying the rest from the older grammar. This action is dubbed |
| 159 |
<strong>completing</strong>. |
| 160 |
<p/> |
| 161 |
<h2><a name="section_Applying"></a>Applying</h2> |
| 162 |
<p/> |
| 163 |
Once a new rule set is created and <a href="#section_Completing">completed</a>, and |
| 164 |
all captures are correctly <a href="#section_Piping">piped</a>, all that's left is |
| 165 |
to put them together, a process called <strong>applying</strong>. The result is a grammar ready for <a href="http://www.inf.puc-rio.br/~roberto/lpeg.html#lpeg">lpeg.P</a> |
| 166 |
consumption, whose pattern will return the intended result when a match is made. |
| 167 |
<p/> |
| 168 |
<h2><a name="section_Example"></a>Example</h2> |
| 169 |
<p/> |
| 170 |
Let's consider the problem of documenting a Lua module. In this case, comments |
| 171 |
must be captured before every function declaration when in the outermost scope: |
| 172 |
<p/> |
| 173 |
<pre class="example"> |
| 174 |
<font color="#808080"> -- the code to parse</font> |
| 175 |
subject = <font color="#008000">[[ |
| 176 |
-- Calculates the sum a+b. |
| 177 |
-- An extra line. |
| 178 |
function sum (a, b) |
| 179 |
-- code |
| 180 |
end |
| 181 |
|
| 182 |
-- f1: assume a variable assignment is not a proper declaration for an |
| 183 |
-- exported function |
| 184 |
f1 = function () |
| 185 |
-- code |
| 186 |
end |
| 187 |
|
| 188 |
while true do |
| 189 |
-- this function is not in the outermost scope |
| 190 |
function aux() end |
| 191 |
end |
| 192 |
|
| 193 |
function something:other(a, ...) |
| 194 |
-- a global function without comments |
| 195 |
end |
| 196 |
]]</font> |
| 197 |
</pre> |
| 198 |
<p/> |
| 199 |
In the code above only <code>sum</code> and <code>something:other</code> should be documented, as <code>f1</code> isn't properly (by our standards) declared and <code>aux</code> is not in the outermost scope. |
| 200 |
<p/> |
| 201 |
By combining <a href="http://www.inf.puc-rio.br/~roberto/lpeg.html">LPeg</a> and the modules <a href="scanner.html">scanner</a>, <a href="parser.html">parser</a> and <a href="grammar.html">grammar</a>, this specific problem can be solved as follows: |
| 202 |
<p/> |
| 203 |
<pre class="example"> |
| 204 |
<font color="#808080"> -- change only the initial rule and make no captures</font> |
| 205 |
patt = <b>grammar.apply</b>(parser.rules, scanner.COMMENT^-<font color="#B00000"><i>1</i></font> * <b>lpeg.V</b><font color="#008000">'GlobalFunction'</font>, <font color="#0000FF"><b>nil</b></font>) |
| 206 |
|
| 207 |
<font color="#808080"> -- transform the new grammar into a LPeg pattern</font> |
| 208 |
patt = <b>lpeg.P</b>(patt) |
| 209 |
|
| 210 |
<font color="#808080"> -- making a pattern that matches any Lua statement, also without captures</font> |
| 211 |
Stat = <b>lpeg.P</b>( <b>grammar.apply</b>(parser.rules, <b>lpeg.V</b><font color="#008000">'Stat'</font>, <font color="#0000FF"><b>nil</b></font>) ) |
| 212 |
|
| 213 |
<font color="#808080"> -- a pattern which matches function declarations and skips statements in</font> |
| 214 |
<font color="#808080"> -- inner scopes or undesired tokens</font> |
| 215 |
patt = (patt + Stat + scanner.ANY)^<font color="#B00000"><i>0</i></font> |
| 216 |
|
| 217 |
<font color="#808080"> -- matching a string</font> |
| 218 |
<b>patt:match</b>(subject) |
| 219 |
</pre> |
| 220 |
<p/> |
| 221 |
These are the relevant rules in <a href="parser.html#section_The_Grammar">the grammar</a>: |
| 222 |
<p/> |
| 223 |
<pre class="example"> |
| 224 |
GlobalFunction = <font color="#008000">'function'</font> * FuncName * FuncBody |
| 225 |
FuncName = ID * (<font color="#008000">'.'</font> * ID)^<font color="#B00000"><i>0</i></font> * (<font color="#008000">':'</font> * ID)^-<font color="#B00000"><i>1</i></font> |
| 226 |
FuncBody = <font color="#008000">'('</font> * (ParList + EPSILON) * <font color="#008000">')'</font> * Block * <font color="#008000">'end'</font> |
| 227 |
ParList = NameList * (<font color="#008000">','</font> * <font color="#008000">'...'</font>)^-<font color="#B00000"><i>1</i></font> |
| 228 |
NameList = ID * (<font color="#008000">','</font> * ID)^<font color="#B00000"><i>0</i></font> |
| 229 |
ID = scanner.ID |
| 230 |
EPSILON = <b>lpeg.P</b>(<font color="#0000FF"><b>true</b></font>) |
| 231 |
</pre> |
| 232 |
<p/> |
| 233 |
It may seem that <code>ParList + EPSILON</code> could be substituted for <code>ParList^-1</code> (optionally match <code>ParList</code>), but then no captures would be made for empty parameter lists, and <code>GlobalFunction</code> would get all strings matched by <code>FuncBody</code>. The <code>EPSILON</code> rule acts in this manner as a placeholder in the argument list, avoiding any argument list processing in the capture function. |
| 234 |
<p/> |
| 235 |
Since no captures are being made, <a href="http://www.inf.puc-rio.br/~roberto/lpeg.html#basic">lpeg.match</a> doesn't return anything interesting. Here are some possible captures: |
| 236 |
<p/> |
| 237 |
<pre class="example"> |
| 238 |
<font color="#808080"> -- some interesting captures bundled up in a table. Note that the table keys</font> |
| 239 |
<font color="#808080"> -- match the grammar rules we want to add captures to. Whatever rules aren't in</font> |
| 240 |
<font color="#808080"> -- the rules table below will come from parser.rules .</font> |
| 241 |
captures = { |
| 242 |
[<font color="#B00000"><i>1</i></font>] = <font color="#0000FF"><b>function</b></font> (...) <font color="#808080"> -- the initial rule</font> |
| 243 |
<font color="#0000FF"><b>return</b></font> <font color="#008000">'<function>'</font>..<font color = "#800040"><b>table.concat</b></font>{...}..<font color="#008000">'</function>'</font> |
| 244 |
<font color="#0000FF"><b>end</b></font>, |
| 245 |
|
| 246 |
GlobalFunction = <font color="#0000FF"><b>function</b></font> (name, parlist) |
| 247 |
<font color="#0000FF"><b>return</b></font> <font color="#008000">'<name>'</font>..name..<font color="#008000">'</name><parlist>'</font>..(parlist <font color="#0000FF"><b>or</b></font> <font color="#008000">''</font>)..<font color="#008000">'</parlist>'</font> |
| 248 |
<font color="#0000FF"><b>end</b></font>, |
| 249 |
|
| 250 |
FuncName = grammar.C, <font color="#808080"> -- capture the raw text</font> |
| 251 |
ParList = grammar.C, <font color="#808080"> -- capture the raw text</font> |
| 252 |
COMMENT = scanner.comment2text, <font color="#808080"> -- remove the comment trappings</font> |
| 253 |
} |
| 254 |
|
| 255 |
<font color="#808080"> -- spacing rule</font> |
| 256 |
<font color="#0000FF"><b>local</b></font> S = scanner.SPACE ^ <font color="#B00000"><i>0</i></font> |
| 257 |
|
| 258 |
<font color="#808080"> -- rules table</font> |
| 259 |
rules = { |
| 260 |
[<font color="#B00000"><i>1</i></font>] = ((<b>lpeg.V</b><font color="#008000">'COMMENT'</font> *S) ^ <font color="#B00000"><i>0</i></font>) *S* <b>lpeg.V</b><font color="#008000">'GlobalFunction'</font>, |
| 261 |
COMMENT = scanner.COMMENT, |
| 262 |
} |
| 263 |
|
| 264 |
<font color="#808080"> -- building the new grammar and adding the captures</font> |
| 265 |
patt = <b>lpeg.P</b>( <b>grammar.apply</b>(parser.rules, rules, captures) ) |
| 266 |
|
| 267 |
<font color="#808080"> -- a pattern that matches a sequence of patts and concatenates the results</font> |
| 268 |
patt = (patt + Stat + scanner.ANY)^<font color="#B00000"><i>0</i></font> / <font color="#0000FF"><b>function</b></font>(...) |
| 269 |
<font color="#0000FF"><b>return</b></font> <font color = "#800040"><b>table.concat</b></font>({...}, <font color="#008000">'\n\n'</font>) <font color="#808080"> -- some line breaks for easier reading</font> |
| 270 |
<font color="#0000FF"><b>end</b></font> |
| 271 |
|
| 272 |
<font color="#808080"> -- finally, matching a string</font> |
| 273 |
<b><font color ="#800040">print</font></b>(<b>patt:match</b>(subject)) |
| 274 |
</pre> |
| 275 |
<p/> |
| 276 |
<code>FuncBody</code> needs no captures, as <code>Block</code> and all its non-terminals have none; it |
| 277 |
just needs to pass along any captures made by <code>ParList</code>. <code>NameList</code> and <code>ID</code> also have no captures, and the whole subject string is passed further. |
| 278 |
<p/> |
| 279 |
The printed result is: |
| 280 |
<pre class="example"> |
| 281 |
<function>Calculates the sum a+b. An extra line.<name>sum</name><parlist>a, b</parlist></function> |
| 282 |
<br/> |
| 283 |
<function><name>something:other</name><parlist>a, ...</parlist></function> |
| 284 |
</pre> |
| 285 |
<p/> |
| 286 |
<p/> |
| 287 |
<p/> |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
<hr/> |
| 294 |
<h1><a name="functions"></a> Functions </h1> |
| 295 |
<table border="0" width="95%"> |
| 296 |
|
| 297 |
<tr> |
| 298 |
<!-- <td> <pre class="example"><big><strong><a href="#functions_anyOf">anyOf</a></strong></big> (list)</pre> </td> --> |
| 299 |
<td> <code><big><strong> <a href="#function_anyOf">anyOf</a></strong></big> (list) </code> </td> |
| 300 |
<td> Returns a pattern which matches any of the patterns received. </td> |
| 301 |
</tr> |
| 302 |
|
| 303 |
<tr> |
| 304 |
<!-- <td> <pre class="example"><big><strong><a href="#functions_apply">apply</a></strong></big> (grammar, rules, captures)</pre> </td> --> |
| 305 |
<td> <code><big><strong> <a href="#function_apply">apply</a></strong></big> (grammar, rules, captures) </code> </td> |
| 306 |
<td> <a href="#section_Completing">Completes</a> <code>rules</code> with <code>grammar</code> and then <a href="#Applying">applies</a> <code>captures</code>. </td> |
| 307 |
</tr> |
| 308 |
|
| 309 |
<tr> |
| 310 |
<!-- <td> <pre class="example"><big><strong><a href="#functions_C">C</a></strong></big> ()</pre> </td> --> |
| 311 |
<td> <code><big><strong> <a href="#function_C">C</a></strong></big> () </code> </td> |
| 312 |
<td> A capture function, made so that <code>patt / C</code> is equivalent to <code>m.C(patt)</code>. It's intended to be used in capture tables, such as those required by <a href="#function_pipe">pipe</a> and <a href="#function_apply">apply</a>. </td> |
| 313 |
</tr> |
| 314 |
|
| 315 |
<tr> |
| 316 |
<!-- <td> <pre class="example"><big><strong><a href="#functions_complete">complete</a></strong></big> (dest, orig)</pre> </td> --> |
| 317 |
<td> <code><big><strong> <a href="#function_complete">complete</a></strong></big> (dest, orig) </code> </td> |
| 318 |
<td> <a href="#section_Completing">Completes</a> <code>dest</code> with <code>orig</code>. </td> |
| 319 |
</tr> |
| 320 |
|
| 321 |
<tr> |
| 322 |
<!-- <td> <pre class="example"><big><strong><a href="#functions_copy">copy</a></strong></big> (grammar)</pre> </td> --> |
| 323 |
<td> <code><big><strong> <a href="#function_copy">copy</a></strong></big> (grammar) </code> </td> |
| 324 |
<td> Creates a shallow copy of <code>grammar</code>. </td> |
| 325 |
</tr> |
| 326 |
|
| 327 |
<tr> |
| 328 |
<!-- <td> <pre class="example"><big><strong><a href="#functions_Ct">Ct</a></strong></big> ()</pre> </td> --> |
| 329 |
<td> <code><big><strong> <a href="#function_Ct">Ct</a></strong></big> () </code> </td> |
| 330 |
<td> A capture function, made so that <code>patt / Ct</code> is equivalent to <code>m.Ct(patt)</code>. It's intended to be used in capture tables, such as those required by <a href="#function_pipe">pipe</a> and <a href="#function_apply">apply</a>. </td> |
| 331 |
</tr> |
| 332 |
|
| 333 |
<tr> |
| 334 |
<!-- <td> <pre class="example"><big><strong><a href="#functions_listOf">listOf</a></strong></big> (patt, sep)</pre> </td> --> |
| 335 |
<td> <code><big><strong> <a href="#function_listOf">listOf</a></strong></big> (patt, sep) </code> </td> |
| 336 |
<td> Returns a pattern which matches a list of <code>patt</code>s, separated by <code>sep</code>. </td> |
| 337 |
</tr> |
| 338 |
|
| 339 |
<tr> |
| 340 |
<!-- <td> <pre class="example"><big><strong><a href="#functions_pipe">pipe</a></strong></big> (dest, orig)</pre> </td> --> |
| 341 |
<td> <code><big><strong> <a href="#function_pipe">pipe</a></strong></big> (dest, orig) </code> </td> |
| 342 |
<td> <a href="#section_Piping">Pipes</a> the captures in <code>orig</code> to the ones in <code>dest</code>. </td> |
| 343 |
</tr> |
| 344 |
|
| 345 |
</table> |
| 346 |
|
| 347 |
|
| 348 |
<p/><a name="function_anyOf"></a> |
| 349 |
<hr/><code><big>anyOf (list)</big></code> |
| 350 |
<ul>Returns a pattern which matches any of the patterns received. |
| 351 |
<p/> |
| 352 |
<strong>Example:</strong> |
| 353 |
<pre class="example"> |
| 354 |
<font color="#0000FF"><b>local</b></font> g, s, m = <b><font color ="#800040">require</font></b> <font color="#008000">'leg.grammar'</font>, <b><font color ="#800040">require</font></b> <font color="#008000">'leg.scanner'</font>, <b><font color ="#800040">require</font></b> <font color="#008000">'lpeg'</font> |
| 355 |
|
| 356 |
<font color="#808080"> -- match numbers or operators, capture the numbers</font> |
| 357 |
<b><font color ="#800040">print</font></b>( (<b>g.anyOf</b> { <font color="#008000">'+'</font>, <font color="#008000">'-'</font>, <font color="#008000">'*'</font>, <font color="#008000">'/'</font>, <b>m.C</b>(s.NUMBER) }):match <font color="#008000">'34.5@23 * 56 / 45 - 45'</font> ) |
| 358 |
<font color="#808080"> --> prints 34.5</font> |
| 359 |
</pre> |
| 360 |
<p/> |
| 361 |
<strong>Parameters:</strong><ul> |
| 362 |
<li><code>list</code>: a list of zero or more LPeg patterns or values which can be fed to <a href="http://www.inf.puc-rio.br/~roberto/lpeg.html#lpeg">lpeg.P</a>.</li></ul> |
| 363 |
<p/> |
| 364 |
<strong>Returns:</strong><ul> |
| 365 |
<li>a pattern which matches any of the patterns received.</li></ul></ul> |
| 366 |
|
| 367 |
<p/><a name="function_apply"></a> |
| 368 |
<hr/><code><big>apply (grammar, rules, captures)</big></code> |
| 369 |
<ul><a href="#section_Completing">Completes</a> <code>rules</code> with <code>grammar</code> and then <a href="#Applying">applies</a> <code>captures</code>. |
| 370 |
<p/> |
| 371 |
<code>rules</code> can either be:<ul> |
| 372 |
<li>a single pattern, which is taken to be the new initial rule, </li> |
| 373 |
<li>a possibly incomplete LPeg grammar, as per <a href="#function_complete">complete</a>, or </li> |
| 374 |
<li><code>nil</code>, which means no new rules are added.</li></ul> |
| 375 |
<p/> |
| 376 |
<code>captures</code> can either be:<ul> |
| 377 |
<li>a capture table, as per <a href="#function_pipe">pipe</a>, or</li> |
| 378 |
<li><code>nil</code>, which means no captures are applied.</li></ul> |
| 379 |
<p/> |
| 380 |
<strong>Parameters:</strong><ul> |
| 381 |
<li><code>grammar</code>: the old grammar. It stays unmodified.</li> |
| 382 |
<li><code>rules</code>: optional, the new rules. </li> |
| 383 |
<li><code>captures</code>: optional, the final capture table.</li></ul> |
| 384 |
<p/> |
| 385 |
<strong>Returns:</strong><ul> |
| 386 |
<li><code>rules</code>, suitably augmented by <code>grammar</code> and <code>captures</code>.</li></ul></ul> |
| 387 |
|
| 388 |
<p/><a name="function_C"></a> |
| 389 |
<hr/><code><big>C ()</big></code> |
| 390 |
<ul>A capture function, made so that <code>patt / C</code> is equivalent to <code>m.C(patt)</code>. It's intended to be used in capture tables, such as those required by <a href="#function_pipe">pipe</a> and <a href="#function_apply">apply</a>.</ul> |
| 391 |
|
| 392 |
<p/><a name="function_complete"></a> |
| 393 |
<hr/><code><big>complete (dest, orig)</big></code> |
| 394 |
<ul><a href="#section_Completing">Completes</a> <code>dest</code> with <code>orig</code>. |
| 395 |
<p/> |
| 396 |
<strong>Parameters:</strong><ul> |
| 397 |
<li><code>dest</code>: the new grammar. Must be a table.</li> |
| 398 |
<li><code>orig</code>: the original grammar. Must be a table.</li></ul> |
| 399 |
<p/> |
| 400 |
<strong>Returns:</strong><ul> |
| 401 |
<li><code>dest</code>, with new rules inherited from <code>orig</code>.</li></ul></ul> |
| 402 |
|
| 403 |
<p/><a name="function_copy"></a> |
| 404 |
<hr/><code><big>copy (grammar)</big></code> |
| 405 |
<ul>Creates a shallow copy of <code>grammar</code>. |
| 406 |
<p/> |
| 407 |
<strong>Parameters:</strong><ul> |
| 408 |
<li><code>grammar</code>: a regular table.</li></ul> |
| 409 |
<p/> |
| 410 |
<strong>Returns:</strong><ul> |
| 411 |
<li>a newly created table, with <code>grammar</code>'s keys and values.</li></ul></ul> |
| 412 |
|
| 413 |
<p/><a name="function_Ct"></a> |
| 414 |
<hr/><code><big>Ct ()</big></code> |
| 415 |
<ul>A capture function, made so that <code>patt / Ct</code> is equivalent to <code>m.Ct(patt)</code>. It's intended to be used in capture tables, such as those required by <a href="#function_pipe">pipe</a> and <a href="#function_apply">apply</a>.</ul> |
| 416 |
|
| 417 |
<p/><a name="function_listOf"></a> |
| 418 |
<hr/><code><big>listOf (patt, sep)</big></code> |
| 419 |
<ul>Returns a pattern which matches a list of <code>patt</code>s, separated by <code>sep</code>. |
| 420 |
<p/> |
| 421 |
<strong>Example:</strong> matching comma-separated values: |
| 422 |
<pre class="example"> |
| 423 |
<font color="#0000FF"><b>local</b></font> g, m = <b><font color ="#800040">require</font></b> <font color="#008000">'leg.grammar'</font>, <b><font color ="#800040">require</font></b> <font color="#008000">'lpeg'</font> |
| 424 |
|
| 425 |
<font color="#808080"> -- separator</font> |
| 426 |
<font color="#0000FF"><b>local</b></font> sep = <b>m.P</b><font color="#008000">','</font> + <b>m.P</b><font color="#008000">'\n'</font> |
| 427 |
|
| 428 |
<font color="#808080"> -- element: anything but sep, capture it</font> |
| 429 |
<font color="#0000FF"><b>local</b></font> elem = <b>m.C</b>((<font color="#B00000"><i>1</i></font> - sep)^<font color="#B00000"><i>0</i></font>) |
| 430 |
|
| 431 |
<font color="#808080"> -- pattern</font> |
| 432 |
<font color="#0000FF"><b>local</b></font> patt = <b>g.listOf</b>(elem, sep) |
| 433 |
|
| 434 |
<font color="#808080"> -- matching</font> |
| 435 |
<b><font color ="#800040">print</font></b>( <b>patt:match</b> <font color="#008000">[[a, b, 'christmas eve' |
| 436 |
d, evening; mate! |
| 437 |
f]]</font>) |
| 438 |
<font color="#808080"> --> prints out "a b 'christmas eve' d evening; mate! f"</font> |
| 439 |
</pre> |
| 440 |
<p/> |
| 441 |
<strong>Parameters:</strong><ul> |
| 442 |
<li><code>patt</code>: a LPeg pattern.</li> |
| 443 |
<li><code>sep</code>: a LPeg pattern.</li></ul> |
| 444 |
<p/> |
| 445 |
<strong>Returns:</strong><ul> |
| 446 |
<li>the following pattern: <pre class="example">patt * (sep * patt)^<font color="#B00000"><i>0</i></font></pre></li></ul></ul> |
| 447 |
|
| 448 |
<p/><a name="function_pipe"></a> |
| 449 |
<hr/><code><big>pipe (dest, orig)</big></code> |
| 450 |
<ul><a href="#section_Piping">Pipes</a> the captures in <code>orig</code> to the ones in <code>dest</code>. |
| 451 |
<p/> |
| 452 |
<code>dest</code> and <code>orig</code> should be tables, with each key storing a capture function. Each capture in <code>dest</code> will be altered to use the results for the matching one in <code>orig</code> as input, using function composition. Should <code>orig</code> possess keys not in <code>dest</code>, <code>dest</code> will copy them. |
| 453 |
<p/> |
| 454 |
<strong>Parameters:</strong><ul> |
| 455 |
<li><code>dest</code>: a capture table.</li> |
| 456 |
<li><code>orig</code>: a capture table.</li></ul> |
| 457 |
<p/> |
| 458 |
<strong>Returns:</strong><ul> |
| 459 |
<li><code>dest</code>, suitably modified.</li></ul></ul> |
| 460 |
|
| 461 |
<hr/> |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
</div> <!-- id="content" --> |
| 467 |
</div> <!-- id="main" --> |
| 468 |
</div> <!-- id="container" --> |
| 469 |
</body> |
| 470 |
</html> |