
S0j             g   @   s   d  Z  d Z d Z d Z d d l Z d d l m Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l Z d d l Z d d l Z d d d	 d
 d d d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= d> d? d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ dR dS dT dU dV dW dX dY dZ d[ d\ d] d^ d_ d` da db dc dd de df dg dh di dj dk dl dm gg Z e	 j j dn  Z e r e	 j Z e Z e Z e Z e e e e e e e  e! e" e# e$ g Z% nv e	 j& Z e' Z( do dp   Z g  Z% d d l) Z) xF dq j*   D]8 Z+ y e% j, e- e) e+   Wn e. k
 rwZYn XqZWe/ dr ds   e( dt  D  Z0 du dv   Z1 Gdw dx   dx e2  Z3 e j4 e j5 Z6 dy Z7 e7 dz Z8 e6 e7 Z9 e d{  Z: d| j; d} ds   e j< D  Z= Gd~ d   d e>  Z? Gd d    d  e?  Z@ Gd d"   d" e?  ZA Gd d$   d$ eA  ZB Gd d'   d' e>  ZC Gd d   d e2  ZD Gd d#   d# e2  ZE e jF jG eE  d d;   ZH d dM   ZI d dJ   ZJ d d   ZK d d   ZL d d   ZM d dT   ZN d d d  ZO Gd d%   d% e2  ZP Gd d-   d- eP  ZQ Gd d   d eQ  ZR Gd d   d eQ  ZS Gd d   d eQ  ZT eT ZU eT eP _V Gd d   d eQ  ZW Gd d	   d	 eT  ZX Gd d   d eW  ZY Gd d1   d1 eQ  ZZ Gd d(   d( eQ  Z[ Gd d&   d& eQ  Z\ Gd d
   d
 eQ  Z] Gd d0   d0 eQ  Z^ Gd d   d eQ  Z_ Gd d   d e_  Z` Gd d   d e_  Za Gd d   d e_  Zb Gd d+   d+ e_  Zc Gd d*   d* e_  Zd Gd d3   d3 e_  Ze Gd d2   d2 e_  Zf Gd d!   d! eP  Zg Gd d   d eg  Zh Gd d   d eg  Zi Gd d   d eg  Zj Gd d   d eg  Zk Gd d   d eP  Zl Gd d   d el  Zm Gd d   d el  Zn Gd d4   d4 el  Zo Gd d   d el  Zp Gd d   d e2  Zq eq   Zr Gd d   d el  Zs Gd d)   d) el  Zt Gd d   d el  Zu Gd d   d eu  Zv Gd d.   d. el  Zw Gd d/   d/ ew  Zx Gd d   d ew  Zy Gd d   d ew  Zz Gd d   d ew  Z{ Gd d,   d, ew  Z| Gd d   d e2  Z} d de   Z~ d d d dB  Z d d d>  Z d d   Z d dR   Z d dQ   Z d d   Z d d d dV  Z d dC   Z d d dj  Z d dk   Z d dm   Z eR   j dE  Z ea   j dL  Z eb   j dK  Z ec   j dd  Z ed   j dc  Z eZ e: d d d j d d    Z e[ d  j d d    Z e[ d  j d d    Z e e Be BeZ e= d d d dt BZ ez e e| d  e  Z eT d  es d  j d  ez ep e e B  j d  d Z d db   Z d dP   Z d d_   Z d d]   Z d dg   Z d dD   Z d dI   Z d d   Z d d   Z d dN   Z d dO   Z d dh   Z e2   e _ e3   Z e2   e _ e2   e _ e| d  e| d  d dl  Z e Z e[ d  j d  Z e[ d  j d  Z e[ d  j d  Z ey eU d  e j    Z d d d e j   d dS  Z d d di  Z e d  Z e d  Z e eZ e6 e9 d   \ Z Z ey eU d  e d  j d d j   Z e e d j*   d  Z dd   Z e[ d j d Z e[ d Z e[ d j   Z e[ d	 j d
 Z e[ d j d Z e Z e[ d j d Z ey ep eZ e= d d es eZ d eT d  eb      j   j d Z e es e j   e Bdd|  j d<  Z e dk rdd  Z eX d Z eX d Z eZ e6 e9 d Z e e ddd j e  Z ez e e   Z e e ddd j e  Z ez e e   Z e de Bj d e e j d Z e d e d e d e d  e d! e d! e d" e d# e d$ e d% e d& n  d S('  aw  
pyparsing module - Classes and methods to define and execute parsing grammars

The pyparsing module is an alternative approach to creating and executing simple grammars,
vs. the traditional lex/yacc approach, or the use of regular expressions.  With pyparsing, you
don't need to learn a new syntax for defining grammars or matching expressions - the parsing module
provides a library of classes that you use to construct the grammar directly in Python.

Here is a program to parse "Hello, World!" (or any greeting of the form C{"<salutation>, <addressee>!"})::

    from pyparsing import Word, alphas

    # define grammar of a greeting
    greet = Word( alphas ) + "," + Word( alphas ) + "!"

    hello = "Hello, World!"
    print (hello, "->", greet.parseString( hello ))

The program outputs the following::

    Hello, World! -> ['Hello', ',', 'World', '!']

The Python representation of the grammar is quite readable, owing to the self-explanatory
class names, and the use of '+', '|' and '^' operators.

The parsed results returned from C{parseString()} can be accessed as a nested list, a dictionary, or an
object with named attributes.

The pyparsing module handles some of the problems that are typically vexing when writing text parsers:
 - extra or missing whitespace (the above program will also handle "Hello,World!", "Hello  ,  World  !", etc.)
 - quoted strings
 - embedded comments
z2.0.3z16 Aug 2014 00:12z*Paul McGuire <ptmcg@users.sourceforge.net>    N)refAndCaselessKeywordCaselessLiteral
CharsNotInCombineDictEachEmpty
FollowedByForward
GoToColumnGroupKeywordLineEnd	LineStartLiteral
MatchFirstNoMatchNotAny	OneOrMoreOnlyOnceOptionalOrParseBaseExceptionParseElementEnhanceParseExceptionParseExpressionParseFatalExceptionParseResultsParseSyntaxExceptionParserElementQuotedStringRecursiveGrammarExceptionRegexSkipTo	StringEndStringStartSuppressTokenTokenConverterUpcaseWhiteWordWordEnd	WordStart
ZeroOrMore	alphanumsalphas
alphas8bitanyCloseTag
anyOpenTagcStyleCommentcolcommaSeparatedListcommonHTMLEntitycountedArraycppStyleCommentdblQuotedStringdblSlashCommentdelimitedListdictOfdowncaseTokensemptyhexnumshtmlCommentjavaStyleCommentkeepOriginalTextlinelineEnd	lineStartlinenomakeHTMLTagsmakeXMLTagsmatchOnlyAtColmatchPreviousExprmatchPreviousLiteral
nestedExprnullDebugActionnumsoneOfopAssocoperatorPrecedence
printablespunc8bitpythonStyleCommentquotedStringremoveQuotesreplaceHTMLEntityreplaceWith
restOfLinesglQuotedStringsrange	stringEndstringStarttraceParseActionunicodeStringupcaseTokenswithAttributeindentedBlockoriginalTextForungroupinfixNotationlocatedExpr3c             C   sD   t  |  t  r |  Sy t |   SWn t k
 r? t |   SYn Xd S)a  Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries
           str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It
           then < returns the unicode object | encodes it with the default encoding | ... >.
        N)
isinstanceZunicodestrUnicodeEncodeError)obj ro   +/usr/lib/python3/dist-packages/pyparsing.py_ustrm   s    rq   z6sum len sorted reversed list tuple set any all min maxc             c   s   |  ] } | Vq d  S)Nro   ).0yro   ro   rp   	<genexpr>   s    rt      c             C   sU   d } d d   d j    D } x/ t | |  D] \ } } |  j | |  }  q/ W|  S)z/Escape &, <, >, ", ', etc. in a string of data.z&><"'c             s   s   |  ] } d  | d Vq d S)&;Nro   )rr   sro   ro   rp   rt      s    z_xml_escape.<locals>.<genexpr>zamp gt lt quot apos)splitzipreplace)dataZfrom_symbolsZ
to_symbolsZfrom_Zto_ro   ro   rp   _xml_escape   s
    r}   c               @   s   e  Z d  Z d S)
_ConstantsN)__name__
__module____qualname__ro   ro   ro   rp   r~      s   r~   
0123456789ZABCDEFabcdef\    c             c   s$   |  ] } | t  j k r | Vq d  S)N)stringZ
whitespace)rr   cro   ro   rp   rt      s    c               @   sj   e  Z d  Z d Z d d d d d  Z d d   Z d d	   Z d
 d   Z d d d  Z d d   Z	 d S)r   z7base exception class for all parsing runtime exceptionsr   Nc             C   sI   | |  _  | d  k r* | |  _ d |  _ n | |  _ | |  _ | |  _ d  S)Nr   )locmsgpstrparserElement)selfr   r   r   elemro   ro   rp   __init__   s    				zParseBaseException.__init__c             C   sm   | d k r t  |  j |  j  S| d k r> t |  j |  j  S| d k r] t |  j |  j  St |   d S)zsupported attributes by name are:
            - lineno - returns the line number of the exception text
            - col - returns the column number of the exception text
            - line - returns the line containing the exception text
        rI   r7   columnrF   N)zcolzcolumn)rI   r   r   r7   rF   AttributeError)r   Zanamero   ro   rp   __getattr__   s    zParseBaseException.__getattr__c             C   s    d |  j  |  j |  j |  j f S)Nz"%s (at char %d), (line:%d, col:%d))r   r   rI   r   )r   ro   ro   rp   __str__   s    zParseBaseException.__str__c             C   s
   t  |   S)N)rq   )r   ro   ro   rp   __repr__   s    zParseBaseException.__repr__z>!<c             C   sU   |  j  } |  j d } | rK d j | d |  | | | d  f  } n  | j   S)zExtracts the exception line from the input string, and marks
           the location of the exception with a special symbol.
        ru   r   N)rF   r   joinstrip)r   ZmarkerStringZline_strZline_columnro   ro   rp   markInputline   s    	z ParseBaseException.markInputlinec             C   s
   d j    S)NzIloc msg pstr parserElement lineno col line markInputline __str__ __repr__)ry   )r   ro   ro   rp   __dir__   s    zParseBaseException.__dir__)
r   r   r   __doc__r   r   r   r   r   r   ro   ro   ro   rp   r      s   

c               @   s   e  Z d  Z d Z d S)r   a)  exception thrown when parse expressions don't match class;
       supported attributes by name are:
        - lineno - returns the line number of the exception text
        - col - returns the column number of the exception text
        - line - returns the line containing the exception text
    N)r   r   r   r   ro   ro   ro   rp   r      s   c               @   s   e  Z d  Z d Z d S)r   znuser-throwable exception thrown when inconsistent parse content
       is found; stops all parsing immediatelyN)r   r   r   r   ro   ro   ro   rp   r      s   c                   s(   e  Z d  Z d Z   f d d   Z   S)r    zjust like C{L{ParseFatalException}}, but thrown internally when an
       C{L{ErrorStop<And._ErrorStop>}} ('-' operator) indicates that parsing is to stop immediately because
       an unbacktrackable syntax error has been foundc                s/   t  t |   j | j | j | j | j  d  S)N)superr    r   r   r   r   r   )r   pe)	__class__ro   rp   r      s    zParseSyntaxException.__init__)r   r   r   r   r   ro   ro   )r   rp   r       s   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)r#   zNexception thrown by C{validate()} if the grammar could be improperly recursivec             C   s   | |  _  d  S)N)parseElementTrace)r   parseElementListro   ro   rp   r      s    z"RecursiveGrammarException.__init__c             C   s   d |  j  S)NzRecursiveGrammarException: %s)r   )r   ro   ro   rp   r      s    z!RecursiveGrammarException.__str__N)r   r   r   r   r   r   ro   ro   ro   rp   r#      s   c               @   s@   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 S)
_ParseResultsWithOffsetc             C   s   | | f |  _  d  S)N)tup)r   Zp1Zp2ro   ro   rp   r     s    z _ParseResultsWithOffset.__init__c             C   s   |  j  | S)N)r   )r   iro   ro   rp   __getitem__  s    z#_ParseResultsWithOffset.__getitem__c             C   s   t  |  j  S)N)reprr   )r   ro   ro   rp   r     s    z _ParseResultsWithOffset.__repr__c             C   s   |  j  d | f |  _  d  S)Nr   )r   )r   r   ro   ro   rp   	setOffset	  s    z!_ParseResultsWithOffset.setOffsetN)r   r   r   r   r   r   r   ro   ro   ro   rp   r     s   r   c                   sY  e  Z d  Z d Z d d d d d  Z d d d e d d  Z d d	   Z e d
 d  Z d d   Z	 d d   Z
 d d   Z d d   Z e Z d d   Z d d   Z d d   Z d d   Z d d   Z e r e Z e Z e Z n$ d d   Z d  d!   Z d" d#   Z d$ d%   Z d& d'   Z d d( d)  Z d* d+   Z d, d-   Z d. d/   Z d0 d1   Z d2 d3   Z d4 d5   Z d6 d7   Z  d8 d9   Z! d: d;   Z" d< d=   Z# d> d? d@  Z$ dA dB   Z% dC dD   Z& dE dF   Z' d dG d> d dH dI  Z( dJ dK   Z) dL dM   Z* d> dN dO dP  Z+ dQ dR   Z, dS dT   Z- dU dV   Z.   f dW dX   Z/   S)Yr   zStructured parse results, to provide multiple means of access to the parsed data:
       - as a list (C{len(results)})
       - by list index (C{results[0], results[1]}, etc.)
       - by attribute (C{results.<resultsName>})
       NTc             C   s/   t  | |   r | St j |   } d | _ | S)NT)rk   object__new___ParseResults__doinit)clstoklistnameasListmodalZretobjro   ro   rp   r     s
    	zParseResults.__new__c             C   s  |  j  r d |  _  d  |  _ d  |  _ i  |  _ | | t  rR | d  d   |  _ n- | | t  rs t |  |  _ n | g |  _ t   |  _ n  | d  k	 r| r| s d |  j | <n  | | t	  r t
 |  } n  | |  _ | | t d   t t f  o| d  d g  f k s| | t  r+| g } n  | r| | t  r\t | j   d  |  | <n t t | d  d  |  | <| |  | _ qy | d |  | <Wqt t t f k
 r| |  | <YqXqn  d  S)NFr   r   )r   _ParseResults__name_ParseResults__parent_ParseResults__accumNameslist_ParseResults__toklist_generatorTypedict_ParseResults__tokdictintrq   type
basestringr   r   copyKeyError	TypeError
IndexError)r   r   r   r   r   rk   ro   ro   rp   r     s:    						3zParseResults.__init__c             C   sd   t  | t t f  r  |  j | S| |  j k rB |  j | d d St d d   |  j | D  Sd  S)Nru   r   c             S   s   g  |  ] } | d   q S)r   ro   )rr   vro   ro   rp   
<listcomp>E  s   	 z,ParseResults.__getitem__.<locals>.<listcomp>)rk   r   slicer   r   r   r   )r   r   ro   ro   rp   r   >  s
    zParseResults.__getitem__c             C   s   | | t   rB |  j j | t    | g |  j | <| d } nZ | | t  rg | |  j | <| } n5 |  j j | t    t  | d  g |  j | <| } | | t  r t |   | _ n  d  S)Nr   )	r   r   getr   r   r   r   wkrefr   )r   kr   rk   subro   ro   rp   __setitem__G  s    &	/zParseResults.__setitem__c       
      C   s  t  | t t f  rt |  j  } |  j | =t  | t  rl | d k  rV | | 7} n  t | | d  } n  t t | j |     } | j   x| |  j	 D]d } |  j	 | } xN | D]F } x= t
 |  D]/ \ } \ } }	 t | |	 |	 | k  | | <q Wq Wq Wn
 |  j	 | =d  S)Nr   ru   )rk   r   r   lenr   r   rangeindicesreverser   	enumerater   )
r   r   ZmylenZremovedr   occurrencesjr   valuepositionro   ro   rp   __delitem__T  s    

,zParseResults.__delitem__c             C   s   | |  j  k S)N)r   )r   r   ro   ro   rp   __contains__j  s    zParseResults.__contains__c             C   s   t  |  j  S)N)r   r   )r   ro   ro   rp   __len__m  s    zParseResults.__len__c             C   s   t  |  j  d k S)Nr   )r   r   )r   ro   ro   rp   __bool__n  s    zParseResults.__bool__c             C   s   t  |  j  S)N)iterr   )r   ro   ro   rp   __iter__p  s    zParseResults.__iter__c             C   s   t  |  j d  d  d   S)Nru   r   )r   r   )r   ro   ro   rp   __reversed__q  s    zParseResults.__reversed__c             C   s0   t  |  j d  r |  j j   St |  j  Sd S)zReturns all named result keys.iterkeysN)hasattrr   r   r   )r   ro   ro   rp   r   r  s    zParseResults.iterkeysc                s     f d d     j    D S)z Returns all named result values.c             3   s   |  ] }   | Vq d  S)Nro   )rr   r   )r   ro   rp   rt   {  s    z*ParseResults.itervalues.<locals>.<genexpr>)r   )r   ro   )r   rp   
itervaluesy  s    zParseResults.itervaluesc                s     f d d     j    D S)Nc             3   s   |  ] } |   | f Vq d  S)Nro   )rr   r   )r   ro   rp   rt   ~  s    z)ParseResults.iteritems.<locals>.<genexpr>)r   )r   ro   )r   rp   	iteritems}  s    zParseResults.iteritemsc             C   s   t  |  j    S)zReturns all named result keys.)r   r   )r   ro   ro   rp   keys  s    zParseResults.keysc             C   s   t  |  j    S)z Returns all named result values.)r   r   )r   ro   ro   rp   values  s    zParseResults.valuesc             C   s   t  |  j    S)z=Returns all named result keys and values as a list of tuples.)r   r   )r   ro   ro   rp   items  s    zParseResults.itemsc             C   s   t  |  j  S)zSince keys() returns an iterator, this method is helpful in bypassing
           code that looks for the existence of any defined results names.)boolr   )r   ro   ro   rp   haskeys  s    zParseResults.haskeysc             O   s   | s d g } n  xI | j    D]; \ } } | d k rJ | d | f } q t d |   q Wt | d t  s t |  d k s | d |  k r | d } |  | } |  | =| S| d } | Sd 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().ru   defaultr   z-pop() got an unexpected keyword argument '%s'Nr   )r   r   rk   r   r   )r   argskwargsr   r   indexretZdefaultvaluero   ro   rp   pop  s    	


