
    Pi#                       d Z 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mZ ddlmZmZmZmZ ddlmZmZmZ erddlmZ 	 	 d,d-dZ G d d          Z G d de          Z G d de          Z ed          Z  G d de          Z! G d de!          Z" G d de          Z#d.d/d'Z$	 	 d.d0d)Z%dd*lm&Z&m'Z' d+S )1a  

NOTE: PyParsing setResultName/__call__ provides a very similar solution to this
I didn't realise at the time of writing and I will remove a
lot of this code at some point

Utility classes for creating an abstract-syntax tree out with pyparsing actions

Lets you label and group parts of parser production rules

For example:

# [5] BaseDecl ::= 'BASE' IRIREF
BaseDecl = Comp('Base', Keyword('BASE') + Param('iri',IRIREF))

After parsing, this gives you back an CompValue object,
which is a dict/object with the parameters specified.
So you can access the parameters are attributes or as keys:

baseDecl.iri

Comp lets you set an evalFn that is bound to the eval method of
the resulting CompValue
    )annotations)OrderedDict)
MethodType)	TYPE_CHECKINGAnyCallableListMappingOptionalTupleTypeVarUnion)ParserElementParseResultsTokenConverteroriginalTextFor)BNode
IdentifierVariable)FrozenBindingsFctxr   valr   	variablesboolerrorsreturnc                *    t          |t                    r|                               S t          |t                    rt	          d|z            t          |t
                    r fd|D             S t          |t          t          f          r=                     |          }t          |t                    rs|||S r|S t          t          |t                    r+t          |          dk    rt           |d                   S |S )a  Utility function for evaluating something...

    Variables will be looked up in the context
    Normally, non-bound vars is an error,
    set variables=True to return unbound vars

    Normally, an error raises the error,
    set errors=True to return error
    z$What do I do with this CompValue? %sc                4    g | ]}t          |          S  )value).0xr   r   r   s     u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/rdflib/plugins/sparql/parserutils.py
