
    4Ysh;)                        d Z ddlZddlZddlmZ ddlmZ ddlm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 g d
ZdZdZdez   dz   Z ej0                  d      Z ej0                  d      Z ej0                  d      Z ej0                  d      Z ej0                  e      Z ej0                  d      Z ej0                  eez         ZdZ dZ! G d de"      Z#ddddddZ$ ej0                  d      Z% ej0                  d      Z& ej0                  d       Z'd! Z( ej0                  d"      Z)d# Z* G d$ d%e"      Z+ G d& d'e"      Z, G d( d)e      Z-y)*zWN-Triples Parser
License: GPL 2, W3C, BSD, or MIT
Author: Sean B. Palmer, inamidst.com
    N)URIRef)BNode)Literal)decodeUnicodeEscape)ParserError)Parser)StringIO
TextIOBaseBytesIO)unquoteuriquoteW3CNTriplesParserNTGraphSinkNTParserz<([^:]+:[^\s"<>]*)>z"([^"\\]*(?:\\.[^"\\]*)*)"z&(?:@([a-zA-Z]+(?:-[a-zA-Z0-9]+)*)|\^\^z)?z([^\r\n]*)(?:\r\n|\r|\n)z[ \t]*z[ \t]+z[ \t]*\.[ \t]*(#.*)?z3_:([A-Za-z0-9_:]([-A-Za-z0-9_:\.]*[-A-Za-z0-9_:])?)i   Fc                       e Zd Zd Zd Zy)	DummySinkc                     d| _         y )Nr   )length)selfs    /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/parsers/ntriples.py__init__zDummySink.__init__(   s	        c                 H    | xj                   dz  c_         t        |||       y )N   )r   printr   spos       r   triplezDummySink.triple+   s    qaAr   N)__name__
__module____qualname__r   r     r   r   r   r   '   s    r   r   	
"\)tnrr(   r)   z([\x20\x21\x23-\x5B\x5D-\x7E]+)z\\(t|n|r|"|\\)z!\\u([0-9A-F]{4})|\\U([0-9A-F]{8})c                    t         s0t        | t              rt        |       } | S | j	                  d      } | S g }| rAt
        j                  |       }|r4| |j                         d } |j                  |j                  d             Nt        j                  |       }|r-| dd } |j                  t        |j                  d                t        j                  |       }|rd| |j                         d } |j                         \  }}t        |xs |d      }|dkD  rt        d|z        |j                  t!        |             n3| j#                  d      rt        d	| dd
 z        t        d| d   z        | rAdj%                  |      S )zUnquote an N-Triples string.zunicode-escapeNr         i zDisallowed codepoint: %08Xr)   zIllegal escape at: %s...
   zIllegal literal character: %rr    )validate
isinstancestrr   decoder_safematchendappendgroupr_quotquot	r_uniquotgroupsint
ParseErrorchr
startswithjoin)r   resultmuU	codepoints         r   r   r   6   sc   a#A&A  )*AQAaeegiLaggaj)QAabEd1771:./"AaeegiLxxz1QO	x'$%AI%MNNc)n-d# !;af!DEE !@1Q4!GHH1 2 wwvr   z([\x80-\xFF])c                 @    t         s| S t        j                  d |       S )Nc                 <    dt        | j                  d            z  S )Nz%%%02Xr   )ordr:   )rE   s    r   <lambda>zuriquote.<locals>.<lambda>d   s    hQWWQZ&@ r   )r2   r_hibytesub)uris    r   r   r   `   s    
||@#FFr   c                   n    e Zd ZdZdZddZddZd Zd ZddZ	d	 Z
d
 ZddZd ZddZd ZddZd Zy)r   a  An N-Triples Parser.
    This is a legacy-style Triples parser for NTriples provided by W3C
    Usage::

          p = NTriplesParser(sink=MySink())
          sink = p.parse(f) # file; use parsestring for a string

    To define a context in which blank node identifiers refer to the same blank node
    across instances of NTriplesParser, pass the same dict as `bnode_context` to each
    instance. By default, a new blank node context is created for each instance of
    `NTriplesParser`.
    )
