
    =                         S r SSKJr  SSKrSSKJr  SSKJrJrJ	r	J
r
JrJr  SSKJrJr  SrS	r " S
 S\5      r " S S\5      r " S S\5      r " S S\5      rSS jrS rg)a  
Compiler for a regular grammar.

Example usage::

    # Create and compile grammar.
    p = compile('add \s+ (?P<var1>[^\s]+)  \s+  (?P<var2>[^\s]+)')

    # Match input string.
    m = p.match('add 23 432')

    # Get variables.
    m.variables().get('var1')  # Returns "23"
    m.variables().get('var2')  # Returns "432"


Partial matches are possible::

    # Create and compile grammar.
    p = compile('''
        # Operators with two arguments.
        ((?P<operator1>[^\s]+)  \s+ (?P<var1>[^\s]+)  \s+  (?P<var2>[^\s]+)) |

        # Operators with only one arguments.
        ((?P<operator2>[^\s]+)  \s+ (?P<var1>[^\s]+))
    ''')

    # Match partial input string.
    m = p.match_prefix('add 23')

    # Get variables. (Notice that both operator1 and operator2 contain the
    # value "add".) This is because our input is incomplete, and we don't know
    # yet in which rule of the regex we we'll end up. It could also be that
    # `operator1` and `operator2` have a different autocompleter and we want to
    # call all possible autocompleters that would result in valid input.)
    m.variables().get('var1')  # Returns "23"
    m.variables().get('operator1')  # Returns "add"
    m.variables().get('operator2')  # Returns "add"

    )unicode_literalsN)range   )AnySequenceRegexVariableRepeat	Lookahead)parse_regextokenize_regex)compileinvalid_trailingc                   Z    \ rS rSrSrSS jrS rS r\S 5       r	\S 5       r
S	 rS
 rSrg)_CompiledGrammar;   a1  
Compiles a grammar. This will take the parse tree of a regular expression
and compile the grammar.