<listcomp>zvalue.<locals>.<listcomp>M   s'    >>>Qc1i00>>>    N   r   )
isinstanceExpreval	CompValue	Exceptionlistr   r   getSPARQLErrorNotBoundErrorr   lenr    )r   r   r   r   rs   ` `` r#   r    r    7   s!     #t xx}}	C	#	# >DEEE	C		 >>>>>>#>>>>	C%*	+	+ GGCLLa%% 	f 	G=H  	 J	C	&	& 3s88q==S#a&)V444
r%   c                  "    e Zd ZdZddZdd
ZdS )
ParamValuezq
    The result of parsing a Param
    This just keeps the name/value
    All cleverness is in the CompValue
    namestr	tokenListUnion[List[Any], ParseResults]isListr   c                    || _         || _        t          |t          t          f          rt          |          dk    r|d         }|| _        d S )Nr&   r   )r8   r4   r'   r,   r   r0   r6   )selfr4   r6   r8   s       r#   __init__zParamValue.__init__i   sM     	i$!566 	%3y>>Q;N;N!!I"r%   r   c                (    d| j         d| j        dS )NzParam(z, ))r4   r6   r:   s    r#   __str__zParamValue.__str__s   s     "&)))T^^^<<r%   N)r4   r5   r6   r7   r8   r   r   r5   )__name__
__module____qualname____doc__r;   r?   r   r%   r#   r3   r3   b   sF         # # # #= = = = = =r%   r3   c                  $    e Zd ZdZdddZddZdS )Paramz
    A pyparsing token for labelling a part of the parse-tree
    if isList is true repeat occurrences of ParamList have
    their values merged in a list
    Fr4   r5   r8   r   c                    || _         t          j        | |           |                     |           |                     | j                   d S N)r8   r   r;   setNameaddParseAction
postParse2)r:   r4   exprr8   s       r#   r;   zParam.__init__~   sJ    d+++TDO,,,,,r%   r6   r7   r   r3   c                8    t          | j        || j                  S rH   )r3   r4   r8   )r:   r6   s     r#   rK   zParam.postParse2   s    $)Y<<<r%   N)F)r4   r5   r8   r   )r6   r7   r   r3   )rA   rB   rC   rD   r;   rK   r   r%   r#   rF   rF   w   sK         - - - - -= = = = = =r%   rF   c                      e Zd ZdZddZdS )	ParamListz1
    A shortcut for a Param with isList=True
    r4   r5   c                @    t                               | ||d           d S )NT)rF   r;   r:   r4   rL   s      r#   r;   zParamList.__init__   s     tT4.....r%   Nr4   r5   )rA   rB   rC   rD   r;   r   r%   r#   rO   rO      s2         / / / / / /r%   rO   _ValTc                  f    e Zd ZdZddZddZddZddZ	 dddZd Z	dd dZ
d!dZerd"dZdS dS )#r*   zn
    The result of parsing a Comp
    Any included Params are available as Dict keys
    or as attributes
    r4   r5   c                f    t          j        |            || _        |                     |           d S rH   )r   r;   r4   update)r:   r4   valuess      r#   r;   zCompValue.__init__   s2    T"""	Fr%   r   c                &    t          | j        fi | S rH   )r*   r4   r>   s    r#   clonezCompValue.clone   s    ++d+++r%   c                @    | j         dz   t          j        |           z   S N_)r4   r   r?   r>   s    r#   r?   zCompValue.__str__   s    y3!4T!:!:::r%   c                L    | j         dz   t                              |           z   S r[   )r4   dict__repr__r>   s    r#   r_   zCompValue.__repr__   s    y3t!4!444r%   Fr   rS   r   r   r   Union[_ValT, Any]c                @    | j         t          | j         ||          S |S rH   )r   r    )r:   r   r   r   s       r#   _valuezCompValue._value   s%     83	222Jr%   c                R    |                      t          j        | |                    S rH   )rb   r   __getitem__r:   as     r#   rd   zCompValue.__getitem__   s!    {{;24;;<<<r%   c                X    |                      t          j        | ||          ||          S rH   )rb   r   r-   )r:   rf   r   r   s       r#   r-   zCompValue.get   s&    {{;?4A66	6JJJr%   rf   r   c                Z    |dv rt                      	 | |         S # t          $ r Y d S w xY w)N)_OrderedDict__root_OrderedDict__end)AttributeErrorKeyErrorre   s     r#   __getattr__zCompValue.__getattr__   sJ    ;;; """	7N 	 	 	44	s    
**_CompValue__name_CompValue__valueNonec                    d S rH   r   )r:   rn   ro   s      r#   __setattr__zCompValue.__setattr__   s      r%   NrR   )r   r*   r@   FF)r   rS   r   r   r   r   r   r`   )r   r   r   r   )rf   r5   r   r   )rn   r5   ro   r   r   rp   )rA   rB   rC   rD   r;   rY   r?   r_   rb   rd   r-   rm   r   rr   r   r%   r#   r*   r*      s            
, , , ,; ; ; ;5 5 5 5 CH    = = =
K K K K K     FEEEEEEF Fr%   r*   c                  4     e Zd ZdZ	 dd fdZi fddZ xZS )r(   z)
    A CompValue that is evaluatable
    Nr4   r5   evalfn#Optional[Callable[[Any, Any], Any]]c                     t          t          |           j        |fi | d | _        |rt	          ||           | _        d S d S rH   )superr(   r;   _evalfnr   )r:   r4   ru   rW   	__class__s       r#   r;   zExpr.__init__   sX     	#dD"4226222 	4%fd33DLLL	4 	4r%   r   r   r   Union[SPARQLError, Any]c                    	 || _         |                     |          d | _         S # t          $ r}|cY d }~d | _         S d }~ww xY w# d | _         w xY wrH   )r   ry   r.   )r:   r   es      r#   r)   z	Expr.eval   sq    	ADDH<<$$ DHH  	 	 	HHHHHDHH	 DHOOOOs(   % 
