def load_tables(self, grammar_str, filename, make_grammar_file): if make_grammar_file: with open(filename, 'wb') as fh: fh.write(grammar_str) if self.sig_changed(filename): dparser_swigc.make_tables(grammar_str, filename) with open(filename + '.md5', 'wb') as fh: fh.write(self.sig.digest()) if self.tables: dparser_swigc.unload_parser_tables(self.tables) self.tables = dparser_swigc.load_parser_tables(filename + ".d_parser.dat")
def load_tables(self, grammar_str, filename, make_grammar_file): if make_grammar_file: with open(filename, 'wb') as fh: fh.write(grammar_str) if self.sig_changed(filename): dparser_swigc.make_tables(grammar_str, filename.encode()) with open(filename + '.md5', 'wb') as fh: fh.write(self.sig.digest()) if self.tables: dparser_swigc.unload_parser_tables(self.tables) self.tables = dparser_swigc.load_parser_tables( (filename + ".d_parser.dat").encode('utf-8'))
def load_tables(self,grammar_str, filename, make_grammar_file): if make_grammar_file: g_file = open(filename, "wb") # 'binary' mode has been set to force \n on end of the line g_file.write(grammar_str) g_file.close() if self.sig_changed(filename): dparser_swigc.make_tables(grammar_str, filename) sig_file = open(filename + ".md5", "wb") sig_file.write(self.sig.digest()) sig_file.close() if self.tables: dparser_swigc.unload_parser_tables(self.tables) self.tables = dparser_swigc.load_parser_tables(filename + ".d_parser.dat")
def load_tables(self, grammar_str, filename, make_grammar_file): if make_grammar_file: g_file = open( filename, "wb" ) # 'binary' mode has been set to force \n on end of the line g_file.write(grammar_str) g_file.close() if self.sig_changed(filename): dparser_swigc.make_tables(grammar_str, filename) sig_file = open(filename + ".md5", "wb") sig_file.write(self.sig.digest()) sig_file.close() if self.tables: dparser_swigc.unload_parser_tables(self.tables) self.tables = dparser_swigc.load_parser_tables(filename + ".d_parser.dat")
def __init__(self, modules=None, file_prefix="d_parser_mach_gen"): self.file_prefix = file_prefix self.actions = [] sig = md5.new() sig.update('1.9') if not modules: try: raise RuntimeError except RuntimeError: e, b, t = sys.exc_info() dicts = [t.tb_frame.f_back.f_globals] else: if type(modules) == list: dicts = [module.__dict__ for module in modules] else: dicts = [modules.__dict__] functions = [] for dict in dicts: f = [ val for name, val in dict.items() if (isinstance(val, types.FunctionType)) and name[0:2] == 'd_' ] f.sort(lambda x, y: cmp(x.func_code.co_filename, y.func_code.co_filename) or cmp(x.func_code.co_firstlineno, y.func_code.co_firstlineno)) functions.extend(f) if len(functions) == 0: raise "\nno actions found. Action names must start with 'd_'" app_path = os.path.dirname(sys.argv[0]) if len(app_path) == 0: app_path = os.getcwd() app_path = string.replace(app_path, '\\', '/') self.filename = app_path + '/' + self.file_prefix + ".g" filename = self.filename g_file = open( self.filename, "wb") # 'binary' mode has been set to force \n on end of the line for f in functions: if f.__doc__: g_file.write(f.__doc__) sig.update(f.__doc__) else: raise "\naction missing doc string:\n\t" + f.__name__ g_file.write(";\n${action}\n") if f.func_code.co_argcount == 0: raise "\naction " + f.__name__ + " must take at least one argument\n" speculative = 0 takes_strings = 0 arg_types = [0] for i in range(1, f.func_code.co_argcount): var = f.func_code.co_varnames[i] if var == 'spec': arg_types.append(1) speculative = 1 elif var == 'g': arg_types.append(2) elif var == 's': arg_types.append(3) takes_strings = 1 elif var == 'nodes': arg_types.append(4) elif var == 'this': arg_types.append(5) elif var == 'spec_only': arg_types.append(6) speculative = -1 elif var == 'parser': arg_types.append(7) else: raise "\nunknown argument name:\n\t" + var + "\nin function:\n\t" + f.__name__ self.actions.append((f, arg_types, speculative, takes_strings)) g_file.close() if self.sig_changed(sig): dparser_swigc.make_tables(filename) sig_file = open(self.filename + ".md5", "w") sig_file.write("%s\n" % repr(sig.digest())) sig_file.close() self.tables = dparser_swigc.load_parser_tables(filename + ".d_parser.dat")