Skip to content

Commit 472ec28

Browse files
committed
Fixing bug when there is only one reserved word. Collecting implicit lex rules before other transformations.
1 parent faca134 commit 472ec28

File tree

5 files changed

+79
-13
lines changed

5 files changed

+79
-13
lines changed

cfg2peg.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ function Cfg2Peg:collectKeywords ()
459459
if nKey > 0 then
460460
local pKey
461461
if nKey == 1 then
462-
pKey = Node.char(tKey)
462+
pKey = Node.char(tKey[1])
463463
else
464464
pKey = {}
465465
for i, v in ipairs(tKey) do
@@ -502,7 +502,7 @@ end
502502
function Cfg2Peg:convertLexRule (ruleId)
503503
self.ruleId = ruleId or self.ruleId
504504
self:initId()
505-
self:checkImplicitLexRulesG()
505+
--self:checkImplicitLexRulesG()
506506
self:collectKeywords()
507507
self:checkLexicalPrefixes()
508508
end
@@ -571,6 +571,9 @@ function Cfg2Peg:convert (ruleId, checkIdReserved)
571571
end
572572

573573
self:initConflictStats()
574+
if checkIdReserved then
575+
self:checkImplicitLexRulesG()
576+
end
574577

575578
for i, var in ipairs(self.peg:getVars()) do
576579
if Grammar.isSynRule(var) then

spec/abnf_spec.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ BIT <- [0-1]
9797
DIGIT <- [0-9]
9898
HEX_DIGIT <- [0-9] / [a-f] / [A-F]
9999
EOF <- !.
100-
__rep_001 <- repetition __rep_001 / repetition &(')' / '/' / ']' / EOF / ID)
101-
__IdBegin <- LETTER
102-
__IdRest <- LETTER / DIGIT / '-'
103100
ZLex_001 <- '='
104101
ZLex_002 <- '/'
105102
ZLex_003 <- '*'
106103
ZLex_004 <- '('
107104
ZLex_005 <- ')'
108105
ZLex_006 <- '['
109106
ZLex_007 <- ']'
107+
__rep_001 <- repetition __rep_001 / repetition &(')' / '/' / ']' / EOF / ID)
108+
__IdBegin <- LETTER
109+
__IdRest <- LETTER / DIGIT / '-'
110110
]]
111111
checkConversionToPeg(g, peg, {prefix = true, reserved = true, idRule = 'ID'})
112112

spec/cfg2peg_spec.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ describe("Transforming a CFG into an equivalent PEG\n", function()
228228
a <- ZLex_001 / ZLex_002
229229
b <- ZLex_001 / ZLex_001 ZLex_002
230230
Id <- !__Keywords [a-z] [a-z0-9]*
231-
__IdBegin <- [a-z]
232-
__IdRest <- [a-z0-9]
233231
ZLex_001 <- 'a' !__IdRest
234232
ZLex_002 <- 'y' !__IdRest
233+
__IdBegin <- [a-z]
234+
__IdRest <- [a-z0-9]
235235
__Keywords <- ZLex_001 / ZLex_002
236236
]]
237237

@@ -255,13 +255,13 @@ describe("Transforming a CFG into an equivalent PEG\n", function()
255255
z <- id
256256
Number <- ('x' / 'X') [0-9]+ !__IdRest
257257
id <- !__Keywords [a-z] [a-z0-9]*
258-
__IdBegin <- [a-z]
259-
__IdRest <- [a-z0-9]
260258
ZLex_001 <- 'there' !__IdRest
261259
ZLex_002 <- 'AB' !__IdRest
262260
ZLex_003 <- 'x9' !__IdRest
263261
ZLex_004 <- '3'
264262
ZLex_005 <- 'bb' !__IdRest
263+
__IdBegin <- [a-z]
264+
__IdRest <- [a-z0-9]
265265
__Keywords <- Number / ZLex_001 / ZLex_002 / ZLex_003 / ZLex_005
266266
]]
267267

