Skip to content

Commit

Permalink
Removed global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
vsbenas committed Sep 21, 2017
1 parent 27b5640 commit c71f836
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 129 deletions.
2 changes: 1 addition & 1 deletion equals.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- this function compares if two tables are equal
function equals(o1, o2, ignore_mt)
local function equals(o1, o2, ignore_mt)
if o1 == o2 then return true end
local o1Type = type(o1)
local o2Type = type(o2)
Expand Down
2 changes: 1 addition & 1 deletion errorgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ end

-- functions to add errors
-- find size of table
function getn (t)
local function getn (t)
local size = 0
for _, _ in pairs(t) do
size = size+1
Expand Down
252 changes: 128 additions & 124 deletions parser-gen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ local eg = require "errorgen"
local s = require "stack"

-- create stack for tokens inside captures. nil - not inside capture, 0 - inside capture, 1 - token found inside capture
local tokenstack = Stack:Create()
local tokenstack = s.Stack:Create()



local subject, errors, errorfunc

-- Lua 5.1 compatibility:
local unpack = unpack or table.unpack
Expand Down Expand Up @@ -138,96 +142,7 @@ local function finalNode (t)
end
return nil
end
local function specialrules(t, builder)
-- initialize values
SKIP = (Predef.space + Predef.nl)
skipspaces = true
SYNC = nil
recovery = true
-- find SPACE and SYNC rules
for i, v in ipairs(ast) do
local name = v["rulename"]
local rule
if name == "SKIP" then
rule = traverse(v["rule"], true)
if v["rule"]["t"] == '' then
skipspaces = false
else
skipspaces = true
SKIP = rule
end
builder[name] = rule
elseif name == "SYNC" then
rule = traverse(v["rule"], true)
if v["rule"]["t"] == '' then-- SYNC <- ''
recovery=false
else
recovery= true
SYNC = rule
end
builder[name] = rule
end
end
if not SYNC and recovery then
SYNC = defaultsync(SKIP)
end
end

local function buildrecovery(grammar)

local synctoken = sync(SYNC)
local grec = grammar

for k,v in pairs(tlabels) do

if trecs[v] then -- custom sync token
grec = m.Rec(grec,record(v) * trecs[v], v)
else -- use global sync token
grec = m.Rec(grec,record(v) * synctoken, v)
end
end
return grec

end
local function buildgrammar (ast)
local builder = {}
specialrules(ast, builder)
local initialrule
for i, v in ipairs(ast) do
local istokenrule = v["token"] == "1"
local isfragment = v["fragment"] == "1"

local name = v["rulename"]
currentrule = name
local isspecial = name == "SKIP" or name == "SYNC"
local rule = v["rule"]
if i == 1 then
initialrule = name
table.insert(builder, name) -- lpeg syntax
builder[name] = traverse(rule, istokenrule)
else
if not builder[name] then -- dont traverse rules for SKIP and SYNC twice
builder[name] = traverse(rule, istokenrule)
end
end
if buildast and not isfragment and not isspecial then
if istokenrule then
builder[name] = m.C(builder[name])
end
builder[name] = m.Ct(m.Cg(m.Cc(name),"rule") * builder[name])
end
end

if skipspaces then
builder[initialrule] = SKIP^0 * builder[initialrule] -- skip spaces at the beginning of the input
end
if recovery then
builder[initialrule] = buildrecovery(builder[initialrule]) -- build recovery on top of initial rule
end
return builder
end


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


local function addspaces (caps)
Expand Down Expand Up @@ -362,30 +277,7 @@ local function applygrammar(gram)
return m.P(gram)
end


local function build(ast, defs)
if defs then
definitions = defs
end
if isgrammar(ast) then
return traverse(ast)
else
SKIP = (Predef.space + Predef.nl)
skipspaces = true
SYNC = nil
recovery = true
SYNC = defaultsync(SKIP)
currentrule = ''
local res = SKIP ^0 * traverse(ast)
if buildast then
res = m.Ct(res)
end
return res -- input is not a grammar - skip spaces and sync by default
end
end


function traverse (ast, tokenrule)
local function traverse (ast, tokenrule)
if not ast then
return nil
end
Expand Down Expand Up @@ -415,7 +307,7 @@ function traverse (ast, tokenrule)

