aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSérgio Queiroz <sqmedeiros@gmail.com>2017-12-19 13:43:21 -0300
committerSérgio Queiroz <sqmedeiros@gmail.com>2017-12-19 13:43:21 -0300
commit44d16477a2c412ce9d010e2f793558902ffb737f (patch)
tree7089a1f9b84dc433a1c5651623d68b0c78ad0f78
parente4b15b0debdb65dff377c6ff841abb6fc30acd23 (diff)
downloadlpeglabel-44d16477a2c412ce9d010e2f793558902ffb737f.tar.gz
lpeglabel-44d16477a2c412ce9d010e2f793558902ffb737f.tar.bz2
lpeglabel-44d16477a2c412ce9d010e2f793558902ffb737f.zip
Updating relabel to use the new semantics of m.T
-rw-r--r--relabel.lua76
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
4local tonumber, type, print, error, ipairs = tonumber, type, print, error, ipairs 4local tonumber, type, print, error = tonumber, type, print, error
5local pcall = pcall 5local pcall = pcall
6local setmetatable = setmetatable 6local setmetatable = setmetatable
7local unpack, tinsert, concat = table.unpack or unpack, table.insert, table.concat 7local tinsert, concat = table.insert, table.concat
8local rep = string.rep 8local rep = string.rep
9local m = require"lpeglabel" 9local m = require"lpeglabel"
10 10
@@ -28,68 +28,58 @@ local dummy = mm.P(false)
28 28
29 29
30local errinfo = { 30local 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
76local errmsgs = {} 76local function expect (pattern, label)
77local labels = {}
78
79for i, err in ipairs(errinfo) do
80 errmsgs[i] = err[2]
81 labels[err[1]] = i
82end
83
84local function expect (pattern, labelname)
85 local label = labels[labelname]
86 return pattern + m.T(label) 77 return pattern + m.T(label)
87end 78end
88 79
89 80
90-- Pre-defined names 81-- Pre-defined names
91local Predef = { nl = m.P"\n" } 82local Predef = { nl = m.P"\n" }
92local tlabels = {}
93 83
94 84
95local mem 85local 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)