
    
`i                         d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZmZmZ d dlmZmZ d dlmZmZmZ d dlmZmZmZ d dlmZmZ e G d	 d
                      Z G d d          ZdS )    )
namedtuple)	dataclass)combinations)ListTupleAnyDictIterableSet	FrozenSetOptional)InvalidSyntaxREFlags)FSMAlphabetanything_else)PatternUnsupportedparse_pattern)logger	soft_reprc            	       f    e Zd ZU dZeed<   eed<   eed<   	 	 dded	ed
edefdZed             Z	dS )ExampleCollisiona  
    Captures the full text of an example collision between two regex.
    `main_text` is the part that actually gets captured by the two regex
    `prefix` is the part that is potentially needed for lookbehinds
    `postfix` is the part that is potentially needed for lookahead
    prefix	main_textpostfixExample Collision:  Fintroindentforce_pointerreturnc                    t          |          t          |          k     rt          d          t          | j                  }t          | j                  }t          | j                  }| | | }t          |          t          |          k    rQdt          |          t          |          z
  t          |          z   z  }dt          |          z  }	| | d| | |	 S | | S )an  
        Formats this example somewhat similar to a python syntax error.
        - intro is added on the first line
        - indent is added on the second line
        The three parts of the example are concatenated and `^` is used to underline them.

        ExampleCollision(prefix='a', main_text='cd', postfix='ef').format_multiline()

        leads to

        Example Collision: acdef
                             ^^

        This function will escape the character where necessary to stay readable.
        if `force_pointer` is False, the function will not produce the second line if only main_text is set
        z'Can't have intro be shorter than indent ^
)len
ValueErrorr   r   r   r   )
selfr   r    r!   r   r   r   text
whitespacepointerss
             j/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/interegular/comparator.pyformat_multilinez!ExampleCollision.format_multiline   s    $ u::F##FGGG4;''dn--	DL)).).W..t99I&&E

S[[ 83v;; FGJS^^+HCTCCVCZCCCC#T###    c                 0    | j         | j        z   | j        z   S N)r   r   r   r)   s    r-   	full_textzExampleCollision.full_text7   s    {T^+dl::r/   N)r   r   F)
__name__
__module____qualname____doc__str__annotations__boolr.   propertyr3    r/   r-   r   r      s           KKKNNNLLLQS/4$ $c $3 $(,$9<$ $ $ $> ; ; X; ; ;r/   r   c            
          e Zd ZdZdeeef         fdZdedefdZ	dedede
fdZddee         de
deeeef                  fdZddedededefdZdedede
fdZed             Zd ZdedefdZedeeef         fd            Zd	S )
Comparatora  
    A class that represents the main interface for comparing a list of regex to each other.
    It expects a dictionary of arbitrary labels mapped to `Pattern` instances,
    but there is a utility function to create the instances `from_regex` strings.

    The main interface function all expect the abitrary labels to be given, which
    then get mapped to the correct `Pattern` and/or `FSM` instance.

    There is a utility function `mark(a,b)` which allows to mark pairs that shouldn't
    be checked again by `check`.
    patternsc                 r   || _         t                      | _        |sd S t          j        d |                                D              d         | _        d |                                D             }t          d |D                       t          d |D                       f| _        i | _	        i | _
        d S )Nc              3   Z   K   | ]&}|                     t          d                     V  'dS r   N)get_alphabetr   .0ps     r-   	<genexpr>z&Comparator.__init__.<locals>.<genexpr>N   s4      )`)`!..*D*D)`)`)`)`)`)`r/   r   c                     g | ]	}|j         
S r<   )prefix_postfixrD   s     r-   
<listcomp>z'Comparator.__init__.<locals>.<listcomp>O   s    HHHA,HHHr/   c              3   &   K   | ]}|d          V  dS rB   r<   rD   s     r-   rG   z&Comparator.__init__.<locals>.<genexpr>P   s&      "B"BA1Q4"B"B"B"B"B"Br/   c              3   &   K   | ]}|d          V  dS )   Nr<   rD   s     r-   rG   z&Comparator.__init__.<locals>.<genexpr>P   s'      GgGgQR!GgGgGgGgGgGgr/   )	_patternsset_marked_pairsr   unionvalues	_alphabetmax_prefix_postfix_fsms_know_pairs)r)   r?   prefix_postfix_ss      r-   __init__zComparator.__init__I   s    !25%% 	F!)`)`hooN_N_)`)`)`abcdHHhoo6G6GHHH""B"B1A"B"B"BBBCGgGgVfGgGgGgDgDgg%'
8:r/   ar"   c           	      R   || j         vr	 | j        |                             | j        | j                  | j         |<   n]# t
          $ r;}d | j         |<   t          j        d| dt          |                      Y d }~nd }~wt          $ r d | j         |<   Y nw xY w| j         |         S )Nz!Can't compile Pattern to fsm for 
     )