zParseResults.popc             C   s   | |  k r |  | S| Sd S)zReturns named result matching the given key, or if there is no
           such name, then returns the given C{defaultValue} or C{None} if no
           C{defaultValue} is specified.Nro   )r   keydefaultValuero   ro   rp   r     s    zParseResults.getc             C   sx   |  j  j | |  x^ |  j D]S } |  j | } x= t |  D]/ \ } \ } } t | | | | k  | | <q= Wq Wd S)zCInserts new element at location index in the list of parsed tokens.N)r   insertr   r   r   )r   r   ZinsStrr   r   r   r   r   ro   ro   rp   r     s
    zParseResults.insertc             C   s   |  j  j |  d S)z;Add single element to end of ParseResults list of elements.N)r   append)r   itemro   ro   rp   r     s    zParseResults.appendc             C   s0   t  | t  r |  | 7}  n |  j j |  d S)zAAdd sequence of elements to end of ParseResults list of elements.N)rk   r   r   extend)r   Zitemseqro   ro   rp   r     s    zParseResults.extendc             C   s!   |  j  d d  =|  j j   d S)z%Clear all elements and results names.N)r   r   clear)r   ro   ro   rp   r     s    zParseResults.clearc             C   s   y |  | SWn t  k
 r$ d SYn X| |  j k rw | |  j k rV |  j | d d St d d   |  j | D  Sn d Sd  S)Nr   ru   r   c             S   s   g  |  ] } | d   q S)r   ro   )rr   r   ro   ro   rp   r     s   	 z,ParseResults.__getattr__.<locals>.<listcomp>r   )r   r   r   r   )r   r   ro   ro   rp   r     s    	!zParseResults.__getattr__c             C   s   |  j    } | | 7} | S)N)r   )r   otherr   ro   ro   rp   __add__  s    
zParseResults.__add__c                s   | j  r t |  j    f d d     | j  j   }   f d d   | D } xJ | D]? \ } } | |  | <t | d t  rY t |   | d _ qY qY Wn  |  j | j 7_ |  j j	 | j  |  S)Nc                s   |  d k  r   p |    S)Nr   ro   )a)offsetro   rp   <lambda>  s    z'ParseResults.__iadd__.<locals>.<lambda>c          	      sF   g  |  ]< \ } } | D]) } | t  | d    | d   f  q q S)r   ru   )r   )rr   r   vlistr   )	addoffsetro   rp   r     s   	z)ParseResults.__iadd__.<locals>.<listcomp>r   )
r   r   r   r   rk   r   r   r   r   update)r   r   Z
otheritemsZotherdictitemsr   r   ro   )r   r   rp   __iadd__  s    	

zParseResults.__iadd__c             C   s)   t  | t  r% | d k r% |  j   Sd  S)Nr   )rk   r   r   )r   r   ro   ro   rp   __radd__  s    zParseResults.__radd__c             C   s    d t  |  j  t  |  j  f S)Nz(%s, %s))r   r   r   )r   ro   ro   rp   r     s    zParseResults.__repr__c             C   sg   g  } xI |  j  D]> } t | t  r; | j t |   q | j t |   q Wd d j |  d S)N[z, ])r   rk   r   r   rq   r   r   )r   outr   ro   ro   rp   r     s    zParseResults.__str__r   c             C   so   g  } xb |  j  D]W } | r2 | r2 | j |  n  t | t  rT | | j   7} q | j t |   q W| S)N)r   r   rk   r   _asStringListrq   )r   sepr   r   ro   ro   rp   r     s    zParseResults._asStringListc             C   sP   g  } xC |  j  D]8 } t | t  r; | j | j    q | j |  q W| S)zXReturns the parse results as a nested list of matching tokens, all converted to strings.)r   rk   r   r   r   )r   r   resro   ro   rp   r     s    zParseResults.asListc             C   s*   t  r t |  j    St |  j    Sd S)z.Returns the named parse results as dictionary.N)PY_3r   r   r   )r   ro   ro   rp   asDict  s    zParseResults.asDictc             C   sP   t  |  j  } |  j j   | _ |  j | _ | j j |  j  |  j | _ | S)z/Returns a new copy of a C{ParseResults} object.)r   r   r   r   r   r   r   r   )r   r   ro   ro   rp   r   "  s    zParseResults.copyFc             C   s  d } g  } t  d d   |  j j   D  } | d } | sS d } d } d } n  d }	 | d k	 rn | }	 n |  j r |  j }	 n  |	 s | r d Sd }	 n  | | | d |	 d	 g 7} |  j }
 xt |
  D] \ } } t | t  rR| | k r$| | j | | | o| d k | |  g 7} q| | j d | o?| d k | |  g 7} q d } | | k rq| | } n  | s| rq qd } n  t	 t
 |   } | | | d | d	 | d
 | d	 g	 7} q W| | | d
 |	 d	 g 7} d j |  S)zhReturns the parse results as XML. Tags are created for tokens and lists that have defined results names.
c             s   s2   |  ]( \ } } | D] } | d  | f Vq q d S)ru   Nro   )rr   r   r   r   ro   ro   rp   rt   /  s    	z%ParseResults.asXML.<locals>.<genexpr>z  r   NZITEM<>z</)r   r   r   r   r   r   rk   r   asXMLr}   rq   r   )r   ZdoctagZnamedItemsOnlyindentZ	formattednlr   Z
namedItemsZnextLevelIndentZselfTagZworklistr   r   ZresTagZxmlBodyTextro   ro   rp   r   +  sV    "
						zParseResults.asXMLc             C   sK   xD |  j  j   D]3 \ } } x$ | D] \ } } | | k r# | Sq# Wq Wd  S)N)r   r   )r   r   r   r   r   r   ro   ro   rp   Z__lookupg  s
    zParseResults.__lookupc             C   s   |  j  r |  j  S|  j r? |  j   } | r8 | j |   Sd Sn] t |   d k r t |  j  d k r |  j j   d d d d k r |  j j   d Sd Sd S)z3Returns the results name for this token expression.Nru   r   r   )r   r   )r   r   _ParseResults__lookupr   r   r   r   )r   parro   ro   rp   getNamen  s    		!zParseResults.getNamer   c       
      C   s  g  } d } | j  | t |  j     t |  j    } x| D]\ } } | rd | j  |  n  | j  d | d | | f  t | t  r| r| j   r | j  | j | | d   qt	 d d   | D  rx t
 |  D] \ } }	 t |	 t  rJ| j  d | d | d | | d | d |	 j | | d  f  q | j  d | d | d | | d | d t |	  f  q Wq| j  t |   q| j  t |   qB | j  t |   qB Wd	 j |  S)
zDiagnostic method for listing out the contents of a C{ParseResults}.
           Accepts an optional C{indent} argument so that this string can be embedded
           in a nested display of other data.r   z
%s%s- %s: z  ru   c             s   s   |  ] } t  | t  Vq d  S)N)rk   r   )rr   vvro   ro   rp   rt     s    z$ParseResults.dump.<locals>.<genexpr>z
%s%s[%d]:
%s%s%s   r   )r   rq   r   sortedr   rk   r   r   dumpanyr   r   )
r   r   Zdepthr   NLr   r   r   r   r  ro   ro   rp   r    s*     F@zParseResults.dumpc             O   s   t  j  |  j   | |  d S)zPretty-printer for parsed results as a list, using the C{pprint} module.
           Accepts additional positional or keyword args as defined for the 
           C{pprint.pprint} method. (U{http://docs.python.org/3/library/pprint.html#pprint.pprint})N)pprintr   )r   r   r   ro   ro   rp   r
    s    zParseResults.pprintc             C   sC   |  j  |  j j   |  j d  k	 r- |  j   p0 d  |  j |  j f f S)N)r   r   r   r   r   r   )r   ro   ro   rp   __getstate__  s
    zParseResults.__getstate__c             C   sm   | d |  _  | d \ |  _ } } |  _ i  |  _ |  j j |  | d  k	 r` t |  |  _ n	 d  |  _ d  S)Nr   ru   )r   r   r   r   r   r   r   )r   stater  ZinAccumNamesro   ro   rp   __setstate__  s    	zParseResults.__setstate__c                s#   t  t t |    t |  j    S)N)dirr   r   r   r   )r   )r   ro   rp   r     s    zParseResults.__dir__)0r   r   r   r   r   rk   r   r   r   r   r   r   r   __nonzero__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r
  r  r  r   ro   ro   )r   rp   r     s^   	#					
	<c             C   s?   |  t  |  k  r( | |  d k r( d p> |  | j d d |   S)a  Returns current column within a string, counting newlines as line separators.
   The first column is number 1.

   Note: the default parsing behavior is to expand tabs in the input string
   before starting the parsing process.  See L{I{ParserElement.parseString}<ParserElement.parseString>} for more information
   on parsing strings containing C{<TAB>}s, and suggested methods to maintain a
   consistent view of the parsed string, the parse location, and line and column
   positions within the parsed string.
   r   ru   r   )r   rfind)r   strgro   ro   rp   r7     s    
c             C   s   | j  d d |   d S)a  Returns current line number within a string, counting newlines as line separators.
   The first line is number 1.

   Note: the default parsing behavior is to expand tabs in the input string
   before starting the parsing process.  See L{I{ParserElement.parseString}<ParserElement.parseString>} for more information
   on parsing strings containing C{<TAB>}s, and suggested methods to maintain a
   consistent view of the parsed string, the parse location, and line and column
   positions within the parsed string.
   r   r   ru   )count)r   r  ro   ro   rp   rI     s    
c             C   s[   | j  d d |   } | j d |   } | d k rE | | d |  S| | d d  Sd S)zfReturns the line of text containing loc within a string, counting newlines as line separators.
       r   r   ru   N)r  find)r   r  ZlastCRZnextCRro   ro   rp   rF     s
    c             C   sF   t  d t |  d t |  d t | |   t | |   f  d  S)NzMatch z at loc z(%d,%d))printrq   rI   r7   )instringr   exprro   ro   rp   _defaultStartDebugAction  s    r  c             C   s,   t  d t |  d t | j     d  S)NzMatched z -> )r  rq   rl   r   )r  Zstartlocendlocr  toksro   ro   rp   _defaultSuccessDebugAction  s    r  c             C   s   t  d t |   d  S)NzException raised:)r  rq   )r  r   r  excro   ro   rp   _defaultExceptionDebugAction  s    r  c              G   s   d S)zG'Do-nothing' debug action, to suppress debugging output during parsing.Nro   )r   ro   ro   rp   rP     s    r  c                sM    t  k r  f d d   Sd g  d g        f d d   } | S)Nc                s
     |  S)Nro   )rx   lt)funcro   rp   r     s    z_trim_arity.<locals>.<lambda>r   Fc                 s}   xv y,  |   d d     } d   d <| SWq t  k
 ru  d  k rn   d rn  d d 7<w n    Yq Xq d  S)Nr   Tru   )r   )r   r   )
foundArityr  limitmaxargsro   rp   wrapper  s    
z_trim_arity.<locals>.wrapper)singleArgBuiltins)r  r"  r#  ro   )r   r  r!  r"  rp   _trim_arity  s    		r%  c                   s.  e  Z d  Z d Z d Z d Z d d   Z e e  Z d d   Z e e  Z d d d	  Z	 d
 d   Z
 d d   Z d d d  Z d d d  Z d d   Z d d   Z d d   Z d d   Z d d   Z d d d  Z d d    Z d d d! d"  Z d# d$   Z d d d% d&  Z e Z i  Z d' d(   Z e e  Z d Z d) d*   Z e e  Z d d+ d,  Z e d d- d.  Z d/ d0   Z  e d1 d2  Z! d3 d4   Z" d5 d6   Z# d7 d8   Z$ d9 d:   Z% d; d<   Z& d= d>   Z' d? d@   Z( dA dB   Z) dC dD   Z* dE dF   Z+ dG dH   Z, dI dJ   Z- dK dL   Z. dM dN dO  Z/ dP dQ   Z0 dR dS   Z1 dT dU   Z2 dV dW   Z3 dX dY   Z4 dZ d[   Z5 d d\ d]  Z6 d^ d_   Z7 d` da   Z8 db dc   Z9 dd de   Z: g  df dg  Z; d dh di  Z<   f dj dk   Z= dl dm   Z> dn do   Z? dp dq   Z@ dr ds   ZA   S)tr!   z)Abstract base level parser element class.z 
	Fc             C   s   |  t  _ d S)z/Overrides the default whitespace chars
        N)r!   DEFAULT_WHITE_CHARS)charsro   ro   rp   setDefaultWhitespaceChars  s    z'ParserElement.setDefaultWhitespaceCharsc             C   s   |  t  _ d S)zV
        Set class to be used for inclusion of string literals into a parser.
        N)r!   literalStringClass)r   ro   ro   rp   inlineLiteralsUsing   s    z!ParserElement.inlineLiteralsUsingc             C   s   t    |  _ d  |  _ d  |  _ d  |  _ | |  _ d |  _ t j |  _	 d |  _
 d |  _ d |  _ t    |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d  |  _ d |  _ d |  _ d  S)NTFr   )NNN)r   parseAction
failActionstrReprresultsName
saveAsListskipWhitespacer!   r&  
whiteCharscopyDefaultWhiteCharsmayReturnEmptykeepTabsignoreExprsdebugstreamlinedmayIndexErrorerrmsgmodalResultsdebugActionsrecallPreparsecallDuringTry)r   savelistro   ro   rp   r   '  s(    																zParserElement.__init__c             C   sW   t  j  |   } |  j d d  | _ |  j d d  | _ |  j rS t j | _ n  | S)zMake a copy of this C{ParserElement}.  Useful for defining different parse actions
           for the same parsing pattern, using copies of the original parse element.N)r   r+  r5  r2  r!   r&  r1  )r   Zcpyro   ro   rp   r   >  s    	zParserElement.copyc             C   s>   | |  _  d |  j  |  _ t |  d  r: |  j |  j _ n  |  S)z6Define name for this expression, for use in debugging.z	Expected 	exception)r   r9  r   r@  r   )r   r   ro   ro   rp   setNameH  s
    	zParserElement.setNamec             C   sK   |  j    } | j d  r4 | d d  } d } n  | | _ | | _ | S)a%  Define name for referencing matching tokens as a nested attribute
           of the returned parse results.
           NOTE: this returns a *copy* of the original C{ParserElement} object;
           this is so that the client can define a basic element, such as an
           integer, and reference it in multiple places with different names.
           
           You can also set results names using the abbreviated syntax,
           C{expr("name")} in place of C{expr.setResultsName("name")} - 
           see L{I{__call__}<__call__>}.
        *Nru   Tr   )r   endswithr.  r:  )r   r   listAllMatchesZnewselfro   ro   rp   setResultsNameP  s    		
