1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # Copyright (c) 2009 Martin Decky
|
---|
4 | # All rights reserved.
|
---|
5 | #
|
---|
6 | # Redistribution and use in source and binary forms, with or without
|
---|
7 | # modification, are permitted provided that the following conditions
|
---|
8 | # are met:
|
---|
9 | #
|
---|
10 | # - Redistributions of source code must retain the above copyright
|
---|
11 | # notice, this list of conditions and the following disclaimer.
|
---|
12 | # - Redistributions in binary form must reproduce the above copyright
|
---|
13 | # notice, this list of conditions and the following disclaimer in the
|
---|
14 | # documentation and/or other materials provided with the distribution.
|
---|
15 | # - The name of the author may not be used to endorse or promote products
|
---|
16 | # derived from this software without specific prior written permission.
|
---|
17 | #
|
---|
18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | #
|
---|
29 | """
|
---|
30 | HelenOS Architecture Description Language and Behavior Protocols preprocessor
|
---|
31 | """
|
---|
32 |
|
---|
33 | import sys
|
---|
34 | import os
|
---|
35 |
|
---|
36 | def usage(prname):
|
---|
37 | "Print usage syntax"
|
---|
38 | print prname + " <OUTPUT>"
|
---|
39 |
|
---|
40 | def tabs(cnt):
|
---|
41 | "Return given number of tabs"
|
---|
42 |
|
---|
43 | return ("\t" * cnt)
|
---|
44 |
|
---|
45 | def cond_append(tokens, token, trim):
|
---|
46 | "Conditionally append token to tokens with trim"
|
---|
47 |
|
---|
48 | if (trim):
|
---|
49 | token = token.strip(" \t")
|
---|
50 |
|
---|
51 | if (token != ""):
|
---|
52 | tokens.append(token)
|
---|
53 |
|
---|
54 | return tokens
|
---|
55 |
|
---|
56 | def split_tokens(string, delimiters, trim = False, separate = False):
|
---|
57 | "Split string to tokens by delimiters, keep the delimiters"
|
---|
58 |
|
---|
59 | tokens = []
|
---|
60 | last = 0
|
---|
61 | i = 0
|
---|
62 |
|
---|
63 | while (i < len(string)):
|
---|
64 | for delim in delimiters:
|
---|
65 | if (len(delim) > 0):
|
---|
66 |
|
---|
67 | if (string[i:(i + len(delim))] == delim):
|
---|
68 | if (separate):
|
---|
69 | tokens = cond_append(tokens, string[last:i], trim)
|
---|
70 | tokens = cond_append(tokens, delim, trim)
|
---|
71 | last = i + len(delim)
|
---|
72 | elif (i > 0):
|
---|
73 | tokens = cond_append(tokens, string[last:i], trim)
|
---|
74 | last = i
|
---|
75 |
|
---|
76 | i += len(delim) - 1
|
---|
77 | break
|
---|
78 |
|
---|
79 | i += 1
|
---|
80 |
|
---|
81 | tokens = cond_append(tokens, string[last:len(string)], trim)
|
---|
82 |
|
---|
83 | return tokens
|
---|
84 |
|
---|
85 | def preproc_bp(outname, tokens):
|
---|
86 | "Preprocess tentative statements in Behavior Protocol"
|
---|
87 |
|
---|
88 | result = []
|
---|
89 | i = 0
|
---|
90 |
|
---|
91 | while (i < len(tokens)):
|
---|
92 | if (tokens[i] == "tentative"):
|
---|
93 | if ((i + 1 < len(tokens)) and (tokens[i + 1] == "{")):
|
---|
94 | i += 2
|
---|
95 | start = i
|
---|
96 | level = 1
|
---|
97 |
|
---|
98 | while ((i < len(tokens)) and (level > 0)):
|
---|
99 | if (tokens[i] == "{"):
|
---|
100 | level += 1
|
---|
101 | elif (tokens[i] == "}"):
|
---|
102 | level -= 1
|
---|
103 |
|
---|
104 | i += 1
|
---|
105 |
|
---|
106 | if (level == 0):
|
---|
107 | result.append("(")
|
---|
108 | result.extend(preproc_bp(outname, tokens[start:(i - 1)]))
|
---|
109 | result.append(")")
|
---|
110 | result.append("+")
|
---|
111 | result.append("NULL")
|
---|
112 | else:
|
---|
113 | print "%s: Syntax error in tentative statement" % outname
|
---|
114 | else:
|
---|
115 | print "%s: Unexpected tentative statement" % outname
|
---|
116 | else:
|
---|
117 | result.append(tokens[i])
|
---|
118 |
|
---|
119 | i += 1
|
---|
120 |
|
---|
121 | return result
|
---|
122 |
|
---|
123 | def parse_bp(base, root, inname, nested, outname, outf, indent):
|
---|
124 | "Parse Behavior Protocol"
|
---|
125 |
|
---|
126 | if (nested):
|
---|
127 | if (inname[0:1] == "/"):
|
---|
128 | path = os.path.join(base, ".%s" % inname)
|
---|
129 | nested_root = os.path.dirname(path)
|
---|
130 | else:
|
---|
131 | path = os.path.join(root, inname)
|
---|
132 | nested_root = root
|
---|
133 |
|
---|
134 | if (not os.path.isfile(path)):
|
---|
135 | print "%s: Unable to include file %s" % (outname, path)
|
---|
136 | return True
|
---|
137 |
|
---|
138 | inf = file(path, "r")
|
---|
139 | else:
|
---|
140 | inf = file(inname, "r")
|
---|
141 | nested_root = root
|
---|
142 |
|
---|
143 | tokens = preproc_bp(outname, split_tokens(inf.read(), ["\n", " ", "\t", "(", ")", "{", "}", "[", "]", "/*", "*/", "#", "*", ";", "+", "||", "|", "!", "?"], True, True))
|
---|
144 |
|
---|
145 | inc = False
|
---|
146 | empty = True
|
---|
147 | comment = False
|
---|
148 | lcomment = False
|
---|
149 |
|
---|
150 | for token in tokens:
|
---|
151 | if (comment):
|
---|
152 | if (token == "*/"):
|
---|
153 | comment = False
|
---|
154 | continue
|
---|
155 |
|
---|
156 | if ((not comment) and (token == "/*")):
|
---|
157 | comment = True
|
---|
158 | continue
|
---|
159 |
|
---|
160 | if (lcomment):
|
---|
161 | if (token == "\n"):
|
---|
162 | lcomment = False
|
---|
163 | continue
|
---|
164 |
|
---|
165 | if ((not lcomment) and (token == "#")):
|
---|
166 | lcomment = True
|
---|
167 | continue
|
---|
168 |
|
---|
169 | if (token == "\n"):
|
---|
170 | continue
|
---|
171 |
|
---|
172 | if (empty):
|
---|
173 | empty = False
|
---|
174 |
|
---|
175 | if (inc):
|
---|
176 | outf.write("\n%s(" % tabs(indent))
|
---|
177 |
|
---|
178 | inc_empty = parse_bp(base, nested_root, token, True, outname, outf, indent + 1)
|
---|
179 | if (inc_empty):
|
---|
180 | outf.write("\n%sNULL" % tabs(indent + 1))
|
---|
181 |
|
---|
182 | outf.write("\n%s)" % tabs(indent))
|
---|
183 | inc = False
|
---|
184 | continue
|
---|
185 |
|
---|
186 | if ((token == ";") or (token == "+") or (token == "||") or (token == "|")):
|
---|
187 | outf.write(" %s" % token)
|
---|
188 | elif (token == "["):
|
---|
189 | inc = True
|
---|
190 | elif (token == "]"):
|
---|
191 | inc = False
|
---|
192 | elif (token == "("):
|
---|
193 | outf.write("\n%s%s" % (tabs(indent), token))
|
---|
194 | indent += 1
|
---|
195 | elif (token == ")"):
|
---|
196 | if (indent == 0):
|
---|
197 | print "%s: Too many closing parentheses" % outname
|
---|
198 |
|
---|
199 | indent -= 1
|
---|
200 | outf.write("\n%s%s" % (tabs(indent), token))
|
---|
201 | elif (token == "{"):
|
---|
202 | outf.write(" %s" % token)
|
---|
203 | indent += 1
|
---|
204 | elif (token == "}"):
|
---|
205 | if (indent == 0):
|
---|
206 | print "%s: Too many closing parentheses" % outname
|
---|
207 |
|
---|
208 | indent -= 1
|
---|
209 | outf.write("\n%s%s" % (tabs(indent), token))
|
---|
210 | elif (token == "*"):
|
---|
211 | outf.write("%s" % token)
|
---|
212 | elif ((token == "!") or (token == "?") or (token == "NULL")):
|
---|
213 | outf.write("\n%s%s" % (tabs(indent), token))
|
---|
214 | else:
|
---|
215 | outf.write("%s" % token)
|
---|
216 |
|
---|
217 | inf.close()
|
---|
218 |
|
---|
219 | return empty
|
---|
220 |
|
---|
221 | def parse_adl(base, root, inname, nested, outname, outf, indent):
|
---|
222 | "Parse Architecture Description Language"
|
---|
223 |
|
---|
224 | if (nested):
|
---|
225 | (infname, inarg) = inname.split("%")
|
---|
226 |
|
---|
227 | if (infname[0:1] == "/"):
|
---|
228 | path = os.path.join(base, ".%s" % infname)
|
---|
229 | nested_root = os.path.dirname(path)
|
---|
230 | else:
|
---|
231 | path = os.path.join(root, infname)
|
---|
232 | nested_root = root
|
---|
233 |
|
---|
234 | if (not os.path.isfile(path)):
|
---|
235 | print "%s: Unable to include file %s" % (outname, path)
|
---|
236 | return True
|
---|
237 |
|
---|
238 | inf = file(path, "r")
|
---|
239 | else:
|
---|
240 | inarg = "%%"
|
---|
241 | inf = file(inname, "r")
|
---|
242 | nested_root = root
|
---|
243 |
|
---|
244 | tokens = split_tokens(inf.read(), ["\n", " ", "\t", "%%", "[", "]"], False, True)
|
---|
245 |
|
---|
246 | inc = False
|
---|
247 | empty = True
|
---|
248 | newline = True
|
---|
249 | locindent = 0
|
---|
250 |
|
---|
251 | for token in tokens:
|
---|
252 | if (empty):
|
---|
253 | empty = False
|
---|
254 |
|
---|
255 | if (inc):
|
---|
256 | if (token.find("%") != -1):
|
---|
257 | parse_adl(base, nested_root, token, True, outname, outf, locindent)
|
---|
258 | else:
|
---|
259 | parse_bp(base, nested_root, token, True, outname, outf, locindent)
|
---|
260 |
|
---|
261 | inc = False
|
---|
262 | continue
|
---|
263 |
|
---|
264 | if (token == "\n"):
|
---|
265 | newline = True
|
---|
266 | locindent = 0
|
---|
267 | outf.write("\n%s" % tabs(indent))
|
---|
268 | elif (token == "\t"):
|
---|
269 | if (newline):
|
---|
270 | locindent += 1
|
---|
271 | outf.write("%s" % token)
|
---|
272 | elif (token == "%%"):
|
---|
273 | newline = False
|
---|
274 | outf.write("%s" % inarg)
|
---|
275 | elif (token == "["):
|
---|
276 | newline = False
|
---|
277 | inc = True
|
---|
278 | elif (token == "]"):
|
---|
279 | newline = False
|
---|
280 | inc = False
|
---|
281 | else:
|
---|
282 | newline = False;
|
---|
283 | outf.write("%s" % token)
|
---|
284 |
|
---|
285 | inf.close()
|
---|
286 |
|
---|
287 | return empty
|
---|
288 |
|
---|
289 | def open_bp(base, root, inname, outname):
|
---|
290 | "Open Behavior Protocol file"
|
---|
291 |
|
---|
292 | outf = file(outname, "w")
|
---|
293 |
|
---|
294 | outf.write("### %s\n" % inname)
|
---|
295 |
|
---|
296 | empty = parse_bp(base, root, inname, False, outname, outf, 0)
|
---|
297 | if (empty):
|
---|
298 | outf.write("NULL")
|
---|
299 |
|
---|
300 | outf.close()
|
---|
301 |
|
---|
302 | def open_adl(base, root, inname, outname):
|
---|
303 | "Open Architecture Description file"
|
---|
304 |
|
---|
305 | outf = file(outname, "w")
|
---|
306 |
|
---|
307 | empty = parse_adl(base, root, inname, False, outname, outf, 0)
|
---|
308 | if (empty):
|
---|
309 | outf.write("/* Empty */")
|
---|
310 |
|
---|
311 | outf.close()
|
---|
312 |
|
---|
313 | def recursion(base, root, output, level):
|
---|
314 | "Recursive directory walk"
|
---|
315 |
|
---|
316 | for name in os.listdir(root):
|
---|
317 | canon = os.path.join(root, name)
|
---|
318 |
|
---|
319 | if (os.path.isfile(canon)):
|
---|
320 | fcomp = split_tokens(canon, ["."])
|
---|
321 | cname = canon.split("/")
|
---|
322 |
|
---|
323 | filtered = False
|
---|
324 | while (not filtered):
|
---|
325 | try:
|
---|
326 | cname.remove(".")
|
---|
327 | except (ValueError):
|
---|
328 | filtered = True
|
---|
329 |
|
---|
330 | output_path = os.path.join(output, ".".join(cname))
|
---|
331 |
|
---|
332 | if (fcomp[-1] == ".bp"):
|
---|
333 | open_bp(base, root, canon, output_path)
|
---|
334 | elif (fcomp[-1] == ".adl"):
|
---|
335 | open_adl(base, root, canon, output_path)
|
---|
336 |
|
---|
337 | if (os.path.isdir(canon)):
|
---|
338 | recursion(base, canon, output, level + 1)
|
---|
339 |
|
---|
340 | def main():
|
---|
341 | if (len(sys.argv) < 2):
|
---|
342 | usage(sys.argv[0])
|
---|
343 | return
|
---|
344 |
|
---|
345 | path = os.path.abspath(sys.argv[1])
|
---|
346 | if (not os.path.isdir(path)):
|
---|
347 | print "Error: <OUTPUT> is not a directory"
|
---|
348 | return
|
---|
349 |
|
---|
350 | recursion(".", ".", path, 0)
|
---|
351 |
|
---|
352 | if __name__ == '__main__':
|
---|
353 | main()
|
---|