diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/recovery.lua | 16 | ||||
-rw-r--r-- | examples/recoveryRe.lua | 16 |
2 files changed, 16 insertions, 16 deletions
diff --git a/examples/recovery.lua b/examples/recovery.lua index 7af8455..3272ae7 100644 --- a/examples/recovery.lua +++ b/examples/recovery.lua | |||
@@ -15,10 +15,10 @@ local labels = { | |||
15 | {"MisClose", "missing a closing ')' after the expression"}, | 15 | {"MisClose", "missing a closing ')' after the expression"}, |
16 | } | 16 | } |
17 | 17 | ||
18 | -- The `labelIndex` function gives us the index of a label in the | 18 | -- The `labelindex` function gives us the index of a label in the |
19 | -- `labels` table, which serves as the integer representation of the label. | 19 | -- `labels` table, which serves as the integer representation of the label. |
20 | -- We need this because LPegLabel requires us to use integers for the labels. | 20 | -- We need this because LPegLabel requires us to use integers for the labels. |
21 | local function labelIndex(labname) | 21 | local function labelindex(labname) |
22 | for i, elem in ipairs(labels) do | 22 | for i, elem in ipairs(labels) do |
23 | if elem[1] == labname then | 23 | if elem[1] == labname then |
24 | return i | 24 | return i |
@@ -35,12 +35,12 @@ local errors = {} | |||
35 | -- Before throwing the label, it records the label to be thrown along with | 35 | -- Before throwing the label, it records the label to be thrown along with |
36 | -- the position of the failure (index in input string) into the `errors` table. | 36 | -- the position of the failure (index in input string) into the `errors` table. |
37 | local function expect(patt, labname) | 37 | local function expect(patt, labname) |
38 | local i = labelIndex(labname) | 38 | local i = labelindex(labname) |
39 | function recordError(input, pos) | 39 | function recorderror(input, pos) |
40 | table.insert(errors, {i, pos}) | 40 | table.insert(errors, {i, pos}) |
41 | return true | 41 | return true |
42 | end | 42 | end |
43 | return patt + Cmt("", recordError) * T(i) | 43 | return patt + Cmt("", recorderror) * T(i) |
44 | end | 44 | end |
45 | 45 | ||
46 | local num = R("09")^1 / tonumber | 46 | local num = R("09")^1 / tonumber |
@@ -76,15 +76,15 @@ local g = P { | |||
76 | "Exp", | 76 | "Exp", |
77 | Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; | 77 | Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; |
78 | -- `OpRecov` handles missing terms/operands by returning a dummy (zero). | 78 | -- `OpRecov` handles missing terms/operands by returning a dummy (zero). |
79 | OpRecov = Lc(V"Operand", Cc(0), labelIndex("ExpTerm")); | 79 | OpRecov = Lc(V"Operand", Cc(0), labelindex("ExpTerm")); |
80 | Operand = expect(V"Term", "ExpTerm"); | 80 | Operand = expect(V"Term", "ExpTerm"); |
81 | Term = num + V"Group"; | 81 | Term = num + V"Group"; |
82 | -- `Group` handles missing closing parenthesis by simply ignoring it. | 82 | -- `Group` handles missing closing parenthesis by simply ignoring it. |
83 | -- Like all the others, the error is still recorded of course. | 83 | -- Like all the others, the error is still recorded of course. |
84 | Group = "(" * V"InnerExp" * Lc(expect(")", "MisClose"), P"", labelIndex("MisClose")); | 84 | Group = "(" * V"InnerExp" * Lc(expect(")", "MisClose"), P"", labelindex("MisClose")); |
85 | -- `InnerExp` handles missing expressions by skipping to the next closing | 85 | -- `InnerExp` handles missing expressions by skipping to the next closing |
86 | -- parenthesis. A dummy (zero) is returned in place of the expression. | 86 | -- parenthesis. A dummy (zero) is returned in place of the expression. |
87 | InnerExp = Lc(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelIndex("ExpExp")); | 87 | InnerExp = Lc(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelindex("ExpExp")); |
88 | } | 88 | } |
89 | 89 | ||
90 | g = expect(g, "NoExp") * expect(-P(1), "Extra") | 90 | g = expect(g, "NoExp") * expect(-P(1), "Extra") |
diff --git a/examples/recoveryRe.lua b/examples/recoveryRe.lua index 795e01d..3b83d88 100644 --- a/examples/recoveryRe.lua +++ b/examples/recoveryRe.lua | |||
@@ -31,11 +31,11 @@ re.setlabels(labels) | |||
31 | -- The `errors` table will hold the list of errors recorded during parsing | 31 | -- The `errors` table will hold the list of errors recorded during parsing |
32 | local errors = {} | 32 | local errors = {} |
33 | 33 | ||
34 | -- The `recordError` function simply records the label and position of | 34 | -- The `recorderror` function simply records the label and position of |
35 | -- the failure (index in input string) into the `errors` table. | 35 | -- the failure (index in input string) into the `errors` table. |
36 | -- Note: The unused `input` parameter is necessary, as this will be called | 36 | -- Note: The unused `input` parameter is necessary, as this will be called |
37 | -- by LPeg's match-time capture. | 37 | -- by LPeg's match-time capture. |
38 | function recordError(input, pos, label) | 38 | local function recorderror(input, pos, label) |
39 | table.insert(errors, {label, pos}) | 39 | table.insert(errors, {label, pos}) |
40 | return true | 40 | return true |
41 | end | 41 | end |
@@ -82,16 +82,16 @@ local g = re.compile([[ | |||
82 | num <- [0-9]+ -> tonumber | 82 | num <- [0-9]+ -> tonumber |
83 | 83 | ||
84 | -- Before throwing an error, we make sure to record it first. | 84 | -- Before throwing an error, we make sure to record it first. |
85 | ErrNoExp <- ("" -> "NoExp" => recordError) %{NoExp} | 85 | ErrNoExp <- ("" -> "NoExp" => recorderror) %{NoExp} |
86 | ErrExtra <- ("" -> "Extra" => recordError) %{Extra} | 86 | ErrExtra <- ("" -> "Extra" => recorderror) %{Extra} |
87 | ErrExpTerm <- ("" -> "ExpTerm" => recordError) %{ExpTerm} | 87 | ErrExpTerm <- ("" -> "ExpTerm" => recorderror) %{ExpTerm} |
88 | ErrExpExp <- ("" -> "ExpExp" => recordError) %{ExpExp} | 88 | ErrExpExp <- ("" -> "ExpExp" => recorderror) %{ExpExp} |
89 | ErrMisClose <- ("" -> "MisClose" => recordError) %{MisClose} | 89 | ErrMisClose <- ("" -> "MisClose" => recorderror) %{MisClose} |
90 | 90 | ||
91 | dummy <- "" -> "0" -> tonumber | 91 | dummy <- "" -> "0" -> tonumber |
92 | ]], { | 92 | ]], { |
93 | compute = compute; | 93 | compute = compute; |
94 | recordError = recordError; | 94 | recorderror = recorderror; |
95 | tonumber = tonumber; | 95 | tonumber = tonumber; |
96 | }) | 96 | }) |
97 | 97 | ||