A=AA AA 	ArH   )r4   r5   ru   rv   )r   r   r   r{   )rA   rB   rC   rD   r;   r)   __classcell__)rz   s   @r#   r(   r(      so          7;
4 
4 
4 
4 
4 
4 
4 !         r%   r(   c                  *    e Zd ZdZddZddZddZdS )Compz
    A pyparsing token for grouping together things with a label
    Any sub-tokens that are not Params will be ignored.

    Returns CompValue / Expr objects - depending on whether evalFn is set.
    r4   r5   rL   r   c                v    || _         t          j        | |           |                     |           d | _        d S rH   )rL   r   r;   rI   ru   rQ   s      r#   r;   zComp.__init__   s9    	d+++T;?r%   instringlocintr6   r   r   Union[Expr, CompValue]c                   | j         r/t          | j                  }t          | j         |          |_        nYt          | j                  }| j        dk    r:t          | j                  }|                    |          d         d         }||d<   |D ]f}t          |t                    rO|j        r9|j        |vr
g ||j        <   ||j                                     |j                   W|j        ||j        <   g|S )NServiceGraphPatternr   service_string)ru   r(   r4   r   ry   r*   r   rL   searchStringr'   r3   r8   appendr6   )r:   r   r   r6   ressgpr   ts           r#   	postParsezComp.postParse   s     ; 	7ty//C$T[#66CKKDI&&Cy111 &di00!$!1!1(!;!;A!>q!A(6$% 	. 	.A!Z(( .8 .vS((&(AFK&&q{3333"#+CK 
r%   ru   Callable[[Any, Any], Any]c                    || _         | S rH   )ru   )r:   ru   s     r#   	setEvalFnzComp.setEvalFn  s    r%   N)r4   r5   rL   r   )r   r5   r   r   r6   r   r   r   )ru   r   r   r   )rA   rB   rC   rD   r;   r   r   r   r%   r#   r   r      s^         @ @ @ @   <     r%   r    r   r   indentr5   depthr   c           	        g }|                                  D ])}|                    t          |||dz                        *t          |                                           D ]L\  }}|                    |d|z  d|d           |                    t          |||dz                        Md                    |          S )Nr&     - :
r   )asListr   _prettify_sub_parsetreesorteditemsjoin)r   r   r   outr}   kvs          r#   prettify_parsetreer     s    CXXZZ B B

*1feai@@AAAAqwwyy!! B B1

FFD5LLL!!!<===

*1feai@@AAAA773<<r%   @Union[Identifier, CompValue, set, list, dict, Tuple, bool, None]c           	     &   g }t          | t                    r|                    |d|z  d| j        d           |                                 D ]O\  }}|                    |d|dz   z  d|d           |                    t          |||dz                        Pnt          | t                    re|                                 D ]O\  }}|                    |d|dz   z  d|d           |                    t          |||dz                        Pnbt          | t                    r-| D ])}|                    t          |||dz                        *n |                    |d|z  d| d           d                    |          S )	Nr   z> r   r&   r      
r   )	r'   r*   r   r4   r   r   r^   r,   r   )r   r   r   r   r   r   r}   s          r#   r   r     s   
 C!Y =

FFD5LLL!&&&ABBBGGII 	F 	FDAqJJ	0B0B0BAAAFGGGJJ.q&%!)DDEEEE	F 
At		 =GGII 	F 	FDAqJJ	0B0B0BAAAFGGGJJ.q&%!)DDEEEE	F 
At		 = 	F 	FAJJ.q&%!)DDEEEE	F 	

664%<<<;<<<773<<r%   )r/   r.   Nrs   )
r   r   r   r   r   r   r   r   r   r   )r   r   )r   r   r   r5   r   r   r   r5   )r   r   r   r5   r   r   r   r5   )(rD   
__future__r   collectionsr   typesr   typingr   r   r   r	   r
   r   r   r   r   	pyparsingr   r   r   r   rdflib.termr   r   r   rdflib.plugins.sparql.sparqlr   r    r3   rF   rO   rS   r*   r(   r   r   r   r/   r.   r   r%   r#   <module>r      s   2 # " " " " " # # # # # #      
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 S R R R R R R R R R R R 3 3 3 3 3 3 3 3 3 3 <;;;;;; 	( ( ( ( (V= = = = = = = =*= = = = =N = = ="/ / / / / / / / 	1F 1F 1F 1F 1F 1F 1F 1Fh    9   8. . . . .> . . .b         0 D C C C C C C C C Cr%   