3 L]o)@s@ddlZddlmZGdddeZGdddeZdZdZdS) N)Templatec@s(eZdZd ddZd ddZddZdS) ASTCodeGenerator _c_ast.cfgcCs ||_dd|j|D|_dS)zN Initialize the code generator from a configuration file. cSsg|]\}}t||qS)NodeCfg).0namecontentsrr9/tmp/pip-install-wfra5znf/pycparser/pycparser/_ast_gen.py sz-ASTCodeGenerator.__init__..N) cfg_filename parse_cfgfilenode_cfg)selfr rrr __init__szASTCodeGenerator.__init__NcCsHttj|jd}|t7}x|jD]}||jd7}q"W|j|dS)z< Generates the code into file, an open file buffer. )r z N)r_PROLOGUE_COMMENT substituter _PROLOGUE_CODErgenerate_sourcewrite)rfilesrcrrrr generates   zASTCodeGenerator.generatec cst|d}x|D]}|j}| s|jdr0q|jd}|jd}|jd}|dksf||ksf||krvtd||f|d|}||d|}|rd d |jd Dng} || fVqWWdQRXdS) ze Parse the configuration file and yield pairs of (name, contents) for each node. r#:[]zInvalid line in %s: %s NcSsg|] }|jqSr)strip)rvrrr r 7sz2ASTCodeGenerator.parse_cfgfile..,)openr startswithfind RuntimeErrorsplit) rfilenameflineZcolon_iZ lbracket_iZ rbracket_irvalZvallistrrr r &s      zASTCodeGenerator.parse_cfgfile)r)N)__name__ __module__ __qualname__rrr rrrr rs  rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)rz Node configuration. name: node name contents: a list of contents - attributes and child nodes See comment at the top of the configuration file for details. cCs||_g|_g|_g|_g|_x^|D]V}|jd}|jj||jdrV|jj|q$|jdrn|jj|q$|jj|q$WdS)N*z**)r all_entriesattrchild seq_childrstripappendendswith)rrr entryZ clean_entryrrr rCs     zNodeCfg.__init__cCs<|j}|d|j7}|d|j7}|d|j7}|S)N ) _gen_init _gen_children _gen_iter_gen_attr_names)rrrrr rUs zNodeCfg.generate_sourcecCsd|j}|jrDdj|j}djdd|jD}|d7}d|}nd}d}|d |7}|d |7}x$|jd gD]}|d ||f7}qrW|S) Nzclass %s(Node): z, css|]}dj|VqdS)z'{0}'N)format)rerrr bsz$NodeCfg._gen_init..z, 'coord', '__weakref__'z(self, %s, coord=None)z'coord', '__weakref__'z(self, coord=None)z __slots__ = (%s) z def __init__%s: Zcoordz self.%s = %s )rr/join)rrargsslotsZarglistrrrr r8]s     zNodeCfg._gen_initcCsld}|jr`|d7}x |jD]}|d t|d7}qWx |jD]}|dt|d7}qsz*NodeCfg._gen_attr_names..))r?r0)rrrrr r;szNodeCfg._gen_attr_namesN) r+r,r-__doc__rrr8r9r:r;rrrr r;sra#----------------------------------------------------------------- # ** ATTENTION ** # This code was automatically generated from the file: # $cfg_filename # # Do not modify it directly. Modify the configuration file and # run the generator again. # ** ** *** ** ** # # pycparser: c_ast.py # # AST Node classes. # # Eli Bendersky [https://eli.thegreenplace.net/] # License: BSD #----------------------------------------------------------------- aX import sys def _repr(obj): """ Get the representation of an object, with dedicated pprint-like format for lists. """ if isinstance(obj, list): return '[' + (',\n '.join((_repr(e).replace('\n', '\n ') for e in obj))) + '\n]' else: return repr(obj) class Node(object): __slots__ = () """ Abstract base class for AST nodes. """ def __repr__(self): """ Generates a python representation of the current node """ result = self.__class__.__name__ + '(' indent = '' separator = '' for name in self.__slots__[:-2]: result += separator result += indent result += name + '=' + (_repr(getattr(self, name)).replace('\n', '\n ' + (' ' * (len(name) + len(self.__class__.__name__))))) separator = ',' indent = '\n ' + (' ' * len(self.__class__.__name__)) result += indent + ')' return result def children(self): """ A sequence of all children that are Nodes """ pass def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showcoord=False, _my_node_name=None): """ Pretty print the Node and all its attributes and children (recursively) to a buffer. buf: Open IO buffer into which the Node is printed. offset: Initial offset (amount of leading spaces) attrnames: True if you want to see the attribute names in name=value pairs. False to only see the values. nodenames: True if you want to see the actual node names within their parents. showcoord: Do you want the coordinates of each Node to be displayed. """ lead = ' ' * offset if nodenames and _my_node_name is not None: buf.write(lead + self.__class__.__name__+ ' <' + _my_node_name + '>: ') else: buf.write(lead + self.__class__.__name__+ ': ') if self.attr_names: if attrnames: nvlist = [(n, getattr(self,n)) for n in self.attr_names] attrstr = ', '.join('%s=%s' % nv for nv in nvlist) else: vlist = [getattr(self, n) for n in self.attr_names] attrstr = ', '.join('%s' % v for v in vlist) buf.write(attrstr) if showcoord: buf.write(' (at %s)' % self.coord) buf.write('\n') for (child_name, child) in self.children(): child.show( buf, offset=offset + 2, attrnames=attrnames, nodenames=nodenames, showcoord=showcoord, _my_node_name=child_name) class NodeVisitor(object): """ A base NodeVisitor class for visiting c_ast nodes. Subclass it and define your own visit_XXX methods, where XXX is the class name you want to visit with these methods. For example: class ConstantVisitor(NodeVisitor): def __init__(self): self.values = [] def visit_Constant(self, node): self.values.append(node.value) Creates a list of values of all the constant nodes encountered below the given node. To use it: cv = ConstantVisitor() cv.visit(node) Notes: * generic_visit() will be called for AST nodes for which no visit_XXX method was defined. * The children of nodes for which a visit_XXX was defined will not be visited - if you need this, call generic_visit() on the node. You can use: NodeVisitor.generic_visit(self, node) * Modeled after Python's own AST visiting facilities (the ast module of Python 3.0) """ _method_cache = None def visit(self, node): """ Visit a node. """ if self._method_cache is None: self._method_cache = {} visitor = self._method_cache.get(node.__class__.__name__, None) if visitor is None: method = 'visit_' + node.__class__.__name__ visitor = getattr(self, method, self.generic_visit) self._method_cache[node.__class__.__name__] = visitor return visitor(node) def generic_visit(self, node): """ Called if no explicit visitor function exists for a node. Implements preorder visiting of the node. """ for c in node: self.visit(c) )pprintstringrobjectrrrrrrrr  s *