summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable-parse.lua
blob: 8947535a227b749e7d9a54b72b58f4ea070740b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
require "luaprogtable-utility.lua"

local _std_shape = {1, 1}

function _lpt_parse_new_cell()
    local cur_cell = {data={}, shape=_std_shape}
    return cur_cell
end

function _lpt_parse_table_view(str)
    local str = _lpt_strip(str)

    local level = 0
    local pos = 1

    local cur_cell = nil
    local cur_row = {}
    local ret = {cur_row}

    function _add_str(s)
        if level > 0 then
            table.insert(cur_cell["data"], s)
        end
    end

    while pos <= str:len() do
        local chr = str:sub(pos, pos)
        local next_pos = pos + 1

        if chr == "\\" then
            local next_chr = str:sub(pos + 1, pos + 1)
            if next_chr == nil then
                -- do nothing
            elseif next_chr == "\\" then
                if level == 0 then
                    cur_row = {}
                    -- change of row
                    table.insert(ret, cur_row)
                end
            elseif next_chr == "{"  or next_chr == "}" then
                next_pos = pos + 2
                _add_str(str:sub(pos, pos + 1))
            else
                _add_str(chr)
            end
        elseif chr == "{" then
            if level == 0 then
                cur_cell = _lpt_parse_new_cell()
            else
                _add_str(chr)
            end
            level = level + 1
        elseif chr == "}" then
            level = level - 1
            if level < 0 then
                raise_error("extra '}' found when parsing table view expression")
            elseif level == 0 then
                local data_str = table.concat(cur_cell["data"], "")
                cur_cell["data"] = data_str
                table.insert(cur_row, cur_cell)
                cur_cell = nil
            else
                _add_str(chr)
            end
        elseif chr == "[" then
            if level == 0 then
                local close = string.find(str, "]", pos + 1)

                if close == nil then
                    raise_error("unable to find closing ']'")
                end

                local size_str = str:sub(pos + 1, close - 1)
                size_str = _lpt_strip(size_str)
                
                local size_1 = size_str:sub(1, 1)
                local shape = nil

                if size_1 == "|" then
                    local nrow = _lpt_tonumber(size_str:sub(2))
                    shape = {nrow, 1}
                elseif size_1 == "-" then
                    local ncol = _lpt_tonumber(size_str:sub(2))
                    shape = {1, ncol}
                else
                    raise_error(string.format("invalid size '[%s]' in table view, size_str", size_str))
                end

                if shape[1] < 1 or shape[2] < 1 then
                    raise_error(string.format("invalid size '[%s]' in table view, size_str", size_str))
                end

                local n_cell = #cur_row
                local cell = cur_row[n_cell]
                if cell == nil then
                    raise_error(string.format("no suitable cell for size [%s]", size_str))
                end
                cell["shape"] = shape

                pos = close + 1
            else
                _add_str(chr)
            end
        else
            _add_str(chr)
        end

        pos = next_pos
    end

    if level ~= 0 then
        raise_error("extra '{' found when parsing table view expression")
    end

    return ret
end