_bnode_idssinkbufferfilelineNc                     ||| _         ni | _         ||| _        nt               | _        d | _        d | _        d| _        y )Nr1   )rQ   rR   r   rS   rT   rU   )r   rR   bnode_contexts      r   r   zW3CNTriplesParser.__init__w   sA    $+DO DODI!DI		r   c                    t        |d      st        d      t        |d      s't        |d      s t        j                  d      |      }|| _        d| _        	 | j                         | _        | j                  	 | j                  S 	 | j                  |       B# t        $ r% t        dj                  | j                              w xY w)	a  
        Parse f as an N-Triples file.

        :type f: :term:`file object`
        :param f: the N-Triples source
        :type bnode_context: `dict`, optional
        :param bnode_context: a dict mapping blank node identifiers (e.g., ``a`` in ``_:a``)
                              to `~rdflib.term.BNode` instances. An empty dict can be
                              passed in to define a distinct context for a given call to
                              `parse`.
        readz)Item to parse must be a file-like object.encoding
charbufferutf-8r1   )rW   zInvalid line: {})hasattrr@   codecs	getreaderrT   rS   readlinerU   	parselineformatrR   )r   frW   s      r   parsezW3CNTriplesParser.parse   s     q&!HIIq*%ga.F)  )!,A	DIyy 
 yy	G];   G !3!:!:499!EFFGs   	B .C
c                    t        |t        t        t        f      st	        d      t        |t        t        f      r% t        j                  d      t        |            }nt        |      } | j                  |fi | y)zParse s as an N-Triples string.z(Item to parse must be a string instance.r\   N)
r3   r4   bytes	bytearrayr@   r^   r_   r   r	   rd   )r   r   kwargsrc   s       r   parsestringzW3CNTriplesParser.parsestring   se    !c5)45GHHa%+,)  )'!*5AA

