Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions parser-gen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ local function resetOptions()
return opts
end


-- check values and fill default to the opts
local function mergeOptions(options)

Expand Down Expand Up @@ -81,10 +80,8 @@ local function mergeOptions(options)
-- Lua 5.1 compatibility:
local unpack = unpack or table.unpack


local Predef = { nl = m.P"\n", cr = m.P"\r", tab = m.P"\t" }


local function updatelocale()
m.locale(Predef)
local any = m.P(1)
Expand Down Expand Up @@ -112,20 +109,14 @@ local function mergeOptions(options)

updatelocale()



local function defaultsync(patt)
return (m.P(1)^-1) * (-patt * m.P(1))^0
end




local function sync (patt)
return patt --(-patt * m.P(1))^0 * patt^0 -- skip until we find the pattern and consume it(if we do)
end


local function pattspaces (patt)
if opts.skipspaces then
return patt * opts.specialrules.SKIP ^0
Expand All @@ -150,7 +141,6 @@ local function mergeOptions(options)

local bg = {} -- local variable to keep global function buildgrammar


local function addspaces (caps)
local hastoken = tokenstack:pop()
if hastoken == 1 then
Expand Down Expand Up @@ -274,7 +264,6 @@ local function mergeOptions(options)
end
end


local function applygrammar(gram, opts)
if opts.trace then
return m.P(pegdebugtrace(gram, opts.traceoptions))
Expand Down Expand Up @@ -307,7 +296,6 @@ local function mergeOptions(options)
ret1 = traverse(op1, tokenrule)
ret2 = traverse(op2, tokenrule)


return applyaction(act, ret1, ret2, tokenrule)

elseif isgrammar(ast) then
Expand Down Expand Up @@ -405,7 +393,6 @@ local function mergeOptions(options)
end
end


function bg.buildgrammar (ast)
local builder = {}

Expand Down Expand Up @@ -450,9 +437,6 @@ local function mergeOptions(options)
return builder
end




local function build(ast, opts)

if isgrammar(ast) then
Expand Down Expand Up @@ -508,7 +492,7 @@ local function mergeOptions(options)
if action == "or" then
return '(' .. op1 ..' / '.. op2 ..')'
elseif action == "and" then
return op1 ..' ' .. op2
return '('..op1..' '..op2..')' --25/04/24 DCN: added brackets
elseif action == "&" then
return '&'..op1
elseif action == "!" then
Expand All @@ -526,7 +510,7 @@ local function mergeOptions(options)
-- if not lab then
-- error("Label '"..op2.."' unspecified using setlabels()")
-- end
return op1 .. '^'..op2
return op1..'^'..op2
elseif action == "->" then
if op1 == "''" then -- spacial case for empty capture
return op1 ..' -> '.. op2
Expand Down Expand Up @@ -727,7 +711,6 @@ local function mergeOptions(options)
builder["SYNC"] = opts.specialrules.SYNC_re
end


end

local function recordAstToPEG(label)
Expand Down
47 changes: 36 additions & 11 deletions parser-gen_tests2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,32 @@ local function cleanGram(s) -- remove leading and ending spaces and tabs
return (s:gsub("^%s*(.-)%s*$", "%1"):gsub("[\9 ]+", " "))
end


local function tst_ast(str, gram, options)
local ast_test_number = 0
local function tst_ast(str, gram, options, test_name)
-- rule round trip
options = options or {}

options = options or {}
ast_test_number = ast_test_number + 1
test_name = test_name or 'anon#'..ast_test_number
print('\nast_tst ('..test_name..') start...')

local ast = peg.pegToAST(gram, options['definitions'])
local gram2 = pg.astToPEG(ast, {recovery=false, skipspaces=false, nocaptures=true, re_useext=true})

print(serpent.block(cleanGram(gram)))
print(serpent.block(cleanGram(gram2)))
print('Given grammar: '..serpent.block(cleanGram(gram)))
print('New grammar: '..serpent.block(cleanGram(gram2)))
print('Given str: '..serpent.block(str))

if not options['not_exact_grammar'] then
-- assert(cleanGram(gram) == cleanGram(gram2))
end
local c1,e1 = pg.parse(str, gram, options)
local c2,e2 = pg.parse(str, gram2, options)

print('Given grammar captures: '..serpent.block(c1))
print('New grammar captures: '..serpent.block(c2))
print('Given grammar errors: '..serpent.block(e1))
print('New grammar errors: '..serpent.block(e2))

assert(equals(c1, c2)) -- same captures
assert(equals(e1, e2)) -- same error

Expand All @@ -55,6 +64,7 @@ local function tst_ast(str, gram, options)
local m3, me3 = p3:match(str)
assert( equals(m1, m3))
end
print('ast_tst ('..test_name..') ...finish\n')
return m1, me1
end

Expand Down Expand Up @@ -93,7 +103,7 @@ SKIP <- DOT
DOT <- '.'
]]
str = "a...b"
--tst_ast(str, rule)
tst_ast(str, rule) --was disabled

-- non terminals
-- space allowed
Expand Down Expand Up @@ -125,7 +135,7 @@ tst_ast(str, rule)

rule = [[ R <- 'a' ( 'b' / 'c' )]]
str = 'ab'
--res = tst_ast(str, rule, {not_exact_grammar = true})
res = tst_ast(str, rule, {not_exact_grammar = true}) --was disabled
-- testing ranges
rule = [[ r <- {[a1b]* } ]]
str = "a1b"
Expand Down Expand Up @@ -302,16 +312,31 @@ A <- 'a'
B <- 'b'
]]


-- SELF-DESCRIPTION
--local res1, errs = tst_ast(peg.gram, peg.gram, {nocaptures=true, labels=peg.errinfo, definitions = {foldtable = peg.foldtable, concat = peg.concat}, not_exact_grammar = true})

local options ={nocaptures=true, labels=peg.errinfo, definitions = {foldtable = peg.foldtable, concat = peg.concat}}

local str = peg.gram

local ast = peg.pegToAST(peg.gram, {foldtable = peg.foldtable, concat = peg.concat})

print( "=====================")
print( "peg.gram:\n")
print( peg.gram)
print( "=====================")

print( "=====================")
print("ast:\n")
print(serpent.block(ast))
print( "=====================")

local gram3 = pg.astToPEG(ast, {recovery=false, skipspaces=false, nocaptures=true, re_useext=true})

print(gram3)
print( "=====================")
print( "gram3:\n")
print( gram3)
print( "=====================")


local optionstrace = tableMerge({trace=true}, options)
local c1,e1 = pg.parse(rule, peg.gram, options)
Expand Down
Loading