-
Notifications
You must be signed in to change notification settings - Fork 9
/
xml2c
executable file
·201 lines (194 loc) · 7.11 KB
/
xml2c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/lua
-- XML Parser based on code by Alexander Makeev
XmlParser = {};
function XmlParser:FromXmlString(value)
value = string.gsub(value, "&#x([%x]+)%;",
function(h)
return string.char(tonumber(h,16))
end);
value = string.gsub(value, "&#([0-9]+)%;",
function(h)
return string.char(tonumber(h,10))
end);
value = string.gsub (value, """, "\"");
value = string.gsub (value, "'", "'");
value = string.gsub (value, ">", ">");
value = string.gsub (value, "<", "<");
value = string.gsub (value, "&", "&");
return value;
end
function XmlParser:ParseArgs(s)
local arg = {}
string.gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a)
arg[w] = self:FromXmlString(a);
end)
return arg
end
function XmlParser:ParseXmlText(xmlText)
local stack = {}
local top = {Name=nil,Value=nil,Attributes={},ChildNodes={}}
table.insert(stack, top)
local ni,c,label,xarg, empty
local i, j = 1, 1
while true do
ni,j,c,label,xarg, empty = string.find(xmlText, "<(%/?)([%w:]+)(.-)(%/?)>", i)
if not ni then break end
if empty == "/" then -- empty element tag
table.insert(top.ChildNodes, {Name=label,Value=nil,Attributes=self:ParseArgs(xarg),ChildNodes=nil})
elseif c == "" then -- start tag
top = {Name=label, Value=nil, Attributes=self:ParseArgs(xarg), ChildNodes={}}
table.insert(stack, top) -- new level
else -- end tag
local toclose = table.remove(stack) -- remove top
top = stack[#stack]
table.insert(top.ChildNodes, toclose)
end
i = j+1
end
return stack[1].ChildNodes[1];
end
function XmlParser:ParseXmlFile(xmlFileName)
local hFile,err = io.open(xmlFileName,"r");
if (not err) then
local xmlText=hFile:read("*a"); -- read file content
io.close(hFile);
return self:ParseXmlText(xmlText),nil;
else
return nil,err;
end
end
function generate_help(tbl, path, depth)
if not depth or depth == 0 then
depth = 0
end
if not path then path = "" end
if tbl["Name"] == "NODE" and
tbl["Attributes"] and tbl["Attributes"]["name"] then
name = tbl["Attributes"]["name"]
path = path .. "/" .. name
path = path:gsub("*", "-")
if tbl["Attributes"]["help"] then
help = "- " .. tbl["Attributes"]["help"]
print(string.format(" %-40s %s", path, help))
else
print(string.format(" %s", path))
end
end
if tbl["Name"] == "VALUE" and
tbl["Attributes"] and tbl["Attributes"]["name"] then
if tbl["Attributes"]["help"] and tbl["Attributes"]["value"] then
help = "- " .. tbl["Attributes"]["value"] .. " = " .. tbl["Attributes"]["help"]
print(string.format(" %-40s %s", " ", help))
end
end
if tbl["ChildNodes"] ~= nil then
for i,field in pairs(tbl["ChildNodes"]) do
generate_help(field, path, depth+1);
end
end
end
function generate_header(xml,filename)
path,name,type = filename:match("(.-)([^\\/]-%.?([^%.\\/]*))$")
module = name:gsub(".xml", "")
module_macro = module:gsub("-", "_")
print(string.format([[
/**
* @file %s.h
* Apteryx API for %s
]], module,module:sub(1,1):upper()..module:sub(2)))
generate_help(xml)
print(string.format([[
*/
#ifndef __%s_APTERYX_SCHEMA_H__
#define __%s_APTERYX_SCHEMA_H__
]], module_macro:upper(),module_macro:upper()))
end
function generate_defines(tbl, path, depth)
if not depth then depth = 0 end
if not path then path = "" end
if tbl["Name"] ~= "MODULE" and tbl["Attributes"] and tbl["Attributes"]["name"] then
name = tbl["Attributes"]["name"]
path = path .. "/" .. name
def = string.sub(string.upper(path), 2)
def = string.gsub(def, "/", "_")
def = string.gsub(def, "-", "_")
def = string.gsub(def, "*_", "")
if name ~= "*" then
_,wildcards = string.gsub(path, "*", "*")
if tbl["Name"] == "VALUE" then
start = def
if tonumber(tbl["Attributes"]["value"]) ~= nil then
finish = string.rep(" ",1+wildcards)..tbl["Attributes"]["value"]
else
finish = string.rep(" ",1+wildcards).."\""..tbl["Attributes"]["value"].."\""
end
elseif wildcards > 0 then
start = def
finish = string.rep(" ",wildcards).."\""..path:match("/[^\\*]*$"):sub(2).."\""
elseif tbl["ChildNodes"] ~= nil then
start = def .. "_PATH"
finish = "\"" .. path .. "\""
else
start = def
finish = "\"" .. path .. "\""
end
print(string.format("#define %-50s %s", start, finish))
end
if tbl["Attributes"]["help"] then
start = def .. "_HELP"
finish = "\"" .. tbl["Attributes"]["help"] .. "\""
end
if tbl["Attributes"]["default"] then
start = def .. "_DEFAULT"
if tonumber(tbl["Attributes"]["default"]) ~= nil then
finish = tbl["Attributes"]["default"]
else
finish = "\"" .. tbl["Attributes"]["default"] .. "\""
end
if string.find(path, "*") then
print(string.format("#define %-50s %s", start, finish))
else
print(string.format("#define %-50s %s", start, finish))
end
end
if tbl["Attributes"]["pattern"] then
-- Just match on patterns with a single range at the start of the pattern.
if string.find (tbl["Attributes"]["pattern"], "%^{{range") ~= nil and
string.find (tbl["Attributes"]["pattern"], "range", 8) == nil then
min, max = string.match(tbl["Attributes"]["pattern"], "([^(,]+),([^,)]+)")
min_start = def .. "_MIN_VALUE"
min_finish = "\"" .. min .. "\""
max_start = def .. "_MAX_VALUE"
max_finish = "\"" .. max .. "\""
if string.find(path, "*") then
print(string.format("#define %-50s %s", min_start, min_finish))
print(string.format("#define %-50s %s", max_start, max_finish))
else
print(string.format("#define %-50s %s", min_start, min_finish))
print(string.format("#define %-50s %s", max_start, max_finish))
end
end
end
end
if tbl["ChildNodes"] ~= nil then
for i,field in pairs(tbl["ChildNodes"]) do
generate_defines(field, path, depth+1);
end
end
end
function generate_footer(filename)
path,name,type = filename:match("(.-)([^\\/]-%.?([^%.\\/]*))$")
module = name:gsub(".xml", "")
print(string.format([[
#endif /*__%s_APTERYX_SCHEMA_H__*/
]], module_macro:upper()))
end
if arg[1] == nil then
print("xml2c <xml schema file>")
return -1
end
filename = arg[1]
local xml = XmlParser:ParseXmlFile(filename)
generate_header(xml,filename)
generate_defines(xml)
generate_footer(filename)