B
    CrX[o)  ã               @   s@   d dl Z d dlmZ G dd„ deƒZG dd„ deƒZdZdZdS )	é    N)ÚTemplatec               @   s(   e Zd Zd	dd„Zd
dd„Zdd„ ZdS )ÚASTCodeGeneratorú
_c_ast.cfgc             C   s    || _ dd„ |  |¡D ƒ| _dS )zN Initialize the code generator from a configuration
            file.
        c             S   s   g | ]\}}t ||ƒ‘qS © )ÚNodeCfg)Ú.0ÚnameÚcontentsr   r   ú1lib/python3.7/site-packages/pycparser/_ast_gen.pyú
<listcomp>   s   z-ASTCodeGenerator.__init__.<locals>.<listcomp>N)Úcfg_filenameÚparse_cfgfileÚnode_cfg)Úselfr   r   r   r
   Ú__init__   s    zASTCodeGenerator.__init__Nc             C   sH   t tƒj| jd}|t7 }x| jD ]}|| ¡ d 7 }q"W | |¡ dS )z< Generates the code into file, an open file buffer.
        )r   z

N)r   Ú_PROLOGUE_COMMENTZ
substituter   Ú_PROLOGUE_CODEr   Úgenerate_sourceÚwrite)r   ÚfileÚsrcr   r   r   r
   Úgenerate   s    
zASTCodeGenerator.generatec       
   	   c   sÈ   t |dƒ´}x¬|D ]¤}| ¡ }|r| d¡r.q| d¡}| d¡}| d¡}|dk sd||ksd||krttd||f ƒ‚|d|… }||d |… }|r¨d	d
„ | d¡D ƒng }	||	fV  qW W dQ R X dS )ze Parse the configuration file and yield pairs of
            (name, contents) for each node.
        Úrú#ú:ú[ú]é   zInvalid line in %s:
%s
Nc             S   s   g | ]}|  ¡ ‘qS r   )Ústrip)r   Úvr   r   r
   r   7   s    z2ASTCodeGenerator.parse_cfgfile.<locals>.<listcomp>ú,)Úopenr   Ú
startswithÚfindÚRuntimeErrorÚsplit)
r   ÚfilenameÚfÚlineZcolon_iZ
lbracket_iZ
rbracket_ir   ÚvalZvallistr   r   r
   r   &   s    



zASTCodeGenerator.parse_cfgfile)r   )N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r
   r      s   

r   c               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )r   zº 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.
    c             C   s‚   || _ g | _g | _g | _g | _x^|D ]V}| d¡}| j |¡ | d¡rV| j |¡ q$| d¡rn| j |¡ q$| j |¡ q$W d S )NÚ*z**)r   Úall_entriesÚattrÚchildÚ	seq_childÚrstripÚappendÚendswith)r   r   r	   ÚentryZclean_entryr   r   r
   r   C   s    



zNodeCfg.__init__c             C   s<   |   ¡ }|d|  ¡  7 }|d|  ¡  7 }|d|  ¡  7 }|S )NÚ
)Ú	_gen_initÚ_gen_childrenÚ	_gen_iterÚ_gen_attr_names)r   r   r   r   r
   r   U   s
    zNodeCfg.generate_sourcec             C   sŽ   d| j  }| jrDd | j¡}d dd„ | jD ƒ¡}|d7 }d| }nd}d}|d	| 7 }|d
| 7 }x$| jdg D ]}|d||f 7 }qrW |S )Nzclass %s(Node):
z, c             s   s   | ]}d   |¡V  qdS )z'{0}'N)Úformat)r   Úer   r   r
   ú	<genexpr>b   s    z$NodeCfg._gen_init.<locals>.<genexpr>z, 'coord', '__weakref__'z(self, %s, coord=None)z'coord', '__weakref__'z(self, coord=None)z    __slots__ = (%s)
z    def __init__%s:
Zcoordz        self.%s = %s
)r   r.   Újoin)r   r   ÚargsÚslotsZarglistr   r   r   r
   r7   ]   s    

zNodeCfg._gen_initc             C   sl   d}| j r`|d7 }x | jD ]}|dt|d 7 }qW x | jD ]}|dt|d 7 }q<W |d7 }n|d7 }|S )Nz    def children(self):
z        nodelist = []
zV        if self.%(child)s is not None: nodelist.append(("%(child)s", self.%(child)s))
)r0   zu        for i, child in enumerate(self.%(child)s or []):
            nodelist.append(("%(child)s[%%d]" %% i, child))
z        return tuple(nodelist)
z        return ()
)r.   r0   Údictr1   )r   r   r0   r1   r   r   r
   r8   q   s    
zNodeCfg._gen_childrenc             C   sp   d}| j rdx | jD ]}|dt|d 7 }qW x | jD ]}|dt|d 7 }q4W | jsl| jsl|d7 }n|d7 }|S )Nz    def __iter__(self):
zH        if self.%(child)s is not None:
            yield self.%(child)s
)r0   zE        for child in (self.%(child)s or []):
            yield child
z        return
        yield
)r.   r0   rA   r1   )r   r   r0   r1   r   r   r
   r9   ‰   s     zNodeCfg._gen_iterc             C   s"   dd  dd„ | jD ƒ¡ d }|S )Nz    attr_names = (Ú c             s   s   | ]}d | V  qdS )z%r, Nr   )r   Znmr   r   r
   r=   ¥   s    z*NodeCfg._gen_attr_names.<locals>.<genexpr>ú))r>   r/   )r   r   r   r   r
   r:   ¤   s    zNodeCfg._gen_attr_namesN)
r*   r+   r,   Ú__doc__r   r   r7   r8   r9   r:   r   r   r   r
   r   ;   s   r   a¾  #-----------------------------------------------------------------
# ** 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)

)ZpprintÚstringr   Úobjectr   r   r   r   r   r   r   r
   Ú<module>   s   *  