zParserElement.setResultsNameTc                sd   | r< |  j    d d   f d d  }   | _ | |  _  n$ t |  j  d  r` |  j  j |  _  n  |  S)zMethod to invoke the Python pdb debugger when this element is
           about to be parsed. Set C{breakFlag} to True to enable, False to
           disable.
        Tc                s)   d d  l  } | j     |  | | |  S)Nr   )pdbZ	set_trace)r  r   	doActionscallPreParserF  )_parseMethodro   rp   breakerj  s    
z'ParserElement.setBreak.<locals>.breaker_originalParseMethod)_parserK  r   )r   Z	breakFlagrJ  ro   )rI  rp   setBreakc  s    		zParserElement.setBreakc             O   s;   t  t t t  |    |  _ d | k o1 | d |  _ |  S)a_  Define action to perform when successfully matching parse element definition.
           Parse action fn is a callable method with 0-3 arguments, called as C{fn(s,loc,toks)},
           C{fn(loc,toks)}, C{fn(toks)}, or just C{fn()}, where:
            - s   = the original string being parsed (see note below)
            - loc = the location of the matching substring
            - toks = a list of the matched tokens, packaged as a C{L{ParseResults}} object
           If the functions in fns modify the tokens, they can return them as the return
           value from fn, and the modified list of tokens will replace the original.
           Otherwise, fn does not need to return any value.

           Note: the default parsing behavior is to expand tabs in the input string
           before starting the parsing process.  See L{I{parseString}<parseString>} for more information
           on parsing strings containing C{<TAB>}s, and suggested methods to maintain a
           consistent view of the parsed string, the parse location, and line and column
           positions within the parsed string.
           r>  )r   mapr%  r+  r>  )r   fnsr   ro   ro   rp   setParseActionu  s    zParserElement.setParseActionc             O   sJ   |  j  t t t t |    7_  |  j p@ d | k o@ | d |  _ |  S)zaAdd parse action to expression's list of parse actions. See L{I{setParseAction}<setParseAction>}.r>  )r+  r   rN  r%  r>  )r   rO  r   ro   ro   rp   addParseAction  s    $"zParserElement.addParseActionc             C   s   | |  _  |  S)a  Define action to perform if parsing fails at this expression.
           Fail acton fn is a callable function that takes the arguments
           C{fn(s,loc,expr,err)} where:
            - s = string being parsed
            - loc = location where expression match was attempted and failed
            - expr = the parse expression that failed
            - err = the exception thrown
           The function returns no value.  It may throw C{L{ParseFatalException}}
           if it is desired to stop parsing immediately.)r,  )r   fnro   ro   rp   setFailAction  s    
	zParserElement.setFailActionc             C   sn   d } xa | ri d } xN |  j  D]C } y( x! | j | |  \ } } d } q+ Wq t k
 ra Yq Xq Wq	 W| S)NTF)r5  rL  r   )r   r  r   Z
exprsFoundeZdummyro   ro   rp   _skipIgnorables  s    	zParserElement._skipIgnorablesc             C   sp   |  j  r |  j | |  } n  |  j rl |  j } t |  } x- | | k  rh | | | k rh | d 7} q? Wn  | S)Nru   )r5  rU  r0  r1  r   )r   r  r   Zwtinstrlenro   ro   rp   preParse  s    			zParserElement.preParsec             C   s
   | g  f S)Nro   )r   r  r   rG  ro   ro   rp   	parseImpl  s    zParserElement.parseImplc             C   s   | S)Nro   )r   r  r   	tokenlistro   ro   rp   	postParse  s    zParserElement.postParsec          "   C   s  |  j  } | s |  j r?|  j d r? |  j d | | |   n  | rc |  j rc |  j | |  } n | } | } yV y |  j | | |  \ } } Wn0 t k
 r t | t |  |  j	 |    Yn XWqt
 k
 r;}	 zT |  j d r|  j d | | |  |	  n  |  j r&|  j | | |  |	  n    WYd  d  }	 ~	 XqXn | rc|  j rc|  j | |  } n | } | } |  j s| t |  k ry |  j | | |  \ } } Wqt k
 rt | t |  |  j	 |    YqXn |  j | | |  \ } } |  j | | |  } t | |  j d |  j d |  j }
 |  j r| sK|  j r| ryr xk |  j D]` } | | | |
  } | d  k	 r^t | |  j d |  j ot | t t f  d |  j }
 q^q^WWqt
 k
 r}	 z2 |  j d r|  j d | | |  |	  n    WYd  d  }	 ~	 XqXqxn |  j D]` } | | | |
  } | d  k	 r%t | |  j d |  j ost | t t f  d |  j }
 q%q%Wn  | r|  j d r|  j d | | | |  |
  qn  | |
 f S)Nr   r  r   r   ru   )r6  r,  r;  r=  rW  rX  r   r   r   r9  r   r8  rZ  r   r.  r/  r:  r+  r>  rk   r   )r   r  r   rG  rH  Z	debuggingprelocZtokensStarttokenserrZ	retTokensrR  ro   ro   rp   _parseNoCache  sp    	'	&$		#zParserElement._parseNoCachec             C   sO   y |  j  | | d d d SWn* t k
 rJ t | | |  j |    Yn Xd  S)NrG  Fr   )rL  r   r   r9  )r   r  r   ro   ro   rp   tryParse  s    zParserElement.tryParsec             C   s   |  | | | | f } | t  j k ra t  j | } t | t  rI |  n  | d | d j   f SyA |  j | | | |  } | d | d j   f t  j | <| SWn> t k
 r } z d  | _ | t  j | <  WYd  d  } ~ Xn Xd  S)Nr   ru   )r!   _exprArgCacherk   	Exceptionr   r^  r   __traceback__)r   r  r   rG  rH  lookupr   r   ro   ro   rp   _parseCache  s    	!	zParserElement._parseCachec               C   s   t  j j   d  S)N)r!   r`  r   ro   ro   ro   rp   
resetCache"  s    zParserElement.resetCachec               C   s%   t  j s! d t  _ t  j t  _ n  d S)a  Enables "packrat" parsing, which adds memoizing to the parsing logic.
           Repeated parse attempts at the same string location (which happens
           often in many complex grammars) can immediately return a cached value,
           instead of re-executing parsing/validating code.  Memoizing is done of
           both valid results and parsing exceptions.

           This speedup may break existing programs that use parse actions that
           have side-effects.  For this reason, packrat parsing is disabled when
           you first import pyparsing.  To activate the packrat feature, your
           program must call the class method C{ParserElement.enablePackrat()}.  If
           your program uses C{psyco} to "compile as you go", you must call
           C{enablePackrat} before calling C{psyco.full()}.  If you do not do this,
           Python will crash.  For best results, call C{enablePackrat()} immediately
           after importing pyparsing.
        TN)r!   _packratEnabledrd  rL  ro   ro   ro   rp   enablePackrat'  s    		zParserElement.enablePackratc             C   s   t  j   |  j s  |  j   n  x |  j D] } | j   q* W|  j sV | j   } n  yW |  j | d  \ } } | r |  j | |  } t	   t
   } | j | |  n  Wn: t k
 r } z t  j r   n |  WYd d } ~ Xn X| Sd S)a  Execute the parse expression with the given string.
           This is the main interface to the client code, once the complete
           expression has been built.

           If you want the grammar to require that the entire input string be
           successfully parsed, then set C{parseAll} to True (equivalent to ending
           the grammar with C{L{StringEnd()}}).

           Note: C{parseString} implicitly calls C{expandtabs()} on the input string,
           in order to report proper column numbers in parse actions.
           If the input string contains tabs and
           the grammar uses parse actions that use the C{loc} argument to index into the
           string being parsed, you can ensure you have a consistent view of the input
           string by:
            - calling C{parseWithTabs} on your grammar before calling C{parseString}
              (see L{I{parseWithTabs}<parseWithTabs>})
            - define your parse action using the full C{(s,loc,toks)} signature, and
              reference the input string using the parse action's C{s} argument
            - explictly expand the tabs in your input string before calling
              C{parseString}
        r   N)r!   re  r7  
streamliner5  r4  
expandtabsrL  rW  r
   r&   r   verbose_stacktrace)r   r  parseAllrT  r   r\  Zser  ro   ro   rp   parseString<  s$    
			zParserElement.parseStringc             c   s  |  j  s |  j   n  x |  j D] } | j   q  W|  j sR t |  j   } n  t |  } d } |  j } |  j } t	 j
   d }	 y x | | k rb|	 | k  rby. | | |  }
 | | |
 d d \ } } Wn t k
 r |
 d } Yq X| | k rU|	 d 7}	 | |
 | f V| rL| | |  } | | k r?| } qR| d 7} q_| } q |
 d } q WWn: t k
 r} z t	 j r  n |  WYd d } ~ Xn Xd S)a"  Scan the input string for expression matches.  Each match will return the
           matching tokens, start location, and end location.  May be called with optional
           C{maxMatches} argument, to clip scanning after 'n' matches are found.  If
           C{overlap} is specified, then overlapping matches will be reported.

           Note that the start and end locations are reported relative to the string
           being parsed.  See L{I{parseString}<parseString>} for more information on parsing
           strings with embedded tabs.r   rH  Fru   N)r7  rh  r5  r4  rq   ri  r   rW  rL  r!   re  r   r   rj  )r   r  
maxMatchesZoverlaprT  rV  r   Z
preparseFnZparseFnZmatchesr[  ZnextLocr\  Znextlocr  ro   ro   rp   
scanStringi  sB    					

			zParserElement.scanStringc             C   s4  g  } d } d |  _  y x |  j |  D] \ } } } | j | | |   | r t | t  rv | | j   7} q t | t  r | | 7} q | j |  n  | } q( W| j | | d   d d   | D } d j t t	 t
 |    SWn: t k
 r/} z t j r  n |  WYd d } ~ Xn Xd S)a  Extension to C{L{scanString}}, to modify matching text with modified tokens that may
           be returned from a parse action.  To use C{transformString}, define a grammar and
           attach a parse action to it that modifies the returned token list.
           Invoking C{transformString()} on a target string will then scan for matches,
           and replace the matched text patterns according to the logic in the parse
           action.  C{transformString()} returns the resulting transformed string.r   TNc             S   s   g  |  ] } | r |  q Sro   ro   )rr   oro   ro   rp   r     s   	 z1ParserElement.transformString.<locals>.<listcomp>r   )r4  rn  r   rk   r   r   r   r   rN  rq   _flattenr   r!   rj  )r   r  r   ZlastEr  rx   rT  r  ro   ro   rp   transformString  s(    	
 	zParserElement.transformStringc             C   sh   y' t  d d   |  j | |  D  SWn: t k
 rc } z t j rK   n |  WYd d } ~ Xn Xd S)zAnother extension to C{L{scanString}}, simplifying the access to the tokens found
           to match the given parse expression.  May be called with optional
           C{maxMatches} argument, to clip searching after 'n' matches are found.
        c             S   s   g  |  ] \ } } } |  q Sro   ro   )rr   r  rx   rT  ro   ro   rp   r     s   	 z.ParserElement.searchString.<locals>.<listcomp>N)r   rn  r   r!   rj  )r   r  rm  r  ro   ro   rp   searchString  s    '	zParserElement.searchStringc             C   sd   t  | t  r! t j |  } n  t  | t  sT t j d t |  t d d d St |  | g  S)z0Implementation of + operator - returns C{L{And}}z4Cannot combine element of type %s with ParserElement
stacklevelr  N)	rk   r   r!   r)  warningswarnr   SyntaxWarningr   )r   r   ro   ro   rp   r     s    zParserElement.__add__c             C   s\   t  | t  r! t j |  } n  t  | t  sT t j d t |  t d d d S| |  S)zKImplementation of + operator when left operand is not a C{L{ParserElement}}z4Cannot combine element of type %s with ParserElementrs  r  N)rk   r   r!   r)  rt  ru  r   rv  )r   r   ro   ro   rp   r     s    zParserElement.__radd__c             C   sm   t  | t  r! t j |  } n  t  | t  sT t j d t |  t d d d St |  t j	   | g  S)z?Implementation of - operator, returns C{L{And}} with error stopz4Cannot combine element of type %s with ParserElementrs  r  N)
rk   r   r!   r)  rt  ru  r   rv  r   
_ErrorStop)r   r   ro   ro   rp   __sub__  s    zParserElement.__sub__c             C   s\   t  | t  r! t j |  } n  t  | t  sT t j d t |  t d d d S| |  S)zKImplementation of - operator when left operand is not a C{L{ParserElement}}z4Cannot combine element of type %s with ParserElementrs  r  N)rk   r   r!   r)  rt  ru  r   rv  )r   r   ro   ro   rp   __rsub__  s    zParserElement.__rsub__c                sN  t  | t  r | d } } n3t  | t  r=| d d d  } | d d k re d | d f } n  t  | d t  r | d d k r | d d k r t   S| d d k r t   S | d t   SqRt  | d t  rt  | d t  r| \ } } | | 8} qRt d t | d  t | d    n t d t |    | d k  rmt d   n  | d k  rt d   n  | | k od k n rt d	   n  | r"   f d
 d     | r| d k r   |  } qt  g |    |  } qJ  |  } n( | d k r7 } n t  g |  } | S)a  Implementation of * operator, allows use of C{expr * 3} in place of
           C{expr + expr + expr}.  Expressions may also me multiplied by a 2-integer
           tuple, similar to C{{min,max}} multipliers in regular expressions.  Tuples
           may also include C{None} as in:
            - C{expr*(n,None)} or C{expr*(n,)} is equivalent
              to C{expr*n + L{ZeroOrMore}(expr)}
              (read as "at least n instances of C{expr}")
            - C{expr*(None,n)} is equivalent to C{expr*(0,n)}
              (read as "0 to n instances of C{expr}")
            - C{expr*(None,None)} is equivalent to C{L{ZeroOrMore}(expr)}
            - C{expr*(1,None)} is equivalent to C{L{OneOrMore}(expr)}

           Note that C{expr*(None,n)} does not raise an exception if
           more than n exprs exist in the input stream; that is,
           C{expr*(None,n)} does not enforce a maximum number of expr
           occurrences.  If this behavior is desired, then write
           C{expr*(None,n) + ~expr}

        r   Nr  ru   z7cannot multiply 'ParserElement' and ('%s','%s') objectsz0cannot multiply 'ParserElement' and '%s' objectsz/cannot multiply ParserElement by negative valuez@second tuple value must be greater or equal to first tuple valuez+cannot multiply ParserElement by 0 or (0,0)c                s2   |  d k r$ t     |  d   St    Sd  S)Nru   )r   )n)makeOptionalListr   ro   rp   r{  #  s    z/ParserElement.__mul__.<locals>.makeOptionalList)NN)	rk   r   tupler0   r   r   r   
ValueErrorr   )r   r   ZminElementsZoptElementsr   ro   )r{  r   rp   __mul__  sD    #

&) 	zParserElement.__mul__c             C   s   |  j  |  S)N)r~  )r   r   ro   ro   rp   __rmul__6  s    zParserElement.__rmul__c             C   sd   t  | t  r! t j |  } n  t  | t  sT t j d t |  t d d d St |  | g  S)z7Implementation of | operator - returns C{L{MatchFirst}}z4Cannot combine element of type %s with ParserElementrs  r  N)	rk   r   r!   r)  rt  ru  r   rv  r   )r   r   ro   ro   rp   __or__9  s    zParserElement.__or__c             C   s\   t  | t  r! t j |  } n  t  | t  sT t j d t |  t d d d S| |  BS)zKImplementation of | operator when left operand is not a C{L{ParserElement}}z4Cannot combine element of type %s with ParserElementrs  r  N)rk   r   r!   r)  rt  ru  r   rv  )r   r   ro   ro   rp   __ror__C  s    zParserElement.__ror__c             C   sd   t  | t  r! t j |  } n  t  | t  sT t j d t |  t d d d St |  | g  S)z/Implementation of ^ operator - returns C{L{Or}}z4Cannot combine element of type %s with ParserElementrs  r  N)	rk   r   r!   r)  rt  ru  r   rv  r   )r   r   ro   ro   rp   __xor__M  s    zParserElement.__xor__c             C   s\   t  | t  r! t j |  } n  t  | t  sT t j d t |  t d d d S| |  AS)zKImplementation of ^ operator when left operand is not a C{L{ParserElement}}z4Cannot combine element of type %s with ParserElementrs  r  N)rk   r   r!   r)  rt  ru  r   rv  )r   r   ro   ro   rp   __rxor__W  s    zParserElement.__rxor__c             C   sd   t  | t  r! t j |  } n  t  | t  sT t j d t |  t d d d St |  | g  S)z1Implementation of & operator - returns C{L{Each}}z4Cannot combine element of type %s with ParserElementrs  r  N)	rk   r   r!   r)  rt  ru  r   rv  r	   )r   r   ro   ro   rp   __and__a  s    zParserElement.__and__c             C   s\   t  | t  r! t j |  } n  t  | t  sT t j d t |  t d d d S| |  @S)zKImplementation of & operator when left operand is not a C{L{ParserElement}}z4Cannot combine element of type %s with ParserElementrs  r  N)rk   r   r!   r)  rt  ru  r   rv  )r   r   ro   ro   rp   __rand__k  s    zParserElement.__rand__c             C   s
   t  |   S)z3Implementation of ~ operator - returns C{L{NotAny}})r   )r   ro   ro   rp   
