def parse(self, lexer, symbols): symbols = Symbols(symbols) tag = "" # anonymous if lexer.tok != '{': tag = lexer.tok if not (tag[0] == '_' or tag[0].isalpha()): self.parse_error(lexer, "expected tag, got '%s'" % tag) lexer.get_token() if tag: self.append(Tag(tag)) else: self.append(Tag()) self.tag = tag if lexer.tok == '{': fieldlist = [] lexer.get_token() if lexer.tok != '}': if not lexer.tok: self.parse_error(lexer) while lexer.tok != '}': node = StructDeclaration() node.parse(lexer, symbols) fieldlist.append(node) self += fieldlist lexer.get_token() if self.verbose: print "%s.__init__() #<--" % (self)
def parse(self, s, verbose=0): self.symbols = Symbols() self.lexer = Lexer(s, verbose=verbose) #,host=__module__) node = None while self.lexer.tok: node = Declaration() node.parse(self.lexer, self.symbols) #sys.stderr.write( "# line %s\n"%self.lexer.lno ) if node: self.append(node)
def parse(self, lexer, symbols): symbols = Symbols(symbols) args = '' #lexer.get_token() if lexer.tok != ')': if not lexer.tok: self.parse_error(lexer) #lexer.unget_token() # unget start of decl while lexer.tok != ')': node = ParameterDeclaration() node.parse(lexer, symbols) self.append(node) if lexer.tok != ')' and lexer.tok != ',': self.parse_error(lexer) if lexer.tok == ',': lexer.get_token() lexer.get_token()
def transform(self, verbose=False, test_parse=False, test_types=False): i = 0 while i < len(self): if verbose: print "##" * 25 declaration = self[i] if verbose: declaration.psource() if verbose: print declaration.deepstr(), '\n' assert isinstance(declaration, Declaration) if verbose: print "# expose declarators from declaration" # STAGE 1 declaration.transform() if verbose: print declaration.deepstr(), '\n' self[i:i + 1] = declaration # expose declarators from declaration for j in range(len(declaration)): declarator = self[i] assert isinstance(declarator, Declarator) if verbose: print "# declarator.transform()" # STAGE 2 declarator.transform() if verbose: print declarator.deepstr(), '\n' if verbose: print "# self.visit_declarator(declarator)" # STAGE 3 self[i] = declarator = self.visit_declarator(declarator) # STAGE 4 if declarator.name: if isinstance(declarator, Typedef): if verbose: print "# typedef %s" % declarator.name self.typedefs[declarator.name] = declarator else: if verbose: print "# sym %s" % declarator.name self.syms[declarator.name] = declarator for node in declarator.nodes(): if isinstance(node, Taged) and node.tag.name: assert type(node.tag.name) == str, node.deepstr() taged = self.tag_lookup.get(node.tag.name, None) if taged is None: if verbose: print "# tag lookup %s = %s" % ( declarator.name, node.tag.name) self.tag_lookup[node.tag.name] = node elif not taged.has_members(): # this is (maybe) the definition of this tag if verbose: print "# definition %s = %s" % ( declarator.name, node.tag.name) self.tag_lookup[node.tag.name] = node # Annotate the TypeAlias's for node in declarator.deepfilter(TypeAlias): name = node[0] assert type(name) == str node.typedef = self.typedefs[name] if verbose: print declarator.deepstr(), '\n' #print declarator.ctype().deepstr(),'\n' #assert declarator.clone() == declarator ################################################### # TESTS: if test_parse: # test that parse of cstr gives same answer cstr = declarator.cstr() + ';\n' if verbose: print '# ' + cstr.replace('\n', '\n# ') #print if isinstance(declarator, Typedef): name = declarator[0][0] assert type(name) == str self.lexer.rmtypedef(name) declaration = cparse.Declaration() self.lexer.lex(cstr) #print self.lexer.err_string() declaration.parse(self.lexer, Symbols()) # use new name-space #declaration.parse( Lexer( cstr ), Symbols() ) declaration = self.convert(declaration) declaration.transform() assert len(declaration) == 1 decl = declaration[0] decl.transform() decl = self.visit_declarator(decl) if decl != declarator: if verbose: print "#???????????" if verbose: print decl.deepstr(), '\n\n' #if verbose: print declaration.deepstr(),'\n\n' #assert 0 elif verbose: print '# OK\n' if test_types: node = declarator.ctype() declare_str = node.declare_str("my_name") if verbose: print "# declarator.ctype() " if verbose: print node.deepstr(), "\n" if verbose: print "#", declare_str.replace('\n', '\n# '), '\n' i = i + 1 return self
def run1(): cstr = "char *(*)() ," node = AbstractDeclarator() node.parse(Lexer(cstr, True), Symbols()) print node.deepstr()