forked from stm32duino/Arduino_Core_STM32
-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate_boards.bri
221 lines (182 loc) · 5.59 KB
/
generate_boards.bri
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#/usr/bin/env bright
/*
Module: generate_boards.bri
Function:
Generate boards.txt from templates
Copyright notice:
Copyright (c) 2018 MCCI Corporation.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Author:
Terry Moore, MCCI Corporation
*/
// fatal error
function Fatal(...)
{
local sMsg = call(format, arg);
write(_STDERR, sMsg .. "\n");
exit(1);
}
// hack parser of json for bright
function parseJson(sJson)
{
// this doesn't know about strings so will change brackets inside quotes.
sJson = gsub(sJson, "%[", "{");
sJson = gsub(sJson, "%]", "}");
sJson = gsub(sJson, "(\"[^\"]*\")%s*:", "[ %1 ] =");
sJson = "return "..sJson;
local tError = { ["msg"] = NULL };
local tResult = call(dostring, { sJson }, "x", function (sError) { %tError.msg = sError; });
if (tError.msg != NULL)
return NULL, tError.msg;
else
return tResult;
}
function ExpandSection(sTemplate, tBoard, tDefault, tTemplate)
{
local sNew;
sNew = gsub(
"\n"..sTemplate.."\n",
"\n%$%s*section%s+(%w+)([^\n]*)",
function (sSection, sJunk)
{
local tSections = {};
if (sJunk != "")
Fatal("extra text after \"$section %s\": %s", sSection, sJunk);
if (.extra_sections in %tBoard)
tSections = %tBoard.extra_sections;
else if (.extra_sections in %tDefault)
tSections = %tDefault.extra_sections;
for iSection = 0, getn(tSections)-1 do
{
if (sSection == tSections[iSection].name)
{
if ("using" in tSections[iSection])
{
local sUsing = tSections[iSection]["using"];
if (! (sUsing in %tTemplate))
{
foreach(%tTemplate, function(k,v) {write(_STDERR, k.."\n"); });
Fatal("Illegal section using reference: %%%%*%s ==> %s", sSection, sUsing);
}
sSection = sUsing;
}
else if (! (sSection in %tTemplate))
{
Fatal("Illegal section reference %%%%*%s", sSection);
}
return "\n"..strsub(%tTemplate[sSection], 0, -2);
}
}
// no section found by that name.
return "";
}
);
return strsub(sNew, 1, -2);
}
function ExpandTemplate(sTemplate, tBoard, tDefault, tStack)
{
local sNew, sOld;
if (tStack == NULL)
tStack = {};
sNew = sTemplate;
do {
sOld = sNew;
sNew = gsub(sNew, "{{([^}]*)}}",
function (sKey)
{
local sValue;
if (sKey in %tStack)
Fatal("recursive expansion of {{%s}}", sKey);
if (sKey in %tBoard)
sValue = %tBoard[sKey];
else if (sKey in %tDefault)
sValue = %tDefault[sKey];
else
Fatal("no value for {{%s}}", sKey);
%tStack[sKey] = 1;
local sResult = ExpandTemplate(sValue, %tBoard, %tDefault, %tStack);
tdelete(%tStack, sKey);
return sResult;
}
);
} while (sNew != sOld);
return sNew;
}
// parse template, return prefix and boards section
function ParseTemplate(sTemplate, sFile)
{
local tResult = {};
// Many editors these days don't append newline to a file;
// rather than fool with the regex, we just add an extra line
// to the template before matching. This is OK because
// everything outside tags is supposed to be discarded.
// search for matched-pair of tags, and call function for
// each pair found.
gsub(sTemplate .. "\n", "%%%%%+([%w_]+)\n(.-\n)%%%%%-([%w_]+)\n",
function(sKey, sBody, sCloseKey)
{
if (sKey != sCloseKey)
Fatal("template section %s end tag mismatch (found %%%%-%s): %s", sKey, sCloseKey, %sFile)
%tResult[sKey] = sBody;
}
);
return tResult;
}
// main: arg1 is template file, arg2 is json params file
function main(args)
{
if (getn(args) < 3)
Fatal("usage: {template} {json}");
local sTemplateFile = args[1];
local sJsonFile = args[2];
local hTemplate, sError = openfile(sTemplateFile, "r");
if (sError != NULL)
Fatal("can't open template file %s: %s", sTemplateFile, sError);
local hJson;
hJson, sError = openfile(sJsonFile, "r");
if (sError != NULL)
Fatal("can't open json file %s: %s", sJsonFile, sError);
local sTemplate;
sTemplate, sError = read(hTemplate, "*a");
if (sError != NULL)
Fatal("error reading %s: %s", sTemplateFile, sError);
local tTemplate = ParseTemplate(sTemplate, sTemplateFile);
if (! (.prefix in tTemplate))
Fatal("no %%%%prefix section in %s", sTemplateFile)
if (! (.boards in tTemplate))
Fatal("no %%%%boards section in %s", sTemplateFile);
local sJson;
sJson, sError = read(hJson, "*a");
if (sError != NULL)
Fatal("error reading %s: %s", sJsonFile, sError);
/* parse the JSON file */
local tJson;
tJson, sError = parseJson(sJson);
if (sError != NULL)
{
write(_STDERR, sJson);
Fatal("error parsing JSON file %s: %s", sJsonFile, sError);
}
local tDefaults = {};
if (.defaults in tJson)
tDefaults = tJson.defaults;
if (! (.boards in tJson))
Fatal("no boards object in JSON file %s", sJsonFile);
write(tTemplate.prefix);
for i = 0, getn(tJson.boards)-1 do
{
local sTemplate = ExpandSection(tTemplate.boards, tJson.boards[i], tDefaults, tTemplate);
write(ExpandTemplate(sTemplate, tJson.boards[i], tDefaults));
}
exit(0);
}