Changeset 1993f9a in mainline for contrib/arch/hadlbppp.py
- Timestamp:
- 2009-09-15T13:45:23Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ea5f46d
- Parents:
- ec8bab59
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
contrib/arch/hadlbppp.py
rec8bab59 r1993f9a 48 48 if (trim): 49 49 token = token.strip(" \t") 50 if (token != ""): 51 tokens.append(token) 52 else: 50 51 if (token != ""): 53 52 tokens.append(token) 54 53 55 54 return tokens 56 55 57 def split_tokens(string, delimiters, trim = False ):56 def split_tokens(string, delimiters, trim = False, separate = False): 58 57 "Split string to tokens by delimiters, keep the delimiters" 59 58 … … 66 65 if (len(delim) > 0): 67 66 68 if ((string[i:(i + len(delim))] == delim) and (i > 0)): 69 tokens = cond_append(tokens, string[last:i], trim) 70 last = i 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 71 76 i += len(delim) - 1 72 77 break … … 78 83 return tokens 79 84 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 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 88 146 empty = True 89 147 comment = False 90 148 lcomment = False 91 indent = 092 149 93 150 for token in tokens: … … 116 173 empty = False 117 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 118 186 if ((token == ";") or (token == "+") or (token == "||") or (token == "|")): 119 outf.write(" %s\n" % token) 187 outf.write(" %s" % token) 188 elif (token == "["): 189 inc = True 190 elif (token == "]"): 191 inc = False 120 192 elif (token == "("): 121 outf.write(" %s%s\n" % (tabs(indent), token))193 outf.write("\n%s%s" % (tabs(indent), token)) 122 194 indent += 1 123 195 elif (token == ")"): 196 if (indent == 0): 197 print "%s: Too many closing parentheses" % outname 198 124 199 indent -= 1 125 200 outf.write("\n%s%s" % (tabs(indent), token)) 126 201 elif (token == "{"): 127 outf.write(" %s \n" % token)202 outf.write(" %s" % token) 128 203 indent += 1 129 204 elif (token == "}"): 205 if (indent == 0): 206 print "%s: Too many closing parentheses" % outname 207 130 208 indent -= 1 131 209 outf.write("\n%s%s" % (tabs(indent), token)) 132 210 elif (token == "*"): 133 211 outf.write("%s" % token) 134 else: 135 outf.write("%s%s" % (tabs(indent), token)) 136 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) 137 297 if (empty): 138 298 outf.write("NULL") 139 299 140 outf.write("\n\n\n") 141 inf.close() 142 143 def recursion(root, output, level): 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): 144 314 "Recursive directory walk" 145 315 … … 147 317 canon = os.path.join(root, name) 148 318 149 if ( (os.path.isfile(canon)) and (level > 0)):319 if (os.path.isfile(canon)): 150 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 151 332 if (fcomp[-1] == ".bp"): 152 parse(canon, outf) 333 open_bp(base, root, canon, output_path) 334 elif (fcomp[-1] == ".adl"): 335 open_adl(base, root, canon, output_path) 153 336 154 337 if (os.path.isdir(canon)): 155 recursion( canon, outf, level + 1)338 recursion(base, canon, output, level + 1) 156 339 157 340 def main(): … … 162 345 path = os.path.abspath(sys.argv[1]) 163 346 if (not os.path.isdir(path)): 164 print " <OUTPUT> is not a directory"347 print "Error: <OUTPUT> is not a directory" 165 348 return 166 349 167 recursion(".", path, 0)168 350 recursion(".", ".", path, 0) 351 169 352 if __name__ == '__main__': 170 353 main()
Note:
See TracChangeset
for help on using the changeset viewer.