U
    ڲg                     @   sT   d dl Z G dd deZG dd deZG dd deZdd	 Zd
d Zdd ZdS )    Nc                   @   s&   e Zd ZdZdZdddZdd ZdS )	Coordz Coordinates of a syntactic element. Consists of:
            - File name
            - Line number
            - (optional) column number, for the Lexer
    )filelinecolumn__weakref__Nc                 C   s   || _ || _|| _d S Nr   r   r   )selfr   r   r    r
   7/tmp/pip-unpacked-wheel-f3oyftnv/pycparser/plyparser.py__init__   s    zCoord.__init__c                 C   s(   d| j | jf }| jr$|d| j 7 }|S )Nz%s:%sz:%sr   )r	   strr
   r
   r   __str__   s     zCoord.__str__)N)__name__
__module____qualname____doc__	__slots__r   r   r
   r
   r
   r   r      s   
r   c                   @   s   e Zd ZdS )
ParseErrorN)r   r   r   r
   r
   r
   r   r      s    r   c                   @   s.   e Zd Zdd Zd
ddZdd Zdd	 ZdS )	PLYParserc                 C   s<   |d }dd }d||f |_ d| |_t| j|j| dS )z Given a rule name, creates an optional ply.yacc rule
            for it. The name of the optional rule is
            <rulename>_opt
        Z_optc                 S   s   |d |d< d S )N   r   r
   r	   pr
   r
   r   optrule*   s    z+PLYParser._create_opt_rule.<locals>.optrulez%s : empty
| %szp_%sN)r   r   setattr	__class__)r	   Zrulenameoptnamer   r
   r
   r   _create_opt_rule#   s
    
zPLYParser._create_opt_ruleNc                 C   s   t | jj||dS )Nr   )r   Zclexfilename)r	   linenor   r
   r
   r   _coord1   s
    zPLYParser._coordc                 C   sF   |j j jdd||}|dk r&d}||| }| |||S )z Returns the coordinates for the YaccProduction object 'p' indexed
            with 'token_idx'. The coordinate includes the 'lineno' and
            'column'. Both follow the lex semantic, starting from 1.
        
r   )lexerZlexdatarfindZlexposr    r   )r	   r   Z	token_idxZlast_crr   r
   r
   r   _token_coord7   s
    zPLYParser._token_coordc                 C   s   t d||f d S )Nz%s: %s)r   )r	   msgZcoordr
   r
   r   _parse_errorB   s    zPLYParser._parse_error)N)r   r   r   r   r    r%   r'   r
   r
   r
   r   r   "   s   
r   c                     s    fdd}|S )a   Decorator to create parameterized rules.

    Parameterized rule methods must be named starting with 'p_' and contain
    'xxx', and their docstrings may contain 'xxx' and 'yyy'. These will be
    replaced by the given parameter tuples. For example, ``p_xxx_rule()`` with
    docstring 'xxx_rule  : yyy' when decorated with
    ``@parameterized(('id', 'ID'))`` produces ``p_id_rule()`` with the docstring
    'id_rule  : ID'. Using multiple tuples produces multiple rules.
    c                    s
    | _ | S r   )_params)Z	rule_funcparamsr
   r   decorateP   s    zparameterized.<locals>.decorater
   )r*   r+   r
   r)   r   parameterizedF   s    
r,   c                 C   sl   d}t | D ]Z}|drt| |}t|drt| | |jdk	rNt| | q|stjdt	dd d}q| S )	z Class decorator to generate rules from parameterized rule templates.

    See `parameterized` for more information on parameterized rules.
    FZp_r(   Nz@parsing methods must have __doc__ for pycparser to work properly   )
stacklevelT)
dir
startswithgetattrhasattrdelattrr   _create_param_ruleswarningswarnRuntimeWarning)clsZissued_nodoc_warning	attr_namemethodr
   r
   r   templateV   s     




r;   c                    sV    j D ]J\}} fdd} jd|d||_ jd||_t| |j| qdS )a   Create ply.yacc rules based on a parameterized rule function

    Generates new methods (one per each pair of parameters) based on the
    template rule function `func`, and attaches them to `cls`. The rule
    function's parameters must be accessible via its `_params` attribute.
    c                    s    | | d S r   r
   r   funcr
   r   
param_rule}   s    z'_create_param_rules.<locals>.param_rulexxxyyyN)r(   r   replacer   r   )r8   r=   r?   r@   r>   r
   r<   r   r4   t   s
    r4   )	r5   objectr   	Exceptionr   r   r,   r;   r4   r
   r
   r
   r   <module>   s   $