:param root_node: :class~`.regex_parser.Node` instance.
:param escape_funcs: `dict` mapping variable names to escape callables.
:param unescape_funcs: `dict` mapping variable names to unescape callables.
Nc           
      z  ^ ^ UT l         U=(       d    0 T l        U=(       d    0 T l        0 T l        S/mUU 4S jnST R	                  X5      -  T l        [        T R                  X5      5      T l        [        R                  n[        R                  " T R
                  U5      T l        T R                   Vs/ s H  n[        R                  " Xe5      PM     snT l        T R                   Vs/ s H6  n[        R                  " SUR                  S5      < S[        < S3U5      PM8     snT l        g s  snf s  snf )Nr   c                 d   > STS   -  nU R                   TR                  U'   TS==   S-  ss'   U$ )Nzn%sr   r   )varname_group_names_to_nodes)nodenamecounterselfs     Dlib/third_party/prompt_toolkit/contrib/regular_languages/compiler.pycreate_group_func4_CompiledGrammar.__init__.<locals>.create_group_funcM   s7    71:%D/3||D&&t,AJ!OJK    ^%s$(?:$z)(?P<z>.*?)$)	root_nodeescape_funcsunescape_funcsr   
_transform_re_patternlist_transform_prefix_re_prefix_patternsreDOTALLr   _re
_re_prefixrstrip_INVALID_TRAILING_INPUT_re_prefix_with_trailing_input)r   r"   r#   r$   r   flagstr   s   `      @r   __init___CompiledGrammar.__init__D   s   "(.B,2 &("#	 "DOOI$QQ#'(>(>y(\#]  		::d..69=9Q9QR9QA2::a/9QR --//- JJ?VWY^_-//+ S
//s   7 D3-=D8c                 Z    U R                   R                  U5      nU(       a  U" U5      $ U$ )zG
Escape `value` to fit in the place of this variable into the grammar.
)r#   getr   r   valuefs       r   escape_CompiledGrammar.escaped   s+     !!'*qx'%'r   c                 Z    U R                   R                  U5      nU(       a  U" U5      $ U$ )z
Unescape `value`.
)r$   r6   r7   s       r   unescape_CompiledGrammar.unescapek   s+     ##G,qx'%'r   c                 $   ^^ UU4S jmT" U5      $ )z
Turn a :class:`Node` object into a regular expression.

:param root_node: The :class:`Node` instance for which we generate the grammar.
:param create_group_func: A callable which takes a `Node` and returns the next
    free name for this node.
c                 "  > [        U [        5      (       a(  SSR                  U4S jU R                   5       5      -  $ [        U [        5      (       a%  SR                  U4S jU R                   5       5      $ [        U [
        5      (       a  U R                  $ [        U [        5      (       a-  U R                  (       a  SOSnUT" U R                  5      -   S-   $ [        U [        5      (       a!  S	T" U 5      < S
T" U R                  5      < S3$ [        U [        5      (       a[  ST" U R                  5      U R                  U R                  c  SO[        U R                  5      U R                  (       a  S4-  $ S4-  $ [!        SU < 35      e)N(?:%s)|c              3   4   >#    U  H  nT" U5      v   M     g 7fN .0c	transforms     r   	<genexpr>A_CompiledGrammar._transform.<locals>.transform.<locals>.<genexpr>~   s     *OA9Q<<    c              3   4   >#    U  H  nT" U5      v   M     g 7frD   rE   rF   s     r   rJ   rK      s     C]y||]rL   z(?!z(=)(?P<>z(?:%s){%i,%s}%s?zGot )
isinstancer   joinchildrenr   r   regexr   negative	childnoder	   r
   
min_repeat
max_repeatstrgreedy	TypeError)r   beforer   rI   s     r   rI   ._CompiledGrammar._transform.<locals>.transform{   sA   $$$#((*O*O"OOO D(++wwCT]]CCC D%((zz!D),,#'==%d	$.. 99C?? D(++'8'>	$..@YZZ D&))(dnn-t??2RDOO8L;;R,   -0,    D 344r   rE   )clsr"   r   rI   s     `@r   r%   _CompiledGrammar._transformr   s    	5> ##r   c              #   N   ^ ^^#    U UU4S jmT" U5       H
  nSU-  v   M     g7f)a  
Yield all the regular expressions matching a prefix of the grammar
defined by the `Node` instance.

This can yield multiple expressions, because in the case of on OR
operation in the grammar, we can have another outcome depending on
which clause would appear first. E.g. "(A|B)C" is not the same as
"(B|A)C" because the regex engine is lazy and takes the first match.
However, because we the current input is actually a prefix of the
grammar which meight not yet contain the data for "C", we need to know
both intermediate states, in order to call the appropriate
autocompletion for both cases.

:param root_node: The :class:`Node` instance for which we generate the grammar.
:param create_group_func: A callable which takes a `Node` and returns the next
    free name for this node.
c           	   3     >#    [        U [        5      (       a*  U R                   H  nT	" U5       H
  nSU-  v   M     M     g [        U [        5      (       a  [	        [        U R                  5      5       Hg  nU R                  S U  Vs/ s H  nTR                  UT5      PM     nnT	" U R                  U   5       H  nSSR                  U5      U-   -  v   M     Mi     g [        U [        5      (       a  SU R                  -  v   g [        U [        5      (       a>  U R                  (       a"  STR                  U R                  T5      -  v   g [        S5      e[        U [        5      (       a-  T	" U R                  5       H  nST" U 5      < SU< S3v   M     g [        U [        5      (       a  TR                  U R                  T5      nT	" U R                  5       HP  nU R                   (       a  S	U R                   S
-
  -  nOSnSU< SU< U R"                  (       a  SOS< SU< S3v   MR     g [%        SU -  5      es  snf 7f)Nz(?:%s)?rA   rM   z(?!%s)z%Positive lookahead not yet supported.rP   rQ   rO   z{,%i}r   *r    rR   z)?zGot %r)rS   r   rU   r   r   lenr%   rT   r   rV   r   rW   rX   	Exceptionr	   r
   rZ   r\   r]   )
r   rH   riaprefixrepeat_signr`   r   rI   s
          r   rI   5_CompiledGrammar._transform_prefix.<locals>.transform   s     $$$A&q\'!m+ * ' D(++s4==12AGK}}UWVWGXYGX!+<=GXAY&t}}Q'78&"''!*q.99 9 3
 D%(($**,,D),,=="S^^DNNDU%VVV $$KLLD(++ #4>>2A*;D*A1EE 3 D&)) 8IJ"4>>2A&-11D&E&)##{{3	  3  400O Zs   BI	
I&F#I	r   NrE   )r`   r"   r   rg   rI   s   ` ` @r   r(   "_CompiledGrammar._transform_prefix   s'     &4	1l 9%A1* &s    %c                     U R                   R                  U5      nU(       a.  [        XR                   U4/U R                  U R                  5      $ g)z
Match the string with the grammar.
Returns a :class:`Match` instance or `None` when the input doesn't match the grammar.

:param string: The input string.
N)r,   matchMatchr   r$   )r   stringms      r   ro   _CompiledGrammar.match   sC     HHNN6"88Q-$2L2LdNaNabb r   c                 .   U R                   U R                  4 Hn  nU Vs/ s H  o3UR                  U5      4PM     nnU VVs/ s H  u  p5U(       d  M  X54PM     nnnU/ :w  d  MM  [        XU R                  U R
                  5      s  $    gs  snf s  snnf )a"  
Do a partial match of the string with the grammar. The returned
:class:`Match` instance can contain multiple representations of the
match. This will never return `None`. If it doesn't match at all, the "trailing input"
part will capture all of the input.

:param string: The input string.
N)r-   r0   ro   rp   r   r$   )r   rq   patternsrg   matchesrr   s         r   match_prefix_CompiledGrammar.match_prefix   s     $*M*MNH5=>X1776?+XG>*17'$!Qvv'G7"}Vd.H.H$J]J]^^ O>7s   BBB)	r   r,   r&   r-   r)   r0   r#   r"   r$   NN)__name__
__module____qualname____firstlineno____doc__r3   r:   r=   classmethodr%   r(   ro   rw   __static_attributes__rE   r   r   r   r   ;   sO    /@(( '$ '$R I IV
c_r   r   c                   B    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
rg)rp   i  z
:param string: The input string.
:param re_matches: List of (compiled_re_pattern, re_match) tuples.
:param group_names_to_nodes: Dictionary mapping all the re group names to the matching Node instances.
c                 4    Xl         X l        X0l        X@l        g rD   )rq   _re_matchesr   _unescape_funcs)r   rq   
re_matchesgroup_names_to_nodesr$   s        r   r3   Match.__init__  s    %%9"-r   c                 0   ^  U 4S jn[        U" 5       5      $ )z)
Return a list of (varname, reg) tuples.
c               3      >#    TR                    HW  u  pU R                  R                  5        H4  u  p#U[        :w  d  M  UR                  U   nTR
                  U   nXT4v   M6     MY     g 7frD   )r   
groupindexitemsr/   regsr   )rg   re_match
group_namegroup_indexregr   r   s         r   
get_tuples(Match._nodes_to_regs.<locals>.get_tuples  sb     #///0||/A/A/C+J!%<<&mmK8#99*E#k)	 0D  0s   9A, ,A,)r'   )r   r   s   ` r   _nodes_to_regsMatch._nodes_to_regs  s    	* JL!!r   c                    ^  S nU 4S jnT R                  5        VVs/ s H  u  p4U" U5      (       a  M  X2" U5      U4PM      snn$ s  snnf )z6
Returns list of list of (Node, string_value) tuples.
c                 0    U S   S:H  =(       a    U S   S:H  $ )Nr   r   rE   )slices    r   is_none'Match._nodes_to_values.<locals>.is_none%  s    8r>4eAh"n4r   c                 .   > TR                   U S   U S    $ Nr   r   )rq   )r   r   s    r   r6   #Match._nodes_to_values.<locals>.get(  s    ;;uQxa11r   )r   )r   r   r6   r   r   s   `    r   _nodes_to_valuesMatch._nodes_to_values!  sM    	5	2 DHCVCVCXoCX`ghm`n,#e*e,CXooos
   AAc                 Z    U R                   R                  U5      nU(       a  U" U5      $ U$ rD   )r   r6   )r   r   r8   	unwrappers       r   	_unescapeMatch._unescape-  s*    ((,,W5	#,y7%7r   c                     [        U R                  5        VVVs/ s H  u  po1U R                  X5      U4PM     snnn5      $ s  snnnf )z&
Returns :class:`Variables` instance.
)	Variablesr   r   )r   kvsls       r   	variablesMatch.variables1  s>     H]H]H_`H_HA"dnnQ2B7H_`aa`s    Ac                 d   / nU R                    HR  u  p#UR                  R                  5        H/  u  pEU[        :X  d  M  UR	                  UR
                  U   5        M1     MT     U(       aE  [        S U 5       5      [        S U 5       5      /nU R                  US   US    n[        SXv5      $ g)z
Get the `MatchVariable` instance, representing trailing input, if there is any.
"Trailing input" is input at the end that does not match the grammar anymore, but
when this is removed from the end of the input, the input would be a valid string.
c              3   *   #    U  H	  oS    v   M     g7f)r   NrE   rG   rh   s     r   rJ   'Match.trailing_input.<locals>.<genexpr>H  s     .v!1v   c              3   *   #    U  H	  oS    v   M     g7f)r   NrE   r   s     r   rJ   r   H  s     3I&QaD&r   r   r   z<trailing_input>N)	r   r   r   r/   appendr   maxrq   MatchVariable)r   slicesrg   r   r   r   r   r8   s           r   trailing_inputMatch.trailing_input7  s       ++KA+,<<+=+=+?'
!88MM(--"<= ,@ , .v..3I&3I0IJEKKaq2E !3UBB r   c              #      #    U R                  5        H]  u  pUS   [        U R                  5      :X  d  M#  U R                  XR                  US   US    5      n[	        XUS   US   45      v   M_     g7f)zn
Yields `MatchVariable` instances for all the nodes having their end
position at the end of the input string.
r   r   N)r   re   rq   r   r   )r   r   r   r8   s       r   	end_nodesMatch.end_nodesL  sl     
 !//1LG1vT[[))wCFCF0KL#GSVSV4DEE	 2s
   0A5?A5)r   r   r   rq   N)rz   r{   r|   r}   r~   r3   r   r   r   r   r   r   r   rE   r   r   rp   rp     s.    
."
p8bC*	Fr   rp   c                   <    \ rS rSrS rS rS
S jrS rS rS r	S	r
g)r   iX  c                     Xl         g rD   _tuples)r   tupless     r   r3   Variables.__init__Y  s    r   c                 ~    U R                   R                  < SSR                  S U R                   5       5      < S3$ )N(, c              3   8   #    U  H  u  po1< S U< 3v   M     g7f)=NrE   )rG   r   r   _s       r   rJ   %Variables.__repr__.<locals>.<genexpr>_  s     .\|GA!!Q/?|s   rO   )	__class__rz   rT   r   r   s    r   __repr__Variables.__repr__]  s0    NN##TYY.\t||.\%\^ 	^r   Nc                 @    U R                  U5      nU(       a  US   $ U$ )Nr   )getall)r   keydefaultr   s       r   r6   Variables.geta  s!    C  uQx-g-r   c                 f    U R                    VVVs/ s H  u  p#oBU:X  d  M  UPM     snnn$ s  snnnf rD   r   )r   r   r   r   r   s        r   r   Variables.getalle  s&    !%:gaAc:::s   ,,c                 $    U R                  U5      $ rD   )r6   )r   r   s     r   __getitem__Variables.__getitem__h  s    xx}r   c              #   T   #    U R                    H  u  pn[        XU5      v   M     g7f)z"
Yield `MatchVariable` instances.
N)r   r   r   r   r8   r   s       r   __iter__Variables.__iter__k  s'      &*\\!GE66 &2s   &(r   rD   )rz   r{   r|   r}   r3   r   r6   r   r   r   r   rE   r   r   r   r   X  s!    ^.;7r   r   c                   $    \ rS rSrSrS rS rSrg)r   is  a   
Represents a match of a variable in the grammar.

:param varname: (string) Name of the variable.
:param value: (string) Value of this variable.
:param slice: (start, stop) tuple, indicating the position of this variable
              in the input string.
c                 x    Xl         X l        X0l        U R                  S   U l        U R                  S   U l        g r   )r   r8   r   startstopr   s       r   r3   MatchVariable.__init__|  s/    

ZZ]
JJqM	r   c                 n    U R                   R                  < SU R                  < SU R                  < S3$ )Nr   r   rO   )r   rz   r   r8   r   s    r   r   MatchVariable.__repr__  s!    #~~66djjQQr   )r   r   r   r8   r   N)rz   r{   r|   r}   r~   r3   r   r   rE   r   r   r   r   s  s    "Rr   r   c                 <    [        [        [        U 5      5      UUS9$ )zR
Compile grammar (given as regex string), returning a `CompiledGrammar`
instance.
)r#   r$   )_compile_from_parse_treer   r   )
expressionr#   r$   s      r   r   r     s$    
 $N:./!%' 'r   c                      [        U /UQ70 UD6$ )zP
Compile grammar (given as parse tree), returning a `CompiledGrammar`
instance.
)r   )r"   ri   kws      r   r   r     s    
 I00R00r   ry   )r~   
__future__r   r*   	six.movesr   regex_parserr   r   r   r	   r
   r   r   r   __all__r/   objectr   rp   r   r   r   r   rE   r   r   <module>r      st   'P ( 	  K K 5 - I_v I_XNFF NFb7 76RF R*'1r   