__invert__u  s    zParserElement.__invert__Nc             C   s'   | d k	 r |  j  |  S|  j   Sd S)a  Shortcut for C{L{setResultsName}}, with C{listAllMatches=default}::
             userdata = Word(alphas).setResultsName("name") + Word(nums+"-").setResultsName("socsecno")
           could be written as::
             userdata = Word(alphas)("name") + Word(nums+"-")("socsecno")
             
           If C{name} is given with a trailing C{'*'} character, then C{listAllMatches} will be
           passed as C{True}.
           
           If C{name} is omitted, same as calling C{L{copy}}.
           N)rE  r   )r   r   ro   ro   rp   __call__y  s    zParserElement.__call__c             C   s
   t  |   S)zSuppresses the output of this C{ParserElement}; useful to keep punctuation from
           cluttering up returned output.
        )r(   )r   ro   ro   rp   suppress  s    zParserElement.suppressc             C   s   d |  _  |  S)a  Disables the skipping of whitespace before matching the characters in the
           C{ParserElement}'s defined pattern.  This is normally only used internally by
           the pyparsing module, but may be needed in some whitespace-sensitive grammars.
        F)r0  )r   ro   ro   rp   leaveWhitespace  s    	zParserElement.leaveWhitespacec             C   s   d |  _  | |  _ d |  _ |  S)z/Overrides the default whitespace chars
        TF)r0  r1  r2  )r   r'  ro   ro   rp   setWhitespaceChars  s    			z ParserElement.setWhitespaceCharsc             C   s   d |  _  |  S)zOverrides default behavior to expand C{<TAB>}s to spaces before parsing the input string.
           Must be called before C{parseString} when the input grammar contains elements that
           match C{<TAB>} characters.T)r4  )r   ro   ro   rp   parseWithTabs  s    	zParserElement.parseWithTabsc             C   sZ   t  | t  r: | |  j k rV |  j j | j    qV n |  j j t | j     |  S)zDefine expression to be ignored (e.g., comments) while doing pattern
           matching; may be called repeatedly, to define multiple comment or other
           ignorable patterns.
        )rk   r(   r5  r   r   )r   r   ro   ro   rp   ignore  s
    zParserElement.ignorec             C   s1   | p	 t  | p t | p t f |  _ d |  _ |  S)zBEnable display of debugging messages while doing pattern matching.T)r  r  r  r;  r6  )r   ZstartActionZsuccessActionZexceptionActionro   ro   rp   setDebugActions  s
    			zParserElement.setDebugActionsc             C   s)   | r |  j  t t t  n	 d |  _ |  S)z~Enable display of debugging messages while doing pattern matching.
           Set C{flag} to True to enable, False to disable.F)r  r  r  r  r6  )r   Zflagro   ro   rp   setDebug  s    	zParserElement.setDebugc             C   s   |  j  S)N)r   )r   ro   ro   rp   r     s    zParserElement.__str__c             C   s
   t  |   S)N)rq   )r   ro   ro   rp   r     s    zParserElement.__repr__c             C   s   d |  _  d  |  _ |  S)NT)r7  r-  )r   ro   ro   rp   rh    s    		zParserElement.streamlinec             C   s   d  S)Nro   )r   r   ro   ro   rp   checkRecursion  s    zParserElement.checkRecursionc             C   s   |  j  g   d S)zXCheck defined expressions for valid structure, check for infinite recursive definitions.N)r  )r   validateTracero   ro   rp   validate  s    zParserElement.validatec             C   s   y | j    } Wn7 t k
 rI t | d  } | j    } | j   Yn Xy |  j | |  SWn: t k
 r } z t j r   n |  WYd d } ~ Xn Xd S)zExecute the parse expression on the given file or filename.
           If a filename is specified (instead of a file object),
           the entire file is opened, read, and closed before parsing.
        rN)readr   opencloserl  r   r!   rj  )r   Zfile_or_filenamerk  Zfile_contentsfr  ro   ro   rp   	parseFile  s    	zParserElement.parseFilec                s   t  | t  r+ |  | k p* |  j | j k St  | t  rw y! |  j t |  d d d SWq t k
 rs d SYq Xn t t |   | k Sd  S)Nrk  TF)rk   r!   __dict__r   rl  rq   r   r   )r   r   )r   ro   rp   __eq__  s    zParserElement.__eq__c             C   s   |  | k S)Nro   )r   r   ro   ro   rp   __ne__  s    zParserElement.__ne__c             C   s   t  t |    S)N)hashid)r   ro   ro   rp   __hash__  s    zParserElement.__hash__c             C   s
   |  | k S)Nro   )r   r   ro   ro   rp   __req__  s    zParserElement.__req__c             C   s   |  | k S)Nro   )r   r   ro   ro   rp   __rne__  s    zParserElement.__rne__)Br   r   r   r   r&  rj  r(  staticmethodr*  r   r   rA  rE  rM  rP  rQ  rS  rU  rW  rX  rZ  r^  r_  rd  rL  r`  re  rf  rg  rl  _MAX_INTrn  rq  rr  r   r   rx  ry  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   rh  r  r  r  r  r  r  r  r  ro   ro   )r   rp   r!     s   
H-2!



D





	c                   s:   e  Z d  Z d Z   f d d   Z   f d d   Z   S)r)   zJAbstract C{ParserElement} subclass, for defining atomic matching patterns.c                s   t  t |   j d d  d  S)Nr?  F)r   r)   r   )r   )r   ro   rp   r     s    zToken.__init__c                s,   t  t |   j |  } d |  j |  _ | S)Nz	Expected )r   r)   rA  r   r9  )r   r   rx   )r   ro   rp   rA    s    zToken.setName)r   r   r   r   r   rA  ro   ro   )r   rp   r)     s   c                   s(   e  Z d  Z d Z   f d d   Z   S)r
   z"An empty token, will always match.c                s2   t  t |   j   d |  _ d |  _ d |  _ d  S)Nr
   TF)r   r
   r   r   r3  r8  )r   )r   ro   rp   r     s    		zEmpty.__init__)r   r   r   r   r   ro   ro   )r   rp   r
     s   c                   s7   e  Z d  Z d Z   f d d   Z d d d  Z   S)r   zA token that will never match.c                s;   t  t |   j   d |  _ d |  _ d |  _ d |  _ d  S)Nr   TFzUnmatchable token)r   r   r   r   r3  r8  r9  )r   )r   ro   rp   r     s
    			zNoMatch.__init__Tc             C   s   t  | | |  j |    d  S)N)r   r9  )r   r  r   rG  ro   ro   rp   rX    s    zNoMatch.parseImpl)r   r   r   r   r   rX  ro   ro   )r   rp   r     s   c                   s7   e  Z d  Z d Z   f d d   Z d d d  Z   S)r   z*Token to exactly match a specified string.c                s   t  t |   j   | |  _ t |  |  _ y | d |  _ Wn1 t k
 ro t j	 d t
 d d t |  _ Yn Xd t |  j  |  _ d |  j |  _ d |  _ d |  _ d  S)Nr   z2null string passed to Literal; use Empty() insteadrs  r  z"%s"z	Expected F)r   r   r   matchr   matchLenfirstMatchCharr   rt  ru  rv  r
   r   rq   r   r9  r3  r8  )r   matchString)r   ro   rp   r   %  s    			zLiteral.__init__Tc             C   sg   | | |  j  k rK |  j d k s7 | j |  j |  rK | |  j |  j f St | | |  j |    d  S)Nru   )r  r  
startswithr  r   r9  )r   r  r   rG  ro   ro   rp   rX  8  s    $zLiteral.parseImpl)r   r   r   r   r   rX  ro   ro   )r   rp   r   #  s   c                   sq   e  Z d  Z d Z e d Z e d   f d d  Z d d d  Z   f d	 d
   Z d d   Z	 e
 e	  Z	   S)r   a  Token to exactly match a specified string as a keyword, that is, it must be
       immediately followed by a non-keyword character.  Compare with C{L{Literal}}::
         Literal("if") will match the leading C{'if'} in C{'ifAndOnlyIf'}.
         Keyword("if") will not; it will only match the leading C{'if'} in C{'if x=1'}, or C{'if(y==2)'}
       Accepts two optional constructor arguments in addition to the keyword string:
       C{identChars} is a string of characters that would be valid identifier characters,
       defaulting to all alphanumerics + "_" and "$"; C{caseless} allows case-insensitive
       matching, default is C{False}.
    z_$Fc                s   t  t |   j   | |  _ t |  |  _ y | d |  _ Wn( t k
 rf t j	 d t
 d d Yn Xd |  j |  _ d |  j |  _ d |  _ d |  _ | |  _ | r | j   |  _ | j   } n  t |  |  _ d  S)Nr   z2null string passed to Keyword; use Empty() insteadrs  r  z"%s"z	Expected F)r   r   r   r  r   r  r  r   rt  ru  rv  r   r9  r3  r8  caselessuppercaselessmatchset
identChars)r   r  r  r  )r   ro   rp   r   L  s"    					zKeyword.__init__Tc             C   se  |  j  r | | | |  j  j   |  j k rI| t |  |  j k sh | | |  j j   |  j k rI| d k s | | d j   |  j k rI| |  j |  j f Sn | | |  j k rI|  j d k s | j |  j |  rI| t |  |  j k s| | |  j |  j k rI| d k s5| | d |  j k rI| |  j |  j f St	 | | |  j
 |    d  S)Nr   ru   )r  r  r  r  r   r  r  r  r  r   r9  )r   r  r   rG  ro   ro   rp   rX  _  s    	&9)$3#zKeyword.parseImplc                s%   t  t |   j   } t j | _ | S)N)r   r   r   DEFAULT_KEYWORD_CHARSr  )r   r   )r   ro   rp   r   m  s    zKeyword.copyc             C   s   |  t  _ d S)z,Overrides the default Keyword chars
        N)r   r  )r'  ro   ro   rp   setDefaultKeywordCharsr  s    zKeyword.setDefaultKeywordChars)r   r   r   r   r1   r  r   rX  r   r  r  ro   ro   )r   rp   r   @  s   	
c                   s7   e  Z d  Z d Z   f d d   Z d d d  Z   S)r   zToken to match a specified string, ignoring case of letters.
       Note: the matched results will always be in the case of the given
       match string, NOT the case of the input text.
    c                sI   t  t |   j | j    | |  _ d |  j |  _ d |  j |  _ d  S)Nz'%s'z	Expected )r   r   r   r  returnStringr   r9  )r   r  )r   ro   rp   r   }  s    	zCaselessLiteral.__init__Tc             C   sV   | | | |  j   j   |  j k r: | |  j  |  j f St | | |  j |    d  S)N)r  r  r  r  r   r9  )r   r  r   rG  ro   ro   rp   rX    s    &zCaselessLiteral.parseImpl)r   r   r   r   r   rX  ro   ro   )r   rp   r   x  s   c                   s7   e  Z d  Z e j   f d d  Z d d d  Z   S)r   c                s#   t  t |   j | | d d d  S)Nr  T)r   r   r   )r   r  r  )r   ro   rp   r     s    zCaselessKeyword.__init__Tc             C   s   | | | |  j   j   |  j k rs | t |  |  j  k s_ | | |  j  j   |  j k rs | |  j  |  j f St | | |  j |    d  S)N)r  r  r  r   r  r  r   r9  )r   r  r   rG  ro   ro   rp   rX    s    &9zCaselessKeyword.parseImpl)r   r   r   r   r  r   rX  ro   ro   )r   rp   r     s   c            	       s[   e  Z d  Z d Z d d d d d d   f d d  Z d d	 d
  Z   f d d   Z   S)r-   a  Token for matching words composed of allowed character sets.
       Defined with string containing all allowed initial characters,
       an optional string containing allowed body characters (if omitted,
       defaults to the initial character set), and an optional minimum,
       maximum, and/or exact length.  The default value for C{min} is 1 (a
       minimum value < 1 is not valid); the default values for C{max} and C{exact}
       are 0, meaning no maximum or exact length restriction. An optional
       C{exclude} parameter can list characters that might be found in 
       the input C{bodyChars} string; useful to define a word of all printables
       except for one or two characters, for instance.
    Nru   r   Fc          	      s~  t  t |   j     ri d j   f d d   | D  } | ri d j   f d d   | D  } qi n  | |  _ t |  |  _ | r | |  _ t |  |  _ n | |  _ t |  |  _ | d k |  _	 | d k  r t
 d   n  | |  _ | d k r| |  _ n	 t |  _ | d k r/| |  _ | |  _ n  t |   |  _ d |  j |  _ d	 |  _ | |  _ d
 |  j |  j k rz| d k rz| d k rz| d k rz|  j |  j k rd t |  j  |  _ ne t |  j  d k rd t j |  j  t |  j  f |  _ n% d t |  j  t |  j  f |  _ |  j rJd |  j d |  _ n  y t j |  j  |  _ Wqzd  |  _ YqzXn  d  S)Nr   c             3   s!   |  ] } |   k r | Vq d  S)Nro   )rr   r   )excludeCharsro   rp   rt     s    z Word.__init__.<locals>.<genexpr>c             3   s!   |  ] } |   k r | Vq d  S)Nro   )rr   r   )r  ro   rp   rt     s    r   ru   zZcannot specify a minimum length < 1; use Optional(Word()) if zero-length word is permittedz	Expected F z[%s]+z%s[%s]*z	[%s][%s]*z\b)r   r-   r   r   initCharsOrigr  	initCharsbodyCharsOrig	bodyCharsmaxSpecifiedr}  minLenmaxLenr  rq   r   r9  r8  	asKeyword_escapeRegexRangeCharsreStringr   r<  escapecompile)r   r  r  minmaxexactr  r  )r   )r  rp   r     sT    "(								:	zWord.__init__Tc       
      C   s  |  j  r[ |  j  j | |  } | s? t | | |  j |    n  | j   } | | j   f S| | |  j k r t | | |  j |    n  | } | d 7} t |  } |  j } | |  j	 } t
 | |  } x* | | k  r | | | k r | d 7} q Wd }	 | | |  j k  rd }	 n  |  j rG| | k  rG| | | k rGd }	 n  |  j r| d k rp| | d | k s| | k  r| | | k rd }	 qn  |	 rt | | |  j |    n  | | | |  f S)Nru   FTr   )r<  r  r   r9  endgroupr  r   r  r  r  r  r  r  )
r   r  r   rG  resultstartrV  Z	bodycharsmaxlocZthrowExceptionro   ro   rp   rX    s6    	
		%		<zWord.parseImplc          
      s   y t  t |   j   SWn Yn X|  j d  k r d d   } |  j |  j k rw d | |  j  | |  j  f |  _ q d | |  j  |  _ n  |  j S)Nc             S   s,   t  |   d k r$ |  d  d  d S|  Sd  S)N   z...)r   )rx   ro   ro   rp   
charsAsStr  s    z Word.__str__.<locals>.charsAsStrz	W:(%s,%s)zW:(%s))r   r-   r   r-  r  r  )r   r  )r   ro   rp   r     s    (zWord.__str__)r   r   r   r   r   rX  r   ro   ro   )r   rp   r-     s   $6#c                   sa   e  Z d  Z d Z e e j d   Z d   f d d  Z d d d  Z	   f d	 d
   Z
   S)r$   zToken for matching strings that match a given regular expression.
       Defined with string specifying the regular expression in a form recognized by the inbuilt Python re module.
    z[A-Z]r   c                s@  t  t |   j   t | t  r t |  d k rM t j d t d d n  | |  _	 | |  _
 y+ t j |  j	 |  j
  |  _ |  j	 |  _ Wqt j k
 r t j d | t d d   YqXnI t | t j  r | |  _ t |  |  _	 |  _ | |  _
 n t d   t |   |  _ d |  j |  _ d |  _ d	 |  _ d
 S)zThe parameters C{pattern} and C{flags} are passed to the C{re.compile()} function as-is. See the Python C{re} module for an explanation of the acceptable patterns and flags.r   z0null string passed to Regex; use Empty() insteadrs  r  z$invalid pattern (%s) passed to RegexzCRegex may only be constructed with a string or a compiled RE objectz	Expected FTN)r   r$   r   rk   r   r   rt  ru  rv  patternflagsr<  r  r  sre_constantserrorcompiledREtyperl   r}  rq   r   r9  r8  r3  )r   r  r  )r   ro   rp   r     s.    					zRegex.__init__Tc             C   s   |  j  j | |  } | s6 t | | |  j |    n  | j   } | j   } t | j    } | r x | D] } | | | | <qm Wn  | | f S)N)r<  r  r   r9  r  	groupdictr   r  )r   r  r   rG  r  dr   r   ro   ro   rp   rX  6  s    zRegex.parseImplc          	      sQ   y t  t |   j   SWn Yn X|  j d  k rJ d t |  j  |  _ n  |  j S)NzRe:(%s))r   r$   r   r-  r   r  )r   )r   ro   rp   r   C  s    zRegex.__str__)r   r   r   r   r   r<  r  r  r   rX  r   ro   ro   )r   rp   r$     s
   "c                   sX   e  Z d  Z d Z d d d d d   f d d  Z d d d  Z   f d	 d
   Z   S)r"   zIToken for matching strings that are delimited by quoting characters.
    NFTc                s1  t  t    j   | j   } t |  d k rS t j d t d d t    n  | d k rh | } n@ | j   } t |  d k r t j d t d d t    n  |   _	 t |    _
 | d   _ |   _ t |    _ |   _ |   _ |   _ | rct j t j B  _ d t j   j	  t   j d  | d k	 rSt |  pVd f   _ nP d   _ d	 t j   j	  t   j d  | d k	 rt |  pd f   _ t   j  d
 k r  j d d j   f d d   t t   j  d
 d d  D  d 7_ n  | r<  j d t j |  7_ n  | rz  j d t j |  7_ t j   j  d   _ n    j d t j   j  7_ y+ t j   j   j    _   j   _ Wn5 t j k
 rt j d   j t d d   Yn Xt      _  d   j    _! d   _" d   _# d S)a  
           Defined with the following parameters:
            - quoteChar - string of one or more characters defining the quote delimiting string
            - escChar - character to escape quotes, typically backslash (default=None)
            - escQuote - special quote sequence to escape an embedded quote string (such as SQL's "" to escape an embedded ") (default=None)
            - multiline - boolean indicating whether quotes can span multiple lines (default=C{False})
            - unquoteResults - boolean indicating whether the matched text should be unquoted (default=C{True})
            - endQuoteChar - string of one or more characters defining the end of the quote delimited string (default=C{None} => same as quoteChar)
        r   z$quoteChar cannot be the empty stringrs  r  Nz'endQuoteChar cannot be the empty stringz%s(?:[^%s%s]r   z%s(?:[^%s\n\r%s]ru   z|(?:z)|(?:c             3   sB   |  ]8 } d  t  j   j d |   t   j |  f Vq d S)z%s[^%s]N)r<  r  endQuoteCharr  )rr   r   )r   ro   rp   rt     s   z(QuotedString.__init__.<locals>.<genexpr>)z|(?:%s)z|(?:%s.)z(.)z)*%sz$invalid pattern (%s) passed to Regexz	Expected FTr   )$r   r"   r   r   r   rt  ru  rv  SyntaxError	quoteCharquoteCharLenfirstQuoteCharr  endQuoteCharLenescCharescQuoteunquoteResultsr<  	MULTILINEDOTALLr  r  r  r  r   r   escCharReplacePatternr  r  r  r  rq   r   r9  r8  r3  )r   r  r  r  Z	multiliner  r  )r   )r   rp   r   R  sd    
						(	%H	zQuotedString.__init__c             C   s   | | |  j  k r( |  j j | |  p+ d  } | sO t | | |  j |    n  | j   } | j   } |  j r | |  j |  j	  } t
 | t  r |  j r t j |  j d |  } n  |  j r | j |  j |  j  } q q n  | | f S)Nz\g<1>)r  r<  r  r   r9  r  r  r  r  r  rk   r   r  r   r  r  r{   r  )r   r  r   rG  r  r   ro   ro   rp   rX    s    .			!zQuotedString.parseImplc          	      sT   y t  t |   j   SWn Yn X|  j d  k rM d |  j |  j f |  _ n  |  j S)Nz.quoted string, starting with %s ending with %s)r   r"   r   r-  r  r  )r   )r   ro   rp   r     s    zQuotedString.__str__)r   r   r   r   r   rX  r   ro   ro   )r   rp   r"   O  s   !Ic                   sR   e  Z d  Z d Z d d d   f d d  Z d d d  Z   f d	 d
   Z   S)r   a  Token for matching words composed of characters *not* in a given set.
       Defined with string containing all disallowed characters, and an optional
       minimum, maximum, and/or exact length.  The default value for C{min} is 1 (a
       minimum value < 1 is not valid); the default values for C{max} and C{exact}
       are 0, meaning no maximum or exact length restriction.
    ru   r   c                s   t  t |   j   d |  _ | |  _ | d k  r@ t d   n  | |  _ | d k ra | |  _ n	 t |  _ | d k r | |  _ | |  _ n  t	 |   |  _
 d |  j
 |  _ |  j d k |  _ d |  _ d  S)NFru   zfcannot specify a minimum length < 1; use Optional(CharsNotIn()) if zero-length char group is permittedr   z	Expected )r   r   r   r0  notCharsr}  r  r  r  rq   r   r9  r3  r8  )r   r  r  r  r  )r   ro   rp   r     s     					zCharsNotIn.__init__Tc             C   s   | | |  j  k r. t | | |  j |    n  | } | d 7} |  j  } t | |  j t |   } x* | | k  r | | | k r | d 7} qf W| | |  j k  r t | | |  j |    n  | | | |  f S)Nru   )r  r   r9  r  r  r   r  )r   r  r   rG  r  Znotcharsmaxlenro   ro   rp   rX    s    
	zCharsNotIn.parseImplc          
      s}   y t  t |   j   SWn Yn X|  j d  k rv t |  j  d k rc d |  j d  d  |  _ qv d |  j |  _ n  |  j S)Nr  z
!W:(%s...)z!W:(%s))r   r   r   r-  r   r  )r   )r   ro   rp   r     s    zCharsNotIn.__str__)r   r   r   r   r   rX  r   ro   ro   )r   rp   r     s   c                   sl   e  Z d  Z d Z i d d 6d d 6d d 6d d	 6d
 d 6Z d d d d   f d d  Z d d d  Z   S)r,   a  Special matching class for matching whitespace.  Normally, whitespace is ignored
       by pyparsing grammars.  This class is included when some whitespace structures
       are significant.  Define with a string containing the whitespace characters to be
       matched; default is C{" \t\r\n"}.  Also takes optional C{min}, C{max}, and C{exact} arguments,
       as defined for the C{L{Word}} class.z<SPC>r  z<TAB>	z<LF>r   z<CR>z<FF>z 	
ru   r   c                s   t  t    j   |   _   j d j   f d d     j D   d j d d     j D    _ d   _ d   j   _	 |   _
 | d k r |   _ n	 t   _ | d k r |   _ |   _
 n  d  S)Nr   c             3   s$   |  ] } |   j  k r | Vq d  S)N)
matchWhite)rr   r   )r   ro   rp   rt     s    z!White.__init__.<locals>.<genexpr>c             s   s   |  ] } t  j | Vq d  S)N)r,   	whiteStrs)rr   r   ro   ro   rp   rt     s    Tz	Expected r   )r   r,   r   r  r  r   r1  r   r3  r9  r  r  r  )r   Zwsr  r  r  )r   )r   rp   r   
  s    	,"				zWhite.__init__Tc             C   s   | | |  j  k r. t | | |  j |    n  | } | d 7} | |  j } t | t |   } x- | | k  r | | |  j  k r | d 7} qc W| | |  j k  r t | | |  j |    n  | | | |  f S)Nru   )r  r   r9  r  r  r   r  )r   r  r   rG  r  r  ro   ro   rp   rX    s    
"zWhite.parseImpl)r   r   r   r   r  r   rX  ro   ro   )r   rp   r,     s   
c                   s"   e  Z d  Z   f d d   Z   S)_PositionTokenc                s8   t  t |   j   |  j j |  _ d |  _ d |  _ d  S)NTF)r   r  r   r   r   r   r3  r8  )r   )r   ro   rp   r   /  s    	z_PositionToken.__init__)r   r   r   r   ro   ro   )r   rp   r  .  s   r  c                   sC   e  Z d  Z d Z   f d d   Z d d   Z d d d  Z   S)	r   zXToken to advance to a specific column of input text; useful for tabular report scraping.c                s    t  t |   j   | |  _ d  S)N)r   r   r   r7   )r   Zcolno)r   ro   rp   r   7  s    zGoToColumn.__init__c             C   s   t  | |  |  j  k r t |  } |  j rB |  j | |  } n  xE | | k  r | | j   r t  | |  |  j  k r | d 7} qE Wn  | S)Nru   )r7   r   r5  rU  isspace)r   r  r   rV  ro   ro   rp   rW  ;  s    	7zGoToColumn.preParseTc             C   sa   t  | |  } | |  j  k r6 t | | d |    n  | |  j  | } | | |  } | | f S)NzText not in expected column)r7   r   )r   r  r   rG  ZthiscolZnewlocr   ro   ro   rp   rX  D  s    zGoToColumn.parseImpl)r   r   r   r   r   rW  rX  ro   ro   )r   rp   r   5  s   	c                   sI   e  Z d  Z d Z   f d d   Z   f d d   Z d d d  Z   S)	r   zQMatches if current position is at the beginning of a line within the parse stringc                s<   t  t |   j   |  j t j j d d   d |  _ d  S)Nr   r   zExpected start of line)r   r   r   r  r!   r&  r{   r9  )r   )r   ro   rp   r   N  s    zLineStart.__init__c                s<   t  t |   j | |  } | | d k r8 | d 7} n  | S)Nr   ru   )r   r   rW  )r   r  r   r[  )r   ro   rp   rW  S  s    zLineStart.preParseTc             C   s]   | d k p5 | |  j  | d  k p5 | | d d k sS t | | |  j |    n  | g  f S)Nr   ru   r   )rW  r   r9  )r   r  r   rG  ro   ro   rp   rX  Y  s
    zLineStart.parseImpl)r   r   r   r   r   rW  rX  ro   ro   )r   rp   r   L  s   c                   s7   e  Z d  Z d Z   f d d   Z d d d  Z   S)r   zKMatches if current position is at the end of a line within the parse stringc                s<   t  t |   j   |  j t j j d d   d |  _ d  S)Nr   r   zExpected end of line)r   r   r   r  r!   r&  r{   r9  )r   )r   ro   rp   r   b  s    zLineEnd.__init__Tc             C   s   | t  |  k  rK | | d k r0 | d d f St | | |  j |    n8 | t  |  k rk | d g  f St | | |  j |    d  S)Nr   ru   )r   r   r9  )r   r  r   rG  ro   ro   rp   rX  g  s    zLineEnd.parseImpl)r   r   r   r   r   rX  ro   ro   )r   rp   r   `  s   c                   s7   e  Z d  Z d Z   f d d   Z d d d  Z   S)r'   zCMatches if current position is at the beginning of the parse stringc                s    t  t |   j   d |  _ d  S)NzExpected start of text)r   r'   r   r9  )r   )r   ro   rp   r   t  s    zStringStart.__init__Tc             C   sL   | d k rB | |  j  | d  k rB t | | |  j |    qB n  | g  f S)Nr   )rW  r   r9  )r   r  r   rG  ro   ro   rp   rX  x  s    zStringStart.parseImpl)r   r   r   r   r   rX  ro   ro   )r   rp   r'   r  s   c                   s7   e  Z d  Z d Z   f d d   Z d d d  Z   S)r&   z=Matches if current position is at the end of the parse stringc                s    t  t |   j   d |  _ d  S)NzExpected end of text)r   r&   r   r9  )r   )r   ro   rp   r     s    zStringEnd.__init__Tc             C   s   | t  |  k  r- t | | |  j |    nT | t  |  k rM | d g  f S| t  |  k ri | g  f St | | |  j |    d  S)Nru   )r   r   r9  )r   r  r   rG  ro   ro   rp   rX    s    
