3
h\d                 @   s   U d dl mZmZmZmZmZ d dlZd dlmZm	Z	m
Z
mZmZ ddlmZ eefZeedf edd f D ZG d	d
 d
ZG dd dZeje eje dS )    )MutableMappingMappingMutableSequenceIteratorIterableN)TupleAnyDictSetList   )replaced_by_pep8.c             c   s   | ]
}|V  qd S )N ).0_r   r   6/tmp/pip-build-fibhr3ey/pyparsing/pyparsing/results.py	<genexpr>   s    r   c               @   sH   e Zd ZU edef dgZdedddZdd Zdd	 Z	d
d Z
dS )_ParseResultsWithOffsetParseResultstup)p1p2c             C   s   ||f| _ d S )N)r   )selfr   r   r   r   r   __init__   s    z _ParseResultsWithOffset.__init__c             C   s
   | j | S )N)r   )r   ir   r   r   __getitem__   s    z#_ParseResultsWithOffset.__getitem__c             C   s   | j S )N)r   )r   r   r   r   __getstate__   s    z$_ParseResultsWithOffset.__getstate__c             G   s   |d | _ d S )Nr   )r   )r   argsr   r   r   __setstate__    s    z$_ParseResultsWithOffset.__setstate__N)__name__
__module____qualname__r   intr   	__slots__r   r   r   r   r   r   r   r   r      s   
r   c               @   s6  e Zd ZU dZdg f fZeedf ed 	e
e eee eeef d_ZG d
d deZd`ddZddddefddZdd ZefddZdd ZedddZedddZedddZedddZedd d!Z d"d# Z!d$d% Z"d&d' Z#edd(d)Z$d*d+ Z%dad,d-Z&d.d/ Z'd0d1 Z(d2d3 Z)d4d5 Z*d6d7 Z+d d d8d9d:Z,d d d8d;d<Z-d dd=d>Z.edd?d@Z/eddAdBZ0dbdDdEZ1eddFdGZ2e3ddHdIZ4d ddJdKZ5d ddLdMZ6eddNdOZ7dceddQdRZ8dSdT Z9dUdV Z:dWdX Z;dYdZ Z<d[d\ Z=e>ddd dd]d^Z?e2Z@e4ZAe7ZBdS )er   a{  Structured parse results, to provide multiple means of access to
    the parsed data:

    - as a list (``len(results)``)
    - by list index (``results[0], results[1]``, etc.)
    - by attribute (``results.<results_name>`` - see :class:`ParserElement.set_results_name`)

    Example::

        integer = Word(nums)
        date_str = (integer.set_results_name("year") + '/'
                    + integer.set_results_name("month") + '/'
                    + integer.set_results_name("day"))
        # equivalent form:
        # date_str = (integer("year") + '/'
        #             + integer("month") + '/'
        #             + integer("day"))

        # parse_string returns a ParseResults object
        result = date_str.parse_string("1999/12/31")

        def test(s, fn=repr):
            print(f"{s} -> {fn(eval(s))}")
        test("list(result)")
        test("result[0]")
        test("result['month']")
        test("result.day")
        test("'month' in result")
        test("'minutes' in result")
        test("result.dump()", str)

    prints::

        list(result) -> ['1999', '/', '12', '/', '31']
        result[0] -> '1999'
        result['month'] -> '12'
        result.day -> '31'
        'month' in result -> True
        'minutes' in result -> False
        result.dump() -> ['1999', '/', '12', '/', '31']
        - day: '31'
        - month: '12'
        - year: '1999'
    N._name_parent
_all_names_modal_toklist_tokdictc               @   s   e Zd ZdZdddZdS )zParseResults.Lista  
        Simple wrapper class to distinguish parsed list results that should be preserved
        as actual Python lists, instead of being converted to :class:`ParseResults`::

            LBRACK, RBRACK = map(pp.Suppress, "[]")
            element = pp.Forward()
            item = ppc.integer
            element_list = LBRACK + pp.DelimitedList(element) + RBRACK

            # add parse actions to convert from ParseResults to actual Python collection types
            def as_python_list(t):
                return pp.ParseResults.List(t.as_list())
            element_list.add_parse_action(as_python_list)

            element <<= item | element_list

            element.run_tests('''
                100
                [2,3,4]
                [[2, 1],3,4]
                [(2, 1),3,4]
                (2,3,4)
                ''', post_parse=lambda s, r: (r[0], type(r[0])))

        prints::

            100
            (100, <class 'int'>)

            [2,3,4]
            ([2, 3, 4], <class 'list'>)

            [[2, 1],3,4]
            ([[2, 1], 3, 4], <class 'list'>)

        (Used internally by :class:`Group` when `aslist=True`.)
        Nc             C   s:   |d krg }t |ts0t| j dt|j tj| S )Nz* may only be constructed with a list, not )
isinstancelist	TypeErrorr   type__new__)clsZ	containedr   r   r   r.      s    
zParseResults.List.__new__)N)r   r    r!   __doc__r.   r   r   r   r   r   d   s   %r   c             K   s   t |tr|S tj| }d |_d |_t |_|d kr<g |_n<t |t	t
frpt |tjrd|d d  gnt	||_n|g|_t |_|S )N)r*   r   objectr.   r$   r%   setr&   r(   r+   _generator_typer   dictr)   )r/   toklistnamekwargsr   r   r   r   r.      s    

zParseResults.__new__Tc             C   s   |  || _ |d ks|dkrd S ||tr0t|}|s<|h| _|| _|| jkrPd S ||ttfrd|g}|r||trt	t|j
d| |< nt	t|d d| |< || | _d S y|d | |< W n2 tttfk
r   || k	r|| |< n|| _Y nX d S )N r   )r'   r"   strr&   r$   _null_valuesstr_typer-   r   r   r(   KeyErrorr,   
IndexError)r   r5   r6   asListmodalr*   r   r   r   r      s2    




zParseResults.__init__c             C   sL   t |ttfr| j| S || jkr4| j| d d S tdd | j| D S )Nr   r   c             S   s   g | ]}|d  qS )r   r   )r   vr   r   r   
<listcomp>   s    z,ParseResults.__getitem__.<locals>.<listcomp>)r*   r"   slicer(   r&   r)   r   )r   r   r   r   r   r      s
    

zParseResults.__getitem__c             C   s   ||t r0| jj|t |g | j|< |d }nD||ttfrN|| j|< |}n&| jj|t t |dg | j|< |}||tr| |_d S )Nr   )	r   r)   getr+   r"   rC   r(   r   r%   )r   kr@   r*   subr   r   r   __setitem__   s    



zParseResults.__setitem__c       	      C   s   t |ttfs| j|= d S t| j}| j|= t |trT|dk rF||7 }t||d }tt|j| }|j	  xP| jj
 D ]B}x<|D ]4}x.t|D ]"\}\}}t||||k ||< qW qW qzW d S )Nr   r   )r*   r"   rC   r)   lenr(   r+   rangeindicesreversevalues	enumerater   )	r   r   mylenremovedoccurrencesjrE   valuepositionr   r   r   __delitem__   s     


zParseResults.__delitem__)returnc             C   s
   || j kS )N)r)   )r   rE   r   r   r   __contains__  s    zParseResults.__contains__c             C   s
   t | jS )N)rH   r(   )r   r   r   r   __len__  s    zParseResults.__len__c             C   s   | j p
| j  S )N)r(   r)   )r   r   r   r   __bool__
  s    zParseResults.__bool__c             C   s
   t | jS )N)iterr(   )r   r   r   r   __iter__  s    zParseResults.__iter__c             C   s   t | jd d d S )Nr   rB   )rY   r(   )r   r   r   r   __reversed__  s    zParseResults.__reversed__c             C   s
   t | jS )N)rY   r)   )r   r   r   r   keys  s    zParseResults.keysc                s    fdd j  D S )Nc             3   s   | ]} | V  qd S )Nr   )r   rE   )r   r   r   r     s    z&ParseResults.values.<locals>.<genexpr>)r\   )r   r   )r   r   rL     s    zParseResults.valuesc                s    fdd j  D S )Nc             3   s   | ]}| | fV  qd S )Nr   )r   rE   )r   r   r   r     s    z%ParseResults.items.<locals>.<genexpr>)r\   )r   r   )r   r   items  s    zParseResults.itemsc             C   s
   | j   S )z
        Since ``keys()`` returns an iterator, this method is helpful in bypassing
        code that looks for the existence of any defined results names.)r)   )r   r   r   r   haskeys  s    zParseResults.haskeysc             O   s   |s
dg}x8|j  D ],\}}|dkr2|d |f}qtd|qW t|d tsjt|dksj|d | kr|d }| | }| |= |S |d }|S dS )a  
        Removes and returns item at specified index (default= ``last``).
        Supports both ``list`` and ``dict`` semantics for ``pop()``. If
        passed no argument or an integer argument, it will use ``list``
        semantics and pop tokens from the list of parsed tokens. If passed
        a non-integer argument (most likely a string), it will use ``dict``
        semantics and pop the corresponding value from any defined results
        names. A second default return value argument is supported, just as in
        ``dict.pop()``.

        Example::

            numlist = Word(nums)[...]
            print(numlist.parse_string("0 123 321")) # -> ['0', '123', '321']

            def remove_first(tokens):
                tokens.pop(0)
            numlist.add_parse_action(remove_first)
            print(numlist.parse_string("0 123 321")) # -> ['123', '321']

            label = Word(alphas)
            patt = label("LABEL") + Word(nums)[1, ...]
            print(patt.parse_string("AAB 123 321").dump())

            # Use pop() in a parse action to remove named result (note that corresponding value is not
            # removed from list form of results)
            def remove_LABEL(tokens):
                tokens.pop("LABEL")
                return tokens
            patt.add_parse_action(remove_LABEL)
            print(patt.parse_string("AAB 123 321").dump())

        prints::

            ['AAB', '123', '321']
            - LABEL: 'AAB'

            ['AAB', '123', '321']
        r   defaultr   z)pop() got an unexpected keyword argument NrB   )r]   r,   r*   r"   rH   )r   r   r7   rE   r@   indexretdefaultvaluer   r   r   pop"  s    (&zParseResults.popc             C   s   || kr| | S |S dS )a^  
        Returns named result matching the given key, or if there is no
        such name, then returns the given ``default_value`` or ``None`` if no
        ``default_value`` is specified.

        Similar to ``dict.get()``.

        Example::

            integer = Word(nums)
            date_str = integer("year") + '/' + integer("month") + '/' + integer("day")

            result = date_str.parse_string("1999/12/31")
            print(result.get("year")) # -> '1999'
            print(result.get("hour", "not specified")) # -> 'not specified'
            print(result.get("hour")) # -> None
        Nr   )r   keydefault_valuer   r   r   rD   Z  s    zParseResults.getc             C   sV   | j j|| xB| jj D ]4}x.t|D ]"\}\}}t||||k ||< q(W qW dS )a;  
        Inserts new element at location index in the list of parsed tokens.

        Similar to ``list.insert()``.

        Example::

            numlist = Word(nums)[...]
            print(numlist.parse_string("0 123 321")) # -> ['0', '123', '321']

            # use a parse action to insert the parse location in the front of the parsed results
            def insert_locn(locn, tokens):
                tokens.insert(0, locn)
            numlist.add_parse_action(insert_locn)
            print(numlist.parse_string("0 123 321")) # -> [0, '0', '123', '321']
        N)r(   insertr)   rL   rM   r   )r   r`   Z
