
    
`iL8                        d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddl
mZmZ dd	lmZmZ dd
lmZ  G d d          Z G d d          Zd Z G d d          Z G d de          Z G d de          Zd Zdee         fdZ G d d          Zd Z G d d          Zd Zd Z  G d  d!          Z!d"S )#zLProvides functions for the automatic building and shaping of the parse-tree.    )List   )GrammarErrorConfigurationError)Token)Tree)Transformer_InPlace)_vargs_meta_vargs_meta_inline)partialwraps)productc                       e Zd Zd Zd ZdS )ExpandSingleChildc                     || _         d S N)node_builder)selfr   s     k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/lark/parse_tree_builder.py__init__zExpandSingleChild.__init__   s    (    c                 b    t          |          dk    r|d         S |                     |          S )Nr   r   )lenr   )r   childrens     r   __call__zExpandSingleChild.__call__   s1    x==AA;$$X...r   N__name__
__module____qualname__r   r    r   r   r   r      s2        ) ) )/ / / / /r   r   c                   "    e Zd ZddZd Zd ZdS )PropagatePositionsNc                 "    || _         || _        d S r   )r   node_filter)r   r   r$   s      r   r   zPropagatePositions.__init__   s    (&r   c                    |                      |          }t          |t                    r|j        }|                     |          }|t          |d          sXt          |d|j                  |_        t          |d|j                  |_        t          |d|j	                  |_	        d|_
        t          |d|j                  |_        t          |d|j                  |_        t          |d|j	                  |_        |                     t          |                    }|t          |d          sXt          |d|j                  |_        t          |d|j                  |_        t          |d	|j                  |_        d|_
        t          |d|j                  |_        t          |d|j                  |_        t          |d	|j                  |_        |S )
Nlinecontainer_linecontainer_columncontainer_start_posFend_linecontainer_end_linecontainer_end_columncontainer_end_pos)r   
isinstancer   meta_pp_get_metahasattrgetattrr&   column	start_posemptyr'   r(   r)   reversedr*   
end_columnend_posr+   r,   r-   )r   r   resres_meta
first_meta	last_metas         r   r   zPropagatePositions.__call__!   s   ))c4   	h xH**844J%x00 +$+J8H*/$Z$ZHM&-j:LjN_&`&`HO)0=RT^Th)i)iH&%*HN*1*>NPZP_*`*`',3J@RT^Te,f,f)/6zCXZdZn/o/o,))(8*<*<==I$x44 +(/	;OQZQc(d(dH%*1)=SU^Ui*j*jH''.y:MyO`'a'aH$%*HN.5iAUW`Wi.j.j+07	CY[d[o0p0p--4Y@SU^Uf-g-g*
r   c                    |D ]}| j         |                      |          st          |t                    r|j        j        s	|j        c S Jt          |t
                    r|c S t          |d          r|                                c S d S )N__lark_meta__)r$   r.   r   r/   r5   r   r1   r>   )r   r   cs      r   r0   zPropagatePositions._pp_get_metaG   s     		) 		)A+D4D4DQ4G4G+!T"" )v| "6MMM"Au%% )O,, )((((()		) 		)r   r   )r   r   r   r   r   r0   r    r   r   r"   r"      sG        ' ' ' '$ $ $L
) 
) 
) 
) 
)r   r"   c                     t          |           rt          t          |           S | du rt          S | du rd S t          d| z            )N)r$   TFz*Invalid option for propagate_positions: %r)callabler   r"   r   )options    r   make_propagate_positionsrC   S   sW     )v>>>>	4!!	5t
IFR
S
SSr   c                       e Zd Zd Zd ZdS )ChildFilterc                 0    || _         || _        || _        d S r   )r   
to_includeappend_none)r   rG   rH   r   s       r   r   zChildFilter.__init___   s    ($&r   c                     g }| j         D ]?\  }}}|r	|d g|z  z  }|r|||         j        z  }$|                    ||                    @| j        r|d g| j        z  z  }|                     |          S r   rG   r   appendrH   r   r   r   filteredi	to_expandadd_nones         r   r   zChildFilter.__call__d   s    &*o 	- 	-"Ay( .TFX-- -HQK00,,,, 	2!111H  ***r   Nr   r    r   r   rE   rE   ^   s2        ' ' '
+ + + + +r   rE   c                       e Zd ZdZd ZdS )ChildFilterLALR`Optimized childfilter for LALR (assumes no duplication in parse tree, so it's safe to change it)c                    g }| j         D ]O\  }}}|r	|d g|z  z  }|r!|r|||         j        z  }&||         j        }4|                    ||                    P| j        r|d g| j        z  z  }|                     |          S r   rJ   rL   s         r   r   zChildFilterLALR.__call__x   s    &*o 		- 		-"Ay( .TFX-- - 4 44HH'{3HH,,,, 	2!111H  ***r   N)r   r   r   __doc__r   r    r   r   rR   rR   u   s)        jj+ + + + +r   rR   c                       e Zd ZdZd Zd ZdS )ChildFilterLALR_NoPlaceholdersrS   c                 "    || _         || _        d S r   )r   rG   )r   rG   r   s      r   r   z'ChildFilterLALR_NoPlaceholders.__init__       ($r   c                     g }| j         D ]C\  }}|r!|r|||         j        z  }||         j        }(|                    ||                    D|                     |          S r   )rG   r   rK   r   )r   r   rM   rN   rO   s        r   r   z'ChildFilterLALR_NoPlaceholders.__call__   sz     O 	- 	-LAy - 4 44HH'{3HH,,,,  ***r   Nr   r   r   rU   r   r   r    r   r   rW   rW      s8        ff% % %
+ 
+ 
+ 
+ 
+r   rW   c                 F    | j          o| j                            d          S )N_)is_termname
startswith)syms    r   _should_expandrb      s!    {?7sx223777r   _empty_indicesc                 \   |r|                     d          t          |           k    sJ d                    d |D                       }d |                    d          D             }t          |          t          |           dz   k    sJ |t          |           f            ndgt          |           dz   z  }g }d}t	          |           D ]G\  }}	|||         z  }|s|	j        r|	j        s'|                    |t          |	          |f           d}H||t          |                    z  }|s9t          |          t          |           k     st          d |D                       rB|s|rt          |rt          nt          ||          S t          t          d	 |D                       S d S )
NF c              3   N   K   | ] }t          t          |                    V  !d S r   )strint).0bs     r   	<genexpr>z,maybe_create_child_filter.<locals>.<genexpr>   s.      88ACAKK888888r   c                 ,    g | ]}t          |          S r    )r   )ri   oness     r   
<listcomp>z-maybe_create_child_filter.<locals>.<listcomp>   s    <<<tT<<<r   0r   r   c              3   "   K   | ]
\  }}}|V  d S r   r    )ri   rN   rO   r]   s       r   rk   z,maybe_create_child_filter.<locals>.<genexpr>   s*      @l@l~qR[\]@l@l@l@l@l@lr   c                     g | ]
\  }}}||fS r    r    )ri   rN   xr]   s       r   rn   z-maybe_create_child_filter.<locals>.<listcomp>   s"    ;[;[;[uq1QF;[;[;[r   )countr   joinsplit	enumerater^   
filter_outrK   rb   anyr   rE   rR   rW   )
	expansionkeep_all_tokens	ambiguousrc   sempty_indicesrG   nones_to_addrN   ra   s
             r   maybe_create_child_filterr      s    1##E**c)nn<<<<GG8888888<<qwws||<<<=!!S^^A%5555s9~~7V5555s9~~a/0JLI&&  3a(( 	3; 	3> 	q."5"5|DEEELM#i..11L ]Z3y>>99S@l@lak@l@l@l=l=l9 	]Y 	])H;;*Vbccc 9;[;[PZ;[;[;[\\\ :9r   c                       e Zd ZdZd Zd ZdS )AmbiguousExpandera  Deal with the case where we're expanding children ('_rule') into a parent but the children
       are ambiguous. i.e. (parent->_ambig->_expand_this_rule). In this case, make the parent itself
       ambiguous with as many copies as there are ambiguous children, and then copy the ambiguous children
       into the right parents in the right places, essentially shifting the ambiguity up the tree.c                 0    || _         || _        || _        d S r   )r   
