def map_reduce(file_path=None):
  from parser import parse
  from lexer import lexer
  from utils import function, debug_msg, insert_in_tbl
  #build hash table from file or scanning the complete source file
  #check for input args
  #global the_tbl
  the_tbl = global_tbl()
  if not file_path:
    file_path = "."
    print "No file_path specified for parsing the source code starting from current directory"
  if os.path.isfile(file_path) and file_path.endswith(".tbl"):
    #read file to create the_tbl
    f = open(file_path)
    entry = None
    func = None
    for line in f:
      if line.startswith("&#?filename:"):
        #acquire the global_etntry_tbl lock and add this entry
        #for this file
        if entry:
          the_tbl.lock.qcquire()
          the_tbl.tbl[entry.get_file_name()] = entry
          the_tbl.lock.release()
        
        filename_start = line.find(':')
        file_name = line[filename_start+1:]
        print "File name is: ", file_name
        entry = global_tbl_entry()
        entry.set_file_name(file_name)
      elif line.startswith("\t&#?func_name:"):
        #	&#?func_name:func&#?func_args:int a&#?macro_name:MSG&#?message:I am Nandan&#?msg_args:
        func = function()
        function_name_start = line.find(':')
        function_name_end = line.index("&#?func_args:")
        func_name = line[function_name_start+1:function_name_end]
        print "Func name: ", func_name
        function_args_start = function_name_end + 13
        func_args_end = line.index("&#?macro_name:")
        func_args = line[function_args_start+1:func_args_end]
        func.set_name(func_name)
        func.set_args(func_args)
        print "Func args: ", func_args
        #message
        dbg_msg = debug_msg()
        dbg_macro_start = line.find("&#?macro_name:") + 14
        dbg_msg_macro_end = line.index("&#?message:")
        macro_name = line[dbg_macro_start:dbg_msg_macro_end]
        print "Macro name: ", macro_name
        dbg_msg.set_macro_name(macro_name)
        dbg_msg_start = dbg_msg_macro_end + 11
        dbg_msg_end = line.index("&#?msg_args:")
        msg = line[dbg_msg_start:dbg_msg_end]
        print "message: ", msg
        dbg_msg.set_message(msg)
        dbg_msg_args_start = dbg_msg_end + 12
        args = line[dbg_msg_args_start:]
        dbg_msg.set_args(args)
        print "msg args: ", args
        insert_in_tbl(entry,dbg_msg, func)
    #insert the last entry
    if entry:
      the_tbl.lock.acquire()
      the_tbl.tbl[entry.get_file_name()] = entry
      the_tbl.lock.release()

    return the_tbl.tbl
  #else scan/parse directory to make the_tbl

  print file_path
  #create table write table
  the_tbl = make_tbl(file_path)
  save_tbl_in_file(the_tbl.tbl)
  return the_tbl
