]> git.lizzy.rs Git - hydra-dragonfire.git/blob - parse_spec.lua
Migrate to gopher-lua
[hydra-dragonfire.git] / parse_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
44                 if value_first and first:sub(1, 1) ~= "[" then
45                         return second, first
46                 else
47                         return first, second
48                 end
49         else
50                 return pair
51         end
52 end
53
54 function parse_spec(name, value_first)
55         local f = io.open("../spec/" .. name, "r")
56         local spec = {}
57         local top
58
59         for l in f:lines() do
60                 if l:sub(1, 1) == "\t" then
61                         local key, val = parse_pair(l:sub(2), value_first)
62
63                         if val then
64                                 top[key] = val
65                         elseif key then
66                                 table.insert(top, key)
67                         end
68                 else
69                         local key, val = parse_pair(l, value_first)
70
71                         if val then
72                                 spec[key] = val
73                         elseif key then
74                                 top = {}
75                                 spec[key] = top
76                         end
77                 end
78         end
79
80         f:close()
81         return spec
82 end
83
84 local casemap = parse_spec("casemap")
85
86 function camel_case(snake)
87         if casemap[snake] then
88                 return casemap[snake]
89         end
90
91         local camel = ""
92
93         while #snake > 0 do
94                 local idx = snake:find("_") or #snake + 1
95
96                 camel = camel
97                         .. snake:sub(1, 1):upper()
98                         .. snake:sub(2, idx - 1)
99
100                 snake = snake:sub(idx + 1)
101         end
102
103         return camel
104 end
105