ins_stringrP   rE   rR   rS   r   r   r   rf   q  s
    zParseResults.insertc             C   s   | j j| dS )a  
        Add single element to end of ``ParseResults`` list of elements.

        Example::

            numlist = Word(nums)[...]
            print(numlist.parse_string("0 123 321")) # -> ['0', '123', '321']

            # use a parse action to compute the sum of the parsed integers, and add it to the end
            def append_sum(tokens):
                tokens.append(sum(map(int, tokens)))
            numlist.add_parse_action(append_sum)
            print(numlist.parse_string("0 123 321")) # -> ['0', '123', '321', 444]
        N)r(   append)r   itemr   r   r   rg     s    zParseResults.appendc             C   s&   t |tr| j| n| jj| dS )a  
        Add sequence of elements to end of ``ParseResults`` list of elements.

        Example::

            patt = Word(alphas)[1, ...]

            # use a parse action to append the reverse of the matched strings, to make a palindrome
            def make_palindrome(tokens):
                tokens.extend(reversed([t[::-1] for t in tokens]))
                return ''.join(tokens)
            patt.add_parse_action(make_palindrome)
            print(patt.parse_string("lskdj sdlkjf lksd")) # -> 'lskdjsdlkjflksddsklfjkldsjdksl'
        N)r*   r   __iadd__r(   extend)r   itemseqr   r   r   rj     s    
