| 1 |
require "luaprogtable-utility.lua" |
| 2 |
|
| 3 |
local _std_shape = {1, 1} |
| 4 |
|
| 5 |
function _lpt_parse_new_cell() |
| 6 |
local cur_cell = {data={}, shape=_std_shape} |
| 7 |
return cur_cell |
| 8 |
end |
| 9 |
|
| 10 |
function _lpt_parse_table_view(str) |
| 11 |
local str = _lpt_strip(str) |
| 12 |
|
| 13 |
local level = 0 |
| 14 |
local pos = 1 |
| 15 |
|
| 16 |
local cur_cell = nil |
| 17 |
local cur_row = {} |
| 18 |
local ret = {cur_row} |
| 19 |
|
| 20 |
function _add_str(s) |
| 21 |
if level > 0 then |
| 22 |
table.insert(cur_cell["data"], s) |
| 23 |
end |
| 24 |
end |
| 25 |
|
| 26 |
while pos <= str:len() do |
| 27 |
local chr = str:sub(pos, pos) |
| 28 |
local next_pos = pos + 1 |
| 29 |
|
| 30 |
if chr == "\\" then |
| 31 |
local next_chr = str:sub(pos + 1, pos + 1) |
| 32 |
if next_chr == nil then |
| 33 |
-- do nothing |
| 34 |
elseif next_chr == "\\" then |
| 35 |
if level == 0 then |
| 36 |
cur_row = {} |
| 37 |
-- change of row |
| 38 |
table.insert(ret, cur_row) |
| 39 |
end |
| 40 |
elseif next_chr == "{" or next_chr == "}" then |
| 41 |
next_pos = pos + 2 |
| 42 |
_add_str(str:sub(pos, pos + 1)) |
| 43 |
else |
| 44 |
_add_str(chr) |
| 45 |
end |
| 46 |
elseif chr == "{" then |
| 47 |
if level == 0 then |
| 48 |
cur_cell = _lpt_parse_new_cell() |
| 49 |
else |
| 50 |
_add_str(chr) |
| 51 |
end |
| 52 |
level = level + 1 |
| 53 |
elseif chr == "}" then |
| 54 |
level = level - 1 |
| 55 |
if level < 0 then |
| 56 |
raise_error("extra '}' found when parsing table view expression") |
| 57 |
elseif level == 0 then |
| 58 |
local data_str = table.concat(cur_cell["data"], "") |
| 59 |
cur_cell["data"] = data_str |
| 60 |
table.insert(cur_row, cur_cell) |
| 61 |
cur_cell = nil |
| 62 |
else |
| 63 |
_add_str(chr) |
| 64 |
end |
| 65 |
elseif chr == "[" then |
| 66 |
if level == 0 then |
| 67 |
local close = string.find(str, "]", pos + 1) |
| 68 |
|
| 69 |
if close == nil then |
| 70 |
raise_error("unable to find closing ']'") |
| 71 |
end |
| 72 |
|
| 73 |
local size_str = str:sub(pos + 1, close - 1) |
| 74 |
size_str = _lpt_strip(size_str) |
| 75 |
|
| 76 |
local size_1 = size_str:sub(1, 1) |
| 77 |
local shape = nil |
| 78 |
|
| 79 |
if size_1 == "|" then |
| 80 |
local nrow = _lpt_tonumber(size_str:sub(2)) |
| 81 |
shape = {nrow, 1} |
| 82 |
elseif size_1 == "-" then |
| 83 |
local ncol = _lpt_tonumber(size_str:sub(2)) |
| 84 |
shape = {1, ncol} |
| 85 |
else |
| 86 |
raise_error(string.format("invalid size '[%s]' in table view, size_str", size_str)) |
| 87 |
end |
| 88 |
|
| 89 |
if shape[1] < 1 or shape[2] < 1 then |
| 90 |
raise_error(string.format("invalid size '[%s]' in table view, size_str", size_str)) |
| 91 |
end |
| 92 |
|
| 93 |
local n_cell = #cur_row |
| 94 |
local cell = cur_row[n_cell] |
| 95 |
if cell == nil then |
| 96 |
raise_error(string.format("no suitable cell for size [%s]", size_str)) |
| 97 |
end |
| 98 |
cell["shape"] = shape |
| 99 |
|
| 100 |
pos = close + 1 |
| 101 |
else |
| 102 |
_add_str(chr) |
| 103 |
end |
| 104 |
else |
| 105 |
_add_str(chr) |
| 106 |
end |
| 107 |
|
| 108 |
pos = next_pos |
| 109 |
end |
| 110 |
|
| 111 |
if level ~= 0 then |
| 112 |
raise_error("extra '{' found when parsing table view expression") |
| 113 |
end |
| 114 |
|
| 115 |
return ret |
| 116 |
end |