zStringEnd.parseImpl)r   r   r   r   r   rX  ro   ro   )r   rp   r&     s   c                   s:   e  Z d  Z d Z e   f d d  Z d d d  Z   S)r/   aw  Matches if the current position is at the beginning of a Word, and
       is not preceded by any character in a given set of C{wordChars}
       (default=C{printables}). To emulate the C{} behavior of regular expressions,
       use C{WordStart(alphanums)}. C{WordStart} will also match at the beginning of
       the string being parsed, or at the beginning of a line.
    c                s/   t  t |   j   t |  |  _ d |  _ d  S)NzNot at the start of a word)r   r/   r   r  	wordCharsr9  )r   r  )r   ro   rp   r     s    zWordStart.__init__Tc             C   s^   | d k rT | | d |  j  k s6 | | |  j  k rT t | | |  j |    qT n  | g  f S)Nr   ru   )r  r   r9  )r   r  r   rG  ro   ro   rp   rX    s
    zWordStart.parseImpl)r   r   r   r   rU   r   rX  ro   ro   )r   rp   r/     s   c                   s:   e  Z d  Z d Z e   f d d  Z d d d  Z   S)r.   aa  Matches if the current position is at the end of a Word, and
       is not followed by any character in a given set of C{wordChars}
       (default=C{printables}). To emulate the C{} behavior of regular expressions,
       use C{WordEnd(alphanums)}. C{WordEnd} will also match at the end of
       the string being parsed, or at the end of a line.
    c                s8   t  t |   j   t |  |  _ d |  _ d |  _ d  S)NFzNot at the end of a word)r   r.   r   r  r  r0  r9  )r   r  )r   ro   rp   r     s    	zWordEnd.__init__Tc             C   sv   t  |  } | d k rl | | k  rl | | |  j k sN | | d |  j k rl t | | |  j |    ql n  | g  f S)Nr   ru   )r   r  r   r9  )r   r  r   rG  rV  ro   ro   rp   rX    s    zWordEnd.parseImpl)r   r   r   r   rU   r   rX  ro   ro   )r   rp   r.     s   c                   s   e  Z d  Z d Z d   f d d  Z d d   Z d d   Z d	 d
   Z   f d d   Z   f d d   Z	   f d d   Z
 d   f d d  Z g  d d  Z   f d d   Z   S)r   zTAbstract subclass of ParserElement, for combining and post-processing parsed tokens.Fc                s   t  t |   j |  t | t  r4 t |  } n  t | t  rX t |  g |  _ n t | t	 j
  r t d d   | D  r t t |  } n  t |  |  _ n4 y t |  |  _ Wn t k
 r | g |  _ Yn Xd |  _ d  S)Nc             s   s   |  ] } t  | t  Vq d  S)N)rk   r   )rr   r  ro   ro   rp   rt     s    z+ParseExpression.__init__.<locals>.<genexpr>F)r   r   r   rk   r   r   r   r   exprscollectionsSequenceallrN  r   r=  )r   r  r?  )r   ro   rp   r     s    zParseExpression.__init__c             C   s   |  j  | S)N)r  )r   r   ro   ro   rp   r     s    zParseExpression.__getitem__c             C   s   |  j  j |  d  |  _ |  S)N)r  r   r-  )r   r   ro   ro   rp   r     s    	zParseExpression.appendc             C   sD   d |  _  d d   |  j D |  _ x |  j D] } | j   q, W|  S)z~Extends C{leaveWhitespace} defined in base class, and also invokes C{leaveWhitespace} on
           all contained expressions.Fc             S   s   g  |  ] } | j     q Sro   )r   )rr   rT  ro   ro   rp   r     s   	 z3ParseExpression.leaveWhitespace.<locals>.<listcomp>)r0  r  r  )r   rT  ro   ro   rp   r    s
    	zParseExpression.leaveWhitespacec                s   t  | t  rb | |  j k r t t |   j |  x( |  j D] } | j |  j d  q> Wq n> t t |   j |  x% |  j D] } | j |  j d  q W|  S)Nru   r   r   )rk   r(   r5  r   r   r  r  )r   r   rT  )r   ro   rp   r    s    zParseExpression.ignorec          
      s]   y t  t |   j   SWn Yn X|  j d  k rV d |  j j t |  j  f |  _ n  |  j S)Nz%s:(%s))r   r   r   r-  r   r   rq   r  )r   )r   ro   rp   r     s    %zParseExpression.__str__c                s|  t  t |   j   x |  j D] } | j   q Wt |  j  d k rx|  j d } t | |  j  r | j r | j d  k r | j	 r | j d  d   |  j d g |  _ d  |  _
 |  j | j O_ |  j | j O_ n  |  j d } t | |  j  rx| j rx| j d  k rx| j	 rx|  j d  d  | j d  d   |  _ d  |  _
 |  j | j O_ |  j | j O_ qxn  |  S)Nr  r   ru   r   r   )r   r   rh  r  r   rk   r   r+  r.  r6  r-  r3  r8  )r   rT  r   )r   ro   rp   rh    s.    

$	

'	zParseExpression.streamlinec                s   t  t |   j | |  } | S)N)r   r   rE  )r   r   rD  r   )r   ro   rp   rE  	  s    zParseExpression.setResultsNamec             C   sI   | d  d   |  g } x |  j  D] } | j |  q! W|  j g   d  S)N)r  r  r  )r   r  tmprT  ro   ro   rp   r  	  s    zParseExpression.validatec                s2   t  t |   j   } d d   |  j D | _ | S)Nc             S   s   g  |  ] } | j     q Sro   )r   )rr   rT  ro   ro   rp   r    	  s   	 z(ParseExpression.copy.<locals>.<listcomp>)r   r   r   r  )r   r   )r   ro   rp   r   	  s    zParseExpression.copy)r   r   r   r   r   r   r   r  r  r   rh  rE  r  r   ro   ro   )r   rp   r     s   	
 c                   st   e  Z d  Z d Z Gd d   d e  Z d   f d d  Z d d d  Z d	 d
   Z d d   Z	 d d   Z
   S)r   zRequires all given C{ParseExpression}s to be found in the given order.
       Expressions may be separated by whitespace.
       May be constructed using the C{'+'} operator.
    c                   s"   e  Z d  Z   f d d   Z   S)zAnd._ErrorStopc                s3   t  t j |   j | |   d |  _ |  j   d  S)N-)r   r   rw  r   r   r  )r   r   r   )r   ro   rp   r   *	  s    	zAnd._ErrorStop.__init__)r   r   r   r   ro   ro   )r   rp   rw  )	  s   rw  Tc                so   t  t |   j | |  t d d   |  j D  |  _ |  j |  j d j  |  j d j |  _ d |  _	 d  S)Nc             s   s   |  ] } | j  Vq d  S)N)r3  )rr   rT  ro   ro   rp   rt   1	  s    zAnd.__init__.<locals>.<genexpr>r   T)
r   r   r   r  r  r3  r  r1  r0  r=  )r   r  r?  )r   ro   rp   r   /	  s
    zAnd.__init__c       	      C   s\  |  j  d j | | | d d \ } } d } x!|  j  d d   D]} t | t j  rf d } qB n  | ry | j | | |  \ } } Wq/t k
 r   Yq/t k
 r } z d  | _ t |   WYd  d  } ~ Xq/t k
 rt t	 | t
 |  |  j |     Yq/Xn | j | | |  \ } } | sA| j   rB | | 7} qB qB W| | f S)Nr   rH  Fru   T)r  rL  rk   r   rw  r    r   rb  r   r   r   r9  r   )	r   r  r   rG  