zParseResults.extendc             C   s   | j dd= | jj  dS )z7
        Clear all elements and results names.
        N)r(   r)   clear)r   r   r   r   rl     s    zParseResults.clearc             C   s4   y| | S  t k
r.   |jdr*t|dS X d S )N__r8   )r<   
startswithAttributeError)r   r6   r   r   r   __getattr__  s    
zParseResults.__getattr__)otherrU   c             C   s   | j  }||7 }|S )N)copy)r   rq   ra   r   r   r   __add__  s    zParseResults.__add__c                s   |s| S |j rrt| jfdd |j j } fdd|D }x0|D ](\}}|| |< t|d trF| |d _qFW |  j|j7  _|  j|jO  _| S )Nc                s   | dk r S |   S )Nr   r   )a)offsetr   r   <lambda>  s    z'ParseResults.__iadd__.<locals>.<lambda>c          	      s4   g | ],\}}|D ]}|t |d   |d fqqS )r   r   )r   )r   rE   vlistr@   )	addoffsetr   r   rA     s   z)ParseResults.__iadd__.<locals>.<listcomp>r   )r)   rH   r(   r]   r*   r   r%   r&   )r   rq   
otheritemsotherdictitemsrE   r@   r   )rx   ru   r   ri     s    


zParseResults.__iadd__c             C   s&   t |tr|dkr| j S ||  S d S )Nr   )r*   r"   rr   )r   rq   r   r   r   __radd__  s    zParseResults.__radd__c             C   s"   t | j d| jd| j  dS )N(z, ))r-   r   r(   as_dict)r   r   r   r   __repr__  s    zParseResults.__repr__c             C   s   ddj dd | jD  d S )N[z, c             S   s&   g | ]}t |trt|nt|qS r   )r*   r   r9   repr)r   r   r   r   r   rA     s   z(ParseResults.__str__.<locals>.<listcomp>])joinr(   )r   r   r   r   __str__  s    	zParseResults.__str__r8   c             C   sP   g }xF| j D ]<}|r"|r"|j| t|tr:||j 7 }q|jt| qW |S )N)r(   rg   r*   r   _asStringListr9   )r   sepoutrh   r   r   r   r     s    

