Changeset 1993f9a in mainline for contrib/arch/hadlbppp.py


Ignore:
Timestamp:
2009-09-15T13:45:23Z (16 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ea5f46d
Parents:
ec8bab59
Message:

update architecture and behavior description
update preprocessor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • contrib/arch/hadlbppp.py

    rec8bab59 r1993f9a  
    4848        if (trim):
    4949                token = token.strip(" \t")
    50                 if (token != ""):
    51                         tokens.append(token)
    52         else:
     50       
     51        if (token != ""):
    5352                tokens.append(token)
    5453       
    5554        return tokens
    5655
    57 def split_tokens(string, delimiters, trim = False):
     56def split_tokens(string, delimiters, trim = False, separate = False):
    5857        "Split string to tokens by delimiters, keep the delimiters"
    5958       
     
    6665                        if (len(delim) > 0):
    6766                               
    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                                       
    7176                                        i += len(delim) - 1
    7277                                        break
     
    7883        return tokens
    7984
    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        
     85def 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
     123def 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
    88146        empty = True
    89147        comment = False
    90148        lcomment = False
    91         indent = 0
    92149       
    93150        for token in tokens:
     
    116173                        empty = False
    117174               
     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               
    118186                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
    120192                elif (token == "("):
    121                         outf.write("%s%s\n" % (tabs(indent), token))
     193                        outf.write("\n%s%s" % (tabs(indent), token))
    122194                        indent += 1
    123195                elif (token == ")"):
     196                        if (indent == 0):
     197                                print "%s: Too many closing parentheses" % outname
     198                       
    124199                        indent -= 1
    125200                        outf.write("\n%s%s" % (tabs(indent), token))
    126201                elif (token == "{"):
    127                         outf.write(" %s\n" % token)
     202                        outf.write(" %s" % token)
    128203                        indent += 1
    129204                elif (token == "}"):
     205                        if (indent == 0):
     206                                print "%s: Too many closing parentheses" % outname
     207                       
    130208                        indent -= 1
    131209                        outf.write("\n%s%s" % (tabs(indent), token))
    132210                elif (token == "*"):
    133211                        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
     221def 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
     289def 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)
    137297        if (empty):
    138298                outf.write("NULL")
    139299       
    140         outf.write("\n\n\n")
    141         inf.close()
    142 
    143 def recursion(root, output, level):
     300        outf.close()
     301
     302def 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
     313def recursion(base, root, output, level):
    144314        "Recursive directory walk"
    145315       
     
    147317                canon = os.path.join(root, name)
    148318               
    149                 if ((os.path.isfile(canon)) and (level > 0)):
     319                if (os.path.isfile(canon)):
    150320                        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                       
    151332                        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)
    153336               
    154337                if (os.path.isdir(canon)):
    155                         recursion(canon, outf, level + 1)
     338                        recursion(base, canon, output, level + 1)
    156339
    157340def main():
     
    162345        path = os.path.abspath(sys.argv[1])
    163346        if (not os.path.isdir(path)):
    164                 print "<OUTPUT> is not a directory"
     347                print "Error: <OUTPUT> is not a directory"
    165348                return
    166349       
    167         recursion(".", path, 0)
    168        
     350        recursion(".", ".", path, 0)
     351
    169352if __name__ == '__main__':
    170353        main()
Note: See TracChangeset for help on using the changeset viewer.