3

Y'ícýbã@s˜UddlmZmZmZmZddlZddlmZddl	m
Z
mZee
fZe
edfedd„fDƒƒZGdd	„d	ƒZGd
d„dƒZejeƒejeƒdS)é)ÚMutableMappingÚMappingÚMutableSequenceÚIteratorN)Úref)ÚTupleÚAny.ccs|]
}|VqdS)N©)Ú.0Ú_r	r	ú6/tmp/pip-build-gk9425m9/pyparsing/pyparsing/results.pyú	<genexpr>sr
c@s2eZdZdgZdd„Zdd„Zdd„Zdd	„Zd
S)Ú_ParseResultsWithOffsetÚtupcCs||f|_dS)N)r)ÚselfÚp1Úp2r	r	rÚ__init__sz _ParseResultsWithOffset.__init__cCs
|j|S)N)r)rÚir	r	rÚ__getitem__sz#_ParseResultsWithOffset.__getitem__cCs|jS)N)r)rr	r	rÚ__getstate__sz$_ParseResultsWithOffset.__getstate__cGs|d|_dS)Nr)r)rÚargsr	r	rÚ__setstate__sz$_ParseResultsWithOffset.__setstate__N)Ú__name__Ú
__module__Ú__qualname__Ú	__slots__rrrrr	r	r	rrs
rc@seZdZUdZdgdffZeedfddddd	d
dgZGdd
„d
eƒZ	d]dd„Z
ddddefdd„Zdd„Z
efdd„Zdd„Zedœdd„Zedœdd„Zedœdd„Zedœd d!„Zedœd"d#„Zd$d%„Zd&d'„Zd(d)„Zedœd*d+„Zd,d-„Zd^d.d/„Zd0d1„Zd2d3„Zd4d5„Z d6d7„Z!d8d9„Z"ddœd:d;„Z#ddœd<d=„Z$ddœd>d?„Z%e&dœd@dA„Z'e&dœdBdC„Z(d_dDdE„Z)edœdFdG„Z*e+dœdHdI„Z,ddœdJdK„Z-dLdM„Z.d`e&dœdOdP„Z/dQdR„Z0dSdT„Z1dUdV„Z2dWdX„Z3dYdZ„Z4e5daddœd[d\„ƒZ6e*Z7e,Z8e.Z9dS)bÚParseResultsa…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("{} -> {}".format(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Ú_tokdictÚ__weakref__c@seZdZdZddd„ZdS)zParseResults.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.delimited_list(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`.)
        NcCs8|dkrg}t|tƒs.tdj|jt|ƒjƒƒ‚tj|ƒS)Nz.{} may only be constructed with a list, not {})Ú
isinstanceÚlistÚ	TypeErrorÚformatrÚtypeÚ__new__)ÚclsZ	containedr	r	rr+|s
zParseResults.List.__new__)N)rrrÚ__doc__r+r	r	r	rÚListUs%r.cKs„t|tƒr|Stj|ƒ}d|_d|_tƒ|_|dkr<g|_n<t|t	t
fƒrpt|tjƒrd|dd…gnt	|ƒ|_n|g|_tƒ|_
|S)N)r&rÚobjectr+rr Úsetr!r#r'Ú_generator_typer.Údictr$)r,ÚtoklistÚnameÚkwargsrr	r	rr+ˆs

zParseResults.__new__Tc
Csæ||_|dk	râ|dkrâ||tƒr(t|ƒ}|s4|h|_||_||jkrâ||ttfƒrX|g}|rž||tƒr|t	t|j
ƒdƒ||<nt	t|dƒdƒ||<|||_nDy|d||<Wn2ttt
fk
rà||k	rÖ|||<n||_YnXdS)Nrr)r"ÚintÚstrr!rÚ_null_valuesÚstr_typer*rrr#ÚKeyErrorr(Ú
IndexError)rr3r4ÚasListÚmodalr&r	r	rrŸs.



zParseResults.__init__cCsPt|ttfƒr|j|S||jkr4|j|ddStdd„|j|DƒƒSdS)NércSsg|]}|d‘qS)rr	)r
Úvr	r	rú
<listcomp>Æsz,ParseResults.__getitem__.<locals>.<listcomp>éÿÿÿÿ)r&r6Úslicer#r!r$r)rrr	r	rr¿s


zParseResults.__getitem__cCsŒ||tƒr0|jj|tƒƒ|g|j|<|d}nD||ttfƒrN||j|<|}n&|jj|tƒƒt|dƒg|j|<|}||tƒrˆt|ƒ|_	dS)Nr)
rr$Úgetr'r6rBr#rÚwkrefr )rÚkr?r&Úsubr	r	rÚ__setitem__Ès