zParseResults._asStringListc             C   s   dd | j D S )ax  
        Returns the parse results as a nested list of matching tokens, all converted to strings.

        Example::

            patt = Word(alphas)[1, ...]
            result = patt.parse_string("sldkj lsdkj sldkj")
            # even though the result prints in string-like form, it is actually a pyparsing ParseResults
            print(type(result), result) # -> <class 'pyparsing.ParseResults'> ['sldkj', 'lsdkj', 'sldkj']

            # Use as_list() to create an actual list
            result_list = result.as_list()
            print(type(result_list), result_list) # -> <class 'list'> ['sldkj', 'lsdkj', 'sldkj']
        c             S   s"   g | ]}t |tr|j n|qS r   )r*   r   as_list)r   resr   r   r   rA     s   z(ParseResults.as_list.<locals>.<listcomp>)r(   )r   r   r   r   r     s    zParseResults.as_listc                s&    fdd t  fdd| j D S )a  
        Returns the named parse results as a nested dictionary.

        Example::

            integer = Word(nums)
            date_str = integer("year") + '/' + integer("month") + '/' + integer("day")

            result = date_str.parse_string('12/31/1999')
            print(type(result), repr(result)) # -> <class 'pyparsing.ParseResults'> (['12', '/', '31', '/', '1999'], {'day': [('1999', 4)], 'year': [('12', 0)], 'month': [('31', 2)]})

            result_dict = result.as_dict()
            print(type(result_dict), repr(result_dict)) # -> <class 'dict'> {'day': '1999', 'year': '12', 'month': '31'}

            # even though a ParseResults supports dict-like access, sometime you just need to have a dict
            import json
            print(json.dumps(result)) # -> Exception: TypeError: ... is not JSON serializable
            print(json.dumps(result.as_dict())) # -> {"month": "31", "day": "1999", "year": "12"}
        c                s4   t | tr,| j r| j S  fdd| D S | S d S )Nc                s   g | ]} |qS r   r   )r   r@   )to_itemr   r   rA   &  s    z9ParseResults.as_dict.<locals>.to_item.<locals>.<listcomp>)r*   r   r^   r~   )obj)r   r   r   r   $  s    
