| 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 | INC, POST_INC, BLOCK_COMMENT, LINE_COMMENT, SYSTEM, ARCH, HEAD, BODY, NULL, \
|
|---|
| 37 | INST, VAR, FIN, BIND, TO, SUBSUME, DELEGATE, IFACE, PROTOTYPE, PAR_LEFT, \
|
|---|
| 38 | PAR_RIGHT, SIGNATURE, PROTOCOL, FRAME, PROVIDES, REQUIRES = range(25)
|
|---|
| 39 |
|
|---|
| 40 | def usage(prname):
|
|---|
| 41 | "Print usage syntax"
|
|---|
| 42 |
|
|---|
| 43 | print "%s <OUTPUT>" % prname
|
|---|
| 44 |
|
|---|
| 45 | def tabs(cnt):
|
|---|
| 46 | "Return given number of tabs"
|
|---|
| 47 |
|
|---|
| 48 | return ("\t" * cnt)
|
|---|
| 49 |
|
|---|
| 50 | def cond_append(tokens, token, trim):
|
|---|
| 51 | "Conditionally append token to tokens with trim"
|
|---|
| 52 |
|
|---|
| 53 | if (trim):
|
|---|
| 54 | token = token.strip(" \t")
|
|---|
| 55 |
|
|---|
| 56 | if (token != ""):
|
|---|
| 57 | tokens.append(token)
|
|---|
| 58 |
|
|---|
| 59 | return tokens
|
|---|
| 60 |
|
|---|
| 61 | def split_tokens(string, delimiters, trim = False, separate = False):
|
|---|
| 62 | "Split string to tokens by delimiters, keep the delimiters"
|
|---|
| 63 |
|
|---|
| 64 | tokens = []
|
|---|
| 65 | last = 0
|
|---|
| 66 | i = 0
|
|---|
| 67 |
|
|---|
| 68 | while (i < len(string)):
|
|---|
| 69 | for delim in delimiters:
|
|---|
| 70 | if (len(delim) > 0):
|
|---|
| 71 |
|
|---|
| 72 | if (string[i:(i + len(delim))] == delim):
|
|---|
| 73 | if (separate):
|
|---|
| 74 | tokens = cond_append(tokens, string[last:i], trim)
|
|---|
| 75 | tokens = cond_append(tokens, delim, trim)
|
|---|
| 76 | last = i + len(delim)
|
|---|
| 77 | elif (i > 0):
|
|---|
| 78 | tokens = cond_append(tokens, string[last:i], trim)
|
|---|
| 79 | last = i
|
|---|
| 80 |
|
|---|
| 81 | i += len(delim) - 1
|
|---|
| 82 | break
|
|---|
| 83 |
|
|---|
| 84 | i += 1
|
|---|
| 85 |
|
|---|
| 86 | tokens = cond_append(tokens, string[last:len(string)], trim)
|
|---|
| 87 |
|
|---|
| 88 | return tokens
|
|---|
| 89 |
|
|---|
| 90 | def identifier(token):
|
|---|
| 91 | "Check whether the token is an identifier"
|
|---|
| 92 |
|
|---|
| 93 | if (len(token) == 0):
|
|---|
| 94 | return False
|
|---|
| 95 |
|
|---|
| 96 | for i, char in enumerate(token):
|
|---|
| 97 | if (i == 0):
|
|---|
| 98 | if ((not char.isalpha()) and (char != "_")):
|
|---|
| 99 | return False
|
|---|
| 100 | else:
|
|---|
| 101 | if ((not char.isalnum()) and (char != "_")):
|
|---|
| 102 | return False
|
|---|
| 103 |
|
|---|
| 104 | return True
|
|---|
| 105 |
|
|---|
| 106 | def descriptor(token):
|
|---|
| 107 | "Check whether the token is an interface descriptor"
|
|---|
| 108 |
|
|---|
| 109 | parts = token.split(":")
|
|---|
| 110 | if (len(parts) != 2):
|
|---|
| 111 | return False
|
|---|
| 112 |
|
|---|
| 113 | return (identifier(parts[0]) and identifier(parts[1]))
|
|---|
| 114 |
|
|---|
| 115 | def word(token):
|
|---|
| 116 | "Check whether the token is a word"
|
|---|
| 117 |
|
|---|
| 118 | if (len(token) == 0):
|
|---|
| 119 | return False
|
|---|
| 120 |
|
|---|
| 121 | for i, char in enumerate(token):
|
|---|
| 122 | if ((not char.isalnum()) and (char != "_") and (char != ".")):
|
|---|
| 123 | return False
|
|---|
| 124 |
|
|---|
| 125 | return True
|
|---|
| 126 |
|
|---|
| 127 | def preproc_bp(name, tokens):
|
|---|
| 128 | "Preprocess tentative statements in Behavior Protocol"
|
|---|
| 129 |
|
|---|
| 130 | result = []
|
|---|
| 131 | i = 0
|
|---|
| 132 |
|
|---|
| 133 | while (i < len(tokens)):
|
|---|
| 134 | if (tokens[i] == "tentative"):
|
|---|
| 135 | if ((i + 1 < len(tokens)) and (tokens[i + 1] == "{")):
|
|---|
| 136 | i += 2
|
|---|
| 137 | start = i
|
|---|
| 138 | level = 1
|
|---|
| 139 |
|
|---|
| 140 | while ((i < len(tokens)) and (level > 0)):
|
|---|
| 141 | if (tokens[i] == "{"):
|
|---|
| 142 | level += 1
|
|---|
| 143 | elif (tokens[i] == "}"):
|
|---|
| 144 | level -= 1
|
|---|
| 145 |
|
|---|
| 146 | i += 1
|
|---|
| 147 |
|
|---|
| 148 | if (level == 0):
|
|---|
| 149 | result.append("(")
|
|---|
| 150 | result.extend(preproc_bp(name, tokens[start:(i - 1)]))
|
|---|
| 151 | result.append(")")
|
|---|
| 152 | result.append("+")
|
|---|
| 153 | result.append("NULL")
|
|---|
| 154 | if (i < len(tokens)):
|
|---|
| 155 | result.append(tokens[i])
|
|---|
| 156 | else:
|
|---|
| 157 | print "%s: Syntax error in tentative statement" % name
|
|---|
| 158 | else:
|
|---|
| 159 | print "%s: Unexpected token in tentative statement" % name
|
|---|
| 160 | else:
|
|---|
| 161 | result.append(tokens[i])
|
|---|
| 162 |
|
|---|
| 163 | i += 1
|
|---|
| 164 |
|
|---|
| 165 | return result
|
|---|
| 166 |
|
|---|
| 167 | def parse_bp(name, protocol, base_indent):
|
|---|
| 168 | "Parse Behavior Protocol"
|
|---|
| 169 |
|
|---|
| 170 | tokens = preproc_bp(name, split_tokens(protocol, ["\n", " ", "\t", "(", ")", "{", "}", "*", ";", "+", "||", "|", "!", "?"], True, True))
|
|---|
| 171 |
|
|---|
| 172 | indent = base_indent
|
|---|
| 173 | output = ""
|
|---|
| 174 |
|
|---|
| 175 | for token in tokens:
|
|---|
| 176 | if (token == "\n"):
|
|---|
| 177 | continue
|
|---|
| 178 |
|
|---|
| 179 | if ((token == ";") or (token == "+") or (token == "||") or (token == "|")):
|
|---|
| 180 | output += " %s" % token
|
|---|
| 181 | elif (token == "("):
|
|---|
| 182 | output += "\n%s%s" % (tabs(indent), token)
|
|---|
| 183 | indent += 1
|
|---|
| 184 | elif (token == ")"):
|
|---|
| 185 | if (indent < base_indent):
|
|---|
| 186 | print "%s: Too many parentheses" % name
|
|---|
| 187 |
|
|---|
| 188 | indent -= 1
|
|---|
| 189 | output += "\n%s%s" % (tabs(indent), token)
|
|---|
| 190 | elif (token == "{"):
|
|---|
| 191 | output += " %s" % token
|
|---|
| 192 | indent += 1
|
|---|
| 193 | elif (token == "}"):
|
|---|
| 194 | if (indent < base_indent):
|
|---|
| 195 | print "%s: Too many parentheses" % name
|
|---|
| 196 |
|
|---|
| 197 | indent -= 1
|
|---|
| 198 | output += "\n%s%s" % (tabs(indent), token)
|
|---|
| 199 | elif (token == "*"):
|
|---|
| 200 | output += "%s" % token
|
|---|
| 201 | elif ((token == "!") or (token == "?") or (token == "NULL")):
|
|---|
| 202 | output += "\n%s%s" % (tabs(indent), token)
|
|---|
| 203 | else:
|
|---|
| 204 | output += "%s" % token
|
|---|
| 205 |
|
|---|
| 206 | if (indent > base_indent):
|
|---|
| 207 | print "%s: Missing parentheses" % name
|
|---|
| 208 |
|
|---|
| 209 | output = output.strip()
|
|---|
| 210 | if (output == ""):
|
|---|
| 211 | return "NULL"
|
|---|
| 212 |
|
|---|
| 213 | return output
|
|---|
| 214 |
|
|---|
| 215 | def dump_iface_bp(interface, protocol, outdir):
|
|---|
| 216 | "Dump Behavior Protocol of a given interface"
|
|---|
| 217 |
|
|---|
| 218 | outname = os.path.join(outdir, "iface_%s.bp" % interface)
|
|---|
| 219 | if (os.path.isfile(outname)):
|
|---|
| 220 | print "%s: File already exists, overwriting" % outname
|
|---|
| 221 |
|
|---|
| 222 | outf = file(outname, "w")
|
|---|
| 223 | outf.write(parse_bp(outname, protocol, 0))
|
|---|
| 224 | outf.close()
|
|---|
| 225 |
|
|---|
| 226 | def dump_frame_bp(frame, protocol, outdir):
|
|---|
| 227 | "Dump Behavior Protocol of a given frame"
|
|---|
| 228 |
|
|---|
| 229 | outname = os.path.join(outdir, "frame_%s.bp" % frame)
|
|---|
| 230 | if (os.path.isfile(outname)):
|
|---|
| 231 | print "%s: File already exists, overwriting" % outname
|
|---|
| 232 |
|
|---|
| 233 | outf = file(outname, "w")
|
|---|
| 234 | outf.write(parse_bp(outname, protocol, 0))
|
|---|
| 235 | outf.close()
|
|---|
| 236 |
|
|---|
| 237 | def preproc_adl(raw, inarg):
|
|---|
| 238 | "Preprocess %% statements in ADL"
|
|---|
| 239 |
|
|---|
| 240 | return raw.replace("%%", inarg)
|
|---|
| 241 |
|
|---|
| 242 | def parse_adl(base, root, inname, nested, indent):
|
|---|
| 243 | "Parse Architecture Description Language"
|
|---|
| 244 |
|
|---|
| 245 | global output
|
|---|
| 246 | global context
|
|---|
| 247 | global architecture
|
|---|
| 248 | global interface
|
|---|
| 249 | global frame
|
|---|
| 250 | global protocol
|
|---|
| 251 | global iface_protocols
|
|---|
| 252 | global frame_protocols
|
|---|
| 253 |
|
|---|
| 254 | if (nested):
|
|---|
| 255 | parts = inname.split("%")
|
|---|
| 256 |
|
|---|
| 257 | if (len(parts) > 1):
|
|---|
| 258 | inarg = parts[1]
|
|---|
| 259 | else:
|
|---|
| 260 | inarg = "%%"
|
|---|
| 261 |
|
|---|
| 262 | if (parts[0][0:1] == "/"):
|
|---|
| 263 | path = os.path.join(base, ".%s" % parts[0])
|
|---|
| 264 | nested_root = os.path.dirname(path)
|
|---|
| 265 | else:
|
|---|
| 266 | path = os.path.join(root, parts[0])
|
|---|
| 267 | nested_root = root
|
|---|
| 268 |
|
|---|
| 269 | if (not os.path.isfile(path)):
|
|---|
| 270 | print "%s: Unable to include file %s" % (inname, path)
|
|---|
| 271 | return ""
|
|---|
| 272 | else:
|
|---|
| 273 | inarg = "%%"
|
|---|
| 274 | path = inname
|
|---|
| 275 | nested_root = root
|
|---|
| 276 |
|
|---|
| 277 | inf = file(path, "r")
|
|---|
| 278 |
|
|---|
| 279 | raw = preproc_adl(inf.read(), inarg)
|
|---|
| 280 | tokens = split_tokens(raw, ["\n", " ", "\t", "(", ")", "{", "}", "[", "]", "/*", "*/", "#", ";"], True, True)
|
|---|
| 281 |
|
|---|
| 282 | for token in tokens:
|
|---|
| 283 |
|
|---|
| 284 | # Includes
|
|---|
| 285 |
|
|---|
| 286 | if (INC in context):
|
|---|
| 287 | context.remove(INC)
|
|---|
| 288 | parse_adl(base, nested_root, token, True, indent)
|
|---|
| 289 | context.add(POST_INC)
|
|---|
| 290 | continue
|
|---|
| 291 |
|
|---|
| 292 | if (POST_INC in context):
|
|---|
| 293 | if (token != "]"):
|
|---|
| 294 | print "%s: Expected ]" % inname
|
|---|
| 295 |
|
|---|
| 296 | context.remove(POST_INC)
|
|---|
| 297 | continue
|
|---|
| 298 |
|
|---|
| 299 | # Comments and newlines
|
|---|
| 300 |
|
|---|
| 301 | if (BLOCK_COMMENT in context):
|
|---|
| 302 | if (token == "*/"):
|
|---|
| 303 | context.remove(BLOCK_COMMENT)
|
|---|
| 304 |
|
|---|
| 305 | continue
|
|---|
| 306 |
|
|---|
| 307 | if (LINE_COMMENT in context):
|
|---|
| 308 | if (token == "\n"):
|
|---|
| 309 | context.remove(LINE_COMMENT)
|
|---|
| 310 |
|
|---|
| 311 | continue
|
|---|
| 312 |
|
|---|
| 313 | # Any context
|
|---|
| 314 |
|
|---|
| 315 | if (token == "/*"):
|
|---|
| 316 | context.add(BLOCK_COMMENT)
|
|---|
| 317 | continue
|
|---|
| 318 |
|
|---|
| 319 | if (token == "#"):
|
|---|
| 320 | context.add(LINE_COMMENT)
|
|---|
| 321 | continue
|
|---|
| 322 |
|
|---|
| 323 | if (token == "["):
|
|---|
| 324 | context.add(INC)
|
|---|
| 325 | continue
|
|---|
| 326 |
|
|---|
| 327 | if (token == "\n"):
|
|---|
| 328 | continue
|
|---|
| 329 |
|
|---|
| 330 | # "frame"
|
|---|
| 331 |
|
|---|
| 332 | if (FRAME in context):
|
|---|
| 333 | if (NULL in context):
|
|---|
| 334 | if (token != ";"):
|
|---|
| 335 | print "%s: Expected ';' in frame '%s'" % (inname, frame)
|
|---|
| 336 | else:
|
|---|
| 337 | output += "%s\n" % token
|
|---|
| 338 |
|
|---|
| 339 | context.remove(NULL)
|
|---|
| 340 | context.remove(FRAME)
|
|---|
| 341 | frame = None
|
|---|
| 342 | continue
|
|---|
| 343 |
|
|---|
| 344 | if (BODY in context):
|
|---|
| 345 | if (PROTOCOL in context):
|
|---|
| 346 | if (token == "{"):
|
|---|
| 347 | indent += 1
|
|---|
| 348 | elif (token == "}"):
|
|---|
| 349 | indent -= 1
|
|---|
| 350 |
|
|---|
| 351 | if (indent == -1):
|
|---|
| 352 | if (frame in frame_protocols):
|
|---|
| 353 | print "%s: Protocol for frame '%s' already defined" % (inname, frame)
|
|---|
| 354 | else:
|
|---|
| 355 | frame_protocols[frame] = protocol
|
|---|
| 356 | output += "\n%s" % tabs(2)
|
|---|
| 357 | output += parse_bp(inname, protocol, 2)
|
|---|
| 358 | protocol = None
|
|---|
| 359 |
|
|---|
| 360 | output += "\n%s" % token
|
|---|
| 361 | indent = 0
|
|---|
| 362 |
|
|---|
| 363 | context.remove(PROTOCOL)
|
|---|
| 364 | context.remove(BODY)
|
|---|
| 365 | context.add(NULL)
|
|---|
| 366 | else:
|
|---|
| 367 | protocol += token
|
|---|
| 368 |
|
|---|
| 369 | continue
|
|---|
| 370 |
|
|---|
| 371 | if (REQUIRES in context):
|
|---|
| 372 | if (FIN in context):
|
|---|
| 373 | if (token != ";"):
|
|---|
| 374 | print "%s: Expected ';' in frame '%s'" % (inname, frame)
|
|---|
| 375 | else:
|
|---|
| 376 | output += "%s" % token
|
|---|
| 377 |
|
|---|
| 378 | context.remove(FIN)
|
|---|
| 379 | continue
|
|---|
| 380 |
|
|---|
| 381 | if (VAR in context):
|
|---|
| 382 | if (not identifier(token)):
|
|---|
| 383 | print "%s: Instance name expected in frame '%s'" % (inname, frame)
|
|---|
| 384 | else:
|
|---|
| 385 | output += "%s" % token
|
|---|
| 386 |
|
|---|
| 387 | context.remove(VAR)
|
|---|
| 388 | context.add(FIN)
|
|---|
| 389 | continue
|
|---|
| 390 |
|
|---|
| 391 | if ((token == "}") or (token[-1] == ":")):
|
|---|
| 392 | context.remove(REQUIRES)
|
|---|
| 393 | else:
|
|---|
| 394 | if (not identifier(token)):
|
|---|
| 395 | print "%s: Interface name expected in frame '%s'" % (inname, frame)
|
|---|
| 396 | else:
|
|---|
| 397 | output += "\n%s%s " % (tabs(indent), token)
|
|---|
| 398 |
|
|---|
| 399 | context.add(VAR)
|
|---|
| 400 | continue
|
|---|
| 401 |
|
|---|
| 402 | if (PROVIDES in context):
|
|---|
| 403 | if (FIN in context):
|
|---|
| 404 | if (token != ";"):
|
|---|
| 405 | print "%s: Expected ';' in frame '%s'" % (inname, frame)
|
|---|
| 406 | else:
|
|---|
| 407 | output += "%s" % token
|
|---|
| 408 |
|
|---|
| 409 | context.remove(FIN)
|
|---|
| 410 | continue
|
|---|
| 411 |
|
|---|
| 412 | if (VAR in context):
|
|---|
| 413 | if (not identifier(token)):
|
|---|
| 414 | print "%s: Instance name expected in frame '%s'" % (inname, frame)
|
|---|
| 415 | else:
|
|---|
| 416 | output += "%s" % token
|
|---|
| 417 |
|
|---|
| 418 | context.remove(VAR)
|
|---|
| 419 | context.add(FIN)
|
|---|
| 420 | continue
|
|---|
| 421 |
|
|---|
| 422 | if ((token == "}") or (token[-1] == ":")):
|
|---|
| 423 | context.remove(PROVIDES)
|
|---|
| 424 | else:
|
|---|
| 425 | if (not identifier(token)):
|
|---|
| 426 | print "%s: Interface name expected in frame '%s'" % (inname, frame)
|
|---|
| 427 | else:
|
|---|
| 428 | output += "\n%s%s " % (tabs(indent), token)
|
|---|
| 429 |
|
|---|
| 430 | context.add(VAR)
|
|---|
| 431 | continue
|
|---|
| 432 |
|
|---|
| 433 | if (token == "}"):
|
|---|
| 434 | if (indent != 2):
|
|---|
| 435 | print "%s: Wrong number of parentheses in frame '%s'" % (inname, frame)
|
|---|
| 436 | else:
|
|---|
| 437 | indent = 0
|
|---|
| 438 | output += "\n%s" % token
|
|---|
| 439 |
|
|---|
| 440 | context.remove(BODY)
|
|---|
| 441 | context.add(NULL)
|
|---|
| 442 | continue
|
|---|
| 443 |
|
|---|
| 444 | if (token == "provides:"):
|
|---|
| 445 | output += "\n%s%s" % (tabs(indent - 1), token)
|
|---|
| 446 | context.add(PROVIDES)
|
|---|
| 447 | protocol = ""
|
|---|
| 448 | continue
|
|---|
| 449 |
|
|---|
| 450 | if (token == "requires:"):
|
|---|
| 451 | output += "\n%s%s" % (tabs(indent - 1), token)
|
|---|
| 452 | context.add(REQUIRES)
|
|---|
| 453 | protocol = ""
|
|---|
| 454 | continue
|
|---|
| 455 |
|
|---|
| 456 | if (token == "protocol:"):
|
|---|
| 457 | output += "\n%s%s" % (tabs(indent - 1), token)
|
|---|
| 458 | indent = 0
|
|---|
| 459 | context.add(PROTOCOL)
|
|---|
| 460 | protocol = ""
|
|---|
| 461 | continue
|
|---|
| 462 |
|
|---|
| 463 | print "%s: Unknown token '%s' in frame '%s'" % (inname, token, frame)
|
|---|
| 464 | continue
|
|---|
| 465 |
|
|---|
| 466 | if (HEAD in context):
|
|---|
| 467 | if (token == "{"):
|
|---|
| 468 | output += "%s" % token
|
|---|
| 469 | indent += 2
|
|---|
| 470 | context.remove(HEAD)
|
|---|
| 471 | context.add(BODY)
|
|---|
| 472 | continue
|
|---|
| 473 |
|
|---|
| 474 | if (token == ";"):
|
|---|
| 475 | output += "%s\n" % token
|
|---|
| 476 | context.remove(HEAD)
|
|---|
| 477 | context.remove(FRAME)
|
|---|
| 478 | continue
|
|---|
| 479 |
|
|---|
| 480 | print "%s: Unknown token '%s' in frame head '%s'" % (inname, token, frame)
|
|---|
| 481 |
|
|---|
| 482 | continue
|
|---|
| 483 |
|
|---|
| 484 | if (not identifier(token)):
|
|---|
| 485 | print "%s: Expected frame name" % inname
|
|---|
| 486 | else:
|
|---|
| 487 | frame = token
|
|---|
| 488 | output += "%s " % token
|
|---|
| 489 |
|
|---|
| 490 | context.add(HEAD)
|
|---|
| 491 | continue
|
|---|
| 492 |
|
|---|
| 493 | # "interface"
|
|---|
| 494 |
|
|---|
| 495 | if (IFACE in context):
|
|---|
| 496 | if (NULL in context):
|
|---|
| 497 | if (token != ";"):
|
|---|
| 498 | print "%s: Expected ';' in interface '%s'" % (inname, interface)
|
|---|
| 499 | else:
|
|---|
| 500 | output += "%s\n" % token
|
|---|
| 501 |
|
|---|
| 502 | context.remove(NULL)
|
|---|
| 503 | context.remove(IFACE)
|
|---|
| 504 | interface = None
|
|---|
| 505 | continue
|
|---|
| 506 |
|
|---|
| 507 | if (BODY in context):
|
|---|
| 508 | if (PROTOCOL in context):
|
|---|
| 509 | if (token == "{"):
|
|---|
| 510 | indent += 1
|
|---|
| 511 | elif (token == "}"):
|
|---|
| 512 | indent -= 1
|
|---|
| 513 |
|
|---|
| 514 | if (indent == -1):
|
|---|
| 515 | if (interface in iface_protocols):
|
|---|
| 516 | print "%s: Protocol for interface '%s' already defined" % (inname, interface)
|
|---|
| 517 | else:
|
|---|
| 518 | iface_protocols[interface] = protocol
|
|---|
| 519 |
|
|---|
| 520 | output += "\n%s" % tabs(2)
|
|---|
| 521 | output += parse_bp(inname, protocol, 2)
|
|---|
| 522 | protocol = None
|
|---|
| 523 |
|
|---|
| 524 | output += "\n%s" % token
|
|---|
| 525 | indent = 0
|
|---|
| 526 |
|
|---|
| 527 | context.remove(PROTOCOL)
|
|---|
| 528 | context.remove(BODY)
|
|---|
| 529 | context.add(NULL)
|
|---|
| 530 | else:
|
|---|
| 531 | protocol += token
|
|---|
| 532 |
|
|---|
| 533 | continue
|
|---|
| 534 |
|
|---|
| 535 | if (PROTOTYPE in context):
|
|---|
| 536 | if (FIN in context):
|
|---|
| 537 | if (token != ";"):
|
|---|
| 538 | print "%s: Expected ';' in interface '%s'" % (inname, interface)
|
|---|
| 539 | else:
|
|---|
| 540 | output += "%s" % token
|
|---|
| 541 |
|
|---|
| 542 | context.remove(FIN)
|
|---|
| 543 | context.remove(PROTOTYPE)
|
|---|
| 544 | continue
|
|---|
| 545 |
|
|---|
| 546 | if (PAR_RIGHT in context):
|
|---|
| 547 | if (token == ")"):
|
|---|
| 548 | output += "%s" % token
|
|---|
| 549 | context.remove(PAR_RIGHT)
|
|---|
| 550 | context.add(FIN)
|
|---|
| 551 | else:
|
|---|
| 552 | output += " %s" % token
|
|---|
| 553 |
|
|---|
| 554 | continue
|
|---|
| 555 |
|
|---|
| 556 | if (SIGNATURE in context):
|
|---|
| 557 | output += "%s" % token
|
|---|
| 558 | if (token == ")"):
|
|---|
| 559 | context.remove(SIGNATURE)
|
|---|
| 560 | context.add(FIN)
|
|---|
| 561 |
|
|---|
| 562 | context.remove(SIGNATURE)
|
|---|
| 563 | context.add(PAR_RIGHT)
|
|---|
| 564 | continue
|
|---|
| 565 |
|
|---|
| 566 | if (PAR_LEFT in context):
|
|---|
| 567 | if (token != "("):
|
|---|
| 568 | print "%s: Expected '(' in interface '%s'" % (inname, interface)
|
|---|
| 569 | else:
|
|---|
| 570 | output += "%s" % token
|
|---|
| 571 |
|
|---|
| 572 | context.remove(PAR_LEFT)
|
|---|
| 573 | context.add(SIGNATURE)
|
|---|
| 574 | continue
|
|---|
| 575 |
|
|---|
| 576 | if (not identifier(token)):
|
|---|
| 577 | print "%s: Method identifier expected in interface '%s'" % (inname, interface)
|
|---|
| 578 | else:
|
|---|
| 579 | output += "%s" % token
|
|---|
| 580 |
|
|---|
| 581 | context.add(PAR_LEFT)
|
|---|
| 582 | continue
|
|---|
| 583 |
|
|---|
| 584 | if (token == "}"):
|
|---|
| 585 | if (indent != 2):
|
|---|
| 586 | print "%s: Wrong number of parentheses in interface '%s'" % (inname, interface)
|
|---|
| 587 | else:
|
|---|
| 588 | indent = 0
|
|---|
| 589 | output += "\n%s" % token
|
|---|
| 590 |
|
|---|
| 591 | context.remove(BODY)
|
|---|
| 592 | context.add(NULL)
|
|---|
| 593 | continue
|
|---|
| 594 |
|
|---|
| 595 | if ((token == "ipcarg_t") or (token == "unative_t")):
|
|---|
| 596 | output += "\n%s%s " % (tabs(indent), token)
|
|---|
| 597 | context.add(PROTOTYPE)
|
|---|
| 598 | continue
|
|---|
| 599 |
|
|---|
| 600 | if (token == "protocol:"):
|
|---|
| 601 | output += "\n%s%s" % (tabs(indent - 1), token)
|
|---|
| 602 | indent = 0
|
|---|
| 603 | context.add(PROTOCOL)
|
|---|
| 604 | protocol = ""
|
|---|
| 605 | continue
|
|---|
| 606 |
|
|---|
| 607 | print "%s: Unknown token '%s' in interface '%s'" % (inname, token, interface)
|
|---|
| 608 | continue
|
|---|
| 609 |
|
|---|
| 610 | if (HEAD in context):
|
|---|
| 611 | if (token == "{"):
|
|---|
| 612 | output += "%s" % token
|
|---|
| 613 | indent += 2
|
|---|
| 614 | context.remove(HEAD)
|
|---|
| 615 | context.add(BODY)
|
|---|
| 616 | continue
|
|---|
| 617 |
|
|---|
| 618 | if (token == ";"):
|
|---|
| 619 | output += "%s\n" % token
|
|---|
| 620 | context.remove(HEAD)
|
|---|
| 621 | context.remove(ARCH)
|
|---|
| 622 | continue
|
|---|
| 623 |
|
|---|
| 624 | if (not word(token)):
|
|---|
| 625 | print "%s: Expected word in interface head '%s'" % (inname, interface)
|
|---|
| 626 | else:
|
|---|
| 627 | output += "%s " % token
|
|---|
| 628 |
|
|---|
| 629 | continue
|
|---|
| 630 |
|
|---|
| 631 | if (not identifier(token)):
|
|---|
| 632 | print "%s: Expected interface name" % inname
|
|---|
| 633 | else:
|
|---|
| 634 | interface = token
|
|---|
| 635 | output += "%s " % token
|
|---|
| 636 |
|
|---|
| 637 | context.add(HEAD)
|
|---|
| 638 | continue
|
|---|
| 639 |
|
|---|
| 640 | # "architecture"
|
|---|
| 641 |
|
|---|
| 642 | if (ARCH in context):
|
|---|
| 643 | if (NULL in context):
|
|---|
| 644 | if (token != ";"):
|
|---|
| 645 | print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
|
|---|
| 646 | else:
|
|---|
| 647 | output += "%s\n" % token
|
|---|
| 648 |
|
|---|
| 649 | context.remove(NULL)
|
|---|
| 650 | context.remove(ARCH)
|
|---|
| 651 | context.discard(SYSTEM)
|
|---|
| 652 | architecture = None
|
|---|
| 653 | continue
|
|---|
| 654 |
|
|---|
| 655 | if (BODY in context):
|
|---|
| 656 | if (DELEGATE in context):
|
|---|
| 657 | if (FIN in context):
|
|---|
| 658 | if (token != ";"):
|
|---|
| 659 | print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
|
|---|
| 660 | else:
|
|---|
| 661 | output += "%s" % token
|
|---|
| 662 |
|
|---|
| 663 | context.remove(FIN)
|
|---|
| 664 | context.remove(DELEGATE)
|
|---|
| 665 | continue
|
|---|
| 666 |
|
|---|
| 667 | if (VAR in context):
|
|---|
| 668 | if (not descriptor(token)):
|
|---|
| 669 | print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
|
|---|
| 670 | else:
|
|---|
| 671 | output += "%s" % token
|
|---|
| 672 |
|
|---|
| 673 | context.add(FIN)
|
|---|
| 674 | context.remove(VAR)
|
|---|
| 675 | continue
|
|---|
| 676 |
|
|---|
| 677 | if (TO in context):
|
|---|
| 678 | if (token != "to"):
|
|---|
| 679 | print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
|
|---|
| 680 | else:
|
|---|
| 681 | output += "%s " % token
|
|---|
| 682 |
|
|---|
| 683 | context.add(VAR)
|
|---|
| 684 | context.remove(TO)
|
|---|
| 685 | continue
|
|---|
| 686 |
|
|---|
| 687 | if (not identifier(token)):
|
|---|
| 688 | print "%s: Expected interface name in architecture '%s'" % (inname, architecture)
|
|---|
| 689 | else:
|
|---|
| 690 | output += "%s " % token
|
|---|
| 691 |
|
|---|
| 692 | context.add(TO)
|
|---|
| 693 | continue
|
|---|
| 694 |
|
|---|
| 695 | if (SUBSUME in context):
|
|---|
| 696 | if (FIN in context):
|
|---|
| 697 | if (token != ";"):
|
|---|
| 698 | print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
|
|---|
| 699 | else:
|
|---|
| 700 | output += "%s" % token
|
|---|
| 701 |
|
|---|
| 702 | context.remove(FIN)
|
|---|
| 703 | context.remove(SUBSUME)
|
|---|
| 704 | continue
|
|---|
| 705 |
|
|---|
| 706 | if (VAR in context):
|
|---|
| 707 | if (not identifier(token)):
|
|---|
| 708 | print "%s: Expected interface name in architecture '%s'" % (inname, architecture)
|
|---|
| 709 | else:
|
|---|
| 710 | output += "%s" % token
|
|---|
| 711 |
|
|---|
| 712 | context.add(FIN)
|
|---|
| 713 | context.remove(VAR)
|
|---|
| 714 | continue
|
|---|
| 715 |
|
|---|
| 716 | if (TO in context):
|
|---|
| 717 | if (token != "to"):
|
|---|
| 718 | print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
|
|---|
| 719 | else:
|
|---|
| 720 | output += "%s " % token
|
|---|
| 721 |
|
|---|
| 722 | context.add(VAR)
|
|---|
| 723 | context.remove(TO)
|
|---|
| 724 | continue
|
|---|
| 725 |
|
|---|
| 726 | if (not descriptor(token)):
|
|---|
| 727 | print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
|
|---|
| 728 | else:
|
|---|
| 729 | output += "%s " % token
|
|---|
| 730 |
|
|---|
| 731 | context.add(TO)
|
|---|
| 732 | continue
|
|---|
| 733 |
|
|---|
| 734 | if (BIND in context):
|
|---|
| 735 | if (FIN in context):
|
|---|
| 736 | if (token != ";"):
|
|---|
| 737 | print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
|
|---|
| 738 | else:
|
|---|
| 739 | output += "%s" % token
|
|---|
| 740 |
|
|---|
| 741 | context.remove(FIN)
|
|---|
| 742 | context.remove(BIND)
|
|---|
| 743 | continue
|
|---|
| 744 |
|
|---|
| 745 | if (VAR in context):
|
|---|
| 746 | if (not descriptor(token)):
|
|---|
| 747 | print "%s: Expected second interface descriptor in architecture '%s'" % (inname, architecture)
|
|---|
| 748 | else:
|
|---|
| 749 | output += "%s" % token
|
|---|
| 750 |
|
|---|
| 751 | context.add(FIN)
|
|---|
| 752 | context.remove(VAR)
|
|---|
| 753 | continue
|
|---|
| 754 |
|
|---|
| 755 | if (TO in context):
|
|---|
| 756 | if (token != "to"):
|
|---|
| 757 | print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
|
|---|
| 758 | else:
|
|---|
| 759 | output += "%s " % token
|
|---|
| 760 |
|
|---|
| 761 | context.add(VAR)
|
|---|
| 762 | context.remove(TO)
|
|---|
| 763 | continue
|
|---|
| 764 |
|
|---|
| 765 | if (not descriptor(token)):
|
|---|
| 766 | print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
|
|---|
| 767 | else:
|
|---|
| 768 | output += "%s " % token
|
|---|
| 769 |
|
|---|
| 770 | context.add(TO)
|
|---|
| 771 | continue
|
|---|
| 772 |
|
|---|
| 773 | if (INST in context):
|
|---|
| 774 | if (FIN in context):
|
|---|
| 775 | if (token != ";"):
|
|---|
| 776 | print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
|
|---|
| 777 | else:
|
|---|
| 778 | output += "%s" % token
|
|---|
| 779 |
|
|---|
| 780 | context.remove(FIN)
|
|---|
| 781 | context.remove(INST)
|
|---|
| 782 | continue
|
|---|
| 783 |
|
|---|
| 784 | if (VAR in context):
|
|---|
| 785 | if (not identifier(token)):
|
|---|
| 786 | print "%s: Expected instance name in architecture '%s'" % (inname, architecture)
|
|---|
| 787 | else:
|
|---|
| 788 | output += "%s" % token
|
|---|
| 789 |
|
|---|
| 790 | context.add(FIN)
|
|---|
| 791 | context.remove(VAR)
|
|---|
| 792 | continue
|
|---|
| 793 |
|
|---|
| 794 | if (not identifier(token)):
|
|---|
| 795 | print "%s: Expected frame/architecture type in architecture '%s'" % (inname, architecture)
|
|---|
| 796 | else:
|
|---|
| 797 | output += "%s " % token
|
|---|
| 798 |
|
|---|
| 799 | context.add(VAR)
|
|---|
| 800 | continue
|
|---|
| 801 |
|
|---|
| 802 | if (token == "}"):
|
|---|
| 803 | if (indent != 1):
|
|---|
| 804 | print "%s: Wrong number of parentheses in architecture '%s'" % (inname, architecture)
|
|---|
| 805 | else:
|
|---|
| 806 | indent -= 1
|
|---|
| 807 | output += "\n%s" % token
|
|---|
| 808 |
|
|---|
| 809 | context.remove(BODY)
|
|---|
| 810 | context.add(NULL)
|
|---|
| 811 | continue
|
|---|
| 812 |
|
|---|
| 813 | if (token == "inst"):
|
|---|
| 814 | output += "\n%s%s " % (tabs(indent), token)
|
|---|
| 815 | context.add(INST)
|
|---|
| 816 | continue
|
|---|
| 817 |
|
|---|
| 818 | if (token == "bind"):
|
|---|
| 819 | output += "\n%s%s " % (tabs(indent), token)
|
|---|
| 820 | context.add(BIND)
|
|---|
| 821 | continue
|
|---|
| 822 |
|
|---|
| 823 | if (token == "subsume"):
|
|---|
| 824 | output += "\n%s%s " % (tabs(indent), token)
|
|---|
| 825 | context.add(SUBSUME)
|
|---|
| 826 | continue
|
|---|
| 827 |
|
|---|
| 828 | if (token == "delegate"):
|
|---|
| 829 | output += "\n%s%s " % (tabs(indent), token)
|
|---|
| 830 | context.add(DELEGATE)
|
|---|
| 831 | continue
|
|---|
| 832 |
|
|---|
| 833 | print "%s: Unknown token '%s' in architecture '%s'" % (inname, token, architecture)
|
|---|
| 834 | continue
|
|---|
| 835 |
|
|---|
| 836 | if (HEAD in context):
|
|---|
| 837 | if (token == "{"):
|
|---|
| 838 | output += "%s" % token
|
|---|
| 839 | indent += 1
|
|---|
| 840 | context.remove(HEAD)
|
|---|
| 841 | context.add(BODY)
|
|---|
| 842 | continue
|
|---|
| 843 |
|
|---|
| 844 | if (token == ";"):
|
|---|
| 845 | output += "%s\n" % token
|
|---|
| 846 | context.remove(HEAD)
|
|---|
| 847 | context.remove(ARCH)
|
|---|
| 848 | context.discard(SYSTEM)
|
|---|
| 849 | continue
|
|---|
| 850 |
|
|---|
| 851 | if (not word(token)):
|
|---|
| 852 | print "%s: Expected word in architecture head '%s'" % (inname, architecture)
|
|---|
| 853 | else:
|
|---|
| 854 | output += "%s " % token
|
|---|
| 855 |
|
|---|
| 856 | continue
|
|---|
| 857 |
|
|---|
| 858 | if (not identifier(token)):
|
|---|
| 859 | print "%s: Expected architecture name" % inname
|
|---|
| 860 | else:
|
|---|
| 861 | architecture = token
|
|---|
| 862 | output += "%s " % token
|
|---|
| 863 |
|
|---|
| 864 | context.add(HEAD)
|
|---|
| 865 | continue
|
|---|
| 866 |
|
|---|
| 867 | # "system architecture"
|
|---|
| 868 |
|
|---|
| 869 | if (SYSTEM in context):
|
|---|
| 870 | if (token != "architecture"):
|
|---|
| 871 | print "%s: Expected 'architecture'" % inname
|
|---|
| 872 | else:
|
|---|
| 873 | output += "%s " % token
|
|---|
| 874 |
|
|---|
| 875 | context.add(ARCH)
|
|---|
| 876 | continue
|
|---|
| 877 |
|
|---|
| 878 | if (token == "frame"):
|
|---|
| 879 | output += "\n%s " % token
|
|---|
| 880 | context.add(FRAME)
|
|---|
| 881 | continue
|
|---|
| 882 |
|
|---|
| 883 | if (token == "interface"):
|
|---|
| 884 | output += "\n%s " % token
|
|---|
| 885 | context.add(IFACE)
|
|---|
| 886 | continue
|
|---|
| 887 |
|
|---|
| 888 | if (token == "system"):
|
|---|
| 889 | output += "\n%s " % token
|
|---|
| 890 | context.add(SYSTEM)
|
|---|
| 891 | continue
|
|---|
| 892 |
|
|---|
| 893 | if (token == "architecture"):
|
|---|
| 894 | output += "\n%s " % token
|
|---|
| 895 | context.add(ARCH)
|
|---|
| 896 | continue
|
|---|
| 897 |
|
|---|
| 898 | print "%s: Unknown token '%s'" % (inname, token)
|
|---|
| 899 |
|
|---|
| 900 | inf.close()
|
|---|
| 901 |
|
|---|
| 902 | def open_adl(base, root, inname, outdir, outname):
|
|---|
| 903 | "Open Architecture Description file"
|
|---|
| 904 |
|
|---|
| 905 | global output
|
|---|
| 906 | global context
|
|---|
| 907 | global architecture
|
|---|
| 908 | global interface
|
|---|
| 909 | global frame
|
|---|
| 910 | global protocol
|
|---|
| 911 |
|
|---|
| 912 | output = ""
|
|---|
| 913 | context = set()
|
|---|
| 914 | architecture = None
|
|---|
| 915 | interface = None
|
|---|
| 916 | frame = None
|
|---|
| 917 | protocol = None
|
|---|
| 918 |
|
|---|
| 919 | parse_adl(base, root, inname, False, 0)
|
|---|
| 920 | output = output.strip()
|
|---|
| 921 |
|
|---|
| 922 | if (output != ""):
|
|---|
| 923 | if (os.path.isfile(outname)):
|
|---|
| 924 | print "%s: File already exists, overwriting" % outname
|
|---|
| 925 |
|
|---|
| 926 | outf = file(outname, "w")
|
|---|
| 927 | outf.write(output)
|
|---|
| 928 | outf.close()
|
|---|
| 929 | else:
|
|---|
| 930 | if (os.path.isfile(outname)):
|
|---|
| 931 | print "%s: File already exists, but should be empty" % outname
|
|---|
| 932 |
|
|---|
| 933 | def recursion(base, root, output, level):
|
|---|
| 934 | "Recursive directory walk"
|
|---|
| 935 |
|
|---|
| 936 | for name in os.listdir(root):
|
|---|
| 937 | canon = os.path.join(root, name)
|
|---|
| 938 |
|
|---|
| 939 | if (os.path.isfile(canon)):
|
|---|
| 940 | fcomp = split_tokens(canon, ["."])
|
|---|
| 941 | cname = canon.split("/")
|
|---|
| 942 |
|
|---|
| 943 | if (fcomp[-1] == ".adl"):
|
|---|
| 944 | output_path = os.path.join(output, cname[-1])
|
|---|
| 945 | open_adl(base, root, canon, output, output_path)
|
|---|
| 946 |
|
|---|
| 947 | if (os.path.isdir(canon)):
|
|---|
| 948 | recursion(base, canon, output, level + 1)
|
|---|
| 949 |
|
|---|
| 950 | def main():
|
|---|
| 951 | global iface_protocols
|
|---|
| 952 | global frame_protocols
|
|---|
| 953 |
|
|---|
| 954 | if (len(sys.argv) < 2):
|
|---|
| 955 | usage(sys.argv[0])
|
|---|
| 956 | return
|
|---|
| 957 |
|
|---|
| 958 | path = os.path.abspath(sys.argv[1])
|
|---|
| 959 | if (not os.path.isdir(path)):
|
|---|
| 960 | print "Error: <OUTPUT> is not a directory"
|
|---|
| 961 | return
|
|---|
| 962 |
|
|---|
| 963 | iface_protocols = {}
|
|---|
| 964 | frame_protocols = {}
|
|---|
| 965 |
|
|---|
| 966 | recursion(".", ".", path, 0)
|
|---|
| 967 |
|
|---|
| 968 | for key, value in iface_protocols.items():
|
|---|
| 969 | dump_iface_bp(key, value, path)
|
|---|
| 970 |
|
|---|
| 971 | for key, value in frame_protocols.items():
|
|---|
| 972 | dump_frame_bp(key, value, path)
|
|---|
| 973 |
|
|---|
| 974 | if __name__ == '__main__':
|
|---|
| 975 | main()
|
|---|