diff options
author | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-19 13:43:21 -0300 |
---|---|---|
committer | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-19 13:43:21 -0300 |
commit | 44d16477a2c412ce9d010e2f793558902ffb737f (patch) | |
tree | 7089a1f9b84dc433a1c5651623d68b0c78ad0f78 | |
parent | e4b15b0debdb65dff377c6ff841abb6fc30acd23 (diff) | |
download | lpeglabel-44d16477a2c412ce9d010e2f793558902ffb737f.tar.gz lpeglabel-44d16477a2c412ce9d010e2f793558902ffb737f.tar.bz2 lpeglabel-44d16477a2c412ce9d010e2f793558902ffb737f.zip |
Updating relabel to use the new semantics of m.T
-rw-r--r-- | relabel.lua | 76 |
1 files changed, 33 insertions, 43 deletions
diff --git a/relabel.lua b/relabel.lua index 85ba79a..f668086 100644 --- a/relabel.lua +++ b/relabel.lua | |||
@@ -1,10 +1,10 @@ | |||
1 | -- $Id: re.lua,v 1.44 2013/03/26 20:11:40 roberto Exp $ | 1 | -- $Id: re.lua,v 1.44 2013/03/26 20:11:40 roberto Exp $ |
2 | 2 | ||
3 | -- imported functions and modules | 3 | -- imported functions and modules |
4 | local tonumber, type, print, error, ipairs = tonumber, type, print, error, ipairs | 4 | local tonumber, type, print, error = tonumber, type, print, error |
5 | local pcall = pcall | 5 | local pcall = pcall |
6 | local setmetatable = setmetatable | 6 | local setmetatable = setmetatable |
7 | local unpack, tinsert, concat = table.unpack or unpack, table.insert, table.concat | 7 | local tinsert, concat = table.insert, table.concat |
8 | local rep = string.rep | 8 | local rep = string.rep |
9 | local m = require"lpeglabel" | 9 | local m = require"lpeglabel" |
10 | 10 | ||
@@ -28,68 +28,58 @@ local dummy = mm.P(false) | |||
28 | 28 | ||
29 | 29 | ||
30 | local errinfo = { | 30 | local errinfo = { |
31 | {"NoPatt", "no pattern found"}, | 31 | NoPatt = "no pattern found", |
32 | {"ExtraChars", "unexpected characters after the pattern"}, | 32 | ExtraChars = "unexpected characters after the pattern", |
33 | 33 | ||
34 | {"ExpPatt1", "expected a pattern after '/'"}, | 34 | ExpPatt1 = "expected a pattern after '/'", |
35 | 35 | ||
36 | {"ExpPatt2", "expected a pattern after '&'"}, | 36 | ExpPatt2 = "expected a pattern after '&'", |
37 | {"ExpPatt3", "expected a pattern after '!'"}, | 37 | ExpPatt3 = "expected a pattern after '!'", |
38 | 38 | ||
39 | {"ExpPatt4", "expected a pattern after '('"}, | 39 | ExpPatt4 = "expected a pattern after '('", |
40 | {"ExpPatt5", "expected a pattern after ':'"}, | 40 | ExpPatt5 = "expected a pattern after ':'", |
41 | {"ExpPatt6", "expected a pattern after '{~'"}, | 41 | ExpPatt6 = "expected a pattern after '{~'", |
42 | {"ExpPatt7", "expected a pattern after '{|'"}, | 42 | ExpPatt7 = "expected a pattern after '{|'", |
43 | 43 | ||
44 | {"ExpPatt8", "expected a pattern after '<-'"}, | 44 | ExpPatt8 = "expected a pattern after '<-'", |
45 | 45 | ||
46 | {"ExpPattOrClose", "expected a pattern or closing '}' after '{'"}, | 46 | ExpPattOrClose = "expected a pattern or closing '}' after '{'", |
47 | 47 | ||
48 | {"ExpNumName", "expected a number, '+', '-' or a name (no space) after '^'"}, | 48 | ExpNumName = "expected a number, '+', '-' or a name (no space) after '^'", |
49 | {"ExpCap", "expected a string, number, '{}' or name after '->'"}, | 49 | ExpCap = "expected a string, number, '{}' or name after '->'", |
50 | 50 | ||
51 | {"ExpName1", "expected the name of a rule after '=>'"}, | 51 | ExpName1 = "expected the name of a rule after '=>'", |
52 | {"ExpName2", "expected the name of a rule after '=' (no space)"}, | 52 | ExpName2 = "expected the name of a rule after '=' (no space)", |
53 | {"ExpName3", "expected the name of a rule after '<' (no space)"}, | 53 | ExpName3 = "expected the name of a rule after '<' (no space)", |
54 | 54 | ||
55 | {"ExpLab1", "expected a label after '{'"}, | 55 | ExpLab1 = "expected a label after '{'", |
56 | 56 | ||
57 | {"ExpNameOrLab", "expected a name or label after '%' (no space)"}, | 57 | ExpNameOrLab = "expected a name or label after '%' (no space)", |
58 | 58 | ||
59 | {"ExpItem", "expected at least one item after '[' or '^'"}, | 59 | ExpItem = "expected at least one item after '[' or '^'", |
60 | 60 | ||
61 | {"MisClose1", "missing closing ')'"}, | 61 | MisClose1 = "missing closing ')'", |
62 | {"MisClose2", "missing closing ':}'"}, | 62 | MisClose2 = "missing closing ':}'", |
63 | {"MisClose3", "missing closing '~}'"}, | 63 | MisClose3 = "missing closing '~}'", |
64 | {"MisClose4", "missing closing '|}'"}, | 64 | MisClose4 = "missing closing '|}'", |
65 | {"MisClose5", "missing closing '}'"}, -- for the captures | 65 | MisClose5 = "missing closing '}'", -- for the captures |
66 | 66 | ||
67 | {"MisClose6", "missing closing '>'"}, | 67 | MisClose6 = "missing closing '>'", |
68 | {"MisClose7", "missing closing '}'"}, -- for the labels | 68 | MisClose7 = "missing closing '}'", -- for the labels |
69 | 69 | ||
70 | {"MisClose8", "missing closing ']'"}, | 70 | MisClose8 = "missing closing ']'", |
71 | 71 | ||
72 | {"MisTerm1", "missing terminating single quote"}, | 72 | MisTerm1 = "missing terminating single quote", |
73 | {"MisTerm2", "missing terminating double quote"}, | 73 | MisTerm2 = "missing terminating double quote", |
74 | } | 74 | } |
75 | 75 | ||
76 | local errmsgs = {} | 76 | local function expect (pattern, label) |
77 | local labels = {} | ||
78 | |||
79 | for i, err in ipairs(errinfo) do | ||
80 | errmsgs[i] = err[2] | ||
81 | labels[err[1]] = i | ||
82 | end | ||
83 | |||
84 | local function expect (pattern, labelname) | ||
85 | local label = labels[labelname] | ||
86 | return pattern + m.T(label) | 77 | return pattern + m.T(label) |
87 | end | 78 | end |
88 | 79 | ||
89 | 80 | ||
90 | -- Pre-defined names | 81 | -- Pre-defined names |
91 | local Predef = { nl = m.P"\n" } | 82 | local Predef = { nl = m.P"\n" } |
92 | local tlabels = {} | ||
93 | 83 | ||
94 | 84 | ||
95 | local mem | 85 | local mem |
@@ -315,7 +305,7 @@ local function compile (p, defs) | |||
315 | local lines = splitlines(p) | 305 | local lines = splitlines(p) |
316 | local line, col = lineno(p, poserr) | 306 | local line, col = lineno(p, poserr) |
317 | local err = {} | 307 | local err = {} |
318 | tinsert(err, "L" .. line .. ":C" .. col .. ": " .. errmsgs[label]) | 308 | tinsert(err, "L" .. line .. ":C" .. col .. ": " .. errinfo[label]) |
319 | tinsert(err, lines[line]) | 309 | tinsert(err, lines[line]) |
320 | tinsert(err, rep(" ", col-1) .. "^") | 310 | tinsert(err, rep(" ", col-1) .. "^") |
321 | error("syntax error(s) in pattern\n" .. concat(err, "\n"), 3) | 311 | error("syntax error(s) in pattern\n" .. concat(err, "\n"), 3) |