a
    ¦ˆc  ã                   @   s–   d dl mZ d dlmZ d dlm  mZ e ¡ Z	dd„ Z
dd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zeejd�dd„ ƒZeejd�dd„ ƒZdS )é    )Ú	lru_cache)Ú_generated_parserNc                 C   s*   | \}}}|j dk}t||ƒ t||ƒ¡S )aj   Handle an expression of the form "a.b" or "a:b".

    Parameters
    ----------
    trees : list of lark.tree.Tree
        The children tree for the "series" rule. It should always
        contain exactly three items.
    notify : bool
        True if the final target should notify, else False.

    Returns
    -------
    expression : ObserverExpression
    Únotify)ÚdataÚ_handle_treeZthen)Útreesr   ÚleftZ	connectorÚrightZnotify_left© r
   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/observation/parsing.pyÚ_handle_series   s    

r   c                 C   s   | \}}t ||ƒt ||ƒB S )ab   Handle an expression of the form "a, b".

    Parameters
    ----------
    trees : list of lark.tree.Tree
        The children tree for the "parallel" rule. It should always
        contain exactly two items.
    notify : bool
        True if the final target should notify, else False.

    Returns
    -------
    expression : ObserverExpression
    )r   )r   r   r   r	   r
   r
   r   Ú_handle_parallel(   s    r   c                 C   s   | \}|j }tj||d�S )aJ   Handle an element for a named trait.

    Parameters
    ----------
    trees : list of lark.tree.Tree
        The children tree for the "trait" rule.
        It contains only one item.
    notify : bool
        True if the final target should notify, else False.

    Returns
    -------
    expression : ObserverExpression
    ©r   )ÚvalueÚexpression_moduleÚtrait)r   r   ÚtokenÚnamer
   r
   r   Ú_handle_trait;   s    r   c                 C   s   t j|d�S )a4   Handle an anytrait element.

    Parameters
    ----------
    trees : list of lark.tree.Tree
        The children tree for the "trait" rule. This should be empty.
    notify : bool
        True if the final target should notify, else False.

    Returns
    -------
    expression : ObserverExpression
    r   )r   Úanytrait©r   r   r
   r
   r   Ú_handle_anytraitO   s    r   c                 C   s   | \}|j }tj||d�S )a[   Handle an element for filtering existing metadata.

    Parameters
    ----------
    trees : list of lark.tree.Tree
        The children tree for the "metadata" rule.
        It contains only one item.
    notify : bool
        True if the final target should notify, else False.

    Returns
    -------
    expression : ObserverExpression
    r   )r   r   Úmetadata)r   r   r   Úmetadata_namer
   r
   r   Ú_handle_metadata`   s    r   c                 C   sL   | rt d | ¡ƒ‚tjd|dd�tj|dd�B tj|dd�B tj|dd�B S )a6   Handle keyword "items".

    Parameters
    ----------
    trees : list of lark.tree.Tree
        The children tree for the "items" rule.
        It should be empty.
    notify : bool
        True if the final target should notify, else False.

    Returns
    -------
    expression : ObserverExpression
    zUnexpected tree: {!r}ÚitemsT)r   Úoptional)Ú
ValueErrorÚformatr   r   Ú
dict_itemsZ
list_itemsZ	set_itemsr   r
   r
   r   Ú_handle_itemst   s    ÿþýÿr    c              	   C   s(   t t ttttttdœ}|| j | j|ƒS )a'   Handle a tree using the specified rule.

    Parameters
    ----------
    tree : lark.tree.Tree
        Tree to be converted to an ObserverExpression.
    notify : bool
        True if the final target should notify, else False.

    Returns
    -------
    expression: ObserverExpression
    )ZseriesZseries_terminalÚparallelZparallel_terminalr   r   r   r   )r   r   r   r   r    r   r   Úchildren)Útreer   Úhandlersr
   r
   r   r   �   s    ø
r   )Úmaxsizec              
   C   sR   zt  | ¡}W n6 tjyD } ztd| ›�ƒ|‚W Y d}~n
d}~0 0 t|dd�S )a-   Top-level function for parsing user's text to an ObserverExpression.

    Parameters
    ----------
    text : str
        Text to be parsed.

    Returns
    -------
    expression : ObserverExpression

    Raises
    ------
    ValueError
        If the text is not a valid observe expression.
    zInvalid expression: NTr   )Ú_LARK_PARSERÚparser   Z	LarkErrorr   r   )Útextr#   Zparser_exceptionr
   r
   r   r'   ¬   s
    &r'   c                 C   s   t  t| ƒ¡S )zÄ Compile a mini-language string to a list of ObserverGraph objects.

    Parameters
    ----------
    text : str
        Text to be parsed.

    Returns
    -------
    list of ObserverGraph
    )r   Zcompile_exprr'   )r(   r
   r
   r   Úcompile_strÆ   s    r)   )Ú	functoolsr   Ztraits.observationr   Ztraits.observation.expressionZobservationZ
expressionr   ZLark_StandAloner&   r   r   r   r   r   r    r   Z"_OBSERVER_EXPRESSION_CACHE_MAXSIZEr'   r)   r
   r
   r
   r   Ú<module>   s   