zParseResults.__setitem__c
CsÆt|ttfƒrºt|jƒ}|j|=t|tƒrH|dkr:||7}t||dƒ}tt|j|ƒŽƒ}|jƒx^|j	j
ƒD]F\}}x<|D]4}x.t|ƒD]"\}\}}	t||	|	|kƒ||<qŠWq|WqnWn|j	|=dS)Nrr>)
r&r6rBÚlenr#r'ÚrangeÚindicesÚreverser$ÚitemsÚ	enumerater)
rrÚmylenÚremovedr4ÚoccurrencesÚjrEÚvalueÚpositionr	r	rÚ__delitem__×s


"zParseResults.__delitem__)ÚreturncCs
||jkS)N)r$)rrEr	r	rÚ__contains__îszParseResults.__contains__cCs
t|jƒS)N)rHr#)rr	r	rÚ__len__ñszParseResults.__len__cCs|jp
|jS)N)r#r$)rr	r	rÚ__bool__ôszParseResults.__bool__cCs
t|jƒS)N)Úiterr#)rr	r	rÚ__iter__÷szParseResults.__iter__cCst|jddd…ƒS)Nr>rA)rYr#)rr	r	rÚ__reversed__úszParseResults.__reversed__cCs
t|jƒS)N)rYr$)rr	r	rÚkeysýszParseResults.keyscs‡fdd„ˆjƒDƒS)Nc3s|]}ˆ|VqdS)Nr	)r
rE)rr	rr
sz&ParseResults.values.<locals>.<genexpr>)r\)rr	)rrÚvaluesszParseResults.valuescs‡fdd„ˆjƒDƒS)Nc3s|]}|ˆ|fVqdS)Nr	)r
rE)rr	rr
sz%ParseResults.items.<locals>.<genexpr>)r\)rr	)rrrLszParseResults.itemscCs
t|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.)Úboolr$)rr	r	rÚhaskeysszParseResults.haskeyscOs”|s
dg}x8|jƒD],\}}|dkr2|d|f}qtdj|ƒƒ‚qWt|dtƒsjt|ƒdksj|d|kr„|d}||}||=|S|d}|SdS)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>Údefaultrz-pop() got an unexpected keyword argument {!r}NrA)rLr(r)r&r6rH)rrr5rEr?ÚindexÚretÚdefaultvaluer	r	rÚpops(&zParseResults.popcCs||kr||S|SdS)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rCFszParseResults.getcCsZ|jj||ƒxF|jjƒD]8\}}x.t|ƒD]"\}\}}t||||kƒ||<q,WqWdS)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$rLrMr)rraZ
ins_stringr4rPrErRrSr	r	rrg]s
zParseResults.insertcCs|jj|ƒdS)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rhvszParseResults.appendcCs&t|tƒr|j|ƒn|jj|ƒdS)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rk‡s
zParseResults.extendcCs|jdd…=|jjƒdS)z7
        Clear all elements and results names.
        N)r#r$Úclear)rr	r	rrm›szParseResults.clearcCs4y||Stk
r.|jdƒr*t|ƒ‚dSXdS)NÚ__r)r:Ú
startswithÚAttributeError)rr4r	r	rÚ__getattr__¢s
zParseResults.__getattr__cCs|jƒ}||7}|S)N)Úcopy)rÚotherrbr	r	rÚ__add__ªszParseResults.__add__cs’|jrnt|jƒ‰‡fdd„‰|jjƒ}‡fdd„|Dƒ}x4|D],\}}|||<t|dtƒr>t|ƒ|d_q>W|j|j7_|j|jO_|S)Ncs|dkrˆS|ˆS)Nrr	)Úa)Úoffsetr	rÚ<lambda>²sz'ParseResults.__iadd__.<locals>.<lambda>c	s4g|],\}}|D]}|t|dˆ|dƒƒf‘qqS)rr>)r)r
rEÚvlistr?)Ú	addoffsetr	rr@µsz)ParseResults.__iadd__.<locals>.<listcomp>r)	r$rHr#rLr&rrDr r!)rrsÚ
otheritemsÚotherdictitemsrEr?r	)ryrvrrj¯s


zParseResults.__iadd__cCs&t|tƒr|dkr|jƒS||SdS)Nr)r&r6rr)rrsr	r	rÚ__radd__ÂszParseResults.__radd__cCsdjt|ƒj|j|jƒƒS)Nz{}({!r}, {}))r)r*rr#Úas_dict)rr	r	rÚ__repr__ÊszParseResults.__repr__cCsddjdd„|jDƒƒdS)Nú[z, cSs&g|]}t|tƒrt|ƒnt|ƒ‘qSr	)r&rr7Úrepr)r
rr	r	rr@Òsz(ParseResults.__str__.<locals>.<listcomp>ú])Újoinr#)rr	r	rÚ__str__Ís	zParseResults.__str__cCsPg}xF|jD]<}|r"|r"|j|ƒt|tƒr:||jƒ7}q|jt|ƒƒqW|S)N)r#rhr&rÚ
_asStringListr7)rÚsepÚoutrir	r	rr„Ùs

zParseResults._asStringListcCsdd„|jDƒ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']
        cSs"g|]}t|tƒr|jƒn|‘qSr	)r&rÚas_list)r
