
Ѯh                 @   sv   d  d l  Z  Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z d d	   Z d
 d   Z d d   Z d S)    Nc               @   s7   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   ../pycparser/plyparser.py__init__   s    		zCoord.__init__c             C   s4   d |  j  |  j f } |  j r0 | d |  j 7} | S)Nz%s:%sz:%s)r   r   r   )r   strr   r   r	   __str__   s    	 zCoord.__str__)r   r   r   r   )__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               @   sC   e  Z d  Z d d   Z d d d  Z d d   Z d d	   Z d S)
	PLYParserc             C   sP   | 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rulenameZoptnamer   r   r   r	   _create_opt_rule#   s
    
zPLYParser._create_opt_ruleNc             C   s   t  d |  j j d | d |  S)Nr   r   r   )r   clexfilename)r   linenor   r   r   r	   _coord1   s    zPLYParser._coordc             C   se   | j  j  j j d d | j |   } | d k  r9 d } | j |  | } |  j | j |  |  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   r   )lexerlexdatarfindlexposr   r   )r   r   Z	token_idxlast_crr   r   r   r	   _token_coord7   s
    'zPLYParser._token_coordc             C   s   t  d | | f   d  S)Nz%s: %s)r   )r   msgcoordr   r   r	   _parse_errorB   s    zPLYParser._parse_error)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)N)_params)Z	rule_func)paramsr   r	   decorateP   s    	zparameterized.<locals>.decorater   )r*   r+   r   )r*   r	   parameterizedF   s    
r,   c             C   s   d } x t  |   D] } | j d  r t |  |  } t | d  r t |  |  | j d k	 rr t |  |  q | s t j d t	 d d d } q W|  S)	z Class decorator to generate rules from parameterized rule templates.

    See `parameterized` for more information on parameterized rules.
    Fp_r)   Nz@parsing methods must have __doc__ for pycparser to work properly
stacklevel   T)
dir
startswithgetattrhasattrdelattrr   _create_param_ruleswarningswarnRuntimeWarning)clsZissued_nodoc_warningZ	attr_namemethodr   r   r	   templateV   s    
r;   c                s   xx   j  D]m \ } }   f d d   }   j j d |  j d |  | _   j j d |  | _ t |  | j |  q
 Wd 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)Nr   )r   r   )funcr   r	   
param_rule}   s    z'_create_param_rules.<locals>.param_rulexxxyyyN)r)   r   replacer   r   )r9   r<   r>   r?   r=   r   )r<   r	   r5   t   s
    $r5   )	r6   objectr   	Exceptionr   r   r,   r;   r5   r   r   r   r	   <module>   s   $