tree_classrO   )r   rO   r   r   s       r   r   zAmbiguousExpander.__init__   s    ($"r   c                 l    d }g t          |          D ]C\  }} ||          r3| j        v r                    |           |                    d           Ds                     |          S fdt          |          D             }                     d fdt          | D                       S )Nc                 8    t          | d          o
| j        dk    S )Ndata_ambigr1   r   )ts    r   _is_ambig_treez2AmbiguousExpander.__call__.<locals>._is_ambig_tree   s    1f%%<!&H*<<r   r   c                 2    g | ]\  }}|v r|j         n|fS r    )r   )ri   rN   childr{   s      r   rn   z.AmbiguousExpander.__call__.<locals>.<listcomp>   s-    eeeXQANN%..eeer   c                 T    g | ]$}                     t          |                    %S r    )r   list)ri   fr   s     r   rn   z.AmbiguousExpander.__call__.<locals>.<listcomp>   s-    )_)_)_$*;*;DGG*D*D)_)_)_r   )rv   rO   rK   expand_kids_by_datar   r   r   )r   r   r   rN   r   expandr{   s   `     @r   r   zAmbiguousExpander.__call__   s    	= 	= 	= 	!(++ 	4 	4HAu~e$$ 4&&$$Q'''))(333 	/$$X...eeeeQZ[cQdQdeeex)_)_)_)_gW]N^)_)_)_```r   Nr[   r    r   r   r   r      sC        f f# # #
a a a a ar   r   c                 n    fdt          |          D             }|rt          t          ||           S d S )Nc                 X    g | ]&\  }}s|j         r|j        t          |          $|'S r    )r^   rw   rb   )ri   rN   ra   rz   s      r   rn   z3maybe_create_ambiguous_expander.<locals>.<listcomp>   s`     i i ivq##i.1ki>AniSabeSfSfi i i ir   )rv   r   r   )r   ry   rz   rO   s     ` r   maybe_create_ambiguous_expanderr      sa    i i i i9!5!5 i i iI A()Z@@@A Ar   c                       e Zd ZdZd Zd ZdS )AmbiguousIntermediateExpanderaq  
    Propagate ambiguous intermediate nodes and their derivations up to the
    current rule.

    In general, converts

    rule
      _iambig
        _inter
          someChildren1
          ...
        _inter
          someChildren2
          ...
      someChildren3
      ...

    to

    _ambig
      rule
        someChildren1
        ...
        someChildren3
        ...
      rule
        someChildren2
        ...
        someChildren3
        ...
      rule
        childrenFromNestedIambigs
        ...
        someChildren3
        ...
      ...

    propagating up any nested '_iambig' nodes along the way.
    c                 "    || _         || _        d S r   )r   r   )r   r   r   s      r   r   z&AmbiguousIntermediateExpander.__init__  rY   r   c                      d  fd |          }|r$ fd|D             }                      d|          S                      |          S )Nc                 8    t          | d          o
