You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
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
205
<p/>
201
-
By combining <ahref="http://www.inf.puc-rio.br/~roberto/lpeg.html">LPeg</a> and the modules <ahref="scanner.html">scanner</a>, <ahref="parser.html">parser</a> and <ahref="grammar.html">grammar</a>, this specific problem can be solved as follows:
206
+
By combining <ahref="http://www.inf.puc-rio.br/~roberto/lpeg.html">LPeg</a> and the modules <ahref="parser.html">parser</a> and <ahref="grammar.html">grammar</a>, this specific problem can be solved as follows:
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.
<fontcolor="#808080"> -- a pattern that matches a sequence of patts and concatenates the results</font>
268
-
patt = (patt + Stat + scanner.ANY)^<fontcolor="#B00000"><i>0</i></font> / <fontcolor="#0000FF"><b>function</b></font>(...)
280
+
patt = (patt + Stat + parser.ANY)^<fontcolor="#B00000"><i>0</i></font> / <fontcolor="#0000FF"><b>function</b></font>(...)
269
281
<fontcolor="#0000FF"><b>return</b></font><fontcolor = "#800040"><b>table.concat</b></font>({...}, <fontcolor="#008000">'\n\n'</font>) <fontcolor="#808080"> -- some line breaks for easier reading</font>
<li><code>list</code>: a list of zero or more LPeg patterns or values which can be fed to <ahref="http://www.inf.puc-rio.br/~roberto/lpeg.html#lpeg">lpeg.P</a>.</li></ul>
395
+
<li><code>t</code>: a table with LPeg patterns as values. The keys are ignored.</li></ul>
363
396
<p/>
364
397
<strong>Returns:</strong><ul>
365
398
<li>a pattern which matches any of the patterns received.</li></ul></ul>
366
399
400
+
<p/><aname="function_anywhere"></a>
401
+
<hr/><code><big>anywhere (patt)</big></code>
402
+
<ul>Returns a pattern which searches for the pattern <code>patt</code> anywhere in a string.
403
+
<p/>
404
+
This code was extracted from the <ahref="http://www.inf.puc-rio.br/~roberto/lpeg.html#ex">LPeg home page</a>, in the examples section.
405
+
<p/>
406
+
<strong>Parameters:</strong><ul>
407
+
<li><code>patt</code>: a LPeg pattern.</li></ul>
408
+
<p/>
409
+
<strong>Returns:</strong><ul>
410
+
<li>a LPeg pattern which searches for <code>patt</code> anywhere in the string.</li></ul></ul>
<ul><ahref="#section_Completing">Completes</a><code>rules</code> with <code>grammar</code> and then <ahref="#Applying">applies</a><code>captures</code>.
370
415
<p/>
371
416
<code>rules</code> can either be:<ul>
372
417
<li>a single pattern, which is taken to be the new initial rule, </li>
373
-
<li>a possibly incomplete LPeg grammar, as per <ahref="#function_complete">complete</a>, or </li>
418
+
<li>a possibly incomplete LPeg grammar table, as per <ahref="#function_complete">complete</a>, or </li>
374
419
<li><code>nil</code>, which means no new rules are added.</li></ul>
<ul>Returns a pattern which simply fails to match if an error is thrown during the matching.
522
+
<p/>
523
+
One usage example is <ahref="parser.html#variable_NUMBER">parser.NUMBER</a>. Originally it threw an error when trying to match a malformed number (such as 1e23e4), since in this case the input is obviously invalid and the pattern would be part of the Lua grammar. So <ahref="#function_pmatch">pmatch</a> is used to catch the error and return <code>nil</code> (signalling a non-match) and the error message.
524
+
<p/>
525
+
<strong>Parameters:</strong><ul>
526
+
<li><code>patt</code>: a LPeg pattern.</li></ul>
527
+
<p/>
528
+
<strong>Returns:</strong><ul>
529
+
<li>a pattern which catches any errors thrown during the matching and simply doesn't match instead of propagating the error.</li></ul></ul>
0 commit comments