1r   c                    | j                   s)| j                  j                  t              }|sy|| _         	 t        j                  | j                         }|r3| j                   |j                         d | _         |j                  d      S | j                  j                  t              }|s | j                   j                         s|dz  }n|sy| xj                   |z  c_         )z+Read an N-Triples line from buffered input.Nr   r&   )	rS   rT   rY   bufsizr_liner7   r8   r:   isspace)r   rS   rE   s      r   r`   zW3CNTriplesParser.readline   s     {{YY^^F+F DKT[[)A"kk!%%')4wwqz!/dkk&9&9&;dNFv% r   c                    | j                  t               | j                  r| j                  j                  d      ry | j	                  |      }| j                  t
               | j                         }| j                  t
               | j                  |      }| j                  t               | j                  r$t        dj                  | j                              | j                  j                  |||       y )N#zTrailing garbage: {})eatr_wspacerU   rB   subject	r_wspaces	predicateobjectr_tailr@   rb   rR   r    )r   rW   rr   rt   object_s        r   ra   zW3CNTriplesParser.parseline   s    		dii2237,,}-NN$	++m,993::499EFF		)W5r   c                 8    | j                   j                  |      S N)rU   rB   )r   tokens     r   peekzW3CNTriplesParser.peek   s    yy##E**r   c                     |j                  | j                        }|s%t        d|j                  d| j                        | j                  |j	                         d  | _        |S )NzFailed to eat z at )r7   rU   r@   patternr8   )r   r}   rE   s      r   rp   zW3CNTriplesParser.eat   sN    MM$))$ $))TUUIIaeegi(	r   c                 f    | j                         xs | j                  |      }|st        d      |S )Nz Subject must be uriref or nodeID)urirefnodeidr@   )r   rW   subjs      r   rr   zW3CNTriplesParser.subject   s/    {{}:M :?@@r   c                 @    | j                         }|st        d      |S )NzPredicate must be uriref)r   r@   )r   preds     r   rt   zW3CNTriplesParser.predicate   s!    {{}788r   c                     | j                         xs# | j                  |      xs | j                         }|du rt        d      |S )NFzUnrecognised object type)r   r   literalr@   )r   rW   objts      r   ru   zW3CNTriplesParser.object   s>    {{}LM :Ldlln5=788r   c                     | j                  d      rE| j                  t              j                  d      }t	        |      }t        |      }t        |      S y)N<r   F)r{   rp   r_urirefr:   r   r   URI)r   rO   s     r   r   zW3CNTriplesParser.uriref   sD    99S>((8$**1-C#,C3-Cs8Or   c                     | j                  d      rb|| j                  }| j                  t              j	                  d      }|j                  |d       }|t        |      S t               }|||<   |S y)N_r   F)r{   rQ   rp   r_nodeidr:   getbNode)r   rW   bnode_idnew_idbnodes        r   r   zW3CNTriplesParser.nodeid  sr    99S>$ $xx)//2H"&&x6F!V}$ */h'r   c                    | j                  d      r{| j                  t              j                         \  }}}|r|}nd }|r"t	        |      }t        |      }t        |      }nd }|r|rt        d      t	        |      }t        |||      S y)Nr(   z)Can't have both a language and a datatypeF)	r{   rp   	r_literalr>   r   r   r   r@   r   )r   litlangdtypes       r   r   zW3CNTriplesParser.literal  s    99S>#xx	299;Cu E
 !LMM#,C3e,,r   )NNry   )r!   r"   r#   __doc__	__slots__r   rd   ri   r`   ra   r{   rp   rr   rt   ru   r   r   r   r$   r   r   r   r   g   sQ     AI@ &06$+$r   r   c                       e Zd ZdZd Zd Zy)r   gc                     || _         y ry   r   )r   graphs     r   r   zNTGraphSink.__init__*  s	    r   c                 @    | j                   j                  |||f       y ry   )r   addr   s       r   r    zNTGraphSink.triple-  s    

Aq!9r   N)r!   r"   r#   r   r   r    r$   r   r   r   r   '  s    Ir   r   c                   $    e Zd ZdZdZed        Zy)r   zyparser for the ntriples format, often stored with the .nt extension

    See http://www.w3.org/TR/rdf-testcases/#ntriplesr$   c                    |j                         }|s>|j                         }t        |t              r|}n t	        j
                  d      |      }t        t        |            } |j                  |fi | |j                          y)a9  
        Parse the NT format

        :type source: `rdflib.parser.InputSource`
        :param source: the source of NT-formatted data
        :type sink: `rdflib.graph.Graph`
        :param sink: where to send parsed triples
        :param kwargs: Additional arguments to pass to `.NTriplesParser.parse`
        r\   N)
getCharacterStreamgetByteStreamr3   r
   r^   r_   r   r   rd   close)clssourcerR   rh   rc   bparsers          r   rd   zNTParser.parse8  sv     %%'$$&A!Z( .F$$W-a0";t#45Q!&!		r   N)r!   r"   r#   r   r   classmethodrd   r$   r   r   r   r   1  s     8 I r   r   ).r   rer^   rdflib.termr   r   r   r   r   rdflib.compatr   rdflib.exceptionsr   r@   rdflib.parserr   ior	   r
   r   __all__r   r   litinfocompilerl   rq   rs   rv   r   r   r   rk   r2   ru   r   r<   r6   r;   r=   r   rM   r   r   r   r   r$   r   r   <module>r      sc   
  % &  - 7   , ,
Q	
'
3f
<u
D	/	02::i BJJy!		+	,2::f2::LMBJJw()		  4c>	6	7	%	&BJJ;<	$N 2::&'G} }@& v r   