| j        dk    S )Nr   _iambigr   )r   s    r   _is_iambig_treez?AmbiguousIntermediateExpander.__call__.<locals>._is_iambig_tree  s    5&))EejI.EEr   c                 >   | r | d                   r| d         }g }|j         D ]r} |j                   }|r#|D ]}|xj         | dd         z  c_         ||z  }7	                    d|j         | dd         z             }|                    |           s|S dS dS )a  
            Recursively flatten the derivations of the parent of an '_iambig'
            node. Returns a list of '_inter' nodes guaranteed not
            to contain any nested '_iambig' nodes, or None if children does
            not contain an '_iambig' node.
            r   r   N_inter)r   r   rK   )
r   iambig_noderesult
grandchild	collapsedr   new_tree_collapse_iambigr   r   s
          r   r   z@AmbiguousIntermediateExpander.__call__.<locals>._collapse_iambig  s      OOHQK88 &qk"-"6 0 0J 0 01D E EI  0%. ; ;E!NNhqrrl:NNN)+#'??8Z=PS[\]\^\^S_=_#`#`h////   r   c                 D    g | ]}                     |j                  S r    )r   r   )ri   r?   r   s     r   rn   z:AmbiguousIntermediateExpander.__call__.<locals>.<listcomp>1  s)    PPPt00<<PPPr   r   )r   r   )r   r   r   processed_nodesr   r   s   `   @@r   r   z&AmbiguousIntermediateExpander.__call__  s    	F 	F 	F	 	 	 	 	 	 	0 %$X..	 	>PPPPiPPPO??8_===  ***r   Nr[   r    r   r   r   r      s=        & &P% % %!+ !+ !+ !+ !+r   r   c                 <     t                      fd            }|S )Nc                 D    t          j        |           } |          S r   )r   r   )r   treefuncs     r   r   zinplace_transformer.<locals>.f9  s$     DM8,,tDzzr   )r   )r   r   s   ` r   inplace_transformerr   8  s3    
