
    i#                    |   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	 	 	 	 	 	 	 ddZ%ddlm&Z&m'Z' y) a  

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)FrozenBindingsc           	        t        |t              r|j                  |       S t        |t              rt	        d|z        t        |t
              r|D cg c]  }t        | |||       c}S t        |t        t        f      r3| j                  |      }t        |t              r|s|||S |r|S t        t        |t              rt        |      dk(  rt        | |d   ||      S |S c c}w )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? %s   r   )
isinstanceExpreval	CompValue	Exceptionlistvaluer   r   getSPARQLErrorNotBoundErrorr   len)ctxval	variableserrorsxrs         PC:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/plugins/sparql/parserutils.pyr   r   9   s    $ #txx}	C	#>DEE	C	:=>#Qc1i0#>>	C%*	+GGCLa%fG=H J	C	&3s8q=S#a&)V44
% ?s   C&c                  ,    e Zd ZdZ	 	 	 	 	 	 ddZddZy)
ParamValuezq
    The result of parsing a Param
    This just keeps the name/value
    All cleverness is in the CompValue
    c                    || _         || _        t        |t        t        f      rt        |      dk(  r|d   }|| _        y )Nr   r   )isListnamer   r   r   r#   	tokenList)selfr/   r0   r.   s       r*   __init__zParamValue.__init__m   s<     	i$!563y>Q;N!!I"    c                <    d| j                   d| j                  dS )NzParam(z, ))r/   r0   r1   s    r*   __str__zParamValue.__str__w   s    "&))T^^<<r3   N)r/   strr0   Union[List[Any], ParseResults]r.   boolreturnr8   )__name__
__module____qualname____doc__r2   r7    r3   r*   r,   r,   f   s)    ##$B#LP#=r3   r,   c                  "    e Zd ZdZdddZddZy)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
    c                    || _         t        j                  | |       | j                  |       | j	                  | j
                         y N)r.   r   r2   setNameaddParseAction
postParse2)r1   r/   exprr.   s       r*   r2   zParam.__init__   s7    d+TDOO,r3   c                D    t        | j                  || j                        S rE   )r,   r/   r.   )r1   r0   s     r*   rH   zParam.postParse2   s    $))Y<<r3   N)F)r/   r8   r.   r:   )r0   r9   r<   r,   )r=   r>   r?   r@   r2   rH   rA   r3   r*   rC   rC   {   s    -=r3   rC   c                      e Zd ZdZddZy)	ParamListz1
    A shortcut for a Param with isList=True
    c                4    t         j                  | ||d       y )NT)rC   r2   r1   r/   rI   s      r*   r2   zParamList.__init__   s    tT4.r3   Nr/   r8   )r=   r>   r?   r@   r2   rA   r3   r*   rL   rL      s    /r3   rL   _ValTc                  p    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yy)r   zo
    The result of parsing a Comp
    Any included Params are available as Dict keys
    or as attributes

    c                ^    t        j                  |        || _        | j                  |       y rE   )r   r2   r/   update)r1   r/   valuess      r*   r2   zCompValue.__init__   s#    T"	Fr3   c                .    t        | j                  fi | S rE   )r   r/   r6   s    r*   clonezCompValue.clone   s    +d++r3   c                L    | j                   dz   t        j                  |       z   S N_)r/   r   r7   r6   s    r*   r7   zCompValue.__str__   s     yy3!4!4T!:::r3   c                L    | j                   dz   t        j                  |       z   S rX   )r/   dict__repr__r6   s    r*   r\   zCompValue.__repr__   s    yy3t!444r3   c                L    | j                   t        | j                   ||      S |S rE   )r$   r   )r1   r%   r&   r'   s       r*   _valuezCompValue._value   s&     883	22Jr3   c                L    | j                  t        j                  | |            S rE   )r^   r   __getitem__r1   as     r*   r`   zCompValue.__getitem__   s    {{;224;<<r3   c                R    | j                  t        j                  | ||      ||      S rE   )r^   r   r    )r1   rb   r&   r'   s       r*   r    zCompValue.get   s"    {{;??4A6	6JJr3   c                H    |dv r
