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