From 8bc00db24975f52b52e6ef8184c216ce2feae9fd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 23 Jun 2023 14:55:48 -0300 Subject: Fold capture deprecated --- lpeg.html | 68 +++++++++++++++------------------------------------------------ 1 file changed, 16 insertions(+), 52 deletions(-) (limited to 'lpeg.html') diff --git a/lpeg.html b/lpeg.html index 5271a52..c9982aa 100644 --- a/lpeg.html +++ b/lpeg.html @@ -444,7 +444,8 @@ letter = lower + upper

patt1 - patt2

-Returns a pattern equivalent to !patt2 patt1. +Returns a pattern equivalent to !patt2 patt1 +in the origial PEG notation. This pattern asserts that the input does not match patt2 and then matches patt1.

@@ -615,7 +616,7 @@ The following table summarizes the basic captures: lpeg.Cc(values) the given values (matches the empty string) lpeg.Cf(patt, func) - a folding of the captures from patt + folding capture (deprecated) lpeg.Cg(patt [, key]) the values produced by patt, optionally tagged with key @@ -638,7 +639,7 @@ or no value when number is zero. patt / function the returns of function applied to the captures of patt -patt % function +patt % function produces no value; it accummulates the captures from patt into the previous capture through function @@ -739,52 +740,12 @@ produces all given values as its captured values.

lpeg.Cf (patt, func)

Creates a fold capture. -If patt produces a list of captures -C1 C2 ... Cn, -this capture will produce the value -func(...func(func(C1, C2), C3)..., - Cn), -that is, it will fold -(or accumulate, or reduce) -the captures from patt using function func. -

- -

-This capture assumes that patt should produce -at least one capture with at least one value (of any type), -which becomes the initial value of an accumulator. -(If you need a specific initial value, -you may prefix a constant capture to patt.) -For each subsequent capture, -LPeg calls func -with this accumulator as the first argument and all values produced -by the capture as extra arguments; -the first result from this call -becomes the new value for the accumulator. -The final value of the accumulator becomes the captured value. -

- -

-As an example, -the following pattern matches a list of numbers separated -by commas and returns their addition: -

-
--- matches a numeral and captures its numerical value
-number = lpeg.R"09"^1 / tonumber
-
--- matches a list of numbers, capturing their values
-list = number * ("," * number)^0
-
--- auxiliary function to add two numbers
-function add (acc, newvalue) return acc + newvalue end
-
--- folds the list of numbers adding them
-sum = lpeg.Cf(list, add)
-
--- example of use
-print(sum:match("10,30,43"))   --> 83
-
+This construction is deprecated; +use an accumulator pattern instead. +In general, a fold like +lpeg.Cf(p1 * p2^0, func) +can be translated to +(p1 * (p2 % func)^0).

lpeg.Cg (patt [, key])

@@ -895,7 +856,7 @@ there is no captured value.

-

patt % function

+

patt % function

Creates an accumulator capture. This pattern behaves similarly to a @@ -977,7 +938,10 @@ all their subcaptures to compute their results.) Moreover, due to implementation details, you should not use this capture directly nested in a substitution capture. -A simple and effective way to avoid these issues is +You should also avoid a direct nesting of this capture inside +a folding capture (deprecated), +as the folding will try to fold each individual accumulator capture. +A simple and effective way to avoid all these issues is to enclose the whole accumulation composition (including the capture that generates the initial value) into an anonymous group capture. @@ -1411,7 +1375,7 @@ and the new term for each repetition.

Download

LPeg -source code.

+source code.

Probably, the easiest way to install LPeg is with -- cgit v1.2.3-55-g6feb