elseif isgrammar(ast) then
--
local g = buildgrammar (ast)
local g = bg.buildgrammar (ast)
return applygrammar (g)

else
Expand All @@ -425,7 +317,40 @@ function traverse (ast, tokenrule)

end

-- recovery grammar
local function specialrules(t, builder)
-- initialize values
SKIP = (Predef.space + Predef.nl)
skipspaces = true
SYNC = nil
recovery = true
-- find SPACE and SYNC rules
for i, v in ipairs(ast) do
local name = v["rulename"]
local rule
if name == "SKIP" then
rule = traverse(v["rule"], true)
if v["rule"]["t"] == '' then
skipspaces = false
else
skipspaces = true
SKIP = rule
end
builder[name] = rule
elseif name == "SYNC" then
rule = traverse(v["rule"], true)
if v["rule"]["t"] == '' then-- SYNC <- ''
recovery=false
else
recovery= true
SYNC = rule
end
builder[name] = rule
end
end
if not SYNC and recovery then
SYNC = defaultsync(SKIP)
end
end

-- from relabel.lua module
local function calcline (s, i)
Expand All @@ -435,13 +360,7 @@ local function calcline (s, i)
return 1 + line, col ~= 0 and col or 1
end

local subject, errors, errorfunc

function record(label)
return (m.Cp() * m.Cc(label)) / recorderror
end

function recorderror(position,label)
local function recorderror(position,label)
-- call error function here
local line, col = calcline(subject, position)
local desc
Expand All @@ -461,6 +380,91 @@ function recorderror(position,label)
table.insert(errors, err)

end
local function record(label)
return (m.Cp() * m.Cc(label)) / recorderror
end

local function buildrecovery(grammar)

local synctoken = sync(SYNC)
local grec = grammar

for k,v in pairs(tlabels) do

if trecs[v] then -- custom sync token
grec = m.Rec(grec,record(v) * trecs[v], v)
else -- use global sync token
grec = m.Rec(grec,record(v) * synctoken, v)
end
end
return grec

end
function bg.buildgrammar (ast)
local builder = {}
specialrules(ast, builder)
local initialrule
for i, v in ipairs(ast) do
local istokenrule = v["token"] == "1"
local isfragment = v["fragment"] == "1"

local name = v["rulename"]
currentrule = name
local isspecial = name == "SKIP" or name == "SYNC"
local rule = v["rule"]
if i == 1 then
initialrule = name
table.insert(builder, name) -- lpeg syntax
builder[name] = traverse(rule, istokenrule)
else
if not builder[name] then -- dont traverse rules for SKIP and SYNC twice
builder[name] = traverse(rule, istokenrule)
end
end
if buildast and not isfragment and not isspecial then
if istokenrule then
builder[name] = m.C(builder[name])
end
builder[name] = m.Ct(m.Cg(m.Cc(name),"rule") * builder[name])
end
end

if skipspaces then
builder[initialrule] = SKIP^0 * builder[initialrule] -- skip spaces at the beginning of the input
end
if recovery then
builder[initialrule] = buildrecovery(builder[initialrule]) -- build recovery on top of initial rule
end
return builder
end




local function build(ast, defs)
if defs then
definitions = defs
end
if isgrammar(ast) then
return traverse(ast)
else
SKIP = (Predef.space + Predef.nl)
skipspaces = true
SYNC = nil
recovery = true
SYNC = defaultsync(SKIP)
currentrule = ''
local res = SKIP ^0 * traverse(ast)
if buildast then
res = m.Ct(res)
end
return res -- input is not a grammar - skip spaces and sync by default
end
end



-- recovery grammar



Expand Down
4 changes: 2 additions & 2 deletions peg-parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ end

re.setlabels(labels)

function concat(a,b)
local function concat(a,b)
return a..b
end
function foldtable(action,t)
local function foldtable(action,t)
local re
local first = true
for key,value in pairs(t) do
Expand Down
4 changes: 3 additions & 1 deletion stack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- Lua 5.1 compatible
local unpack = unpack or table.unpack
-- GLOBAL
Stack = {}
local Stack = {}

-- Create a Table with stack functions
function Stack:Create()
Expand Down Expand Up @@ -62,4 +62,6 @@ function Stack:Create()
return t
end

return {Stack=Stack}

-- CHILLCODE™

0 comments on commit c71f836

Please sign in to comment.