spec/reserved_spec.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ describe("Transforming a CFG into an equivalent PEG\n", function()
6464
a <- ZLex_001 / ZLex_002
6565
b <- ZLex_001 / ZLex_001 ZLex_002
6666
Id <- !__Keywords [a-z] [a-z0-9]*
67-
__IdBegin <- [a-z]
68-
__IdRest <- [a-z0-9]
6967
ZLex_001 <- 'a' !__IdRest
7068
ZLex_002 <- 'y' !__IdRest
69+
__IdBegin <- [a-z]
70+
__IdRest <- [a-z0-9]
7171
__Keywords <- ZLex_001 / ZLex_002
7272
]]
7373

@@ -91,13 +91,13 @@ describe("Transforming a CFG into an equivalent PEG\n", function()
9191
z <- id
9292
Number <- ('x' / 'X') [0-9]+ !__IdRest
9393
id <- !__Keywords [a-z] [a-z0-9]*
94-
__IdBegin <- [a-z]
95-
__IdRest <- [a-z0-9]
9694
ZLex_001 <- 'there' !__IdRest
9795
ZLex_002 <- 'AB' !__IdRest
9896
ZLex_003 <- 'x9' !__IdRest
9997
ZLex_004 <- '3'
10098
ZLex_005 <- 'bb' !__IdRest
99+
__IdBegin <- [a-z]
100+
__IdRest <- [a-z0-9]
101101
__Keywords <- Number / ZLex_001 / ZLex_002 / ZLex_003 / ZLex_005
102102
]]
103103

spec/toy_spec.lua

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
local Parser = require 'pegparser.parser'
2+
local Pretty = require 'pegparser.pretty'
3+
local Util = require'pegparser.util'
4+
local Cfg2Peg = require'pegparser.cfg2peg'
5+
local Coder = require'pegparser.coder'
6+
7+
local function checkConversionToPeg (stringG, stringPeg, config)
8+
local g = Parser.match(stringG)
9+
assert.is_not_nil(g)
10+
11+
config = config or {}
12+
13+
local c2p = Cfg2Peg.new(g)
14+
c2p:setUsePrefix(config.prefix)
15+
c2p:setUseUnique(config.unique)
16+
c2p:convert(config.idRule, config.reserved)
17+
18+
local peg = c2p.peg
19+
local pretty = Pretty.new()
20+
21+
local equal = Util.sameWithoutSpace(pretty:printg(peg, nil, true), stringPeg)
22+
23+
if not equal then
24+
print("---- Different ----\n")
25+
print(">>>> Generated PEG <<<<")
26+
print(pretty:printg(peg, nil, true))
27+
print("\n**** Expected PEG ****")
28+
print(stringPeg)
29+
print("")
30+
end
31+
32+
assert.is_true(equal)
33+
end
34+
35+
36+
describe("Transforming a CFG into an equivalent PEG\n", function()
37+
38+
local pretty = Pretty.new()
39+
40+
41+
test("Converting DOT grammar - Dealing with reserved words", function()
42+
local g = [[
43+
init <- a b
44+
a <- 'x'* 'x'
45+
b <- 'x'
46+
ID <- [a-z][a-z]*
47+
]]
48+
49+
local peg = [[
50+
init <- a b
51+
a <- __rep_001 ZLex_001
52+
b <- ZLex_001
53+
ID <- !__Keywords [a-z][a-z]*
54+
ZLex_001 <- 'x' !__IdRest
55+
__rep_001 <- ZLex_001 __rep_001 / &ZLex_001
56+
__IdBegin <- [a-z]
57+
__IdRest <- [a-z]
58+
__Keywords <- ZLex_001
59+
]]
60+
checkConversionToPeg(g, peg, {unique = true, idRule = "ID", reserved = "true"})
61+
62+
end)
63+
end)

0 commit comments

Comments
 (0)