U
    Ç½„b?#  ã                   @   sÎ   d dl Z d dlZd dlZddlmZmZmZmZ ddlm	Z
 G dd„ de
je
je
je
je
jƒZeejƒZe  de d ¡ZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    Né   )ÚcolÚlineÚlinenoÚ_collapse_string_to_ranges)Úpyparsing_unicodec                   @   s   e Zd ZdS )ÚExceptionWordUnicodeN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú8/tmp/pip-unpacked-wheel-cdpi1ipc/pyparsing/exceptions.pyr      s   r   z([z
]{1,16})|.c                   @   sÎ   e Zd ZdZd eeeje dœdd„Ze	d!dd	„ƒZ
ed
d„ ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZedœdd„Zdd„ Zd"ddœeedœdd„Zd#edœdd„ZeZdS )$ÚParseBaseExceptionz7base exception class for all parsing runtime exceptionsr   N)ÚpstrÚlocÚmsgc                 C   sD   || _ |d kr|| _d| _n|| _|| _| | _| _|||f| _d S )NÚ )r   r   r   Zparser_elementÚparserElementÚargs)Úselfr   r   r   Úelemr   r   r   Ú__init__   s    zParseBaseException.__init__é   c                 C   s„  ddl }ddlm} |dkr$t ¡ }g }t| tƒrV| | j¡ | d| j	d  d ¡ | d 
t| ƒj| ¡¡ |dk�rz|j| j|d�}tƒ }t|| d… ƒD ]Ø\}}|d }	|	j d	d¡}
t|
|ƒ�r|	jjd
krØq t|
ƒ|kræq | t|
ƒ¡ t|
ƒ}| d 
|j|j|
¡¡ nP|
dk	�r@t|
ƒ}| d 
|j|j¡¡ n&|	j}|jdk�rTq | d 
|j¡¡ |d8 }|s  �qzq d |¡S )aì  
        Method to take an exception and translate the Python internal traceback into a list
        of the pyparsing expressions that caused the exception to be raised.

        Parameters:

        - exc - exception raised during parsing (need not be a ParseException, in support
          of Python exceptions that might be raised in a parse action)
        - depth (default=16) - number of levels back in the stack trace to list expression
          and function names; if None, the full stack trace names will be listed; if 0, only
          the failing input line, marker, and exception string will be shown

        Returns a multi-line string listing the ParserElements and/or function names in the
        exception's stack trace.
        r   Nr   )ÚParserElementú ú^z{}: {})Úcontextr   )Ú	parseImplÚ_parseNoCachez
{}.{} - {}z{}.{})Úwrapperz<module>z{}Ú
)ÚinspectÚcorer   ÚsysÚgetrecursionlimitÚ
isinstancer   Úappendr   ÚcolumnÚformatÚtyper	   ÚgetinnerframesÚ__traceback__ÚsetÚ	enumerateÚf_localsÚgetÚf_codeÚco_nameÚidÚaddr
   Újoin)ÚexcÚdepthr!   r   ÚretÚcallersÚseenÚiÚffÚfrmÚf_selfÚ	self_typeÚcoder   r   r   Úexplain_exception)   sP    

  ÿÿ
z$ParseBaseException.explain_exceptionc                 C   s   | |j |j|j|jƒS )z¬
        internal factory method to simplify creating one type of ParseException
        from another - avoids having __init__ signature conflicts among subclasses
        )r   r   r   r   )ÚclsÚper   r   r   Ú_from_exceptionk   s    z"ParseBaseException._from_exception©Úreturnc                 C   s   t | j| jƒS )zG
        Return the line of text where the exception occurred.
        )r   r   r   ©r   r   r   r   r   s   s    zParseBaseException.linec                 C   s   t | j| jƒS )zV
        Return the 1-based line number of text where the exception occurred.
        )r   r   r   rF   r   r   r   r   z   s    zParseBaseException.linenoc                 C   s   t | j| jƒS ©z]
        Return the 1-based column on the line of text where the exception occurred.
        ©r   r   r   rF   r   r   r   r   �   s    zParseBaseException.colc                 C   s   t | j| jƒS rG   rH   rF   r   r   r   r'   ˆ   s    zParseBaseException.columnc                 C   s†   | j rh| jt| j ƒkrd}qlt | j | j¡}|d k	r@| d¡}n| j | j| jd … }d|  dd¡}nd}d | j|| j| j	| j