4[[    [ Hr   c                      t           u s	t          u rt          d          t                      fd            }|S )Nz0Meta args not supported for internal transformerc                       | d           S r   r    )r   r   r_   wrappers    r   r   zapply_visit_wrapper.<locals>.fE  s    wtT8T222r   )r
   r   NotImplementedErrorr   )r   r_   r   r   s   ``` r   apply_visit_wrapperr   A  sb    +,>!>!>!"TUUU
4[[3 3 3 3 3 3 [3Hr   c                   $    e Zd ZddZd ZddZdS )ParseTreeBuilderFc                     || _         || _        || _        || _        t	          |                     |                    | _        d S r   )r   propagate_positionsr{   maybe_placeholdersr   _init_buildersrule_builders)r   rulesr   r   r{   r   s         r   r   zParseTreeBuilder.__init__L  sD    $#6 ""4!$"5"5e"<"<==r   c              #     K   t          | j                  }|D ]}|j        }|j        }|j        }t          t          d |o|j         ot          t          |j
        || j        | j        r|j        nd           || j        ot          | j        |j
        |          | j        ot!          t"          | j                  g                    }||fV  d S r   )rC   r   optionsrz   expand1r   filteraliasr   r   ry   r{   r   r}   r   r   r   r   )r   r   r   ruler   rz   expand_single_childwrapper_chains           r   r   zParseTreeBuilder._init_buildersT  s     6t7OPP 	& 	&DlG%5O")/ $7TZN=N)$./4>tx  uL  \V[b[p[p  RV  W  W#t#B4?TXTbds#t#tZ7+H$/#Z#Z/ " "  M %%%%%	& 	&r   Nc                   
 i }t          |dd           

r
fd}n| j        }| j        D ]\  }}|j        p|j        j        p|j        j        }	 t          ||          }t          |dd           }|t          |||          }n$t          |t                    rt          |          }n # t          $ r t          ||          }Y nw xY w|D ]}	 |	|          }||v rt          d|d          |||<   |S )N__default__c                      | |d           S r   r    )r   r   default_handlers     r   default_callbackz:ParseTreeBuilder.create_callback.<locals>.default_callbackk  s    &tXt<<<r   visit_wrapperzRule 'z' already exists)r2   r   r   r   r   template_sourceoriginr_   r   r.   r	   r   AttributeErrorr   r   )r   transformer	callbacksr   r   r   user_callback_namer   r   wr   s             @r   create_callbackz ParseTreeBuilder.create_callbackf  se   	!+}dCC 	/= = = = = =  $#'#5 	  	 D-!%!_t|/K!_t{O_BK);<<!!_d;;&+A/A7KKAA-@AA /+A..A! B B B,.@AAB #  AaDDy  "l#GHHHIdOOs   AB**CC)FFFr   )r   r   r   r   r   r   r    r   r   r   r   K  sK        > > > >& & &$     r   r   N)"rU   typingr   
exceptionsr   r   lexerr   r   r   visitorsr	   r
   r   	functoolsr   r   	itertoolsr   r   r"   rC   rE   rR   rW   rb   boolr   r   r   r   r   r   r   r    r   r   <module>r      s   R R       8 8 8 8 8 8 8 8             ) ) ) ) ) ) 5 5 5 5 5 5 5 5 % $ $ $ $ $ $ $      / / / / / / / /5) 5) 5) 5) 5) 5) 5) 5)nT T T+ + + + + + + +.+ + + + +k + + +,+ + + + +[ + + +&8 8 8]UYZ^U_ ] ] ] ]8a a a a a a a aBA A AN+ N+ N+ N+ N+ N+ N+ N+d    : : : : : : : : : :r   