
    01i$                       S r SSKJr  SSKJr  SSKJr  SSKJrJ	r	J
r
JrJrJrJrJrJr  SSKJrJrJr  SSKJrJrJr  SS	KJr  \(       a  SS
KJr    S         SS jjr " S S5      r " S S\5      r " S S\5      r \" S5      r! " S S\5      r" " S S\"5      r# " S S\5      r$SS S jjr%  S       S!S jjr&SSKJ'r'J(r(  g)"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)BNode
IdentifierVariable   )original_text_for)FrozenBindingsc           	     (   [        U[        5      (       a  UR                  U 5      $ [        U[        5      (       a  [	        SU-  5      e[        U[
        5      (       a  U Vs/ s H  n[        XX#5      PM     sn$ [        U[        [        45      (       aC  U R                  U5      n[        U[        5      (       a	  U(       d  UeUb  U$ U(       a  U$ [        e[        U[        5      (       a  [        U5      S:X  a  [        XS   X#5      $ U$ s  snf )zUtility 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? %sr   r   )
isinstanceExpreval	CompValue	Exceptionlistvaluer   r   getSPARQLErrorNotBoundErrorr   len)ctxval	variableserrorsxrs         [/home/james-whalen/.local/lib/python3.13/site-packages/rdflib/plugins/sparql/parserutils.pyr   r   9   s      #txx}	C	#	#>DEE	C		:=>#Qci0#>>	C%*	+	+GGCLa%%fG=H J	C	&	&3s8q=Sa&)44
% ?s   #Dc                  8    \ rS rSrSr      SS jrSS jrSrg)	
ParamValued   za
The result of parsing a Param
This just keeps the name/value
All cleverness is in the CompValue
c                    X0l         Xl        [        U[        [        45      (       a  [        U5      S:X  a  US   nX l        g )Nr   r   )isListnamer   r   r   r#   	tokenList)selfr0   r1   r/   s       r*   __init__ParamValue.__init__k   s9     	i$!5663y>Q;N!!I"    c                @    SU R                   < SU R                  < S3$ )NzParam(z, ))r0   r1   r2   s    r*   __str__ParamValue.__str__u   s    "&))T^^<<r5   )r/   r0   r1   N)r0   strr1   Union[List[Any], ParseResults]r/   boolreturnr;   )__name__
__module____qualname____firstlineno____doc__r3   r9   __static_attributes__ r5   r*   r,   r,   d   s)    ##$B#LP#=r5   r,   c                  0    \ rS rSrSrSSS jjrS	S jrSrg)
Paramy   z
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                    X0l         [        R                  " X5        U R                  U5        U R	                  U R
                  5        g N)r/   r   r3   set_nameadd_parse_action
postParse2)r2   r0   exprr/   s       r*   r3   Param.__init__   s3    +ddoo.r5   c                B    [        U R                  XR                  5      $ rK   )r,   r0   r/   )r2   r1   s     r*   rN   Param.postParse2   s    $))Y<<r5   )r/   N)F)r0   r;   r/   r=   )r1   r<   r?   r,   )r@   rA   rB   rC   rD   r3   rN   rE   rF   r5   r*   rH   rH   y   s    /=r5   rH   c                  "    \ rS rSrSrSS jrSrg)	ParamList   z)
A shortcut for a Param with isList=True
c                2    [         R                  XUS5        g )NT)rH   r3   r2   r0   rO   s      r*   r3   ParamList.__init__   s    t4.r5   rF   Nr0   r;   )r@   rA   rB   rC   rD   r3   rE   rF   r5   r*   rT   rT      s    /r5   rT   _ValTc                      \ rS rSrSrSS jrSS jrSS jrSS jr S       SS jjr	S r
SSS	 jjrSS
 jr\(       a  SS jrSrgSrg)r      z^
The result of parsing a Comp
Any included Params are available as Dict keys
or as attributes
c                ^    [         R                  " U 5        Xl        U R                  U5        g rK   )r   r3   r0   update)r2   r0   valuess      r*   r3   CompValue.__init__   s!    T"	Fr5   c                .    [        U R                  40 U D6$ rK   )r   r0   r8   s    r*   cloneCompValue.clone   s    +d++r5   c                N    U R                   S-   [        R                  " U 5      -   $ N_)r0   r   r9   r8   s    r*   r9   CompValue.__str__   s     yy3!4!4T!:::r5   c                L    U R                   S-   [        R                  U 5      -   $ re   )r0   dict__repr__r8   s    r*   rj   CompValue.__repr__   s    yy3t!444r5   c                L    U R                   b  [        U R                   X5      $ U$ rK   )r$   r   )r2   r%   r&   r'   s       r*   _valueCompValue._value   s$     88322Jr5   c                L    U R                  [        R                  " X5      5      $ rK   )rm   r   __getitem__r2   as     r*   rp   CompValue.__getitem__   s    {{;224;<<r5   c                P    U R                  [        R                  " XU5      X#5      $ rK   )rm   r   r    )r2   rr   r&   r'   s       r*   r    CompValue.get   s    {{;??4A6	JJr5   c                L    US;   a
  [        5       e X   $ ! [         a     g f = f)N)_OrderedDict__root_OrderedDict__end)AttributeErrorKeyErrorrq   s     r*   __getattr__CompValue.__getattr__   s2    ;; ""	7N 		s    
##c                    g rK   rF   )r2   _CompValue__name_CompValue__values      r*   __setattr__CompValue.__setattr__   s    #r5   )r0   NrY   )r?   r   r>   FF)r%   rZ   r&   r=   r'   r=   r?   zUnion[_ValT, Any])r&   r=   r'   r=   )rr   r;   r?   r   )r~   r;   r   r   r?   None)r@   rA   rB   rC   rD   r3   rb   r9   rj   rm   rp   r    r{   r   r   rE   rF   r5   r*   r   r      sd    
,;5 CH%);?	=
K E r5   r   c                  L   ^  \ rS rSrSr S   SU 4S jjjr0 4SS jjrSrU =r$ )	r      z!
A CompValue that is evaluatable
c                n   > [         [        U ]
  " U40 UD6  S U l        U(       a  [	        X 5      U l        g g rK   )superr   r3   _evalfnr   )r2   r0   evalfnr_   	__class__s       r*   r3   Expr.__init__   s4     	dD"4262%f3DL r5   c                     Xl         U R                  U5      S U l         $ ! [         a  nUs S nAS U l         $ S nAff = f! S U l         f = frK   )r$   r   r!   )r2   r$   es      r*   r   	Expr.eval   sI    	ADH<<$ DH  	HDH	 DHs$     
=8=A  =A   	A	)r   r$   rK   )r0   r;   r   z#Optional[Callable[[Any, Any], Any]])r$   r   r?   zUnion[SPARQLError, Any])	r@   rA   rB   rC   rD   r3   r   rE   __classcell__)r   s   @r*   r   r      s8     7;
4
4 4
4 
4 !  r5   r   c                  F    \ rS rSrSrSS jr        S	S jrS
S jrSrg)Comp   z
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                l    X l         [        R                  " X5        U R                  U5        S U l        g rK   )rO   r   r3   rL   r   rW   s      r*   r3   Comp.__init__   s(    	+d;?r5   c                T   U R                   (       a1  [        U R                  5      n[        U R                   U5      Ul        OU[        U R                  5      nU R                  S:X  a0  [        U R                  5      nUR                  U5      S   S   nXdS'   U H  n[        U[        5      (       d  M  UR                  (       aG  UR                  U;  a  / XGR                  '   XGR                     R                  UR                  5        Mr  UR                  XGR                  '   M     U$ )NServiceGraphPatternr   service_string)r   r   r0   r   r   r   r   rO   search_stringr   r,   r/   appendr1   )r2   instringlocr1   ressgpr   ts           r*   	postParseComp.postParse   s     ;;tyy/C$T[[#6CKDII&Cyy11 (		2!$!2!28!<Q!?!B(6$%A!Z((88vvS(&(FFK&&q{{3"#++CK  
r5   c                    Xl         U $ rK   )r   )r2   r   s     r*   	setEvalFnComp.setEvalFn  s    r5   )r   rO   N)r0   r;   rO   r   )r   r;   r   intr1   r   r?   zUnion[Expr, CompValue])r   zCallable[[Any, Any], Any]r?   r   )	r@   rA   rB   rC   rD   r3   r   r   rE   rF   r5   r*   r   r      s7    @"%2>	<r5   r   c           	     P   / nU R                  5        H!  nUR                  [        XAUS-   5      5        M#     [        U R	                  5       5       HB  u  pVUR                  U< SU-  < SU< S35        UR                  [        XaUS-   5      5        MD     SR                  U5      $ )Nr     - :
 )as_listr   _prettify_sub_parsetreesorteditemsjoin)r   indentdepthoutr   kvs          r*   prettify_parsetreer     s    CYY[

*1eai@A qwwy!

FD5L!<=

*1eai@A " 773<r5   c           	        / n[        U [        5      (       a  UR                  U< SU-  < SU R                  < S35        U R	                  5        HE  u  pEUR                  U< SUS-   -  < SU< S35        UR                  [        XQUS-   5      5        MG     O[        U [        5      (       aZ  U R	                  5        HE  u  pEUR                  U< SUS-   -  < SU< S35        UR                  [        XQUS-   5      5        MG     O\[        U [        5      (       a(  U  H!  nUR                  [        XaUS-   5      5        M#     OUR                  U< SU-  < SU < S35        SR                  U5      $ )	Nr   z> r   r   r      
r   )	r   r   r   r0   r   r   ri   r   r   )r   r   r   r   r   r   r   s          r*   r   r   !  s$   
 C!Y

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

64%<;<773<r5   )r"   r!   Nr   )
r$   r   r%   r   r&   r=   r'   r=   r?   r   )r   r   )r   r   r   r;   r   r   r?   r;   )r   z@Union[Identifier, CompValue, set, list, dict, Tuple, bool, None]r   r;   r   r   r?   r;   ))rD   
__future__r   collectionsr   typesr   typingr   r   r   r	   r
   r   r   r   r   	pyparsingr   r   r   rdflib.termr   r   r   pyparsing_compatr   rdflib.plugins.sparql.sparqlr   r   r,   rH   rT   rZ   r   r   r   r   r   r"   r!   rF   r5   r*   <module>r      s  2 # # 
 
 
 B A 3 3 /; 	(	(	( ( 	(
 	(V= =*=N ="/ / 	1F 1Fh9 8.> .b G  		0 D Cr5   