resultlistZ	errorStoprT  Z
exprtokensr   ro   ro   rp   rX  6	  s(    (	,zAnd.parseImplc             C   s+   t  | t  r t |  } n  |  j |  S)N)rk   r   r   r   )r   r   ro   ro   rp   r   O	  s    zAnd.__iadd__c             C   sI   | d  d   |  g } x+ |  j  D]  } | j |  | j s! Pq! q! Wd  S)N)r  r  r3  )r   r   subRecCheckListrT  ro   ro   rp   r  T	  s
    	zAnd.checkRecursionc             C   sY   t  |  d  r |  j S|  j d  k rR d d j d d   |  j D  d |  _ n  |  j S)Nr   {r  c             s   s   |  ] } t  |  Vq d  S)N)rq   )rr   rT  ro   ro   rp   rt   `	  s    zAnd.__str__.<locals>.<genexpr>})r   r   r-  r   r  )r   ro   ro   rp   r   [	  s
    -zAnd.__str__)r   r   r   r   r
   rw  r   rX  r   r  r   ro   ro   )r   rp   r   #	  s   c                   s^   e  Z d  Z d Z d   f d d  Z d d d  Z d d	   Z d
 d   Z d d   Z   S)r   zRequires that at least one C{ParseExpression} is found.
       If two expressions match, the expression that matches the longest string will be used.
       May be constructed using the C{'^'} operator.
    Fc                sQ   t  t |   j | |  |  j rD t d d   |  j D  |  _ n	 d |  _ d  S)Nc             s   s   |  ] } | j  Vq d  S)N)r3  )rr   rT  ro   ro   rp   rt   m	  s    zOr.__init__.<locals>.<genexpr>T)r   r   r   r  r  r3  )r   r  r?  )r   ro   rp   r   j	  s    	"zOr.__init__Tc             C   sE  d } d } d  } x |  j  D] } y | j | |  } Wn t k
 r }	 z/ d  |	 _ |	 j | k rw |	 } |	 j } n  WYd  d  }	 ~	 Xq t k
 r t |  | k r t | t |  | j |   } t |  } n  Yq X| | k r | } | }
 q q W| d k  r2| d  k	 r|  q2t | | d |    n  |
 j | | |  S)Nru   r   z no defined alternatives to matchr   r   )	r  r_  r   rb  r   r   r   r9  rL  )r   r  r   rG  	maxExcLocZmaxMatchLocmaxExceptionrT  Zloc2r]  ZmaxMatchExpro   ro   rp   rX  q	  s.    		zOr.parseImplc             C   s.   t  | t  r! t j |  } n  |  j |  S)N)rk   r   r!   r)  r   )r   r   ro   ro   rp   __ixor__	  s    zOr.__ixor__c             C   sY   t  |  d  r |  j S|  j d  k rR d d j d d   |  j D  d |  _ n  |  j S)Nr   r  z ^ c             s   s   |  ] } t  |  Vq d  S)N)rq   )rr   rT  ro   ro   rp   rt   	  s    zOr.__str__.<locals>.<genexpr>r  )r   r   r-  r   r  )r   ro   ro   rp   r   	  s
    -z
Or.__str__c             C   s<   | d  d   |  g } x |  j  D] } | j |  q! Wd  S)N)r  r  )r   r   r  rT  ro   ro   rp   r  	  s    zOr.checkRecursion)	r   r   r   r   r   rX  r  r   r  ro   ro   )r   rp   r   e	  s   	c                   s^   e  Z d  Z d Z d   f d d  Z d d d  Z d d	   Z d
 d   Z d d   Z   S)r   zRequires that at least one C{ParseExpression} is found.
       If two expressions match, the first one listed is the one that will match.
       May be constructed using the C{'|'} operator.
    Fc                sQ   t  t |   j | |  |  j rD t d d   |  j D  |  _ n	 d |  _ d  S)Nc             s   s   |  ] } | j  Vq d  S)N)r3  )rr   rT  ro   ro   rp   rt   	  s    z&MatchFirst.__init__.<locals>.<genexpr>T)r   r   r   r  r  r3  )r   r  r?  )r   ro   rp   r   	  s    	"zMatchFirst.__init__Tc       	      C   s  d } d  } x |  j  D] } y | j | | |  } | SWq t k
 r } z& | j | k ro | } | j } n  WYd  d  } ~ Xq t k
 r t |  | k r t | t |  | j |   } t |  } n  Yq Xq W| d  k	 r |  n t | | d |    d  S)Nru   z no defined alternatives to matchr   )r  rL  r   r   r   r   r9  )	r   r  r   rG  r  r  rT  r   r]  ro   ro   rp   rX  	  s"    	zMatchFirst.parseImplc             C   s.   t  | t  r! t j |  } n  |  j |  S)N)rk   r   r!   r)  r   )r   r   ro   ro   rp   __ior__	  s    zMatchFirst.__ior__c             C   sY   t  |  d  r |  j S|  j d  k rR d d j d d   |  j D  d |  _ n  |  j S)Nr   r  z | c             s   s   |  ] } t  |  Vq d  S)N)rq   )rr   rT  ro   ro   rp   rt   	  s    z%MatchFirst.__str__.<locals>.<genexpr>r  )r   r   r-  r   r  )r   ro   ro   rp   r   	  s
    -zMatchFirst.__str__c             C   s<   | d  d   |  g } x |  j  D] } | j |  q! Wd  S)N)r  r  )r   r   r  rT  ro   ro   rp   r  	  s    zMatchFirst.checkRecursion)	r   r   r   r   r   rX  r  r   r  ro   ro   )r   rp   r   	  s   	c                   sR   e  Z d  Z d Z d   f d d  Z d d d  Z d d   Z d	 d
   Z   S)r	   zRequires all given C{ParseExpression}s to be found, but in any order.
       Expressions may be separated by whitespace.
       May be constructed using the C{'&'} operator.
    Tc                sN   t  t |   j | |  t d d   |  j D  |  _ d |  _ d |  _ d  S)Nc             s   s   |  ] } | j  Vq d  S)N)r3  )rr   rT  ro   ro   rp   rt   	  s    z Each.__init__.<locals>.<genexpr>T)r   r	   r   r  r  r3  r0  initExprGroups)r   r  r?  )r   ro   rp   r   	  s    	zEach.__init__c                s  |  j  r d d   |  j D     f d d   |  j D }   | |  _ d d   |  j D |  _ d d   |  j D |  _ d d   |  j D |  _ |  j |  j 7_ d |  _  n  | } |  j d  d   } |  j d  d    g  } d } x | r|  |  j |  j }	 g  }
 x |	 D] } y | j | |  } Wn t k
 rT|
 j |  YqX| j |  | | k r~| j	 |  q|  k r j	 |  qqWt
 |
  t
 |	  k r d } q q W| rd	 j d
 d   | D  } t | | d |   n  |  f d d   |  j D 7} g  } x6 | D]. } | j | | |  \ } } | j |  q+Wt g   } x | D] } i  } xQ | j   D]C } | | k rt | |  } | t | |  7} | | | <qqW| t |  7} x$ | j   D] \ } } | | | <qWqpW| | f S)Nc             S   s(   g  |  ] } t  | t  r | j  q Sro   )rk   r   r  )rr   rT  ro   ro   rp   r   	  s   	 z"Each.parseImpl.<locals>.<listcomp>c                s+   g  |  ]! } | j  r |   k r |  q Sro   )r3  )rr   rT  )opt1ro   rp   r   	  s   	 c             S   s(   g  |  ] } t  | t  r | j  q Sro   )rk   r0   r  )rr   rT  ro   ro   rp   r   	  s   	 c             S   s(   g  |  ] } t  | t  r | j  q Sro   )rk   r   r  )rr   rT  ro   ro   rp   r   	  s   	 c             S   s.   g  |  ]$ } t  | t t t f  s |  q Sro   )rk   r   r0   r   )rr   rT  ro   ro   rp   r   	  s   	 FTz, c             s   s   |  ] } t  |  Vq d  S)N)rq   )rr   rT  ro   ro   rp   rt   
  s    z!Each.parseImpl.<locals>.<genexpr>z*Missing one or more required elements (%s)c                s4   g  |  ]* } t  | t  r | j   k r |  q Sro   )rk   r   r  )rr   rT  )tmpOptro   rp   r   

  s   	 )r  r  Z	optionalsZmultioptionalsZmultirequiredZrequiredr_  r   r   remover   r   rL  r   r   r   )r   r  r   rG  Zopt2ZtmpLocZtmpReqdZ
matchOrderZkeepMatchingZtmpExprsZfailedrT  Zmissingr  resultsZfinalResultsr  Zdupsr   r  r   ro   )r  r  rp   rX  	  sb    		 zEach.parseImplc             C   sY   t  |  d  r |  j S|  j d  k rR d d j d d   |  j D  d |  _ n  |  j S)Nr   r  z & c             s   s   |  ] } t  |  Vq d  S)N)rq   )rr   rT  ro   ro   rp   rt   #
  s    zEach.__str__.<locals>.<genexpr>r  )r   r   r-  r   r  )r   ro   ro   rp   r   
  s
    -zEach.__str__c             C   s<   | d  d   |  g } x |  j  D] } | j |  q! Wd  S)N)r  r  )r   r   r  rT  ro   ro   rp   r  '
  s    zEach.checkRecursion)r   r   r   r   r   rX  r   r  ro   ro   )r   rp   r	   	  s
   :	c                   s   e  Z d  Z d Z d   f d d  Z d d d  Z d d	   Z   f d
 d   Z   f d d   Z d d   Z	 g  d d  Z
   f d d   Z   S)r   zWAbstract subclass of C{ParserElement}, for combining and post-processing parsed tokens.Fc                s   t  t |   j |  t | t  r4 t |  } n  | |  _ d  |  _ | d  k	 r | j |  _ | j	 |  _	 |  j
 | j  | j |  _ | j |  _ | j |  _ |  j j | j  n  d  S)N)r   r   r   rk   r   r   r  r-  r8  r3  r  r1  r0  r/  r=  r5  r   )r   r  r?  )r   ro   rp   r   /
  s    		zParseElementEnhance.__init__Tc             C   sG   |  j  d  k	 r+ |  j  j | | | d d St d | |  j |    d  S)NrH  Fr   )r  rL  r   r9  )r   r  r   rG  ro   ro   rp   rX  >
  s    zParseElementEnhance.parseImplc             C   s>   d |  _  |  j j   |  _ |  j d  k	 r: |  j j   n  |  S)NF)r0  r  r   r  )r   ro   ro   rp   r  D
  s
    	z#ParseElementEnhance.leaveWhitespacec                s   t  | t  rc | |  j k r t t |   j |  |  j d  k	 r` |  j j |  j d  q` q n? t t |   j |  |  j d  k	 r |  j j |  j d  n  |  S)Nru   r   r   )rk   r(   r5  r   r   r  r  )r   r   )r   ro   rp   r  K
  s     zParseElementEnhance.ignorec                s6   t  t |   j   |  j d  k	 r2 |  j j   n  |  S)N)r   r   rh  r  )r   )r   ro   rp   rh  W
  s    zParseElementEnhance.streamlinec             C   s_   |  | k r" t  | |  g   n  | d  d   |  g } |  j d  k	 r[ |  j j |  n  d  S)N)r#   r  r  )r   r   r  ro   ro   rp   r  ]
  s
    z"ParseElementEnhance.checkRecursionc             C   sJ   | d  d   |  g } |  j  d  k	 r9 |  j  j |  n  |  j g   d  S)N)r  r  r  )r   r  r  ro   ro   rp   r  d
  s    zParseElementEnhance.validatec          
      sl   y t  t |   j   SWn Yn X|  j d  k re |  j d  k	 re d |  j j t |  j  f |  _ n  |  j S)Nz%s:(%s))r   r   r   r-  r  r   r   rq   )r   )r   ro   rp   r   j
  s    %zParseElementEnhance.__str__)r   r   r   r   r   rX  r  r  rh  r  r  r   ro   ro   )r   rp   r   -
  s   c                   s7   e  Z d  Z d Z   f d d   Z d d d  Z   S)r   a  Lookahead matching of the given parse expression.  C{FollowedBy}
    does *not* advance the parsing position within the input string, it only
    verifies that the specified parse expression matches at the current
    position.  C{FollowedBy} always returns a null token list.c                s#   t  t |   j |  d |  _ d  S)NT)r   r   r   r3  )r   r  )r   ro   rp   r   z
  s    zFollowedBy.__init__Tc             C   s   |  j  j | |  | g  f S)N)r  r_  )r   r  r   rG  ro   ro   rp   rX  ~
  s    zFollowedBy.parseImpl)r   r   r   r   r   rX  ro   ro   )r   rp   r   u
  s   c                   sC   e  Z d  Z d Z   f d d   Z d d d  Z d d   Z   S)	r   a  Lookahead to disallow matching with the given parse expression.  C{NotAny}
    does *not* advance the parsing position within the input string, it only
    verifies that the specified parse expression does *not* match at the current
    position.  Also, C{NotAny} does *not* skip over leading whitespace. C{NotAny}
    always returns a null token list.  May be constructed using the '~' operator.c                sB   t  t |   j |  d |  _ d |  _ d t |  j  |  _ d  S)NFTzFound unwanted token, )r   r   r   r0  r3  rq   r  r9  )r   r  )r   ro   rp   r   
  s    		zNotAny.__init__Tc             C   sT   y |  j  j | |  Wn t t f k
 r1 Yn Xt | | |  j |    | g  f S)N)r  r_  r   r   r9  )r   r  r   rG  ro   ro   rp   rX  
  s    zNotAny.parseImplc             C   sI   t  |  d  r |  j S|  j d  k rB d t |  j  d |  _ n  |  j S)Nr   z~{r  )r   r   r-  rq   r  )r   ro   ro   rp   r   
  s
    zNotAny.__str__)r   r   r   r   r   rX  r   ro   ro   )r   rp   r   
  s   	c                   sX   e  Z d  Z d Z   f d d   Z d d d  Z d d   Z d	   f d
 d  Z   S)r0   z<Optional repetition of zero or more of the given expression.c                s#   t  t |   j |  d |  _ d  S)NT)r   r0   r   r3  )r   r  )r   ro   rp   r   
  s    zZeroOrMore.__init__Tc             C   s   g  } y |  j  j | | | d d \ } } t |  j  d k } xa | r` |  j | |  } n | } |  j  j | | |  \ } } | s | j   rE | | 7} qE qE Wn t t f k
 r Yn X| | f S)NrH  Fr   )r  rL  r   r5  rU  r   r   r   )r   r  r   rG  r\  hasIgnoreExprsr[  	tmptokensro   ro   rp   rX  
  s    $zZeroOrMore.parseImplc             C   sI   t  |  d  r |  j S|  j d  k rB d t |  j  d |  _ n  |  j S)Nr   r   z]...)r   r   r-  rq   r  )r   ro   ro   rp   r   
  s
    zZeroOrMore.__str__Fc                s(   t  t |   j | |  } d | _ | S)NT)r   r0   rE  r/  )r   r   rD  r   )r   ro   rp   rE  
  s    	zZeroOrMore.setResultsName)r   r   r   r   r   rX  r   rE  ro   ro   )r   rp   r0   
  s
   	c                   sF   e  Z d  Z d Z d d d  Z d d   Z d   f d d	  Z   S)
r   z2Repetition of one or more of the given expression.Tc             C   s   |  j  j | | | d d \ } } y} t |  j  d k } xa | rZ |  j | |  } n | } |  j  j | | |  \ } } | s | j   r? | | 7} q? q? Wn t t f k
 r Yn X| | f S)NrH  Fr   )r  rL  r   r5  rU  r   r   r   )r   r  r   rG  r\  r  r[  r  ro   ro   rp   rX  
  s    $zOneOrMore.parseImplc             C   sI   t  |  d  r |  j S|  j d  k rB d t |  j  d |  _ n  |  j S)Nr   r  z}...)r   r   r-  rq   r  )r   ro   ro   rp   r   
  s
    zOneOrMore.__str__Fc                s(   t  t |   j | |  } d | _ | S)NT)r   r   rE  r/  )r   r   rD  r   )r   ro   rp   rE  
  s    	zOneOrMore.setResultsName)r   r   r   r   rX  r   rE  ro   ro   )r   rp   r   
  s   	c               @   s.   e  Z d  Z d d   Z e Z d d   Z d S)
_NullTokenc             C   s   d S)NFro   )r   ro   ro   rp   r   
  s    z_NullToken.__bool__c             C   s   d S)Nr   ro   )r   ro   ro   rp   r   
  s    z_NullToken.__str__N)r   r   r   r   r  r   ro   ro   ro   rp   r  
  s   r  c                   sF   e  Z d  Z d Z e   f d d  Z d d d  Z d d   Z   S)	r   zOptional matching of the given expression.
       A default return string can also be specified, if the optional expression
       is not found.
    c                s2   t  t |   j | d d | |  _ d |  _ d  S)Nr?  FT)r   r   r   r   r3  )r   r  r   )r   ro   rp   r   
  s    	zOptional.__init__Tc             C   s   y( |  j  j | | | d d \ } } Wnp t t f k
 r |  j t k	 r |  j  j r t |  j g  } |  j | |  j  j <q |  j g } n g  } Yn X| | f S)NrH  F)r  rL  r   r   r   _optionalNotMatchedr.  r   )r   r  r   rG  r\  ro   ro   rp   rX  
  s    (zOptional.parseImplc             C   sI   t  |  d  r |  j S|  j d  k rB d t |  j  d |  _ n  |  j S)Nr   r   r   )r   r   r-  rq   r  )r   ro   ro   rp   r     s
    zOptional.__str__)r   r   r   r   r  r   rX  r   ro   ro   )r   rp   r   
  s   c                   s@   e  Z d  Z d Z d d d   f d d  Z d d d  Z   S)	r%   a  Token for skipping over all undefined text until the matched expression is found.
       If C{include} is set to true, the matched expression is also parsed (the skipped text
       and matched expression are returned as a 2-element list).  The C{ignore}
       argument is used to define grammars (typically quoted strings and comments) that
       might contain false matches.
    FNc                s   t  t |   j |  | |  _ d |  _ d |  _ | |  _ d |  _ | d  k	 rp t | t	  rp t
 |  |  _ n	 | |  _ d t |  j  |  _ d  S)NTFzNo match found for )r   r%   r   
ignoreExprr3  r8  includeMatchr   rk   r   r   failOnrq   r  r9  )r   r   includer  r  )r   ro   rp   r     s    						zSkipTo.__init__Tc             C   s  | } t  |  } |  j } d } x| | k ryJ|  j r y |  j j | |  Wn t k
 rg Yn& Xd } t | | d t |  j    d } n  |  j d  k	 r x5 y |  j j | |  } Wq t k
 r PYq Xq n  | j | | d d d d | | |  } |  j	 rl| j | | | d d \ } }	 |	 r\t
 |  }
 |
 |	 7}
 | |
 g f S| | g f Sn | | g f SWq$ t t f k
 r| r  n
 | d 7} Yq$ Xq$ Wt | | |  j |    d  S)NFTzFound expression rG  rH  ru   )r   r  r  r_  r   r   rl   r  rL  r  r   r   r9  )r   r  r   rG  startLocrV  r  Z	failParseZskipTextZmatZskipResro   ro   rp   rX  *  sF    				!
zSkipTo.parseImpl)r   r   r   r   r   rX  ro   ro   )r   rp   r%     s   c                   s   e  Z d  Z d Z d   f d d  Z d d   Z d d   Z d	 d
   Z d d   Z g  d d  Z	 d d   Z
   f d d   Z   S)r   a  Forward declaration of an expression to be defined later -
       used for recursive grammars, such as algebraic infix notation.
       When the expression is known, it is assigned to the C{Forward} variable using the '<<' operator.

       Note: take care when assigning to C{Forward} not to overlook precedence of operators.
       Specifically, '|' has a lower precedence than '<<', so that::
          fwdExpr << a | b | c
       will actually be evaluated as::
          (fwdExpr << a) | b | c
       thereby leaving b and c out as parseable alternatives.  It is recommended that you
       explicitly group the values inserted into the C{Forward}::
          fwdExpr << (a | b | c)
       Converting to use the '<<=' operator instead will avoid this problem.
    Nc                s    t  t |   j | d d d  S)Nr?  F)r   r   r   )r   r   )r   ro   rp   r   c  s    zForward.__init__c             C   s   t  | t  r! t j |  } n  | |  _ | j |  _ d  |  _ |  j j |  _ |  j j |  _ |  j |  j j	  |  j j
 |  _
 |  j j |  _ |  j j |  j j  |  S)N)rk   r   r!   r)  r  r3  r-  r8  r  r1  r0  r/  r5  r   )r   r   ro   ro   rp   