rV   rN   to_fsmrS   rU   r   r   warningreprKeyError)r)   rZ   es      r-   get_fsmzComparator.get_fsmT   s    DJ% $q 1 8 8I] ^ ^
1 X X X $
1V1VVTRSWWVVWWWWWWWW % % % $
1%z!}s   3? 
B	1A??BBbc                     ||f| j         vrZ|                     |          |                     |          }}||d| j         ||f<   n|                    |          | j         ||f<   | j         ||f         S )NT)rW   rb   
isdisjoint)r)   rZ   rc   fafbs        r-   re   zComparator.isdisjoint_   s}    q6)))\\!__dll1ooBzRZ)- A&&)+r):): A&1%%r/   NFkeysskip_markedc              #      K   || j         }t          |d          D ]:\  }}|r|                     ||          r|                     ||          s||fV  ;d S )N   )rN   r   	is_markedre   )r)   rh   ri   rZ   rc   s        r-   checkzComparator.checkh   s}      <>D q)) 	 	DAq t~~a33 ??1a(( d


		 	r/   max_timec                    | j         |         | j         |         }}t          |j        d         |j        d                   }t          |j        d         |j        d                   }|                    t	          d                                        |                    t	          d                              d         }|                    |||f          |                    |||f          }
}	|	                    |
          }|d }n%t          |dz
  dt          |          z  z            }	 t          |                    |                    }n$# t          $ r t          d| d| d          w xY wd                    d	 |D                       }|dk    r+t          |d |         |||          || d                    S t          |d |         ||d          d          S )
Nr   rM   g
ףp=
?g_eG|>zNo overlap between z and z existsr   c              3   4   K   | ]}|t           k    r|nd V  dS )?N)r   )rE   cs     r-   rG   z1Comparator.get_example_overlap.<locals>.<genexpr>   s0      FFAA..qqCFFFFFFr/   )rN   rT   rI   rC   r   rQ   r]   intersectionintr'   nextstringsStopIterationr(   joinr   )r)   rZ   rc   rn   papb
needed_preneeded_postalphabetrf   rg   rs   max_iterationsr*   s                 r-   get_example_overlapzComparator.get_example_overlapq   s   "DN1$5B*1-r/@/CDD
"+A.0A!0DEE ??71::..44R__WQZZ5P5PQQRST8j+%>??8V`bmUnAoAoBr**!NN !(T/FS]]4J!KLLN	G,,^<<==DD 	G 	G 	GE1EE1EEEFFF	GwwFFFFFFF??#D*$5tJ|<S7TVZ\g[g[h[hVijjj#D*$5tJKK7H"MMMs   2"E !E6c                 2    t          ||h          | j        v S r1   )	frozensetrP   r)   rZ   rc   s      r-   rl   zComparator.is_marked   s    !Q  D$666r/   c                     | j         S r1   )rP   r2   s    r-   marked_pairszComparator.marked_pairs   s    !!r/   c                 *    t          | j                  S r1   )r'   rP   r2   s    r-   count_marked_pairszComparator.count_marked_pairs   s    4%&&&r/   c                 X    | j                             t          ||h                     d S r1   )rP   addr   r   s      r-   markzComparator.mark   s+    y!Q0011111r/   regexesc           
          i }|                                 D ]]\  }}	 t          |          ||<   # t          t          f$ r1}t	          j        d| dt          |                      Y d }~Vd }~ww xY w | |          S )Nz#Can't compile regex to Pattern for r\   )itemsr   r   r   r   r^   r_   )clsr   r?   krra   s         r-   from_regexeszComparator.from_regexes   s    MMOO 	Z 	ZDAqZ+A../ Z Z ZXQXXtTUwwXXYYYYYYYYZs8}}s   0A2'A--A2)NFr1   )r4   r5   r6   r7   r	   r   r   rY   r   rb   r:   re   r
   r   rm   floatr   r   rl   r;   r   r   r   classmethodr8   r   r<   r/   r-   r>   r>   <   s       
 
	;c7l!3 	; 	; 	; 	;	 	 	 	 	 	&C &C &D & & & & (3- T hW\]`be]eWfNg    N NS NS NE NM] N N N N:73 73 74 7 7 7 7 " " X"' ' '2c 2c 2 2 2 2 4S>    [  r/   r>   N)collectionsr   dataclassesr   	itertoolsr   typingr   r   r   r	   r
   r   r   r   interegularr   r   interegular.fsmr   r   r   interegular.patternsr   r   r   interegular.utilsr   r   r   r>   r<   r/   r-   <module>r      sT   " " " " " " ! ! ! ! ! ! " " " " " " M M M M M M M M M M M M M M M M M M M M . . . . . . . . 8 8 8 8 8 8 8 8 8 8 D D D D D D D D D D / / / / / / / / ,; ,; ,; ,; ,; ,; ,; ,;^g g g g g g g g g gr/   