Skip to content

Commit

Permalink
Updating relabel's parser after removing /{} and //{}
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmedeiros committed Dec 19, 2017
1 parent c6b9810 commit c5574a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 65 deletions.
38 changes: 3 additions & 35 deletions relabel.lua
Expand Up @@ -31,7 +31,7 @@ local errinfo = {
{"NoPatt", "no pattern found"},
{"ExtraChars", "unexpected characters after the pattern"},

{"ExpPatt1", "expected a pattern after '/' and '//{...}', or the label(s) after '/{' and '//{'"},
{"ExpPatt1", "expected a pattern after '/'"},

{"ExpPatt2", "expected a pattern after '&'"},
{"ExpPatt3", "expected a pattern after '!'"},
Expand All @@ -52,8 +52,7 @@ local errinfo = {
{"ExpName2", "expected the name of a rule after '=' (no space)"},
{"ExpName3", "expected the name of a rule after '<' (no space)"},

{"ExpLab1", "expected at least one label after '{'"},
{"ExpLab2", "expected a label after the comma"},
{"ExpLab1", "expected a label after '{'"},

{"ExpNameOrLab", "expected a name or label after '%' (no space)"},

Expand Down Expand Up @@ -216,41 +215,10 @@ local function NT (n, b)
end
end

local function choicerec (...)
local t = { ... }
local n = #t
local p = t[1]
local i = 2

while i + 2 <= n do
-- t[i] is '/' or '//'
-- t[i+1] == false when there are no labels
-- t[i+2] is a pattern
if t[i] == '/' then
if not t[i+1] then
p = mt.__add(p, t[i+2])
else
p = mm.Lc(p, t[i+2], unpack(t[i+1]))
end
else -- t[i] is '//'
p = mm.Rec(p, t[i+2], unpack(t[i+1]))
end
i = i + 3
end

return p
end


local exp = m.P{ "Exp",
Exp = S * ( m.V"Grammar"
+ (m.V"Seq" * (S * ((m.C(m.P"//" + "/") * m.Ct(m.V"Labels")) + (m.C"/" * m.Cc(false)))
* expect(S * m.V"Seq", "ExpPatt1")
)^0
) / choicerec);
Labels = m.P"{" * expect(S * m.V"Label", "ExpLab1")
* (S * "," * expect(S * m.V"Label", "ExpLab2"))^0
* expect(S * "}", "MisClose7");
+ m.Cf(m.V"Seq" * (S * "/" * expect(S * m.V"Seq", "ExpPatt1"))^0, mt.__add) );
Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix" * (S * m.V"Prefix")^0, mt.__mul);
Prefix = "&" * expect(S * m.V"Prefix", "ExpPatt2") / mt.__len
+ "!" * expect(S * m.V"Prefix", "ExpPatt3") / mt.__unm
Expand Down
41 changes: 11 additions & 30 deletions testrelabelparser.lua
Expand Up @@ -53,26 +53,14 @@ L1:C5: unexpected characters after the pattern

-- testing ExpPatt1

testerror([['p' //{1}]], [[
L1:C10: expected a pattern after '/' and '//{...}', or the label(s) after '/{' and '//{'
'p' //{1}
^
]])

testerror([['p' //{1} //{2} 'q']], [[
L1:C10: expected a pattern after '/' and '//{...}', or the label(s) after '/{' and '//{'
'p' //{1} //{2} 'q'
^
]])

testerror([['p' /]], [[
L1:C6: expected a pattern after '/' and '//{...}', or the label(s) after '/{' and '//{'
L1:C6: expected a pattern after '/'
'p' /
^
]])

testerror([['p' / / 'q']], [[
L1:C6: expected a pattern after '/' and '//{...}', or the label(s) after '/{' and '//{'
L1:C6: expected a pattern after '/'
'p' / / 'q'
^
]])
Expand Down Expand Up @@ -249,22 +237,22 @@ L1:C2: expected a pattern or closing '}' after '{'
^
]])

-- testing ExpNum
-- testing ExpNumName

testerror([['p' ^ n]], [[
L1:C6: expected a number after '^', '+' or '-' (no space)
L1:C6: expected a number, '+', '-' or a name (no space) after '^'
'p' ^ n
^
]])

testerror([['p'^+(+1)]], [[
L1:C5: expected a number after '^', '+' or '-' (no space)
L1:C5: expected a number, '+', '-' or a name (no space) after '^'
'p'^+(+1)
^
]])

testerror([['p'^-/'q']], [[
L1:C5: expected a number after '^', '+' or '-' (no space)
L1:C5: expected a number, '+', '-' or a name (no space) after '^'
'p'^-/'q'
^
]])
Expand Down Expand Up @@ -351,25 +339,18 @@ L1:C2: expected the name of a rule after '<' (no space)

-- testing ExpLab1

testerror([['p' //{} 'q']], [[
L1:C8: expected at least one label after '{'
'p' //{} 'q'
^
testerror([['p' %{} 'q']], [[
L1:C7: expected a label after '{'
'p' %{} 'q'
^
]])

testerror([[%{ 'label' }]], [[
L1:C3: expected at least one label after '{'
L1:C3: expected a label after '{'
%{ 'label' }
^
]])

-- testing ExpLab2

testerror([['p' //{1,2,3,} 'q']], [[
L1:C14: expected a label after the comma
'p' //{1,2,3,} 'q'
^
]])

-- testing ExpNameOrLab

Expand Down

0 comments on commit c5574a9

Please sign in to comment.