__lshift__f  s    		zForward.__lshift__c             C   s   |  | >S)Nro   )r   r   ro   ro   rp   __ilshift__t  s    zForward.__ilshift__c             C   s   d |  _  |  S)NF)r0  )r   ro   ro   rp   r  w  s    	zForward.leaveWhitespacec             C   s8   |  j  s4 d |  _  |  j d  k	 r4 |  j j   q4 n  |  S)NT)r7  r  rh  )r   ro   ro   rp   rh  {  s
    		zForward.streamlinec             C   sY   |  | k rH | d  d   |  g } |  j  d  k	 rH |  j  j |  qH n  |  j g   d  S)N)r  r  r  )r   r  r  ro   ro   rp   r    s
    zForward.validatec             C   sx   t  |  d  r |  j S|  j |  _ t |  _ z+ |  j d  k	 rO t |  j  } n d } Wd  |  j |  _ X|  j j d | S)Nr   Nonez: )r   r   r   Z_revertClass_ForwardNoRecurser  rq   r   )r   Z	retStringro   ro   rp   r     s    	
zForward.__str__c                s=   |  j  d  k	 r" t t |   j   St   } | |  K} | Sd  S)N)r  r   r   r   )r   r   )r   ro   rp   r     s
    	
zForward.copy)r   r   r   r   r   r  r  r  rh  r  r   r   ro   ro   )r   rp   r   T  s   c               @   s   e  Z d  Z d d   Z d S)r  c             C   s   d S)Nz...ro   )r   ro   ro   rp   r     s    z_ForwardNoRecurse.__str__N)r   r   r   r   ro   ro   ro   rp   r    s   r  c                   s+   e  Z d  Z d Z d   f d d  Z   S)r*   zGAbstract subclass of C{ParseExpression}, for converting parsed results.Fc                s#   t  t |   j |  d |  _ d  S)NF)r   r*   r   r/  )r   r  r?  )r   ro   rp   r     s    zTokenConverter.__init__)r   r   r   r   r   ro   ro   )r   rp   r*     s   c                   s4   e  Z d  Z d Z   f d d   Z d d   Z   S)r+   z,Converter to upper case all matching tokens.c                s0   t  t |   j |   t j d t d d d  S)NzAUpcase class is deprecated, use upcaseTokens parse action insteadrs  r  )r   r+   r   rt  ru  DeprecationWarning)r   r   )r   ro   rp   r     s    	zUpcase.__init__c             C   s   t  t t j |   S)N)r   rN  rl   r  )r   r  r   rY  ro   ro   rp   rZ    s    zUpcase.postParse)r   r   r   r   r   rZ  ro   ro   )r   rp   r+     s   c                   sL   e  Z d  Z d Z d d   f d d  Z   f d d   Z d d	   Z   S)
r   zConverter to concatenate all matching tokens to a single string.
       By default, the matching patterns must also be contiguous in the input string;
       this can be disabled by specifying C{'adjacent=False'} in the constructor.
    r   Tc                sQ   t  t |   j |  | r) |  j   n  | |  _ d |  _ | |  _ d |  _ d  S)NT)r   r   r   r  adjacentr0  
joinStringr=  )r   r  r  r  )r   ro   rp   r     s    			zCombine.__init__c                s6   |  j  r t j |  |  n t t |   j |  |  S)N)r  r!   r  r   r   )r   r   )r   ro   rp   r    s    	zCombine.ignorec             C   sn   | j    } | d  d   =| t d j | j |  j   g d |  j 7} |  j rf | j   rf | g S| Sd  S)Nr   r   )r   r   r   r   r  r:  r.  r   )r   r  r   rY  ZretToksro   ro   rp   rZ    s    1zCombine.postParse)r   r   r   r   r   r  rZ  ro   ro   )r   rp   r     s   
c                   s4   e  Z d  Z d Z   f d d   Z d d   Z   S)r   zConverter to return the matched tokens as a list - useful for returning tokens of C{L{ZeroOrMore}} and C{L{OneOrMore}} expressions.c                s#   t  t |   j |  d |  _ d  S)NT)r   r   r   r/  )r   r  )r   ro   rp   r     s    zGroup.__init__c             C   s   | g S)Nro   )r   r  r   rY  ro   ro   rp   rZ    s    zGroup.postParse)r   r   r   r   r   rZ  ro   ro   )r   rp   r     s   c                   s4   e  Z d  Z d Z   f d d   Z d d   Z   S)r   a  Converter to return a repetitive expression as a list, but also as a dictionary.
       Each element can also be referenced using the first token in the expression as its key.
       Useful for tabular report scraping when the first column can be used as a item key.
    c                s#   t  t |   j |  d |  _ d  S)NT)r   r   r   r/  )r   r  )r   ro   rp   r     s    zDict.__init__c             C   sT  x9t  |  D]+\ } } t |  d k r1 q n  | d } t | t  rc t | d  j   } n  t |  d k r t d |  | | <q t |  d k r t | d t  r t | d |  | | <q | j   } | d =t |  d k st | t  r!| j	   r!t | |  | | <q t | d |  | | <q W|  j
 rL| g S| Sd  S)Nr   ru   r   r  )r   r   rk   r   rq   r   r   r   r   r   r.  )r   r  r   rY  r   tokZikeyZ	dictvaluero   ro   rp   rZ    s$    
&-	zDict.postParse)r   r   r   r   r   rZ  ro   ro   )r   rp   r     s   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)r(   z:Converter for ignoring the results of a parsed expression.c             C   s   g  S)Nro   )r   r  r   rY  ro   ro   rp   rZ    s    zSuppress.postParsec             C   s   |  S)Nro   )r   ro   ro   rp   r    s    zSuppress.suppressN)r   r   r   r   rZ  r  ro   ro   ro   rp   r(      s   c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	r   z?Wrapper for parse actions, to ensure they are only called once.c             C   s   t  |  |  _ d |  _ d  S)NF)r%  callablecalled)r   Z
methodCallro   ro   rp   r     s    zOnlyOnce.__init__c             C   sA   |  j  s+ |  j | | |  } d |  _  | St | | d   d  S)NTr   )r
  r	  r   )r   rx   r  r  r  ro   ro   rp   r    s
    		zOnlyOnce.__call__c             C   s   d |  _  d  S)NF)r
  )r   ro   ro   rp   reset  s    zOnlyOnce.resetN)r   r   r   r   r   r  r  ro   ro   ro   rp   r   	  s   c                sG   t         f d d   } y   j | _ Wn t k
 rB Yn X| S)z&Decorator for debugging parse actions.c                 s     j  } |  d d   \ } } } t |   d k rO |  d j j d | } n  t j j d | t | |  | | f  y   |    } WnB t k
 r } z" t j j d | | f    WYd  d  } ~ Xn Xt j j d | | f  | S)N   r   .z">>entering %s(line: '%s', %d, %s)
z<<leaving %s (exception: %s)
z<<leaving %s (ret: %s)
)	Z	func_namer   r   r   sysstderrwriterF   ra  )ZpaArgsZthisFuncrx   r  r  r   r  )r  ro   rp   z  s    	)ztraceParseAction.<locals>.z)r%  r   r   )r  r  ro   )r  rp   ra     s    ,Fc             C   sx   t  |   d t  |  d t  |   d } | rS t |  t | |    j |  S|  t t |  |   j |  Sd S)a  Helper to define a delimited list of expressions - the delimiter defaults to ','.
       By default, the list elements and delimiters can have intervening whitespace, and
       comments, but this can be overridden by passing C{combine=True} in the constructor.
       If C{combine} is set to C{True}, the matching tokens are returned as a single token
       string, with the delimiters included; otherwise, the matching tokens are returned
       as a list of tokens, with the delimiters suppressed.
    z [r  z]...N)rq   r   r0   rA  r(   )r  ZdelimcombineZdlNamero   ro   rp   r>   0  s    ,!c                s|   t         f d d   } | d k rH t t  j d d    } n | j   } | j d  | j | d d |   S)	aC  Helper to define a counted list of expressions.
       This helper defines a pattern of the form::
           integer expr expr expr...
       where the leading integer tells how many expr expressions follow.
       The matched tokens returns the array of expr tokens as a list - the leading count token is suppressed.
    c                s;   | d }   | r, t  t  g |   p5 t  t  >g  S)Nr   )r   r   rA   )rx   r  r  rz  )	arrayExprr  ro   rp   countFieldParseActionF  s    
-z+countedArray.<locals>.countFieldParseActionNc             S   s   t  |  d  S)Nr   )r   )r  ro   ro   rp   r   K  s    zcountedArray.<locals>.<lambda>ZarrayLenr>  T)r   r-   rQ   rP  r   rA  rQ  )r  ZintExprr  ro   )r  r  rp   r:   >  s    	c             C   sM   g  } x@ |  D]8 } t  | t  r8 | j t |   q | j |  q W| S)N)rk   r   r   rp  r   )Lr   r   ro   ro   rp   rp  R  s    rp  c                s2   t        f d d   } |  j | d d   S)a?  Helper to define an expression that is indirectly defined from
       the tokens matched in a previous expression, that is, it looks
       for a 'repeat' of a previous expression.  For example::
           first = Word(nums)
           second = matchPreviousLiteral(first)
           matchExpr = first + ":" + second
       will match C{"1:1"}, but not C{"1:2"}.  Because this matches a
       previous literal, will also match the leading C{"1:1"} in C{"1:10"}.
       If this is not desired, use C{matchPreviousExpr}.
       Do *not* use with packrat parsing enabled.
    c                sf   | rW t  |  d k r'   | d >qb t | j    }   t d d   | D  >n   t   >d  S)Nru   r   c             S   s   g  |  ] } t  |   q Sro   )r   )rr   ttro   ro   rp   r   o  s   	 zEmatchPreviousLiteral.<locals>.copyTokenToRepeater.<locals>.<listcomp>)r   rp  r   r   r
   )rx   r  r  Ztflat)repro   rp   copyTokenToRepeaterh  s    z1matchPreviousLiteral.<locals>.copyTokenToRepeaterr>  T)r   rQ  )r  r  ro   )r  rp   rN   [  s    	
c                sH   t      |  j   }   | K    f d d   } |  j | d d   S)aj  Helper to define an expression that is indirectly defined from
       the tokens matched in a previous expression, that is, it looks
       for a 'repeat' of a previous expression.  For example::
           first = Word(nums)
           second = matchPreviousExpr(first)
           matchExpr = first + ":" + second
       will match C{"1:1"}, but not C{"1:2"}.  Because this matches by
       expressions, will *not* match the leading C{"1:1"} in C{"1:10"};
       the expressions are evaluated first, and then compared, so
       C{"1"} is compared with C{"10"}.
       Do *not* use with packrat parsing enabled.
    c                s;   t  | j        f d d   }  j | d d d  S)Nc                s7   t  | j    } |   k r3 t d d d   n  d  S)Nr   r   )rp  r   r   )rx   r  r  ZtheseTokens)matchTokensro   rp   mustMatchTheseTokens  s    zLmatchPreviousExpr.<locals>.copyTokenToRepeater.<locals>.mustMatchTheseTokensr>  T)rp  r   rP  )rx   r  r  r  )r  )r  rp   r    s    z.matchPreviousExpr.<locals>.copyTokenToRepeaterr>  T)r   r   rQ  )r  Ze2r  ro   )r  rp   rM   u  s    	
c             C   sU   x$ d D] } |  j  | t |  }  q W|  j  d d  }  |  j  d d  }  t |   S)Nz\^-]r   z\nr  z\t)r{   _bslashrq   )rx   r   ro   ro   rp   r    s
    r  Tc       
         sH  | r' d d   } d d   } t    n d d   } d d   } t   g  } t |  t  ri |  j   } n_ t |  t j  r t |  d d   } n4 t |  t  r t |   } n t	 j
 d t d d	 | s t   Sd
 } x | t |  d k  r| | } x t | | d d   D]f \ } }	 | |	 |  rG| | | d =Pq| | |	  r| | | d =| j | |	  |	 } PqqW| d 7} q W| r+| r+yi t |  t d j |   k rt d d j d d   | D   St d j d d   | D   SWq+t	 j
 d t d d	 Yq+Xn  t   f d d   | D  S)ar  Helper to quickly define a set of alternative Literals, and makes sure to do
       longest-first testing when there is a conflict, regardless of the input order,
       but returns a C{L{MatchFirst}} for best performance.

       Parameters:
        - strs - a string of space-delimited literals, or a list of string literals
        - caseless - (default=False) - treat all literals as caseless
        - useRegex - (default=True) - as an optimization, will generate a Regex
          object; otherwise, will generate a C{MatchFirst} object (if C{caseless=True}, or
          if creating a C{Regex} raises an exception)
    c             S   s   |  j    | j    k S)N)r  )r   bro   ro   rp   r     s    zoneOf.<locals>.<lambda>c             S   s   | j    j |  j     S)N)r  r  )r   r  ro   ro   rp   r     s    c             S   s
   |  | k S)Nro   )r   r  ro   ro   rp   r     s    c             S   s   | j  |   S)N)r  )r   r  ro   ro   rp   r     s    Nz2Invalid argument to oneOf, expected string or listrs  r  r   ru   r   z[%s]c             s   s   |  ] } t  |  Vq d  S)N)r  )rr   symro   ro   rp   rt     s    zoneOf.<locals>.<genexpr>|c             s   s   |  ] } t  j |  Vq d  S)N)r<  r  )rr   r  ro   ro   rp   rt     s    z7Exception creating Regex for oneOf, building MatchFirstc                s   g  |  ] }   |   q Sro   ro   )rr   r  )parseElementClassro   rp   r     s   	 zoneOf.<locals>.<listcomp>)r   r   rk   r   ry   r  r  r   r   rt  ru  rv  r   r   r   r   r   r$   r   )
Zstrsr  ZuseRegexZisequalZmasksZsymbolsr   Zcurr   r   ro   )r!  rp   rR     sP    		
'!$$	c             C   s   t  t t |  |    S)a  Helper to easily and clearly define a dictionary by specifying the respective patterns
       for the key and value.  Takes care of defining the C{L{Dict}}, C{L{ZeroOrMore}}, and C{L{Group}} tokens
       in the proper order.  The key pattern can include delimiting markers or punctuation,
       as long as they are suppressed, thereby leaving the significant key text.  The value
       pattern can include named results, so that the C{Dict} results can include named token
       fields.
    )r   r0   r   )r   r   ro   ro   rp   r?     s    c             C   sy   t    j d d    } | j   } d | _ | d  |  | d  } | r\ d d   } n d d   } | j |  | S)	a  Helper to return the original, untokenized text for a given expression.  Useful to
       restore the parsed fields of an HTML start tag into the raw tag text itself, or to
       revert separate tokens with intervening whitespace back to the original matching
       input text. Simpler to use than the parse action C{L{keepOriginalText}}, and does not
       require the inspect module to chase up the call stack.  By default, returns a 
       string containing the original parsed text.  
       
       If the optional C{asString} argument is passed as C{False}, then the return value is a 
       C{L{ParseResults}} containing any results names that were originally matched, and a 
       single token containing the original matched text from the input string.  So if 
       the expression passed to C{L{originalTextFor}} contains expressions with defined
       results names, you must set C{asString} to C{False} if you want to preserve those
       results name values.c             S   s   | S)Nro   )rx   r   r  ro   ro   rp   r     s    z!originalTextFor.<locals>.<lambda>F_original_start_original_endc             S   s   |  | j  | j  S)N)r"  r#  )rx   r  r  ro   ro   rp   r     s    c             S   s?   | d  d   =| j  d |  | j | j   | d =| d =d  S)Nr   r"  r#  )r   r"  r#  )rx   r  r  ro   ro   rp   extractText  s     z$originalTextFor.<locals>.extractText)r
   rP  r   r=  )r  ZasStringZ	locMarkerZendlocMarker	matchExprr$  ro   ro   rp   rf     s    	c             C   s   t  |   j d d    S)ziHelper to undo pyparsing's default grouping of And expressions, even
       if all but one are non-empty.c             S   s   |  d S)Nr   ro   )r  ro   ro   rp   r     s    zungroup.<locals>.<lambda>)r*   rP  )r  ro   ro   rp   rg     s    c             C   sH   t    j d d    } t | d  |  d  | j   j   d   S)a  Helper to decorate a returned token with its starting and ending locations in the input string.
       This helper adds the following results names:
        - locn_start = location where matched expression begins
        - locn_end = location where matched expression ends
        - value = the actual parsed results

       Be careful if the input text contains C{<TAB>} characters, you may want to call
       C{L{ParserElement.parseWithTabs}}
    c             S   s   | S)Nro   )rx   r  r  ro   ro   rp   r     s    zlocatedExpr.<locals>.<lambda>Z
locn_startr   Zlocn_end)r
   rP  r   r   r  )r  Zlocatorro   ro   rp   ri     s    
