3
&yV!                 @   sh   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ed	krdd dl	Z	ed
Z
e
jedd 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 | j|D | _dS )zN Initialize the code generator from a configuration
            file.
        c             S   s   g | ]\}}t ||qS  )NodeCfg).0namecontentsr   r   R/var/www/html/enquirykeeper_venv/lib/python3.6/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 ]}||j d 7 }q"W |j| dS )z< Generates the code into file, an open file buffer.
        )r   z

N)r   _PROLOGUE_COMMENT
substituter   _PROLOGUE_CODEr   generate_sourcewrite)r   filesrcr   r   r   r
   generate   s    
zASTCodeGenerator.generatec       
      c   s   t |d}x|D ]}|j }| s|jdr0q|jd}|jd}|jd}|dk sf||ksf||krvtd||f |d| }||d | }|rd	d
 |j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 | ]}|j  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               @   s8   e Zd Z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}|jd}| jj| |jdrV| jj| q$|jdrn| jj| q$| j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   B   s    



zNodeCfg.__init__c             C   s,   | j  }|d| j  7 }|d| j  7 }|S )N
)	_gen_init_gen_children_gen_attr_names)r   r   r   r   r
   r   T   s    zNodeCfg.generate_sourcec             C   s   d| j  }| jrDdj| j}dj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 j |V  qdS )z'{0}'N)format)r   er   r   r
   	<genexpr>_   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slotsarglistr   r   r   r
   r8   Z   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 = []
z&        if self.%(child)s is not None:z0 nodelist.append(("%(child)s", self.%(child)s))
)r1   zu        for i, child in enumerate(self.%(child)s or []):
            nodelist.append(("%(child)s[%%d]" %% i, child))
z        return tuple(nodelist)
z        return ()
zV        if self.%(child)s is not None: nodelist.append(("%(child)s", self.%(child)s))
)r/   r1   dictr2   )r   r   r1   r2   r   r   r
   r9   n   s     
zNodeCfg._gen_childrenc             C   s"   ddj dd | jD  d }|S )Nz    attr_names = ( c             s   s   | ]}d | V  qdS )z%r, Nr   )r   nmr   r   r
   r=      s    z*NodeCfg._gen_attr_names.<locals>.<genexpr>))r>   r0   )r   r   r   r   r
   r:      s    zNodeCfg._gen_attr_namesN)	r+   r,   r-   __doc__r   r   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.
#
# Copyright (C) 2008-2015, Eli Bendersky
# License: BSD
#-----------------------------------------------------------------

a  
import sys


class Node(object):
    __slots__ = ()
    """ Abstract base class for AST nodes.
    """
    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)
    """
    def visit(self, node):
        """ Visit a node.
        """
        method = 'visit_' + node.__class__.__name__
        visitor = getattr(self, method, self.generic_visit)
        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_name, c in node.children():
            self.visit(c)


__main__z
_c_ast.cfgzc_ast.pyw)pprintstringr   objectr   r   r   r   r+   sysZast_genr   r"   r   r   r   r
   <module>   s   *br