]> git.lizzy.rs Git - hydra-dragonfire.git/blob - convert/spec.lua
Add map component
[hydra-dragonfire.git] / convert / spec.lua
1 local function snext(t, state)
2         local key
3
4         if state == nil then
5                 t.__sorted = {}
6                 for k in pairs(t) do
7                         if k ~= "__sorted" then
8                                 table.insert(t.__sorted, k)
9                         end
10                 end
11                 table.sort(t.__sorted)
12
13                 key = t.__sorted[1]
14         else
15                 for i, v in ipairs(t.__sorted) do
16                         if v == state then
17                                 key = t.__sorted[i + 1]
18                                 break
19                         end
20                 end
21         end
22
23         if key then
24                 return key, t[key]
25         end
26
27         t.__sorted = nil
28 end
29
30 function spairs(t)
31         return snext, t, nil
32 end
33
34 local function parse_pair(pair, value_first)
35         if pair:sub(1, 1) == "#" then
36                 return
37         end
38
39         local idx = pair:find(" ")
40
41         if idx then
42                 local first, second = pair:sub(1, idx - 1), pair:sub(idx + 1)
43                 local c = first:sub(1, 1)
44
45                 if value_first and c ~= "{" and c ~= "%" then
46                         return second, first
47                 else
48                         return first, second
49                 end
50         else
51                 return pair
52         end
53 end
54
55 function parse_spec(name, value_first)
56         local f = io.open("spec/" .. name, "r")
57         local spec = {}
58         local top
59
60         for l in f:lines() do
61                 if l:sub(1, 1) == "\t" then
62                         local key, val = parse_pair(l:sub(2), value_first)
63
64                         if val then
65                                 top[key] = val
66                         elseif key then
67                                 table.insert(top, key)
68                         end
69                 else
70                         local key, val = parse_pair(l, value_first)
71
72                         if val then
73                                 spec[key] = val
74                         elseif key then
75                                 top = {}
76                                 spec[key] = top
77                         end
78                 end
79         end
80
81         f:close()
82         return spec
83 end
84
85 local casemap = parse_spec("casemap")
86
87 function camel_case(snake)
88         if casemap[snake] then
89                 return casemap[snake]
90         end
91
92         local camel = ""
93
94         while #snake > 0 do
95                 local idx = snake:find("_") or #snake + 1
96
97                 camel = camel
98                         .. snake:sub(1, 1):upper()
99                         .. snake:sub(2, idx - 1)
100
101                 snake = snake:sub(idx + 1)
102         end
103
104         return camel
105 end
106
107 function apply_prefix(fields, str)
108         return (fields.prefix or "") .. camel_case(str) .. (fields.postfix or "")
109 end