
    }f'                        d dl mZ d dlZd dlmZ d dlmZ ddlmZ  ed      Z	 e
       ZefdZdd	Zdd
Zddd efdZddZy)    )annotationsN)Iterable)TypeVar   )RETYPE_Tc                    	 t        t        |             S # t        $ r }|t        u rt	        d      ||cY d}~S d}~ww xY w)a  Return the first item of *iterable*, or *default* if *iterable* is
    empty.

        >>> first([0, 1, 2, 3])
        0
        >>> first([], 'some default')
        'some default'

    If *default* is not provided and there are no items in the iterable,
    raise ``ValueError``.

    :func:`first` is useful when you have a generator of expensive-to-retrieve
    values and want any arbitrary one. It is marginally shorter than
    ``next(iter(iterable), default)``.

    zKfirst() was called on an empty iterable, and no default value was provided.N)nextiterStopIteration
_undefined
ValueError)iterabledefaultes      N/var/www/feuerwehr-webapp/venv/lib/python3.12/site-packages/tatsu/util/misc.pyfirstr      sN    $DN## 

 j .  
s    	?:??c                    | y | j                  | j                  dd       }t        |      dk(  r|d   S |xs | j                         S )Nr   r   r   )groupsstringlengroup)mgs     r   match_to_findr   /   sH    y	!A'A
1v{t~AGGI    c              #    K   t        | t              r| nt        j                  | |      }||j	                  |||      }n'||j	                  ||      }n|j	                  |      }|D ]  }t        |        yw)z
    like finditer(), but with return values like findall()

    implementation taken from cpython/Modules/_sre.c/findall()
    )flagsN)posendpos)r    )
isinstancer   recompilefinditerr   )patternr   r    r!   r   riteratorr   s           r   findalliterr)   9   s      gv& 	ZZu- 
 ::f#f:=	::f#:.::f% As   A=A?c           	     8    t        t        | ||||      |      S )zN
    Avoids using the inefficient findall(...)[0], or first(findall(...))
    )r    r!   r   r   )r   r)   )r&   r   r    r!   r   r   s         r   	findfirstr+   N   s$     GVV5I r   c                   t              g }dfd}t        t        |        |       z
        }|rl|j                         }|j                  |       D ch c]  \  }}||k(  s| }}}|D ch c]  }||f c}z  |j	                  | |       z
         |rlrt        d      t        t        |            S c c}}w c c}w )Nc                 :    D  ch c]  \  } }|	 c}} S c c}} w )N )_r   orders     r   with_incomingztopsort.<locals>.with_incoming^   s     %&fq!&&&s   z)There are cycles in the topological order)returnzset[_T])setlistpopappendextendr   reversed)	nodesr0   resultr1   pendingnxr   outgoings	    `       r   topsortr?   X   s     JEF' 3u:/0G
KKMa %*4&1aQ!VA44(+Q1a&++x-/12  DEE !! 5+s   C 'C 3C)r   zre.Match)NNr   )r9   zIterable[_T]r0   zIterable[tuple[_T, _T]]r2   zlist[_T])
__future__r   r#   collections.abcr   typingr   _commonr   r   objectr   r   r   r)   r+   r?   r.   r   r   <module>rE      sN    " 	 $  T]X
 ' B* $(Az "r   