z\[]-*.$+^?()~ r  c             C   s   | d d S)Nr   ru   ro   )rx   r  r  ro   ro   rp   r     s    r   z\\0?[xX][0-9a-fA-F]+c             C   s    t  t | d j d  d   S)Nr   z\0x   )unichrr   lstrip)rx   r  r  ro   ro   rp   r     s    z	\\0[0-7]+c             C   s!   t  t | d d d   d   S)Nr   ru      )r'  r   )rx   r  r  ro   ro   rp   r     s    r  z\]r  r   ^negatebodyr   c          
      sO   d d     y0 d j    f d d   t j |   j D  SWn d SYn Xd S)a  Helper to easily define string ranges for use in Word construction.  Borrows
       syntax from regexp '[]' string range definitions::
          srange("[0-9]")   -> "0123456789"
          srange("[a-z]")   -> "abcdefghijklmnopqrstuvwxyz"
          srange("[a-z$_]") -> "abcdefghijklmnopqrstuvwxyz$_"
       The input string must be enclosed in []'s, and the returned string is the expanded
       character set joined into a single string.
       The values enclosed in the []'s may be::
          a single character
          an escaped character with a leading backslash (such as \- or \])
          an escaped hex character with a leading '\x' (\x21, which is a '!' character) 
            (\0x## is also supported for backwards compatibility) 
          an escaped octal character with a leading '\0' (\041, which is a '!' character)
          a range of any of the above, separated by a dash ('a-z', etc.)
          any combination of the above ('aeiouy', 'a-zA-Z0-9_$', etc.)
    c             S   sN   t  |  t  s |  Sd j d d   t t |  d  t |  d  d  D  S)Nr   c             s   s   |  ] } t  |  Vq d  S)N)r'  )rr   r   ro   ro   rp   rt   0  s    z+srange.<locals>.<lambda>.<locals>.<genexpr>r   ru   )rk   r   r   r   ord)pro   ro   rp   r   0  s    zsrange.<locals>.<lambda>r   c             3   s   |  ] }   |  Vq d  S)Nro   )rr   part)	_expandedro   rp   rt   2  s    zsrange.<locals>.<genexpr>N)r   _reBracketExprrl  r,  )rx   ro   )r0  rp   r^     s
    0c                s     f d d   } | S)zrHelper method for defining parse actions that require matching at a specific
       column in the input text.
    c                s2   t  | |     k r. t |  | d     n  d  S)Nzmatched token not at column %d)r7   r   )r  Zlocnr  )rz  ro   rp   	verifyCol:  s    z!matchOnlyAtCol.<locals>.verifyColro   )rz  r2  ro   )rz  rp   rL   6  s    c                s     f d d   } | S)zHelper method for common parse actions that simply return a literal value.  Especially
       useful when used with C{L{transformString<ParserElement.transformString>}()}.
    c                 s     g S)Nro   )r   )replStrro   rp   	_replFuncC  s    zreplaceWith.<locals>._replFuncro   )r3  r4  ro   )r3  rp   r[   ?  s    c             C   s   | d d d  S)zHelper parse action for removing quotation marks from parsed quoted strings.
       To use, add this parse action to quoted string using::
         quotedString.setParseAction( removeQuotes )
    r   ru   r   ro   )rx   r  r  ro   ro   rp   rY   G  s    c             C   s   d d   t  t |  D S)z4Helper parse action to convert tokens to upper case.c             S   s   g  |  ] } | j     q Sro   )r  )rr   r  ro   ro   rp   r   P  s   	 z upcaseTokens.<locals>.<listcomp>)rN  rq   )rx   r  r  ro   ro   rp   rc   N  s    c             C   s   d d   t  t |  D S)z4Helper parse action to convert tokens to lower case.c             S   s   g  |  ] } | j     q Sro   )lower)rr   r  ro   ro   rp   r   T  s   	 z"downcaseTokens.<locals>.<listcomp>)rN  rq   )rx   r  r  ro   ro   rp   r@   R  s    c             C   sY   y t    } Wn t k
 r- t d   Yn X| d d  =| t |  | |   7} | S)zDEPRECATED - use new helper method C{L{originalTextFor}}.
       Helper parse action to preserve original parsed text,
       overriding any nested parse actions.zJincorrect usage of keepOriginalText - may only be called as a parse actionN)getTokensEndLocr   r   r   )rx   r   r  r  ro   ro   rp   rE   V  s    c           	   C   ss   d d l  }  |  j   } zP xI | d d  D]+ } | d d k r, | d j d } | Sq, Wt d   Wd ~ Xd S)ziMethod to be called from within a parse action to determine the end
       location of the parsed tokens.r   Nr  r  r^  r   zRincorrect usage of getTokensEndLoc - may only be called from within a parse action)inspectstackf_localsr   )r7  Zfstackr  r  ro   ro   rp   r6  b  s    r6  c             C   sE  t  |  t  r+ |  } t |  d | }  n	 |  j } t t t d  } | r t j   j	 t
  } t d  |  d  t t t | t d  |    t d d d g j d	  j	 d
 d    t d  } n d j d d   t D  } t j   j	 t
  t |  B} t d  |  d  t t t | j	 t  t t d  |     t d d d g j d	  j	 d d    t d  } t t d  |  d  } | j d d j | j d d  j   j     j d |   } | j d d j | j d d  j   j     j d |   } | | _ | | _ | | f S)zRInternal helper to construct opening and closing tag expressions, given a tag namer  z_-:r   tag=/r   FrA   c             S   s   | d d k S)Nr   r<  ro   )rx   r  r  ro   ro   rp   r     s    z_makeTags.<locals>.<lambda>r   r   c             s   s!   |  ] } | d  k r | Vq d S)r   Nro   )rr   r   ro   ro   rp   rt     s    z_makeTags.<locals>.<genexpr>c             S   s   | d d k S)Nr   r<  ro   )rx   r  r  ro   ro   rp   r     s    z</r  :r  z<%s>r  z</%s>)rk   r   r   r   r-   r2   r1   r<   r   rP  rY   r(   r   r0   r   r   rE  r   rU   rX   r@   r   _Lr{   titlery   rA  r:  )tagStrZxmlZresnameZtagAttrNameZtagAttrValueZopenTagZprintablesLessRAbrackZcloseTagro   ro   rp   	_makeTagsr  s"    	r~AA		rA  c             C   s   t  |  d  S)zRHelper to construct opening and closing tag expressions for HTML, given a tag nameF)rA  )r@  ro   ro   rp   rJ     s    c             C   s   t  |  d  S)zQHelper to construct opening and closing tag expressions for XML, given a tag nameT)rA  )r@  ro   ro   rp   rK     s    c                 sN   |  r |  d d    n | j      d d     D     f d d   } | S)a  Helper to create a validating parse action to be used with start tags created
       with C{L{makeXMLTags}} or C{L{makeHTMLTags}}. Use C{withAttribute} to qualify a starting tag
       with a required attribute value, to avoid false matches on common tags such as
       C{<TD>} or C{<DIV>}.

       Call C{withAttribute} with a series of attribute names and values. Specify the list
       of filter attributes names and values as:
        - keyword arguments, as in C{(align="right")}, or
        - as an explicit dict with C{**} operator, when an attribute name is also a Python
          reserved word, as in C{**{"class":"Customer", "align":"right"}}
        - a list of name-value tuples, as in ( ("ns1:class", "Customer"), ("ns2:align","right") )
       For attribute names with a namespace prefix, you must use the second form.  Attribute
       names are matched insensitive to upper/lower case.

       To verify that the attribute exists, but without specifying a value, pass
       C{withAttribute.ANY_VALUE} as the value.
       Nc             S   s"   g  |  ] \ } } | | f  q Sro   ro   )rr   r   r   ro   ro   rp   r     s   	 z!withAttribute.<locals>.<listcomp>c                s   x~   D]v \ } } | | k r8 t  |  | d |   n  | t j k r | | | k r t  |  | d | | | | f   q q Wd  S)Nzno matching attribute z+attribute '%s' has value '%s', must be '%s')r   rd   	ANY_VALUE)rx   r  r\  ZattrNameZ	attrValue)attrsro   rp   pa  s    zwithAttribute.<locals>.pa)r   )r   ZattrDictrD  ro   )rC  rp   rd     s    (r  c             C   s  t    } |  | | | B} xt |  D]\ } } | d	 d d  \ } }	 }
 } |	 d k r | d k s~ t |  d k r t d   n  | \ } } n  t    } |
 t j k r|	 d k r t | |  t | t |   } q|	 d k rU| d k	 r.t | | |  t | t | |   } qt | |  t | t |   } q|	 d k rt | | | | |  t | | | | |  } qt d   n+|
 t j	 k r|	 d k rt
 | t  st |  } n  t | j |  t | |  } q|	 d k rt| d k	 rMt | | |  t | t | |   } qt | |  t | t |   } q|	 d k rt | | | | |  t | | | | |  } qt d   n t d   | r| j |  n  | | | BK} | } q( W| | K} | S)
a  Helper method for constructing grammars of expressions made up of
       operators working in a precedence hierarchy.  Operators may be unary or
       binary, left- or right-associative.  Parse actions can also be attached
       to operator expressions.

       Parameters:
        - baseExpr - expression representing the most basic element for the nested
        - opList - list of tuples, one for each operator precedence level in the
          expression grammar; each tuple is of the form
          (opExpr, numTerms, rightLeftAssoc, parseAction), where:
           - opExpr is the pyparsing expression for the operator;
              may also be a string, which will be converted to a Literal;
              if numTerms is 3, opExpr is a tuple of two expressions, for the
              two operators separating the 3 terms
           - numTerms is the number of terms for this operator (must
              be 1, 2, or 3)
           - rightLeftAssoc is the indicator whether the operator is
              right or left associative, using the pyparsing-defined
              constants C{opAssoc.RIGHT} and C{opAssoc.LEFT}.
           - parseAction is the parse action to be associated with
              expressions matching this operator expression (the
              parse action tuple member may be omitted)
        - lpar - expression for matching left-parentheses (default=Suppress('('))
        - rpar - expression for matching right-parentheses (default=Suppress(')'))
    Nr  r  r  z@if numterms=3, opExpr must be a tuple or list of two expressionsru   z6operator must be unary (1), binary (2), or ternary (3)z2operator must indicate right or left associativity)N)r   r   r   r}  rS   LEFTr   r   r   RIGHTrk   r   r  rP  )ZbaseExprZopListZlparZrparr   ZlastExprr   ZoperDefZopExprZarityZrightLeftAssocrD  ZopExpr1ZopExpr2ZthisExprr%  ro   ro   rp   rh     sP    	 	'/' $/' 

z4"(?:[^"\n\r\\]|(?:"")|(?:\\x[0-9a-fA-F]+)|(?:\\.))*"z string enclosed in double quotesz4'(?:[^'\n\r\\]|(?:'')|(?:\\x[0-9a-fA-F]+)|(?:\\.))*'z string enclosed in single quoteszq(?:"(?:[^"\n\r\\]|(?:"")|(?:\\x[0-9a-fA-F]+)|(?:\\.))*")|(?:'(?:[^'\n\r\\]|(?:'')|(?:\\x[0-9a-fA-F]+)|(?:\\.))*')z*quotedString using single or double quotesuc             C   s  |  | k r t  d   n  | d k rt |  t  rt | t  rt |   d k r t |  d k r | d k	 r t t | t |  | t j d d   j	 d d    } qt
 j   t |  | t j  j	 d d    } q| d k	 rBt t | t |   t |  t t j d d   j	 d d    } qt t t |   t |  t t j d d   j	 d	 d    } qt  d
   n  t   } | d k	 r| t t |   t | | B| B t |   K} n. | t t |   t | | B t |   K} | S)a  Helper method for defining nested lists enclosed in opening and closing
       delimiters ("(" and ")" are the default).

       Parameters:
        - opener - opening character for a nested list (default="("); can also be a pyparsing expression
        - closer - closing character for a nested list (default=")"); can also be a pyparsing expression
        - content - expression for items within the nested lists (default=None)
        - ignoreExpr - expression for ignoring opening and closing delimiters (default=quotedString)

       If an expression is not provided for the content argument, the nested
       expression will capture all whitespace-delimited content between delimiters
       as a list of separate values.

       Use the C{ignoreExpr} argument to define expressions that may contain
       opening or closing characters that should not be treated as opening
       or closing characters for nesting, such as quotedString or a comment
       expression.  Specify multiple expressions using an C{L{Or}} or C{L{MatchFirst}}.
       The default is L{quotedString}, but if no expressions are to be ignored,
       then pass C{None} for this argument.
    z.opening and closing strings cannot be the sameNru   r  c             S   s   |  d j    S)Nr   )r   )r  ro   ro   rp   r   (  s    znestedExpr.<locals>.<lambda>c             S   s   |  d j    S)Nr   )r   )r  ro   ro   rp   r   +  s    c             S   s   |  d j    S)Nr   )r   )r  ro   ro   rp   r   1  s    c             S   s   |  d j    S)Nr   )r   )r  ro   ro   rp   r   5  s    zOopening and closing arguments must be strings if no content expression is given)r}  rk   r   r   r   r   r   r!   r&  rP  rA   r   r   r   r   r(   r0   )ZopenerZcloserZcontentr  r   ro   ro   rp   rO     s$    $@	*NI	5.c                s    f d d   }   f d d   }   f d d   } t  t   j d  j    } t   t   j |  } t   j |  } t   j |  }	 | r t t |  | t  | t |   t |   |	  }
 n0 t t |  t  | t |   t |    }
 |  j t	 t    |
 S)a  Helper method for defining space-delimited indentation blocks, such as
       those used to define block statements in Python source code.

       Parameters:
        - blockStatementExpr - expression defining syntax of statement that
            is repeated within the indented block
        - indentStack - list created by caller to manage indentation stack
            (multiple statementWithIndentedBlock expressions within a single grammar
            should share a common indentStack)
        - indent - boolean indicating whether block must be indented beyond the
            the current level; set to False for block of left-most statements
            (default=True)

       A valid block must contain at least one C{blockStatement}.
    c                ss   | t  |   k r d  St | |   } |   d k ro |   d k rZ t |  | d   n  t |  | d   n  d  S)Nru   zillegal nestingznot a peer entryr   r   )r   r7   r   r   )rx   r  r  curCol)indentStackro   rp   checkPeerIndentO  s     z&indentedBlock.<locals>.checkPeerIndentc                sE   t  | |   } |   d k r/   j |  n t |  | d   d  S)Nru   znot a subentryr   )r7   r   r   )rx   r  r  rI  )rJ  ro   rp   checkSubIndentW  s    z%indentedBlock.<locals>.checkSubIndentc                sn   | t  |   k r d  St | |   }   oH |   d k  oH |   d k s` t |  | d   n    j   d  S)Nru   r  znot an unindentr   )r   r7   r   r   )rx   r  r  rI  )rJ  ro   rp   checkUnindent^  s     &z$indentedBlock.<locals>.checkUnindentz	 )
r   r   r  r  r
   rP  r   r   r  r  )ZblockStatementExprrJ  r   rK  rL  rN  r	  INDENTZPEERZUNDENTZsmExprro   )rJ  rp   re   ?  s    8$z#[\0xc0-\0xd6\0xd8-\0xf6\0xf8-\0xff]z[\0xa1-\0xbf\0xd7\0xf7]z_:rv   zgt lt amp nbsp quotentityrw   z><& "c             C   s    |  j  t k r t |  j  p d  S)N)rP  _htmlEntityMap)r  ro   ro   rp   r   y  s    z/\*(?:[^*]*\*+)+?/zC style commentz<!--[\s\S]*?-->z.*z\/\/(\\\n|.)*z
// commentz:/(?:\*(?:[^*]*\*+)+?/|/[^\n]*(?:\n[^\n]*)*?(?:(?<!\\)|\Z))zC++ style commentz#.*zPython style commentz 	Z	commaItemr   __main__c             C   s   y t  j |   } | j   } t |  d t |   t d t |   t d t | j   t d t | j   t | j d d   Wnc t k
 r } zC t |  d  t | j	  t d | j
 d d	  t |  WYd  d  } ~ Xn Xt   d  S)
Nz->z	tokens = ztokens.columns = ztokens.tables = ZSQLTr  ru   r*  )	simpleSQLrl  r   r  rl   columnstablesr   r   rF   r   )Z
teststringr\  rY  r]  ro   ro   rp   test  s    rV  Zselectfromz_$r  r  rB  rT  rU  zSELECT * from XYZZY, ABCzselect * from SYS.XYZZYzSelect A from Sys.dualzSelect AA,BB,CC from Sys.dualzSelect A, B, C from Sys.dualzXelect A, B, C from Sys.dualzSelect A, B, C frox Sys.dualZSelectzSelect ^^^ frox Sys.dualz'Select A, B, C from Sys.dual, Table2   )r   __version__Z__versionTime__
__author__r   weakrefr   r   r   r  rt  r<  r  r  r
  __all__versionr  r   maxsizer  rl   r   chrr'  rq   sumr   r  reversedr   r|  r  r  r  r  r  r$  ZmaxintZxranger   Z__builtin__ry   fnamer   getattrr   r   r   r}   r   r~   Zascii_lowercaseZascii_uppercaser2   rQ   rB   r1   r  r   Z	printablerU   ra  r   r   r   r    r#   r   r   MutableMappingregisterr7   rI   rF   r  r  r  rP   r%  r!   r)   r
   r   r   r>  r)  r   r   r   r-   r$   r"   r   r,   r  r   r   r   r'   r&   r/   r.   r   r   r   r   r	   r   r   r   r0   r   r  r  r   r%   r   r  r*   r+   r   r   r   r(   r   ra   r>   r:   rp  rN   rM   r  rR   r?   rf   rg   ri   rA  rA   rH   rG   r`   r_   rP  Z_escapedPuncZ_escapedHexCharZ_escapedOctCharZ_singleCharZ
_charRangerE  r1  r^   rL   r[   rY   rc   r@   rE   r6  rA  rJ   rK   rd   rB  rS   rF  rG  rh   rT   r<   r]   rX   rb   rO   re   r3   rV   r5   r4   rh  r9   r   rz   rQ  rZ   r6   rC   r  r\   r=   r;   rD   rW   Z_commasepitemr8   r   rV  ZselectTokenZ	fromTokenZidentZ
columnNameZcolumnNameListZ	tableNameZtableNameListrS  ro   ro   ro   rp   <module>:   s  	*	


0	
 
  		8
|@p>1kB=7TH '"	">L 	"		@
$$@		H44/P+









