Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • contrib/arch/hadlbppp.py

    r8c73012 r07fdf203  
    3434import os
    3535
    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 
    4036def usage(prname):
    4137        "Print usage syntax"
    42        
    43         print "%s <OUTPUT>" % prname
     38        print prname + " <OUTPUT>"
    4439
    4540def tabs(cnt):
     
    5348        if (trim):
    5449                token = token.strip(" \t")
    55        
    56         if (token != ""):
     50                if (token != ""):
     51                        tokens.append(token)
     52        else:
    5753                tokens.append(token)
    5854       
    5955        return tokens
    6056
    61 def split_tokens(string, delimiters, trim = False, separate = False):
     57def split_tokens(string, delimiters, trim = False):
    6258        "Split string to tokens by delimiters, keep the delimiters"
    6359       
     
    7066                        if (len(delim) > 0):
    7167                               
    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                                        
     68                                if ((string[i:(i + len(delim))] == delim) and (i > 0)):
     69                                        tokens = cond_append(tokens, string[last:i], trim)
     70                                        last = i
    8171                                        i += len(delim) - 1
    8272                                        break
     
    8878        return tokens
    8979
    90 def identifier(token):
    91         "Check whether the token is an identifier"
     80def parse(fname, outf):
     81        "Parse particular protocol"
    9282       
    93         if (len(token) == 0):
    94                 return False
     83        inf = file(fname, "r")
     84        outf.write("### %s\n\n" % fname)
    9585       
    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
     86        tokens = split_tokens(inf.read(), ["\n", " ", "\t", "(", ")", "{", "}", "[", "/*", "*/", "#", "*", ";", "+", "||", "|", "!", "?"], True)
    10387       
    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 split_bp(protocol):
    168         "Convert Behavior Protocol to tokens"
    169        
    170         return split_tokens(protocol, ["\n", " ", "\t", "(", ")", "{", "}", "*", ";", "+", "||", "|", "!", "?"], True, True)
    171 
    172 def extend_bp(name, tokens, iface):
    173         "Add interface to incoming messages"
    174        
    175         result = []
    176         i = 0
    177        
    178         while (i < len(tokens)):
    179                 result.append(tokens[i])
    180                
    181                 if (tokens[i] == "?"):
    182                         if (i + 1 < len(tokens)):
    183                                 i += 1
    184                                 parts = tokens[i].split(".")
    185                                
    186                                 if (len(parts) == 1):
    187                                         result.append("%s.%s" % (iface, tokens[i]))
    188                                 else:
    189                                         result.append(tokens[i])
    190                         else:
    191                                 print "%s: Unexpected end of protocol" % name
    192                
    193                 i += 1
    194        
    195         return result
    196 
    197 def merge_bp(protocols):
    198         "Merge several Behavior Protocols"
    199        
    200         if (len(protocols) > 1):
    201                 result = []
    202                 first = True
    203                
    204                 for protocol in protocols:
    205                         if (first):
    206                                 first = False
    207                         else:
    208                                 result.append("|")
    209                        
    210                         result.append("(")
    211                         result.extend(protocol)
    212                         result.append(")")
    213                
    214                 return result
    215        
    216         if (len(protocols) == 1):
    217                 return protocols[0]
    218        
    219         return []
    220 
    221 def parse_bp(name, tokens, base_indent):
    222         "Parse Behavior Protocol"
    223        
    224         tokens = preproc_bp(name, tokens)
    225        
    226         indent = base_indent
    227         output = ""
     88        empty = True
     89        comment = False
     90        lcomment = False
     91        indent = 0
    22892       
    22993        for token in tokens:
    230                 if (token == "\n"):
     94                if (comment):
     95                        if (token == "*/"):
     96                                comment = False
    23197                        continue
    23298               
    233                 if ((token == ";") or (token == "+") or (token == "||") or (token == "|")):
    234                         output += " %s" % token
    235                 elif (token == "("):
    236                         output += "\n%s%s" % (tabs(indent), token)
    237                         indent += 1
    238                 elif (token == ")"):
    239                         if (indent < base_indent):
    240                                 print "%s: Too many parentheses" % name
    241                        
    242                         indent -= 1
    243                         output += "\n%s%s" % (tabs(indent), token)
    244                 elif (token == "{"):
    245                         output += " %s" % token
    246                         indent += 1
    247                 elif (token == "}"):
    248                         if (indent < base_indent):
    249                                 print "%s: Too many parentheses" % name
    250                        
    251                         indent -= 1
    252                         output += "\n%s%s" % (tabs(indent), token)
    253                 elif (token == "*"):
    254                         output += "%s" % token
    255                 elif ((token == "!") or (token == "?") or (token == "NULL")):
    256                         output += "\n%s%s" % (tabs(indent), token)
    257                 else:
    258                         output += "%s" % token
    259        
    260         if (indent > base_indent):
    261                 print "%s: Missing parentheses" % name
    262        
    263         output = output.strip()
    264         if (output == ""):
    265                 return "NULL"
    266        
    267         return output
    268 
    269 def get_iface(name):
    270         "Get interface by name"
    271        
    272         global iface_properties
    273        
    274         if (name in iface_properties):
    275                 return iface_properties[name]
    276        
    277         return None
    278 
    279 def dump_frame(frame, outdir):
    280         "Dump Behavior Protocol of a given frame"
    281        
    282         outname = os.path.join(outdir, "%s.bp" % frame['name'])
    283         if (os.path.isfile(outname)):
    284                 print "%s: File already exists, overwriting" % outname
    285        
    286         protocols = []
    287         if ('protocol' in frame):
    288                 protocols.append(frame['protocol'])
    289        
    290         if ('provides' in frame):
    291                 for provides in frame['provides']:
    292                         iface = get_iface(provides['iface'])
    293                         if (not iface is None):
    294                                 if ('protocol' in iface):
    295                                         protocols.append(extend_bp(outname, iface['protocol'], iface['name']))
    296                         else:
    297                                 print "%s: Provided interface '%s' is undefined" % (frame['name'], provides['iface'])
    298        
    299         outf = file(outname, "w")
    300         outf.write(parse_bp(outname, merge_bp(protocols), 0))
    301         outf.close()
    302 
    303 def get_system_arch():
    304         "Get system architecture"
    305        
    306         global arch_properties
    307        
    308         for arch, properties in arch_properties.items():
    309                 if ('system' in properties):
    310                         return properties
    311        
    312         return None
    313 
    314 def get_arch(name):
    315         "Get architecture by name"
    316        
    317         global arch_properties
    318        
    319         if (name in arch_properties):
    320                 return arch_properties[name]
    321        
    322         return None
    323 
    324 def get_frame(name):
    325         "Get frame by name"
    326        
    327         global frame_properties
    328        
    329         if (name in frame_properties):
    330                 return frame_properties[name]
    331        
    332         return None
    333 
    334 def create_null_bp(name, outdir):
    335         "Create null frame protocol"
    336        
    337         outname = os.path.join(outdir, name)
    338         if (not os.path.isfile(outname)):
    339                 outf = file(outname, "w")
    340                 outf.write("NULL")
    341                 outf.close()
    342 
    343 def dump_arch(arch, outdir):
    344         "Dump architecture Behavior Protocol"
    345        
    346         outname = os.path.join(outdir, "%s.archbp" % arch['name'])
    347         if (os.path.isfile(outname)):
    348                 print "%s: File already exists, overwriting" % outname
    349        
    350         outf = file(outname, "w")
    351        
    352         create_null_bp("null.bp", outdir)
    353         outf.write("frame \"null.bp\"\n\n")
    354        
    355         if ('inst' in arch):
    356                 for inst in arch['inst']:
    357                         subarch = get_arch(inst['type'])
    358                         if (not subarch is None):
    359                                 outf.write("instantiate %s from \"%s.archbp\"\n" % (inst['var'], inst['type']))
    360                                 dump_arch(subarch, outdir)
    361                         else:
    362                                 subframe = get_frame(inst['type'])
    363                                 if (not subframe is None):
    364                                         outf.write("instantiate %s from \"%s.bp\"\n" % (inst['var'], inst['type']))
    365                                         dump_frame(subframe, outdir)
    366                                 else:
    367                                         print "%s: '%s' is neither architecture nor frame" % (arch['name'], inst['type'])
    368                
    369                 outf.write("\n")
    370        
    371         if ('bind' in arch):
    372                 for bind in arch['bind']:
    373                         outf.write("bind %s.%s to %s.%s\n" % (bind['from'][0], bind['from'][1], bind['to'][0], bind['to'][1]))
    374        
    375         outf.close()
    376 
    377 def preproc_adl(raw, inarg):
    378         "Preprocess %% statements in ADL"
    379        
    380         return raw.replace("%%", inarg)
    381 
    382 def parse_adl(base, root, inname, nested, indent):
    383         "Parse Architecture Description Language"
    384        
    385         global output
    386         global context
    387         global architecture
    388         global interface
    389         global frame
    390         global protocol
    391        
    392         global iface_properties
    393         global frame_properties
    394         global arch_properties
    395        
    396         global arg0
    397        
    398         if (nested):
    399                 parts = inname.split("%")
    400                
    401                 if (len(parts) > 1):
    402                         inarg = parts[1]
    403                 else:
    404                         inarg = "%%"
    405                
    406                 if (parts[0][0:1] == "/"):
    407                         path = os.path.join(base, ".%s" % parts[0])
    408                         nested_root = os.path.dirname(path)
    409                 else:
    410                         path = os.path.join(root, parts[0])
    411                         nested_root = root
    412                
    413                 if (not os.path.isfile(path)):
    414                         print "%s: Unable to include file %s" % (inname, path)
    415                         return ""
    416         else:
    417                 inarg = "%%"
    418                 path = inname
    419                 nested_root = root
    420        
    421         inf = file(path, "r")
    422        
    423         raw = preproc_adl(inf.read(), inarg)
    424         tokens = split_tokens(raw, ["\n", " ", "\t", "(", ")", "{", "}", "[", "]", "/*", "*/", "#", ";"], True, True)
    425        
    426         for token in tokens:
    427                
    428                 # Includes
    429                
    430                 if (INC in context):
    431                         context.remove(INC)
    432                         parse_adl(base, nested_root, token, True, indent)
    433                         context.add(POST_INC)
     99                if ((not comment) and (token == "/*")):
     100                        comment = True
    434101                        continue
    435102               
    436                 if (POST_INC in context):
    437                         if (token != "]"):
    438                                 print "%s: Expected ]" % inname
    439                        
    440                         context.remove(POST_INC)
     103                if (lcomment):
     104                        if (token == "\n"):
     105                                lcomment = False
    441106                        continue
    442107               
    443                 # Comments and newlines
    444                
    445                 if (BLOCK_COMMENT in context):
    446                         if (token == "*/"):
    447                                 context.remove(BLOCK_COMMENT)
    448                        
    449                         continue
    450                
    451                 if (LINE_COMMENT in context):
    452                         if (token == "\n"):
    453                                 context.remove(LINE_COMMENT)
    454                        
    455                         continue
    456                
    457                 # Any context
    458                
    459                 if (token == "/*"):
    460                         context.add(BLOCK_COMMENT)
    461                         continue
    462                
    463                 if (token == "#"):
    464                         context.add(LINE_COMMENT)
    465                         continue
    466                
    467                 if (token == "["):
    468                         context.add(INC)
     108                if ((not lcomment) and (token == "#")):
     109                        lcomment = True
    469110                        continue
    470111               
     
    472113                        continue
    473114               
    474                 # "frame"
     115                if (empty):
     116                        empty = False
    475117               
    476                 if (FRAME in context):
    477                         if (NULL in context):
    478                                 if (token != ";"):
    479                                         print "%s: Expected ';' in frame '%s'" % (inname, frame)
    480                                 else:
    481                                         output += "%s\n" % token
    482                                
    483                                 context.remove(NULL)
    484                                 context.remove(FRAME)
    485                                 frame = None
    486                                 continue
    487                        
    488                         if (BODY in context):
    489                                 if (PROTOCOL in context):
    490                                         if (token == "{"):
    491                                                 indent += 1
    492                                         elif (token == "}"):
    493                                                 indent -= 1
    494                                        
    495                                         if (indent == -1):
    496                                                 bp = split_bp(protocol)
    497                                                 protocol = None
    498                                                
    499                                                 if (not frame in frame_properties):
    500                                                         frame_properties[frame] = {}
    501                                                
    502                                                 if ('protocol' in frame_properties[frame]):
    503                                                         print "%s: Protocol for frame '%s' already defined" % (inname, frame)
    504                                                 else:
    505                                                         frame_properties[frame]['protocol'] = bp
    506                                                
    507                                                 output += "\n%s" % tabs(2)
    508                                                 output += parse_bp(inname, bp, 2)
    509                                                 output += "\n%s" % token
    510                                                 indent = 0
    511                                                
    512                                                 context.remove(PROTOCOL)
    513                                                 context.remove(BODY)
    514                                                 context.add(NULL)
    515                                         else:
    516                                                 protocol += token
    517                                        
    518                                         continue
    519                                
    520                                 if (REQUIRES in context):
    521                                         if (FIN in context):
    522                                                 if (token != ";"):
    523                                                         print "%s: Expected ';' in frame '%s'" % (inname, frame)
    524                                                 else:
    525                                                         output += "%s" % token
    526                                                
    527                                                 context.remove(FIN)
    528                                                 continue
    529                                        
    530                                         if (VAR in context):
    531                                                 if (not identifier(token)):
    532                                                         print "%s: Variable name expected in frame '%s'" % (inname, frame)
    533                                                 else:
    534                                                         if (not frame in frame_properties):
    535                                                                 frame_properties[frame] = {}
    536                                                        
    537                                                         if (not 'requires' in frame_properties[frame]):
    538                                                                 frame_properties[frame]['requires'] = []
    539                                                        
    540                                                         frame_properties[frame]['requires'].append({'iface': arg0, 'var': token})
    541                                                         arg0 = None
    542                                                        
    543                                                         output += "%s" % token
    544                                                
    545                                                 context.remove(VAR)
    546                                                 context.add(FIN)
    547                                                 continue
    548                                        
    549                                         if ((token == "}") or (token[-1] == ":")):
    550                                                 context.remove(REQUIRES)
    551                                         else:
    552                                                 if (not identifier(token)):
    553                                                         print "%s: Interface name expected in frame '%s'" % (inname, frame)
    554                                                 else:
    555                                                         arg0 = token
    556                                                         output += "\n%s%s " % (tabs(indent), token)
    557                                                
    558                                                 context.add(VAR)
    559                                                 continue
    560                                
    561                                 if (PROVIDES in context):
    562                                         if (FIN in context):
    563                                                 if (token != ";"):
    564                                                         print "%s: Expected ';' in frame '%s'" % (inname, frame)
    565                                                 else:
    566                                                         output += "%s" % token
    567                                                
    568                                                 context.remove(FIN)
    569                                                 continue
    570                                        
    571                                         if (VAR in context):
    572                                                 if (not identifier(token)):
    573                                                         print "%s: Variable name expected in frame '%s'" % (inname, frame)
    574                                                 else:
    575                                                         if (not frame in frame_properties):
    576                                                                 frame_properties[frame] = {}
    577                                                        
    578                                                         if (not 'provides' in frame_properties[frame]):
    579                                                                 frame_properties[frame]['provides'] = []
    580                                                        
    581                                                         frame_properties[frame]['provides'].append({'iface': arg0, 'var': token})
    582                                                         arg0 = None
    583                                                        
    584                                                         output += "%s" % token
    585                                                
    586                                                 context.remove(VAR)
    587                                                 context.add(FIN)
    588                                                 continue
    589                                        
    590                                         if ((token == "}") or (token[-1] == ":")):
    591                                                 context.remove(PROVIDES)
    592                                         else:
    593                                                 if (not identifier(token)):
    594                                                         print "%s: Interface name expected in frame '%s'" % (inname, frame)
    595                                                 else:
    596                                                         arg0 = token
    597                                                         output += "\n%s%s " % (tabs(indent), token)
    598                                                
    599                                                 context.add(VAR)
    600                                                 continue
    601                                
    602                                 if (token == "}"):
    603                                         if (indent != 2):
    604                                                 print "%s: Wrong number of parentheses in frame '%s'" % (inname, frame)
    605                                         else:
    606                                                 indent = 0
    607                                                 output += "\n%s" % token
    608                                        
    609                                         context.remove(BODY)
    610                                         context.add(NULL)
    611                                         continue
    612                                
    613                                 if (token == "provides:"):
    614                                         output += "\n%s%s" % (tabs(indent - 1), token)
    615                                         context.add(PROVIDES)
    616                                         protocol = ""
    617                                         continue
    618                                
    619                                 if (token == "requires:"):
    620                                         output += "\n%s%s" % (tabs(indent - 1), token)
    621                                         context.add(REQUIRES)
    622                                         protocol = ""
    623                                         continue
    624                                
    625                                 if (token == "protocol:"):
    626                                         output += "\n%s%s" % (tabs(indent - 1), token)
    627                                         indent = 0
    628                                         context.add(PROTOCOL)
    629                                         protocol = ""
    630                                         continue
    631                                
    632                                 print "%s: Unknown token '%s' in frame '%s'" % (inname, token, frame)
    633                                 continue
    634                        
    635                         if (HEAD in context):
    636                                 if (token == "{"):
    637                                         output += "%s" % token
    638                                         indent += 2
    639                                         context.remove(HEAD)
    640                                         context.add(BODY)
    641                                         continue
    642                                
    643                                 if (token == ";"):
    644                                         output += "%s\n" % token
    645                                         context.remove(HEAD)
    646                                         context.remove(FRAME)
    647                                         continue
    648                                
    649                                 print "%s: Unknown token '%s' in frame head '%s'" % (inname, token, frame)
    650                                
    651                                 continue
    652                        
    653                         if (not identifier(token)):
    654                                 print "%s: Expected frame name" % inname
    655                         else:
    656                                 frame = token
    657                                 output += "%s " % token
    658                                
    659                                 if (not frame in frame_properties):
    660                                         frame_properties[frame] = {}
    661                                
    662                                 frame_properties[frame]['name'] = frame
    663                        
    664                         context.add(HEAD)
    665                         continue
    666                
    667                 # "interface"
    668                
    669                 if (IFACE in context):
    670                         if (NULL in context):
    671                                 if (token != ";"):
    672                                         print "%s: Expected ';' in interface '%s'" % (inname, interface)
    673                                 else:
    674                                         output += "%s\n" % token
    675                                
    676                                 context.remove(NULL)
    677                                 context.remove(IFACE)
    678                                 interface = None
    679                                 continue
    680                        
    681                         if (BODY in context):
    682                                 if (PROTOCOL in context):
    683                                         if (token == "{"):
    684                                                 indent += 1
    685                                         elif (token == "}"):
    686                                                 indent -= 1
    687                                        
    688                                         if (indent == -1):
    689                                                 bp = split_bp(protocol)
    690                                                 protocol = None
    691                                                
    692                                                 if (not interface in iface_properties):
    693                                                         iface_properties[interface] = {}
    694                                                
    695                                                 if ('protocol' in iface_properties[interface]):
    696                                                         print "%s: Protocol for interface '%s' already defined" % (inname, interface)
    697                                                 else:
    698                                                         iface_properties[interface]['protocol'] = bp
    699                                                
    700                                                 output += "\n%s" % tabs(2)
    701                                                 output += parse_bp(inname, bp, 2)
    702                                                 output += "\n%s" % token
    703                                                 indent = 0
    704                                                
    705                                                 context.remove(PROTOCOL)
    706                                                 context.remove(BODY)
    707                                                 context.add(NULL)
    708                                         else:
    709                                                 protocol += token
    710                                        
    711                                         continue
    712                                
    713                                 if (PROTOTYPE in context):
    714                                         if (FIN in context):
    715                                                 if (token != ";"):
    716                                                         print "%s: Expected ';' in interface '%s'" % (inname, interface)
    717                                                 else:
    718                                                         output += "%s" % token
    719                                                
    720                                                 context.remove(FIN)
    721                                                 context.remove(PROTOTYPE)
    722                                                 continue
    723                                        
    724                                         if (PAR_RIGHT in context):
    725                                                 if (token == ")"):
    726                                                         output += "%s" % token
    727                                                         context.remove(PAR_RIGHT)
    728                                                         context.add(FIN)
    729                                                 else:
    730                                                         output += " %s" % token
    731                                                
    732                                                 continue
    733                                        
    734                                         if (SIGNATURE in context):
    735                                                 output += "%s" % token
    736                                                 if (token == ")"):
    737                                                         context.remove(SIGNATURE)
    738                                                         context.add(FIN)
    739                                                
    740                                                 context.remove(SIGNATURE)
    741                                                 context.add(PAR_RIGHT)
    742                                                 continue
    743                                        
    744                                         if (PAR_LEFT in context):
    745                                                 if (token != "("):
    746                                                         print "%s: Expected '(' in interface '%s'" % (inname, interface)
    747                                                 else:
    748                                                         output += "%s" % token
    749                                                
    750                                                 context.remove(PAR_LEFT)
    751                                                 context.add(SIGNATURE)
    752                                                 continue
    753                                        
    754                                         if (not identifier(token)):
    755                                                 print "%s: Method identifier expected in interface '%s'" % (inname, interface)
    756                                         else:
    757                                                 output += "%s" % token
    758                                        
    759                                         context.add(PAR_LEFT)
    760                                         continue
    761                                
    762                                 if (token == "}"):
    763                                         if (indent != 2):
    764                                                 print "%s: Wrong number of parentheses in interface '%s'" % (inname, interface)
    765                                         else:
    766                                                 indent = 0
    767                                                 output += "\n%s" % token
    768                                        
    769                                         context.remove(BODY)
    770                                         context.add(NULL)
    771                                         continue
    772                                
    773                                 if ((token == "ipcarg_t") or (token == "unative_t")):
    774                                         output += "\n%s%s " % (tabs(indent), token)
    775                                         context.add(PROTOTYPE)
    776                                         continue
    777                                
    778                                 if (token == "protocol:"):
    779                                         output += "\n%s%s" % (tabs(indent - 1), token)
    780                                         indent = 0
    781                                         context.add(PROTOCOL)
    782                                         protocol = ""
    783                                         continue
    784                                
    785                                 print "%s: Unknown token '%s' in interface '%s'" % (inname, token, interface)
    786                                 continue
    787                        
    788                         if (HEAD in context):
    789                                 if (token == "{"):
    790                                         output += "%s" % token
    791                                         indent += 2
    792                                         context.remove(HEAD)
    793                                         context.add(BODY)
    794                                         continue
    795                                
    796                                 if (token == ";"):
    797                                         output += "%s\n" % token
    798                                         context.remove(HEAD)
    799                                         context.remove(ARCH)
    800                                         continue
    801                                
    802                                 if (not word(token)):
    803                                         print "%s: Expected word in interface head '%s'" % (inname, interface)
    804                                 else:
    805                                         output += "%s " % token
    806                                
    807                                 continue
    808                        
    809                         if (not identifier(token)):
    810                                 print "%s: Expected interface name" % inname
    811                         else:
    812                                 interface = token
    813                                 output += "%s " % token
    814                                
    815                                 if (not interface in iface_properties):
    816                                         iface_properties[interface] = {}
    817                                
    818                                 iface_properties[interface]['name'] = interface
    819                        
    820                         context.add(HEAD)
    821                         continue
    822                
    823                 # "architecture"
    824                
    825                 if (ARCH in context):
    826                         if (NULL in context):
    827                                 if (token != ";"):
    828                                         print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
    829                                 else:
    830                                         output += "%s\n" % token
    831                                
    832                                 context.remove(NULL)
    833                                 context.remove(ARCH)
    834                                 context.discard(SYSTEM)
    835                                 architecture = None
    836                                 continue
    837                        
    838                         if (BODY in context):
    839                                 if (DELEGATE in context):
    840                                         if (FIN in context):
    841                                                 if (token != ";"):
    842                                                         print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
    843                                                 else:
    844                                                         output += "%s" % token
    845                                                
    846                                                 context.remove(FIN)
    847                                                 context.remove(DELEGATE)
    848                                                 continue
    849                                        
    850                                         if (VAR in context):
    851                                                 if (not descriptor(token)):
    852                                                         print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
    853                                                 else:
    854                                                         if (not architecture in arch_properties):
    855                                                                 arch_properties[architecture] = {}
    856                                                        
    857                                                         if (not 'delegate' in arch_properties[architecture]):
    858                                                                 arch_properties[architecture]['delegate'] = []
    859                                                        
    860                                                         arch_properties[architecture]['delegate'].append({'from': arg0, 'to': token.split(":")})
    861                                                         arg0 = None
    862                                                        
    863                                                         output += "%s" % token
    864                                                
    865                                                 context.add(FIN)
    866                                                 context.remove(VAR)
    867                                                 continue
    868                                        
    869                                         if (TO in context):
    870                                                 if (token != "to"):
    871                                                         print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
    872                                                 else:
    873                                                         output += "%s " % token
    874                                                
    875                                                 context.add(VAR)
    876                                                 context.remove(TO)
    877                                                 continue
    878                                        
    879                                         if (not identifier(token)):
    880                                                 print "%s: Expected interface name in architecture '%s'" % (inname, architecture)
    881                                         else:
    882                                                 output += "%s " % token
    883                                                 arg0 = token
    884                                        
    885                                         context.add(TO)
    886                                         continue
    887                                
    888                                 if (SUBSUME in context):
    889                                         if (FIN in context):
    890                                                 if (token != ";"):
    891                                                         print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
    892                                                 else:
    893                                                         output += "%s" % token
    894                                                
    895                                                 context.remove(FIN)
    896                                                 context.remove(SUBSUME)
    897                                                 continue
    898                                        
    899                                         if (VAR in context):
    900                                                 if (not identifier(token)):
    901                                                         print "%s: Expected interface name in architecture '%s'" % (inname, architecture)
    902                                                 else:
    903                                                         if (not architecture in arch_properties):
    904                                                                 arch_properties[architecture] = {}
    905                                                        
    906                                                         if (not 'subsume' in arch_properties[architecture]):
    907                                                                 arch_properties[architecture]['subsume'] = []
    908                                                        
    909                                                         arch_properties[architecture]['subsume'].append({'from': arg0.split(":"), 'to': token})
    910                                                         arg0 = None
    911                                                        
    912                                                         output += "%s" % token
    913                                                
    914                                                 context.add(FIN)
    915                                                 context.remove(VAR)
    916                                                 continue
    917                                        
    918                                         if (TO in context):
    919                                                 if (token != "to"):
    920                                                         print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
    921                                                 else:
    922                                                         output += "%s " % token
    923                                                
    924                                                 context.add(VAR)
    925                                                 context.remove(TO)
    926                                                 continue
    927                                        
    928                                         if (not descriptor(token)):
    929                                                 print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
    930                                         else:
    931                                                 output += "%s " % token
    932                                                 arg0 = token
    933                                        
    934                                         context.add(TO)
    935                                         continue
    936                                
    937                                 if (BIND in context):
    938                                         if (FIN in context):
    939                                                 if (token != ";"):
    940                                                         print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
    941                                                 else:
    942                                                         output += "%s" % token
    943                                                
    944                                                 context.remove(FIN)
    945                                                 context.remove(BIND)
    946                                                 continue
    947                                        
    948                                         if (VAR in context):
    949                                                 if (not descriptor(token)):
    950                                                         print "%s: Expected second interface descriptor in architecture '%s'" % (inname, architecture)
    951                                                 else:
    952                                                         if (not architecture in arch_properties):
    953                                                                 arch_properties[architecture] = {}
    954                                                        
    955                                                         if (not 'bind' in arch_properties[architecture]):
    956                                                                 arch_properties[architecture]['bind'] = []
    957                                                        
    958                                                         arch_properties[architecture]['bind'].append({'from': arg0.split(":"), 'to': token.split(":")})
    959                                                         arg0 = None
    960                                                        
    961                                                         output += "%s" % token
    962                                                
    963                                                 context.add(FIN)
    964                                                 context.remove(VAR)
    965                                                 continue
    966                                        
    967                                         if (TO in context):
    968                                                 if (token != "to"):
    969                                                         print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
    970                                                 else:
    971                                                         output += "%s " % token
    972                                                
    973                                                 context.add(VAR)
    974                                                 context.remove(TO)
    975                                                 continue
    976                                        
    977                                         if (not descriptor(token)):
    978                                                 print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
    979                                         else:
    980                                                 output += "%s " % token
    981                                                 arg0 = token
    982                                        
    983                                         context.add(TO)
    984                                         continue
    985                                
    986                                 if (INST in context):
    987                                         if (FIN in context):
    988                                                 if (token != ";"):
    989                                                         print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
    990                                                 else:
    991                                                         output += "%s" % token
    992                                                
    993                                                 context.remove(FIN)
    994                                                 context.remove(INST)
    995                                                 continue
    996                                        
    997                                         if (VAR in context):
    998                                                 if (not identifier(token)):
    999                                                         print "%s: Expected instance name in architecture '%s'" % (inname, architecture)
    1000                                                 else:
    1001                                                         if (not architecture in arch_properties):
    1002                                                                 arch_properties[architecture] = {}
    1003                                                        
    1004                                                         if (not 'inst' in arch_properties[architecture]):
    1005                                                                 arch_properties[architecture]['inst'] = []
    1006                                                        
    1007                                                         arch_properties[architecture]['inst'].append({'type': arg0, 'var': token})
    1008                                                         arg0 = None
    1009                                                        
    1010                                                         output += "%s" % token
    1011                                                
    1012                                                 context.add(FIN)
    1013                                                 context.remove(VAR)
    1014                                                 continue
    1015                                        
    1016                                         if (not identifier(token)):
    1017                                                 print "%s: Expected frame/architecture type in architecture '%s'" % (inname, architecture)
    1018                                         else:
    1019                                                 output += "%s " % token
    1020                                                 arg0 = token
    1021                                        
    1022                                         context.add(VAR)
    1023                                         continue
    1024                                
    1025                                 if (token == "}"):
    1026                                         if (indent != 1):
    1027                                                 print "%s: Wrong number of parentheses in architecture '%s'" % (inname, architecture)
    1028                                         else:
    1029                                                 indent -= 1
    1030                                                 output += "\n%s" % token
    1031                                        
    1032                                         context.remove(BODY)
    1033                                         context.add(NULL)
    1034                                         continue
    1035                                
    1036                                 if (token == "inst"):
    1037                                         output += "\n%s%s " % (tabs(indent), token)
    1038                                         context.add(INST)
    1039                                         continue
    1040                                
    1041                                 if (token == "bind"):
    1042                                         output += "\n%s%s " % (tabs(indent), token)
    1043                                         context.add(BIND)
    1044                                         continue
    1045                                
    1046                                 if (token == "subsume"):
    1047                                         output += "\n%s%s " % (tabs(indent), token)
    1048                                         context.add(SUBSUME)
    1049                                         continue
    1050                                
    1051                                 if (token == "delegate"):
    1052                                         output += "\n%s%s " % (tabs(indent), token)
    1053                                         context.add(DELEGATE)
    1054                                         continue
    1055                                
    1056                                 print "%s: Unknown token '%s' in architecture '%s'" % (inname, token, architecture)
    1057                                 continue
    1058                        
    1059                         if (HEAD in context):
    1060                                 if (token == "{"):
    1061                                         output += "%s" % token
    1062                                         indent += 1
    1063                                         context.remove(HEAD)
    1064                                         context.add(BODY)
    1065                                         continue
    1066                                
    1067                                 if (token == ";"):
    1068                                         output += "%s\n" % token
    1069                                         context.remove(HEAD)
    1070                                         context.remove(ARCH)
    1071                                         context.discard(SYSTEM)
    1072                                         continue
    1073                                
    1074                                 if (not word(token)):
    1075                                         print "%s: Expected word in architecture head '%s'" % (inname, architecture)
    1076                                 else:
    1077                                         output += "%s " % token
    1078                                
    1079                                 continue
    1080                        
    1081                         if (not identifier(token)):
    1082                                 print "%s: Expected architecture name" % inname
    1083                         else:
    1084                                 architecture = token
    1085                                 output += "%s " % token
    1086                                
    1087                                 if (not architecture in arch_properties):
    1088                                         arch_properties[architecture] = {}
    1089                                
    1090                                 arch_properties[architecture]['name'] = architecture
    1091                                
    1092                                 if (SYSTEM in context):
    1093                                         arch_properties[architecture]['system'] = True
    1094                        
    1095                         context.add(HEAD)
    1096                         continue
    1097                
    1098                 # "system architecture"
    1099                
    1100                 if (SYSTEM in context):
    1101                         if (token != "architecture"):
    1102                                 print "%s: Expected 'architecture'" % inname
    1103                         else:
    1104                                 output += "%s " % token
    1105                        
    1106                         context.add(ARCH)
    1107                         continue
    1108                
    1109                 if (token == "frame"):
    1110                         output += "\n%s " % token
    1111                         context.add(FRAME)
    1112                         continue
    1113                
    1114                 if (token == "interface"):
    1115                         output += "\n%s " % token
    1116                         context.add(IFACE)
    1117                         continue
    1118                
    1119                 if (token == "system"):
    1120                         output += "\n%s " % token
    1121                         context.add(SYSTEM)
    1122                         continue
    1123                
    1124                 if (token == "architecture"):
    1125                         output += "\n%s " % token
    1126                         context.add(ARCH)
    1127                         continue
    1128                
    1129                 print "%s: Unknown token '%s'" % (inname, token)
     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))
    1130136       
     137        if (empty):
     138                outf.write("NULL")
     139       
     140        outf.write("\n\n\n")
    1131141        inf.close()
    1132142
    1133 def open_adl(base, root, inname, outdir, outname):
    1134         "Open Architecture Description file"
    1135        
    1136         global output
    1137         global context
    1138         global architecture
    1139         global interface
    1140         global frame
    1141         global protocol
    1142        
    1143         global arg0
    1144        
    1145         output = ""
    1146         context = set()
    1147         architecture = None
    1148         interface = None
    1149         frame = None
    1150         protocol = None
    1151         arg0 = None
    1152        
    1153         parse_adl(base, root, inname, False, 0)
    1154         output = output.strip()
    1155        
    1156         if (output != ""):
    1157                 if (os.path.isfile(outname)):
    1158                         print "%s: File already exists, overwriting" % outname
    1159                
    1160                 outf = file(outname, "w")
    1161                 outf.write(output)
    1162                 outf.close()
    1163         else:
    1164                 if (os.path.isfile(outname)):
    1165                         print "%s: File already exists, but should be empty" % outname
    1166 
    1167 def recursion(base, root, output, level):
     143def recursion(root, output, level):
    1168144        "Recursive directory walk"
    1169145       
     
    1171147                canon = os.path.join(root, name)
    1172148               
    1173                 if (os.path.isfile(canon)):
     149                if ((os.path.isfile(canon)) and (level > 0)):
    1174150                        fcomp = split_tokens(canon, ["."])
    1175                         cname = canon.split("/")
    1176                        
    1177                         if (fcomp[-1] == ".adl"):
    1178                                 output_path = os.path.join(output, cname[-1])
    1179                                 open_adl(base, root, canon, output, output_path)
     151                        if (fcomp[-1] == ".bp"):
     152                                parse(canon, outf)
    1180153               
    1181154                if (os.path.isdir(canon)):
    1182                         recursion(base, canon, output, level + 1)
     155                        recursion(canon, outf, level + 1)
    1183156
    1184157def main():
    1185         global iface_properties
    1186         global frame_properties
    1187         global arch_properties
    1188        
    1189158        if (len(sys.argv) < 2):
    1190159                usage(sys.argv[0])
     
    1193162        path = os.path.abspath(sys.argv[1])
    1194163        if (not os.path.isdir(path)):
    1195                 print "Error: <OUTPUT> is not a directory"
     164                print "<OUTPUT> is not a directory"
    1196165                return
    1197166       
    1198         iface_properties = {}
    1199         frame_properties = {}
    1200         arch_properties = {}
     167        recursion(".", path, 0)
    1201168       
    1202         recursion(".", ".", path, 0)
    1203        
    1204         system_arch = get_system_arch()
    1205        
    1206         if (not system_arch is None):
    1207                 dump_arch(system_arch, path)
    1208 
    1209169if __name__ == '__main__':
    1210170        main()
Note: See TracChangeset for help on using the changeset viewer.