¡S )	Nz, found end of textr   r   z
, found %rz\\ú\r   z%{}{}  (at char {}), (line:{}, col:{}))r   r   ÚlenÚ_exception_word_extractorÚmatchÚgroupÚreplacer(   r   r   r'   )r   ÚfoundstrZfound_matchÚfoundr   r   r   Ú__str__�   s         ÿzParseBaseException.__str__c                 C   s   t | ƒS ©N)ÚstrrF   r   r   r   Ú__repr__¡   s    zParseBaseException.__repr__z>!<)ÚmarkerString)Úmarker_stringrE   c                C   sL   |dk	r|n|}| j }| jd }|rDd |d|… |||d… f¡}| ¡ S )z‹
        Extracts the exception line from the input string, and marks
        the location of the exception with a special symbol.
        Nr   r   )r   r'   r4   Ústrip)r   rV   rU   Úline_strÚline_columnr   r   r   Úmark_input_line¤   s    
ÿz"ParseBaseException.mark_input_linec                 C   s   |   | |¡S )a  
        Method to translate the Python internal traceback into a list
        of the pyparsing expressions that caused the exception to be raised.

        Parameters:

        - depth (default=16) - number of levels back in the stack trace to list expression
          and function names; if None, the full stack trace names will be listed; if 0, only
          the failing input line, marker, and exception string will be shown

        Returns a multi-line string listing the ParserElements and/or function names in the
        exception's stack trace.

        Example::

            expr = pp.Word(pp.nums) * 3
            try:
                expr.parse_string("123 456 A789")
            except pp.ParseException as pe:
                print(pe.explain(depth=0))

        prints::

            123 456 A789
                    ^
            ParseException: Expected W:(0-9), found 'A'  (at char 8), (line:1, col:9)

        Note: the diagnostic output will include string representations of the expressions
        that failed to parse. These representations will be more helpful if you use `set_name` to
        give identifiable names to your expressions. Otherwise they will use the default string
        forms, which may be cryptic to read.

        Note: pyparsing's default truncation of exception tracebacks may also truncate the
        stack of expressions that are displayed in the ``explain`` output. To get the full listing
        of parser expressions, you may have to set ``ParserElement.verbose_stacktrace = True``
        )r@   )r   r6   r   r   r   Úexplain²   s    %zParseBaseException.explain)r   NN)r   )N)r   )r	   r
   r   Ú__doc__rS   ÚintÚtypingÚOptionalr   Ústaticmethodr@   ÚclassmethodrC   Úpropertyr   r   r   r'   rQ   rT   rZ   r[   ÚmarkInputliner   r   r   r   r      s4      ûüA
'r   c                   @   s   e Zd ZdZdS )ÚParseExceptionaq  
    Exception thrown when a parse expression doesn't match the input string

    Example::

        try:
            Word(nums).set_name("integer").parse_string("ABC")
        except ParseException as pe:
            print(pe)
            print("column: {}".format(pe.column))

    prints::

       Expected integer (at char 0), (line:1, col:1)
        column: 1

    N©r	   r
   r   r\   r   r   r   r   rd   Ü   s   rd   c                   @   s   e Zd ZdZdS )ÚParseFatalExceptionzu
    User-throwable exception thrown when inconsistent parse content
    is found; stops all parsing immediately
    Nre   r   r   r   r   rf   ð   s   rf   c                   @   s   e Zd ZdZdS )ÚParseSyntaxExceptionzó
    Just like :class:`ParseFatalException`, but thrown internally
    when an :class:`ErrorStop<And._ErrorStop>` ('-' operator) indicates
    that parsing is to stop immediately because an unbacktrackable
    syntax error has been found.
    Nre   r   r   r   r   rg   ÷   s   rg   c                   @   s&   e Zd ZdZdd„ Zedœdd„ZdS )ÚRecursiveGrammarExceptionzî
    Exception thrown by :class:`ParserElement.validate` if the
    grammar could be left-recursive; parser may need to enable
    left recursion using :class:`ParserElement.enable_left_recursion<ParserElement.enable_left_recursion>`
    c                 C   s
   || _ d S rR   )ÚparseElementTrace)r   ÚparseElementListr   r   r   r     s    z"RecursiveGrammarException.__init__rD   c                 C   s   d  | j¡S )NzRecursiveGrammarException: {})r(   ri   rF   r   r   r   rQ   
  s    z!RecursiveGrammarException.__str__N)r	   r
   r   r\   r   rS   rQ   r   r   r   r   rh      s   rh   )Úrer#   r^   Úutilr   r   r   r   Úunicoder   ZppuÚLatin1ÚLatinAÚLatinBÚGreekÚCyrillicr   Ú	alphanumsZ_extract_alphanumsÚcompilerK   Ú	Exceptionr   rd   rf   rg   rh   r   r   r   r   Ú<module>   s   "
 J	