Exemple #2
0
  def parse_file(self):
    from lexer import lexer, token
    from parser import parser
    from utils import function, debug_msg
    filename = self.filename
    entry = global_tbl_entry()
    print "parser: Lexing on file:", self.filename
    body = file(self.filename, 'rt').read()
    print 'parser: rORIGINAL:', repr(body)
    print
    print
    print 'parser: -------------TOKENS:------------------'
    lexer = lexer(body)
    parser = parser()
    func_name = None
    curly_brace = 0
    small_brace = 0
    args = ""
    for token in lexer:
      #first find a function name store id and lookahead if brace move to state
      print "parser: parsing token: ", token.get_value()," of type: ", token.get_type()
      if parser.get_state() == "Begin":
        print "parser: parser state Begin"
        if token.get_type() == "Id":
          parser.set_state("FuncName")
          func_name = token.get_value()
      elif parser.get_state() == "FuncName":
        type(token.get_value())
        type(token.get_type())
        if token.get_value() == "(":
          parser.set_state("FuncArgs")
        elif token.get_type() == "Id":
          parser.set_state("FuncName")
          func_name = token.get_value()
        else:
          parser.set_state("Begin")
      elif parser.get_state() == "FuncArgs":
        if token.get_value() == ")":
          parser.set_state("FuncBody")
        elif token.get_value() == ",":
          print "parser: Comma"
        elif token.get_type() == "Id":
          args+=token.get_value()
        else:
          print "parser: found: ", token.get_value()," while parser in state Args"
          #reset parser
          parser.set_state("Begin")
      elif parser.get_state() == "FuncBody":
        if token.get_value() == "{":
          #confirmed function update everything
          parser.set_state("Function")
          aFunction = function()
          aFunction.set_name(func_name)
          aFunction.set_args(args);
          print "parser: ***********Found a function by name : ", func_name, " **************************"
          curly_brace += 1
          #insert function
        elif token.get_type() == "Id":
          parser.set_state("FuncName")
          func_name = token.get_value()
        else:
          parser.set_state("Begin")
      elif parser.get_state() == "Function":
        if token.get_value() == "}":
          curly_brace -= 1
          if curly_brace == 0:
            print "parser: ********* Finished function: ",func_name ,"******************"
            #function ends update everything
            parser.set_state("Begin")
            #close messages for this func
        elif token.get_value() == "{":
          curly_brace += 1
        elif token.get_type() == "Debug":
          parser.set_state("Debug")
          dbg_msg = debug_msg()
          print "MAcro Name ===================================================",token.get_value()
          dbg_msg.set_macro_name(token.get_value())
        elif token.get_type() == "Entry/Exit":
          parser.set_state("DebugEntry/Exit")
          dbg_msg = debug_msg()
          print "MAcro Name ==================================================",token.get_value()
          dbg_msg.set_macro_name(token.get_value())
      elif parser.get_state() == "Debug":
        if token.get_value() == "(":
          if small_brace == 0:
            parser.set_state("DbgMsg")
          small_brace += 1
      elif parser.get_state() == "DbgMsg":
        if token.get_type() == "Quotes":
          dbg_msg.set_message(token.get_value())
        elif token.get_value() == ")":
          small_brace -= 1
          if small_brace == 0:
            print "parser: **** Finished one Debug message***** "
            insert_in_tbl(entry, dbg_msg, aFunction);
            parser.set_state("Function")

        else:
          parser.set_state("DbgMsgArgs")

      elif parser.get_state() == "DbgMsgArgs":
        if token.get_value() == ")":
          small_brace -= 1
          if small_brace == 0:
            print "parser: **** Finished one Debug message***** "
            insert_in_tbl(entry, dbg_msg,aFunction);
            parser.set_state("Function")
        if token.get_value() == "(":
          small_brace += 1
        if token.get_type() in ["Id","Quotes"]:
          dbg_msg.append_args(token.get_value())


      print "parser: ======TESTING: Token value: ",token.get_value()
      print "parser: ======TESTING: Token type: ",token.get_type()

    print "parser: ***********all tables ***********************"
    print
    print "parser: -----------Rest-------------------"
    for val in entry.rest_in_list:
      print "parser: Function: ", val.get_function().get_func_name(), " Message: ", val.get_dbg_msg().get_message()," Debug Args: ", val.get_dbg_msg().get_args()

    print
    print "parser: ----------cmplt_msg_tbl--------------------"
    for hash_key in entry.cmplt_msg_tbl.keys():
      val = entry.cmplt_msg_tbl[hash_key]
      print "parser: Function: ", val.get_function().get_func_name(), " Message: ", val.get_dbg_msg().get_message()," Debug Args: ", val.get_dbg_msg().get_args()

    print
    print "parser: ----------partial_msg_tbl--------------------"
    for hash_key in entry.partial_msg_tbl.keys():
      print hash_key
      val = entry.partial_msg_tbl[hash_key]
      print "parser: Function: ", val.get_function().get_func_name(), " Message: ", val.get_dbg_msg().get_message()," Debug Args: ", val.get_dbg_msg().get_args()
    return entry