t               	 | |   S # t        $ r Y y w xY w)N)_OrderedDict__root_OrderedDict__end)AttributeErrorKeyErrorra   s     r*   __getattr__zCompValue.__getattr__   s4    ;; ""	7N 		s    	!!c                     y rE   rA   )r1   _CompValue__name_CompValue__values      r*   __setattr__zCompValue.__setattr__   s    #r3   NrO   )r<   r   r;   FF)r%   rP   r&   r:   r'   r:   r<   zUnion[_ValT, Any])r&   r:   r'   r:   )rb   r8   r<   r   )rk   r8   rl   r   r<   None)r=   r>   r?   r@   r2   rV   r7   r\   r^   r`   r    ri   r   rm   rA   r3   r*   r   r      sd    
,;5 CH%);?	=
K E r3   r   c                  :     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
    c                d    t        t        | 
  |fi | d | _        |rt	        ||       | _        y y rE   )superr   r2   _evalfnr   )r1   r/   evalfnrT   	__class__s       r*   r2   zExpr.__init__   s6     	dD"4262%fd3DL r3   c                    	 || _         | j                  |      d | _         S # t        $ r}|cY d }~d | _         S d }~ww xY w# d | _         w xY wrE   )r$   rs   r!   )r1   r$   es      r*   r   z	Expr.eval   sK    	ADDH<<$ DH  	HDH	 DHs$   ! 	=8=A  =A   	A	rE   )r/   r8   rt   z#Optional[Callable[[Any, Any], Any]])r$   r   r<   zUnion[SPARQLError, Any])r=   r>   r?   r@   r2   r   __classcell__)ru   s   @r*   r   r      s3     7;
4
4 4
4 !  r3   r   c                  8    e Zd ZdZddZ	 	 	 	 	 	 	 	 ddZddZy)	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.
    c                n    || _         t        j                  | |       | j                  |       d | _        y rE   )rI   r   r2   rF   rt   rN   s      r*   r2   zComp.__init__   s,    	d+T;?r3   c                0   | j                   r1t        | j                        }t        | j                   |      |_        nUt        | j                        }| j                  dk(  r1t        | j                        }|j                  |      d   d   }||d<   |D ]~  }t        |t              s|j                  rF|j                  |vrg ||j                  <   ||j                     j                  |j                         f|j                  ||j                  <    |S )NServiceGraphPatternr   service_string)rt   r   r/   r   rs   r   r   rI   searchStringr   r,   r.   appendr0   )r1   instringlocr0   ressgpr~   ts           r*   	postParsezComp.postParse   s     ;;tyy/C$T[[#6CKDII&Cyy11 &dii0!$!1!1(!;A!>q!A(6$%A!Z(88vvS(&(AFFK&&q{{3"#++CK  
r3   c                    || _         | S rE   )rt   )r1   rt   s     r*   	setEvalFnzComp.setEvalFn  s    r3   N)r/   r8   rI   r   )r   r8   r   intr0   r   r<   zUnion[Expr, CompValue])rt   zCallable[[Any, Any], Any]r<   rz   )r=   r>   r?   r@   r2   r   r   rA   r3   r*   rz   rz      s7    @"%2>	<r3   rz   c           	     H   g }| j                         D ]!  }|j                  t        |||dz                # t        | j	                               D ]@  \  }}|j                  |d|z  d|d       |j                  t        |||dz                B dj                  |      S )Nr     - :
 )asListr   _prettify_sub_parsetreesorteditemsjoin)r   indentdepthoutrw   kvs          r*   prettify_parsetreer     s    CXXZ

*1feai@A qwwy!1

FD5L!<=

*1feai@A " 773<r3   c           	        g }t        | t              r}|j                  |d|z  d| j                  d       | j	                         D ]C  \  }}|j                  |d|dz   z  d|d       |j                  t        |||dz                E nt        | t              rW| j	                         D ]C  \  }}|j                  |d|dz   z  d|d       |j                  t        |||dz                E nSt        | t              r'| D ]!  }|j                  t        |||dz                # n|j                  |d|z  d| d       dj                  |      S )	Nr   z> r   r   r      
r   )	r   r   r   r/   r   r   r[   r   r   )r   r   r   r   r   r   rw   s          r*   r   r   $  s%   
 C!Y

FD5L!&&ABGGIDAqJJ	0BAFGJJ.q&%!)DE  
At	GGIDAqJJ	0BAFGJJ.q&%!)DE  
At	AJJ.q&%!)DE  	

64%<;<773<r3   )r"   r!   Nrn   )
r$   r   r%   r   r&   r:   r'   r:   r<   r   )r   r   )r   r   r   r8   r   r   r<   r8   )r   z@Union[Identifier, CompValue, set, list, dict, Tuple, bool, None]r   r8   r   r   r<   r8   )(r@   
__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   r,   rC   rL   rP   r   r   rz   r   r   r"   r!   rA   r3   r*   <module>r      s
  6 # # 
 
 
 S R 3 3; 	*	*	* * 	*
 	*Z= =*=N ="/ / 	2F 2Fj9 8.> .b G  		0 D Cr3   