|
| 1 | +local res = {} |
| 2 | + |
| 3 | +local function is_valid_ident(ident) |
| 4 | + local keyword = { |
| 5 | + ['do'] = true, |
| 6 | + ['end'] = true, |
| 7 | + ['if'] = true, |
| 8 | + ['then'] = true, |
| 9 | + ['local'] = true, |
| 10 | + ['function'] = true, |
| 11 | + ['return'] = true, |
| 12 | + ['while'] = true, |
| 13 | + ['repeat'] = true, |
| 14 | + ['until'] = true, |
| 15 | + ['for'] = true, |
| 16 | + ['in'] = true, |
| 17 | + ['true'] = true, |
| 18 | + ['false'] = true, |
| 19 | + ['nil'] = true, |
| 20 | + } |
| 21 | + |
| 22 | + if type(ident) ~= 'string' or keyword[ident] then |
| 23 | + return false |
| 24 | + end |
| 25 | + |
| 26 | + return ident:find('^[_%a][_%w]*$') ~= nil |
| 27 | +end |
| 28 | + |
| 29 | +local function set(cssvar, v) |
| 30 | + local before, last = cssvar:match('^(.+)%-+(.+)$') |
| 31 | + |
| 32 | + -- Top-level key |
| 33 | + if not last then |
| 34 | + res[tonumber(cssvar) or cssvar] = v |
| 35 | + return |
| 36 | + end |
| 37 | + |
| 38 | + last = tonumber(last) or last |
| 39 | + local cur = res |
| 40 | + for k in before:gmatch('[^%-_]+') do |
| 41 | + k = tonumber(k) or k |
| 42 | + cur[k] = cur[k] or {} |
| 43 | + cur = cur[k] |
| 44 | + end |
| 45 | + |
| 46 | + -- Path is too short: append `default` |
| 47 | + if type(cur[last]) == 'table' then |
| 48 | + cur, last = cur[last], 'default' |
| 49 | + end |
| 50 | + |
| 51 | + -- Check duplicates |
| 52 | + assert(cur[last] == nil or cur[last] == v) |
| 53 | + |
| 54 | + cur[last] = v |
| 55 | +end |
| 56 | + |
| 57 | +local function print_recur(value, _ind) |
| 58 | + _ind = _ind or 0 |
| 59 | + |
| 60 | + if type(value) == 'table' then |
| 61 | + io.write('m {') |
| 62 | + _ind = _ind + 2 |
| 63 | + |
| 64 | + for k, v in pairs(value) do |
| 65 | + local fmt = '[%q] = ' |
| 66 | + if type(k) == 'number' then |
| 67 | + fmt = '[%s] = ' |
| 68 | + elseif is_valid_ident(k) then |
| 69 | + fmt = '%s = ' |
| 70 | + end |
| 71 | + io.write(('\n%s' .. fmt):format((' '):rep(_ind), k)) |
| 72 | + print_recur(v, _ind) |
| 73 | + io.write(',') |
| 74 | + end |
| 75 | + |
| 76 | + _ind = _ind - 2 |
| 77 | + io.write(('\n%s}'):format((' '):rep(_ind))) |
| 78 | + else |
| 79 | + io.write(('%q'):format(value)) |
| 80 | + end |
| 81 | +end |
| 82 | + |
| 83 | +local defs = {} |
| 84 | +for ln in io.lines() do |
| 85 | + local k, v = ln:match('^%s*%-%-(%w.-)%s*:%s*(.-)%s*;%s*$') |
| 86 | + if k then |
| 87 | + table.insert(defs, { k, v }) |
| 88 | + end |
| 89 | +end |
| 90 | + |
| 91 | +-- Since we are un-flattening, ensure that longer keys (whose prefix could |
| 92 | +-- match another key) are visited first. |
| 93 | +table.sort(defs, function(a, b) |
| 94 | + return a[1] > b[1] |
| 95 | +end) |
| 96 | + |
| 97 | +for _, kv in ipairs(defs) do |
| 98 | + set(unpack(kv)) |
| 99 | +end |
| 100 | + |
| 101 | +-- Add `scale` key for convenience and backwards-compatibility |
| 102 | +assert(res.scale == nil) |
| 103 | +res.scale = {} |
| 104 | +for color, scale in pairs(res.base.color) do |
| 105 | + if type(scale) == 'table' then |
| 106 | + res.scale[color] = {} |
| 107 | + for i, v in pairs(scale) do |
| 108 | + res.scale[color][i + 1] = v |
| 109 | + end |
| 110 | + else |
| 111 | + res.scale[color] = scale |
| 112 | + end |
| 113 | +end |
| 114 | + |
| 115 | +-- NOTE: the metatable `mt` helps to catch errors (e.g. during CI tests) |
| 116 | +io.write([=[ |
| 117 | +local mt = { |
| 118 | + __index = function(_, k) |
| 119 | + error('invalid index: ' .. k) |
| 120 | + end, |
| 121 | +} |
| 122 | +---@generic T |
| 123 | +---@param tbl T |
| 124 | +---@return T |
| 125 | +local function m(tbl) |
| 126 | + return setmetatable(tbl, mt) |
| 127 | +end |
| 128 | +local M = ]=]) |
| 129 | + |
| 130 | +print_recur(res) |
| 131 | + |
| 132 | +io.write('\n') |
0 commit comments