"z%ParseResults.as_dict.<locals>.to_itemc             3   s   | ]\}}| |fV  qd S )Nr   )r   rE   r@   )r   r   r   r   *  s    z'ParseResults.as_dict.<locals>.<genexpr>)r4   r]   )r   r   )r   r   r~     s    zParseResults.as_dictc             C   s:   t | j}| jj |_| j|_| j| jO  _| j|_|S )a  
        Returns a new shallow copy of a :class:`ParseResults` object. `ParseResults`
        items contained within the source are shared with the copy. Use
        :class:`ParseResults.deepcopy()` to create a copy with its own separate
        content values.
        )r   r(   r)   rr   r%   r&   r$   )r   ra   r   r   r   rr   ,  s    
zParseResults.copyc             C   s   | j  }xt| jD ]\}}t|tr6|j |j|< qt|ttfrFqt|trt	|  |j|< }xV|j
 D ]"\}}t|tr|j n|||< qnW qt|trt	|dd |D |j|< qW |S )zL
        Returns a new deep copy of a :class:`ParseResults` object.
        c             s   s$   | ]}t |tr|j n|V  qd S )N)r*   r   deepcopy)r   r@   r   r   r   r   K  s    z(ParseResults.deepcopy.<locals>.<genexpr>)rr   rM   r(   r*   r   r   r9   bytesr   r-   r]   r   )r   ra   r   r   destrE   r@   r   r   r   r   :  s    

 
zParseResults.deepcopyc                s    j r j S  jr: j}|jj }t fdd|D dS t dkrt jdkrtt jj d d dkrtt jj S dS dS )a  
        Returns the results name for this token expression. Useful when several
        different expressions might match at a particular location.

        Example::

            integer = Word(nums)
            ssn_expr = Regex(r"\d\d\d-\d\d-\d\d\d\d")
            house_number_expr = Suppress('#') + Word(nums, alphanums)
            user_data = (Group(house_number_expr)("house_number")
                        | Group(ssn_expr)("ssn")
                        | Group(integer)("age"))
            user_info = user_data[1, ...]

            result = user_info.parse_string("22 111-22-3333 #221B")
            for item in result:
                print(item.get_name(), ':', item[0])

        prints::

            age : 22
            ssn : 111-22-3333
            house_number : 221B
        c             3   s,   | ]$\}}|D ]\}}| kr|V  qqd S )Nr   )r   rE   rw   r@   loc)r   r   r   r   o  s   z(ParseResults.get_name.<locals>.<genexpr>Nr   r   rB   )r   rB   )	r$   r%   r)   r]   nextrH   rY   rL   r\   )r   parZparent_tokdict_itemsr   )r   r   get_nameO  s    

zParseResults.get_namer   c             C   s  g }d}|j |r |t| j  nd |s4dj|S | j rtdd | j D }x|D ]~\}}	|rn|j | |j | d|  d| d t|	ts|j t	|	 qX|	s|j t|	 qX|j |	j
||||d d	 qXW td
d | D sdj|S | }	d}
d}xt|	D ]\}}t|trr|j
||||d d	}|j | | |
|  d| d| | |
|d   | 
 n:|j | | |
