| 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 | if (token != ""):
|
|---|
| 51 | tokens.append(token)
|
|---|
| 52 | else:
|
|---|
| 53 | tokens.append(token)
|
|---|
| 54 |
|
|---|
| 55 | return tokens
|
|---|
| 56 |
|
|---|
| 57 | def split_tokens(string, delimiters, trim = False):
|
|---|
| 58 | "Split string to tokens by delimiters, keep the delimiters"
|
|---|
| 59 |
|
|---|
| 60 | tokens = []
|
|---|
| 61 | last = 0
|
|---|
| 62 | i = 0
|
|---|
| 63 |
|
|---|
| 64 | while (i < len(string)):
|
|---|
| 65 | for delim in delimiters:
|
|---|
| 66 | if (len(delim) > 0):
|
|---|
| 67 |
|
|---|
| 68 | if ((string[i:(i + len(delim))] == delim) and (i > 0)):
|
|---|
| 69 | tokens = cond_append(tokens, string[last:i], trim)
|
|---|
| 70 | last = i
|
|---|
| 71 | i += len(delim) - 1
|
|---|
| 72 | break
|
|---|
| 73 |
|
|---|
| 74 | i += 1
|
|---|
| 75 |
|
|---|
| 76 | tokens = cond_append(tokens, string[last:len(string)], trim)
|
|---|
| 77 |
|
|---|
| 78 | return tokens
|
|---|
| 79 |
|
|---|
| 80 | def parse(fname, outf):
|
|---|
| 81 | "Parse particular protocol"
|
|---|
| 82 |
|
|---|
| 83 | inf = file(fname, "r")
|
|---|
| 84 | outf.write("### %s\n\n" % fname)
|
|---|
| 85 |
|
|---|
| 86 | tokens = split_tokens(inf.read(), ["\n", " ", "\t", "(", ")", "{", "}", "[", "/*", "*/", "#", "*", ";", "+", "||", "|", "!", "?"], True)
|
|---|
| 87 |
|
|---|
| 88 | empty = True
|
|---|
| 89 | comment = False
|
|---|
| 90 | lcomment = False
|
|---|
| 91 | indent = 0
|
|---|
| 92 |
|
|---|
| 93 | for token in tokens:
|
|---|
| 94 | if (comment):
|
|---|
| 95 | if (token == "*/"):
|
|---|
| 96 | comment = False
|
|---|
| 97 | continue
|
|---|
| 98 |
|
|---|
| 99 | if ((not comment) and (token == "/*")):
|
|---|
| 100 | comment = True
|
|---|
| 101 | continue
|
|---|
| 102 |
|
|---|
| 103 | if (lcomment):
|
|---|
| 104 | if (token == "\n"):
|
|---|
| 105 | lcomment = False
|
|---|
| 106 | continue
|
|---|
| 107 |
|
|---|
| 108 | if ((not lcomment) and (token == "#")):
|
|---|
| 109 | lcomment = True
|
|---|
| 110 | continue
|
|---|
| 111 |
|
|---|
| 112 | if (token == "\n"):
|
|---|
| 113 | continue
|
|---|
| 114 |
|
|---|
| 115 | if (empty):
|
|---|
| 116 | empty = False
|
|---|
| 117 |
|
|---|
| 118 | if ((token == ";") or (token == "+") or (token == "||") or (token == "|")):
|
|---|
| 119 | outf.write(" %s\n" % token)
|
|---|
| 120 | elif (token == "("):
|
|---|
| 121 | outf.write("%s%s\n" % (tabs(indent), token))
|
|---|
| 122 | indent += 1
|
|---|
| 123 | elif (token == ")"):
|
|---|
| 124 | indent -= 1
|
|---|
| 125 | outf.write("\n%s%s" % (tabs(indent), token))
|
|---|
| 126 | elif (token == "{"):
|
|---|
| 127 | outf.write(" %s\n" % token)
|
|---|
| 128 | indent += 1
|
|---|
| 129 | elif (token == "}"):
|
|---|
| 130 | indent -= 1
|
|---|
| 131 | outf.write("\n%s%s" % (tabs(indent), token))
|
|---|
| 132 | elif (token == "*"):
|
|---|
| 133 | outf.write("%s" % token)
|
|---|
| 134 | else:
|
|---|
| 135 | outf.write("%s%s" % (tabs(indent), token))
|
|---|
| 136 |
|
|---|
| 137 | if (empty):
|
|---|
| 138 | outf.write("NULL")
|
|---|
| 139 |
|
|---|
| 140 | outf.write("\n\n\n")
|
|---|
| 141 | inf.close()
|
|---|
| 142 |
|
|---|
| 143 | def recursion(root, output, level):
|
|---|
| 144 | "Recursive directory walk"
|
|---|
| 145 |
|
|---|
| 146 | for name in os.listdir(root):
|
|---|
| 147 | canon = os.path.join(root, name)
|
|---|
| 148 |
|
|---|
| 149 | if ((os.path.isfile(canon)) and (level > 0)):
|
|---|
| 150 | fcomp = split_tokens(canon, ["."])
|
|---|
| 151 | if (fcomp[-1] == ".bp"):
|
|---|
| 152 | parse(canon, outf)
|
|---|
| 153 |
|
|---|
| 154 | if (os.path.isdir(canon)):
|
|---|
| 155 | recursion(canon, outf, level + 1)
|
|---|
| 156 |
|
|---|
| 157 | def main():
|
|---|
| 158 | if (len(sys.argv) < 2):
|
|---|
| 159 | usage(sys.argv[0])
|
|---|
| 160 | return
|
|---|
| 161 |
|
|---|
| 162 | path = os.path.abspath(sys.argv[1])
|
|---|
| 163 | if (not os.path.isdir(path)):
|
|---|
| 164 | print "<OUTPUT> is not a directory"
|
|---|
| 165 | return
|
|---|
| 166 |
|
|---|
| 167 | recursion(".", path, 0)
|
|---|
| 168 |
|
|---|
| 169 | if __name__ == '__main__':
|
|---|
| 170 | main()
|
|---|