summaryrefslogtreecommitdiff
path: root/mod/tex/context/third/rst/rst_helpers.lua
diff options
context:
space:
mode:
authorPhilipp Gesang <gesang@stud.uni-heidelberg.de>2013-03-12 18:33:19 +0100
committerPhilipp Gesang <gesang@stud.uni-heidelberg.de>2013-03-12 18:33:19 +0100
commit8026fb824e8c6505d61f2099642fa4d671198769 (patch)
tree963e6a3fbe3abc86b74e7a2bc74c74962e3f346f /mod/tex/context/third/rst/rst_helpers.lua
parent942daebfbd9ce27aa5208411d58226100a1b2935 (diff)
downloadcontext-rst-8026fb824e8c6505d61f2099642fa4d671198769.tar.gz
kill all the ipairs()
Diffstat (limited to 'mod/tex/context/third/rst/rst_helpers.lua')
-rw-r--r--mod/tex/context/third/rst/rst_helpers.lua93
1 files changed, 57 insertions, 36 deletions
diff --git a/mod/tex/context/third/rst/rst_helpers.lua b/mod/tex/context/third/rst/rst_helpers.lua
index 3ff53fe..936da97 100644
--- a/mod/tex/context/third/rst/rst_helpers.lua
+++ b/mod/tex/context/third/rst/rst_helpers.lua
@@ -8,7 +8,7 @@
--------------------------------------------------------------------------------
--
-local P, R, S, V, match
+local P, R, S, V, lpegmatch
= lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match
local C, Carg, Cb, Cc, Cg,
@@ -58,8 +58,8 @@ do
p.col_start = Cp() * p.dash_or_equals^1
p.col_stop = p.dash_or_equals^1 * Cp()
- p.column_starts = p.col_start * ( p.space^1 * p.col_start)^1
- p.column_stops = p.col_stop * ( p.space^1 * p.col_stop)^1
+ p.column_starts = Ct(p.col_start * ( p.space^1 * p.col_start)^1)
+ p.column_stops = Ct(p.col_stop * ( p.space^1 * p.col_stop)^1)
p.st_headsep = p.equals^1 * (p.space^1 * p.equals^1)^1
p.st_colspan = p.dash^1 * (p.space^1 * p.dash^1)^0 * p.space^0 * p.last
@@ -159,7 +159,9 @@ local function set_layout (line)
layout.widths = {}
layout.slices = {}
- for n, elm in ipairs(slice:match(line)) do
+ local elms = lpegmatch(slice, line)
+ for n=1, #elms do
+ local elm = elms[n]
layout.widths[n] = #elm
layout.slices[n] = elm
end
@@ -187,7 +189,8 @@ function helpers.table.create(raw)
local hc = helpers.cell
local rowcount = 0
local newtablayout = newtab.layout
- for nr, row in ipairs(raw) do
+ for nr=1, #raw do
+ local row = raw[nr]
newtab.rows[nr] = {}
local this_row = newtab.rows[nr]
this_row.sepline = p.sep_line:match(row)
@@ -244,11 +247,13 @@ function helpers.table.create(raw)
local oldrows = newtab.rows
local newrows = oldrows
- for nc, width in ipairs(newtablayout.widths) do
+ for nc=1, #newtablayout.widths do
+ local width = newtablayout.widths[nc]
-- this is gonna be extremely slow but at least it's readable
local newrow
local currentrow = 1
- for nr, row in ipairs(newrows) do
+ for nr=1, #newrows do
+ local row = newrows[nr]
local cell = row[nc]
dbg_write("nc: %s, nr:%2s | %9s | ", nc, nr,cell.variant)
if row.sepline or row.sephead
@@ -300,18 +305,22 @@ function helpers.table.create(raw)
newtab.__init()
- --newtab.__draw_debug = function()
- --for nr, row in ipairs(newtab.rows) do
- --for nc, cell in ipairs(row) do
- --local field = cell.variant:sub(1,7)
- --if cell.parent then
- --field = field .. string.format(" %s,%2s",cell.parent.x, cell.parent.y)
- --end
- --dbg_write("%12s | ", field)
- --end
- --dbg_write("\n")
- --end
- --end
+--[[
+ newtab.__draw_debug = function()
+ for nr=1, #newtab.rows do
+ local row = newtab.rows[nr]
+ for nc=1, #row do
+ local cell = row[nc]
+ local field = cell.variant:sub(1,7)
+ if cell.parent then
+ field = field .. string.format(" %s,%2s",cell.parent.x, cell.parent.y)
+ end
+ dbg_write("%12s | ", field)
+ end
+ dbg_write("\n")
+ end
+ end
+--]]
return newtab
end
@@ -330,13 +339,20 @@ end
-- Check the column boundaries of a simple table.
function helpers.get_st_boundaries (str)
- local p = helpers.patterns
- local starts, stops, slices = {}, {}, {}
- for n, elm in ipairs({ p.column_starts:match(str) }) do
+ local p_column_starts = helpers.patterns.column_starts
+ local p_column_stops = helpers.patterns.column_stops
+ local starts, stops, slices, elms = { }, { }, { }, nil
+
+ elms = lpegmatch(p_column_starts, str)
+ for n=1, #elms do
+ local elm = elms[n]
slices[n] = { start = elm }
starts[elm] = true
end
- for n, elm in ipairs({ p.column_stops :match(str) }) do
+
+ elms = lpegmatch(p_column_stops, str)
+ for n=1, #elms do
+ local elm = elms[n]
slices[n]["stop"] = elm
stops[elm] = true
end
@@ -349,9 +365,9 @@ function helpers.table.simple(raw)
local bounds = helpers.get_st_boundaries(raw[1])
local p = helpers.patterns
- for nr, row in ipairs(raw) do
+ for nr=1, #raw do
+ local row = raw[nr]
local newrow = {}
- local nc = 1
if not p.st_headsep:match(row) and
not p.st_colspan:match(row) then
local starts, stops = {}, {}
@@ -361,13 +377,15 @@ function helpers.table.simple(raw)
stops = p.st_span_stops :match(raw[nr+1])
check_span = true
else
- for colnr, slice in ipairs(bounds.slices) do
- starts[colnr] = slice.start
- stops [colnr] = slice.stop
+ for ncol=1, #bounds.slices do
+ local slice = bounds.slices[ncol]
+ starts[ncol] = slice.start
+ stops [ncol] = slice.stop
end
end
- for nc, start in ipairs(starts) do
+ for nc=1, #starts do
+ local start = starts[nc]
-- last column can exceed layout width
local stop = nc ~= #starts and stops[nc] or #row
local cell = {
@@ -377,18 +395,19 @@ function helpers.table.simple(raw)
cell.content = stringstrip(row:sub(start, stop))
if check_span then
local start_at, stop_at
- for colnr, slice in ipairs(bounds.slices) do
+ for ncol=1, #bounds.slices do
+ local slice = bounds.slices[ncol]
if slice.start == start then
- start_at = colnr
+ start_at = ncol
end
if start_at and
- not (colnr == #bounds.slices) then
+ not (ncol == #bounds.slices) then
if slice.stop == stop then
- stop_at = colnr
+ stop_at = ncol
break
end
else -- last column, width doesn't matter
- stop_at = colnr
+ stop_at = ncol
end
end
cell.span.x = 1 + stop_at - start_at
@@ -408,10 +427,12 @@ function helpers.table.simple(raw)
rows[nr] = newrow
end
- for nr, row in ipairs(rows) do
+ for nr=1, #rows do
+ local row = rows[nr]
if not row.ignore and row[1].content == "" then
row.ignore = true
- for nc, cell in ipairs(row) do
+ for nc=1, #row do
+ local cell = row[nc]
local par_row, par_col = helpers.table.resolve_parent(nr - 1, nc, rows)
parent = rows[par_row][par_col]
parent.content = parent.content .. " " .. cell.content