|  d| d| | |
|d   | 
 qW dj|S )aM  
        Diagnostic method for listing out the contents of
        a :class:`ParseResults`. Accepts an optional ``indent`` argument so
        that this string can be embedded in a nested display of other data.

        Example::

            integer = Word(nums)
            date_str = integer("year") + '/' + integer("month") + '/' + integer("day")

            result = date_str.parse_string('1999/12/31')
            print(result.dump())

        prints::

            ['1999', '/', '12', '/', '31']
            - day: '31'
            - month: '12'
            - year: '1999'
        
r8   c             s   s   | ]\}}t ||fV  qd S )N)r9   )r   rE   r@   r   r   r   r     s    z$ParseResults.dump.<locals>.<genexpr>z  z- z: r   )indentfullinclude_list_depthc             s   s   | ]}t |tV  qd S )N)r*   r   )r   vvr   r   r   r     s    r   z]:)rg   r9   r   r   r^   sortedr]   r*   r   r   dumpanyrM   )r   r   r   r   r   r   NLr]   rE   r@   incrnlr   r   Zvv_dumpr   r   r   r     sP    



8<zParseResults.dumpc             O   s   t j | j f|| dS )a$  
        Pretty-printer for parsed results as a list, using the
        `pprint <https://docs.python.org/3/library/pprint.html>`_ module.
        Accepts additional positional or keyword args as defined for
        `pprint.pprint <https://docs.python.org/3/library/pprint.html#pprint.pprint>`_ .

        Example::

            ident = Word(alphas, alphanums)
            num = Word(nums)
            func = Forward()
            term = ident | num | Group('(' + func + ')')
            func <<= ident + Group(Optional(DelimitedList(term)))
            result = func.parse_string("fna a,b,(fnb c,d,200),100")
            result.pprint(width=40)

        prints::

            ['fna',
             ['a',
              'b',
              ['(', 'fnb', ['c', 'd', '200'], ')'],
              '100']]
        N)pprintr   )r   r   r7   r   r   r   r     s    zParseResults.pprintc             C   s   | j | jj d | j| jffS )N)r(   r)   rr   r&   r$   )r   r   r   r   r     s
    zParseResults.__getstate__c             C   s*   |\| _ \| _}}| _t|| _d | _d S )N)r(   r)   r$   r2   r&   r%   )r   stater   inAccumNamesr   r   r   r     s    
zParseResults.__setstate__c             C   s   | j | jfS )N)r(   r$   )r   r   r   r   __getnewargs__  s    zParseResults.__getnewargs__c             C   s   t t| t| j  S )N)dirr-   r+   r\   )r   r   r   r   __dir__  s    zParseResults.__dir__c             C   sv   dd }| g }xJ|j  D ]>\}}t|tr@|| j||d7 }q|| |g|||d7 }qW |dk	rr| |g|d}|S )z
        Helper classmethod to construct a ``ParseResults`` from a ``dict``, preserving the
        name-value relations as results names. If an optional ``name`` argument is
        given, a nested ``ParseResults`` will be returned.
        c             S   s2   yt |  W n tk
r    dS X t| t S d S )NF)rY   	Exceptionr*   r;   )r   r   r   r   is_iterable  s
    z+ParseResults.from_dict.<locals>.is_iterable)r6   )r6   r>   N)r]   r*   r   	from_dict)r/   rq   r6   r   ra   rE   r@   r   r   r   r     s    	
zParseResults.from_dict)r$   r%   r&   r'   r(   r)   )NN)N)r8   )r8   TTr   )N)Cr   r    r!   r0   r:   r   r   r9   r$   r%   r
   r&   boolr'   r   r(   r	   r)   r#   r+   r.   r*   r   r   rG   rT   rV   r"   rW   rX   r   rZ   r[   r\   rL   r]   r^   rc   rD   rf   rg   rj   rl   rp   rs   ri   r{   r   r   r   r   r4   r~   rr   r   r   r   r   r   r   r   r   classmethodr   r>   asDictgetNamer   r   r   r   r   $   sv   
,     2
&	8

0Jr   )collections.abcr   r   r   r   r   r   typingr   r   r	   r
   r   utilr   r9   r   r;   r-   r3   r   r   registerr   r   r   r   <module>   s            
