Skip to content

Commit

Permalink
Fixing bug when there is only one reserved word. Collecting implicit …
Browse files Browse the repository at this point in the history
…lex rules before other transformations.
  • Loading branch information
sqmedeiros committed Feb 10, 2023
1 parent faca134 commit 472ec28
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
7 changes: 5 additions & 2 deletions cfg2peg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ function Cfg2Peg:collectKeywords ()
if nKey > 0 then
local pKey
if nKey == 1 then
pKey = Node.char(tKey)
pKey = Node.char(tKey[1])
else
pKey = {}
for i, v in ipairs(tKey) do
Expand Down Expand Up @@ -502,7 +502,7 @@ end
function Cfg2Peg:convertLexRule (ruleId)
self.ruleId = ruleId or self.ruleId
self:initId()
self:checkImplicitLexRulesG()
--self:checkImplicitLexRulesG()
self:collectKeywords()
self:checkLexicalPrefixes()
end
Expand Down Expand Up @@ -571,6 +571,9 @@ function Cfg2Peg:convert (ruleId, checkIdReserved)
end

self:initConflictStats()
if checkIdReserved then
self:checkImplicitLexRulesG()
end

for i, var in ipairs(self.peg:getVars()) do
if Grammar.isSynRule(var) then
Expand Down
6 changes: 3 additions & 3 deletions spec/abnf_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ BIT <- [0-1]
DIGIT <- [0-9]
HEX_DIGIT <- [0-9] / [a-f] / [A-F]
EOF <- !.
__rep_001 <- repetition __rep_001 / repetition &(')' / '/' / ']' / EOF / ID)
__IdBegin <- LETTER
__IdRest <- LETTER / DIGIT / '-'
ZLex_001 <- '='
ZLex_002 <- '/'
ZLex_003 <- '*'
ZLex_004 <- '('
ZLex_005 <- ')'
ZLex_006 <- '['
ZLex_007 <- ']'
__rep_001 <- repetition __rep_001 / repetition &(')' / '/' / ']' / EOF / ID)
__IdBegin <- LETTER
__IdRest <- LETTER / DIGIT / '-'
]]
checkConversionToPeg(g, peg, {prefix = true, reserved = true, idRule = 'ID'})

Expand Down
8 changes: 4 additions & 4 deletions spec/cfg2peg_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ describe("Transforming a CFG into an equivalent PEG\n", function()
a <- ZLex_001 / ZLex_002
b <- ZLex_001 / ZLex_001 ZLex_002
Id <- !__Keywords [a-z] [a-z0-9]*
__IdBegin <- [a-z]
__IdRest <- [a-z0-9]
ZLex_001 <- 'a' !__IdRest
ZLex_002 <- 'y' !__IdRest
__IdBegin <- [a-z]
__IdRest <- [a-z0-9]
__Keywords <- ZLex_001 / ZLex_002
]]

Expand All @@ -255,13 +255,13 @@ describe("Transforming a CFG into an equivalent PEG\n", function()
z <- id
Number <- ('x' / 'X') [0-9]+ !__IdRest
id <- !__Keywords [a-z] [a-z0-9]*
__IdBegin <- [a-z]
__IdRest <- [a-z0-9]
ZLex_001 <- 'there' !__IdRest
ZLex_002 <- 'AB' !__IdRest
ZLex_003 <- 'x9' !__IdRest
ZLex_004 <- '3'
ZLex_005 <- 'bb' !__IdRest
__IdBegin <- [a-z]
__IdRest <- [a-z0-9]
__Keywords <- Number / ZLex_001 / ZLex_002 / ZLex_003 / ZLex_005
]]

Expand Down
8 changes: 4 additions & 4 deletions spec/reserved_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ describe("Transforming a CFG into an equivalent PEG\n", function()
a <- ZLex_001 / ZLex_002
b <- ZLex_001 / ZLex_001 ZLex_002
Id <- !__Keywords [a-z] [a-z0-9]*
__IdBegin <- [a-z]
__IdRest <- [a-z0-9]
ZLex_001 <- 'a' !__IdRest
ZLex_002 <- 'y' !__IdRest
__IdBegin <- [a-z]
__IdRest <- [a-z0-9]
__Keywords <- ZLex_001 / ZLex_002
]]

Expand All @@ -91,13 +91,13 @@ describe("Transforming a CFG into an equivalent PEG\n", function()
z <- id
Number <- ('x' / 'X') [0-9]+ !__IdRest
id <- !__Keywords [a-z] [a-z0-9]*
__IdBegin <- [a-z]
__IdRest <- [a-z0-9]
ZLex_001 <- 'there' !__IdRest
ZLex_002 <- 'AB' !__IdRest
ZLex_003 <- 'x9' !__IdRest
ZLex_004 <- '3'
ZLex_005 <- 'bb' !__IdRest
__IdBegin <- [a-z]
__IdRest <- [a-z0-9]
__Keywords <- Number / ZLex_001 / ZLex_002 / ZLex_003 / ZLex_005
]]

Expand Down
63 changes: 63 additions & 0 deletions spec/toy_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local Parser = require 'pegparser.parser'
local Pretty = require 'pegparser.pretty'
local Util = require'pegparser.util'
local Cfg2Peg = require'pegparser.cfg2peg'
local Coder = require'pegparser.coder'

local function checkConversionToPeg (stringG, stringPeg, config)
local g = Parser.match(stringG)
assert.is_not_nil(g)

config = config or {}

local c2p = Cfg2Peg.new(g)
c2p:setUsePrefix(config.prefix)
c2p:setUseUnique(config.unique)
c2p:convert(config.idRule, config.reserved)

local peg = c2p.peg
local pretty = Pretty.new()

local equal = Util.sameWithoutSpace(pretty:printg(peg, nil, true), stringPeg)

if not equal then
print("---- Different ----\n")
print(">>>> Generated PEG <<<<")
print(pretty:printg(peg, nil, true))
print("\n**** Expected PEG ****")
print(stringPeg)
print("")
end

assert.is_true(equal)
end


describe("Transforming a CFG into an equivalent PEG\n", function()

local pretty = Pretty.new()


test("Converting DOT grammar - Dealing with reserved words", function()
local g = [[
init <- a b
a <- 'x'* 'x'
b <- 'x'
ID <- [a-z][a-z]*
]]

local peg = [[
init <- a b
a <- __rep_001 ZLex_001
b <- ZLex_001
ID <- !__Keywords [a-z][a-z]*
ZLex_001 <- 'x' !__IdRest
__rep_001 <- ZLex_001 __rep_001 / &ZLex_001
__IdBegin <- [a-z]
__IdRest <- [a-z]
__Keywords <- ZLex_001
]]
checkConversionToPeg(g, peg, {unique = true, idRule = "ID", reserved = "true"})

end)
end)

0 comments on commit 472ec28

Please sign in to comment.