Úresr	r	rr@ôsz(ParseResults.as_list.<locals>.<listcomp>)r#)rr	r	rr‡äszParseResults.as_listcs&‡fdd„‰t‡fdd„|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"}
        cs4t|tƒr,|jƒr|jƒS‡fdd„|DƒS|SdS)Ncsg|]}ˆ|ƒ‘qSr	r	)r
r?)Úto_itemr	rr@sz9ParseResults.as_dict.<locals>.to_item.<locals>.<listcomp>)r&rr_r})Úobj)r‰r	rr‰
s
"z%ParseResults.as_dict.<locals>.to_itemc3s|]\}}|ˆ|ƒfVqdS)Nr	)r
rEr?)r‰r	rr
sz'ParseResults.as_dict.<locals>.<genexpr>)r2rL)rr	)r‰rr}øszParseResults.as_dictcCs:t|jƒ}|jjƒ|_|j|_|j|jO_|j|_|S)zG
        Returns a new copy of a :class:`ParseResults` object.
        )rr#r$rrr r!r)rrbr	r	rrrs
zParseResults.copycsˆ|jr|jS|jr6|jƒ‰‡fdd„}ˆr2||ƒSdSt|ƒdkr€t|jƒdkr€tt|jjƒƒƒdddkr€tt|jjƒƒƒSdSdS)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
        cst‡fdd„ˆjjƒDƒdƒS)Nc3s,|]$\}}|D]\}}ˆ|kr|VqqdS)Nr	)r
rErxr?Úloc)rFr	rr
Asz@ParseResults.get_name.<locals>.find_in_parent.<locals>.<genexpr>)Únextr$rL)rF)Úpar)rFrÚfind_in_parent>s
z-ParseResults.get_name.<locals>.find_in_parentNr>rrA)rrA)rr rHr$rŒrYr]r\)rrŽr	)rrÚget_name szParseResults.get_namercCstg}d}|j|r |t|jƒƒndƒ|rj|jƒrÌtdd„|jƒDƒƒ}x€|D]x\}}	|rf|j|ƒ|jdj|d||ƒƒt|	tƒrº|	rª|j|	j	||||ddƒqÈ|jt|	ƒƒqP|jt
|	ƒƒqPWtd	d„|Dƒƒrj|}	x„t|	ƒD]x\}
}t|tƒr<|jd
j|d||
|d|d|j	||||ddƒƒqî|jd|d||
|d|dt|ƒfƒqîWdj
|ƒ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'
        Ú
rcss|]\}}t|ƒ|fVqdS)N)r7)r
rEr?r	r	rr
nsz$ParseResults.dump.<locals>.<genexpr>z
{}{}- {}: z  r>)ÚindentÚfullÚinclude_listÚ_depthcss|]}t|tƒVqdS)N)r&r)r
Úvvr	r	rr
sz
{}{}[{}]:
{}{}{}z
%s%s[%d]:
%s%s%s)rhr7r‡r_ÚsortedrLr)r&rÚdumpr€ÚanyrMr‚)rr‘r’r“r”r†ÚNLrLrEr?rr•r	r	rr—SsZ



zParseResults.dumpcOstj|jƒf|ž|ŽdS)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(delimited_list(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‡)rrr5r	r	rrš£szParseResults.pprintcCs.|j|jjƒ|jdk	r|jƒp d|j|jffS)N)r#r$rrr r!r)rr	r	rr¿s
zParseResults.__getstate__cCs>|\|_\|_}}|_t|ƒ|_|dk	r4t|ƒ|_nd|_dS)N)r#r$rr0r!rDr )rÚstaterÚinAccumNamesr	r	rrÊs

zParseResults.__setstate__cCs|j|jfS)N)r#r)rr	r	rÚ__getnewargs__ÒszParseResults.__getnewargs__cCstt|ƒƒt|jƒƒS)N)Údirr*r'r\)rr	r	rÚ__dir__ÕszParseResults.__dir__cCsvdd„}|gƒ}xJ|jƒD]>\}}t|tƒr@||j||d7}q|||g|||ƒd7}qW|dk	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.
        cSs2yt|ƒWntk
r dSXt|tƒSdS)NF)rYÚ	Exceptionr&r9)rŠr	r	rÚis_iterableàs
z+ParseResults.from_dict.<locals>.is_iterable)r4)r4r<N)rLr&rÚ	from_dict)r,rsr4r¡rbrEr?r	r	rr¢Øs
zParseResults.from_dict)NN)N)r)rTTr)N):rrrr-r8rrrr'r.r+r&rrrGrTr^rVr6rWrXrrZr[r\r]rLr_rdrCrgrhrkrmrqrtrjr|r7r~rƒr„r‡r2r}rrrr—ršrrrrŸÚclassmethodr¢r<ÚasDictÚgetNamer	r	r	rrsh
,3
	:

3Pr)Úcollections.abcrrrrršÚweakrefrrDÚtypingrrr7Úbytesr9r*r1rrÚregisterr	r	r	rÚ<module>sa