Source code

Download source archive ↓

src/rdflib/plugins/parsers/notation3.py

1"""
2notation3.py - Standalone Notation3 Parser
3Derived from CWM, the Closed World Machine
4
5Authors of the original suite:
6
7* Dan Connolly <@@>
8* Tim Berners-Lee <@@>
9* Yosi Scharf <@@>
10* Joseph M. Reagle Jr. <reagle@w3.org>
11* Rich Salz <rsalz@zolera.com>
12
13http://www.w3.org/2000/10/swap/notation3.py
14
15Copyright 2000-2007, World Wide Web Consortium.
16Copyright 2001, MIT.
17Copyright 2001, Zolera Systems Inc.
18
19License: W3C Software License
20http://www.w3.org/Consortium/Legal/copyright-software
21
22Modified by Sean B. Palmer
23Copyright 2007, Sean B. Palmer.
24
25Modified to work with rdflib by Gunnar Aastrand Grimnes
26Copyright 2010, Gunnar A. Grimnes
27
28"""
29
30from __future__ import annotations
31
32import codecs
33import os
34import re
35import sys
36
37# importing typing for `typing.List` because `List`` is used for something else
38import typing
39from decimal import Decimal
40from typing import (
41    IO,
42    TYPE_CHECKING,
43    Any,
44    Callable,
45    Dict,
46    Match,
47    MutableSequence,
48    NoReturn,
49    Optional,
50    Pattern,
51    Set,
52    Tuple,
53    TypeVar,
54    Union,
55)
56from uuid import uuid4
57
58from rdflib.compat import long_type
59from rdflib.exceptions import ParserError
60from rdflib.graph import Dataset, Graph, QuotedGraph
61from rdflib.term import (
62    _XSD_PFX,
63    BNode,
64    IdentifiedNode,
65    Identifier,
66    Literal,
67    Node,
68    URIRef,
69    Variable,
70    _unique_id,
71)
72
73__all__ = [
74    "BadSyntax",
75    "N3Parser",
76    "TurtleParser",
77    "splitFragP",
78    "join",
79    "base",
80    "runNamespace",
81    "uniqueURI",
82    "hexify",
83    "Formula",
84    "RDFSink",
85    "SinkParser",
86    "sfloat",
87]
88
89from rdflib.parser import Parser
90
91if TYPE_CHECKING:
92    from rdflib.parser import InputSource
93
94_AnyT = TypeVar("_AnyT")
95
96
97def splitFragP(uriref: str, punc: int = 0) -> Tuple[str, str]:
98    """Split a URI reference before the fragment
99
100    Punctuation is kept. e.g.
101
102    ```python
103    >>> splitFragP("abc#def")
104    ('abc', '#def')
105
106    >>> splitFragP("abcdef")
107    ('abcdef', '')
108
109    ```
110    """
111
112    i = uriref.rfind("#")
113    if i >= 0:
114        return uriref[:i], uriref[i:]
115    else:
116        return uriref, ""
117
118
119_StrT = TypeVar("_StrT", bound=str)
120
121
122def join(here: str, there: str) -> str:
123    """join an absolute URI and URI reference
124    (non-ascii characters are supported/doctested;
125    haven't checked the details of the IRI spec though)
126
127    `here` is assumed to be absolute.
128    `there` is URI reference.
129
130    ```python
131    >>> join('http://example/x/y/z', '../abc')
132    'http://example/x/abc'
133
134    ```
135
136    Raise ValueError if there uses relative path
137    syntax but here has no hierarchical path.
138
139    ```python
140    >>> join('mid:foo@example', '../foo') # doctest: +NORMALIZE_WHITESPACE
141    Traceback (most recent call last):
142        raise ValueError(here)
143    ValueError: Base <mid:foo@example> has no slash
144    after colon - with relative '../foo'.
145
146    >>> join('http://example/x/y/z', '')
147    'http://example/x/y/z'
148
149    >>> join('mid:foo@example', '#foo')
150    'mid:foo@example#foo'
151
152    ```
153
154    We grok IRIs
155
156    ```python
157    >>> len('Andr\\xe9')
158    5
159
160    >>> join('http://example.org/', '#Andr\\xe9')
161    'http://example.org/#Andr\\xe9'
162
163    ```
164    """
165
166    #    assert(here.find("#") < 0), \
167    #        "Base may not contain hash: '%s'" % here  # why must caller splitFrag?
168
169    slashl = there.find("/")
170    colonl = there.find(":")
171
172    # join(base, 'foo:/') -- absolute
173    if colonl >= 0 and (slashl < 0 or colonl < slashl):
174        return there
175
176    bcolonl = here.find(":")
177    assert bcolonl >= 0, (
178        "Base uri '%s' is not absolute" % here
179    )  # else it's not absolute
180
181    path, frag = splitFragP(there)
182    if not path:
183        return here + frag
184
185    # join('mid:foo@example', '../foo') bzzt
186    if here[bcolonl + 1 : bcolonl + 2] != "/":
187        raise ValueError(
188            "Base <%s> has no slash after "
189            "colon - with relative '%s'." % (here, there)
190        )
191
192    if here[bcolonl + 1 : bcolonl + 3] == "//":
193        bpath = here.find("/", bcolonl + 3)
194    else:
195        bpath = bcolonl + 1
196
197    # join('http://xyz', 'foo')
198    if bpath < 0:
199        bpath = len(here)
200        here = here + "/"
201
202    # join('http://xyz/', '//abc') => 'http://abc'
203    if there[:2] == "//":
204        return here[: bcolonl + 1] + there
205
206    # join('http://xyz/', '/abc') => 'http://xyz/abc'
207    if there[:1] == "/":
208        return here[:bpath] + there
209
210    slashr = here.rfind("/")
211
212    while 1:
213        if path[:2] == "./":
214            path = path[2:]
215        if path == ".":
216            path = ""
217        elif path[:3] == "../" or path == "..":
218            path = path[3:]
219            i = here.rfind("/", bpath, slashr)
220            if i >= 0:
221                here = here[: i + 1]
222                slashr = i
223        else:
224            break
225
226    return here[: slashr + 1] + path + frag
227
228
229def base() -> str:
230    """The base URI for this process - the Web equiv of cwd
231
232    Relative or absolute unix-standard filenames parsed relative to
233    this yield the URI of the file.
234    If we had a reliable way of getting a computer name,
235    we should put it in the hostname just to prevent ambiguity
236    """
237    # return "file://" + hostname + os.getcwd() + "/"
238    return "file://" + _fixslash(os.getcwd()) + "/"
239
240
241def _fixslash(s: str) -> str:
242    """Fix windowslike filename to unixlike - (#ifdef WINDOWS)"""
243    s = s.replace("\\", "/")
244    if s[0] != "/" and s[1] == ":":
245        s = s[2:]  # @@@ Hack when drive letter present
246    return s
247
248
249CONTEXT = 0
250PRED = 1
251SUBJ = 2
252OBJ = 3
253
254PARTS = PRED, SUBJ, OBJ
255ALL4 = CONTEXT, PRED, SUBJ, OBJ
256
257SYMBOL = 0
258FORMULA = 1
259LITERAL = 2
260LITERAL_DT = 21
261LITERAL_LANG = 22
262ANONYMOUS = 3
263XMLLITERAL = 25
264
265Logic_NS = "http://www.w3.org/2000/10/swap/log#"
266NODE_MERGE_URI = Logic_NS + "is"  # Pseudo-property indicating node merging
267forSomeSym = Logic_NS + "forSome"
268forAllSym = Logic_NS + "forAll"
269
270RDF_type_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
271RDF_NS_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
272OWL_NS = "http://www.w3.org/2002/07/owl#"
273DAML_sameAs_URI = OWL_NS + "sameAs"
274parsesTo_URI = Logic_NS + "parsesTo"
275RDF_spec = "http://www.w3.org/TR/REC-rdf-syntax/"
276
277List_NS = RDF_NS_URI  # From 20030808
278_Old_Logic_NS = "http://www.w3.org/2000/10/swap/log.n3#"
279
280N3_first = (SYMBOL, List_NS + "first")
281N3_rest = (SYMBOL, List_NS + "rest")
282N3_li = (SYMBOL, List_NS + "li")
283N3_nil = (SYMBOL, List_NS + "nil")
284N3_List = (SYMBOL, List_NS + "List")
285N3_Empty = (SYMBOL, List_NS + "Empty")
286
287
288runNamespaceValue: Optional[str] = None
289
290
291def runNamespace() -> str:
292    """Returns a URI suitable as a namespace for run-local objects"""
293    # @@@ include hostname (privacy?) (hash it?)
294    global runNamespaceValue
295    if runNamespaceValue is None:
296        runNamespaceValue = join(base(), _unique_id()) + "#"
297    return runNamespaceValue
298
299
300nextu = 0
301
302
303def uniqueURI() -> str:
304    """A unique URI"""
305    global nextu
306    nextu += 1
307    return runNamespace() + "u_" + str(nextu)
308
309
310tracking = False
311chatty_flag = 50
312
313# from why import BecauseOfData, becauseSubexpression
314
315
316def BecauseOfData(*args: Any, **kargs: Any) -> None:
317    # print args, kargs
318    pass
319
320
321def becauseSubexpression(*args: Any, **kargs: Any) -> None:
322    # print args, kargs
323    pass
324
325
326N3_forSome_URI = forSomeSym
327N3_forAll_URI = forAllSym
328
329# Magic resources we know about
330
331ADDED_HASH = "#"  # Stop where we use this in case we want to remove it!
332# This is the hash on namespace URIs
333
334RDF_type = (SYMBOL, RDF_type_URI)
335DAML_sameAs = (SYMBOL, DAML_sameAs_URI)
336
337LOG_implies_URI = "http://www.w3.org/2000/10/swap/log#implies"
338
339BOOLEAN_DATATYPE = _XSD_PFX + "boolean"
340DECIMAL_DATATYPE = _XSD_PFX + "decimal"
341DOUBLE_DATATYPE = _XSD_PFX + "double"
342FLOAT_DATATYPE = _XSD_PFX + "float"
343INTEGER_DATATYPE = _XSD_PFX + "integer"
344
345option_noregen = 0  # If set, do not regenerate genids on output
346
347# @@ I18n - the notname chars need extending for well known unicode non-text
348# characters. The XML spec switched to assuming unknown things were name
349# characters.
350# _namechars = string.lowercase + string.uppercase + string.digits + '_-'
351_notQNameChars = set("\t\r\n !\"#$&'()*,+/;<=>?@[\\]^`{|}~")  # else valid qname :-/
352_notKeywordsChars = _notQNameChars | {"."}
353_notNameChars = _notQNameChars | {":"}  # Assume anything else valid name :-/
354_rdfns = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
355
356hexChars = set("ABCDEFabcdef0123456789")
357escapeChars = set("(_~.-!$&'()*+,;=/?#@%)")  # valid for \ escapes in localnames
358numberChars = set("0123456789-")
359numberCharsPlus = numberChars | {"+", "."}
360
361
362def unicodeExpand(m: Match) -> str:
363    try:
364        return chr(int(m.group(1), 16))
365    except Exception:
366        raise Exception("Invalid unicode code point: " + m.group(1))
367
368
369unicodeEscape4 = re.compile(r"\\u([0-9a-fA-F]{4})")
370unicodeEscape8 = re.compile(r"\\U([0-9a-fA-F]{8})")
371
372
373N3CommentCharacter = "#"  # For unix script  # ! compatibility
374
375# Parse string to sink
376#
377# Regular expressions:
378eol = re.compile(r"[ \t]*(#[^\n]*)?\r?\n")  # end  of line, poss. w/comment
379eof = re.compile(r"[ \t]*(#[^\n]*)?$")  # end  of file, poss. w/comment
380ws = re.compile(r"[ \t]*")  # Whitespace not including NL
381signed_integer = re.compile(r"[-+]?[0-9]+")  # integer
382integer_syntax = re.compile(r"[-+]?[0-9]+")
383decimal_syntax = re.compile(r"[-+]?[0-9]*\.[0-9]+")
384exponent_syntax = re.compile(
385    r"[-+]?(?:[0-9]+\.[0-9]*|\.[0-9]+|[0-9]+)(?:e|E)[-+]?[0-9]+"
386)
387digitstring = re.compile(r"[0-9]+")  # Unsigned integer
388interesting = re.compile(r"""[\\\r\n\"\']""")
389langcode = re.compile(r"[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*")
390
391
392class sfloat(str):  # noqa: N801
393    """don't normalize raw XSD.double string representation"""
394
395
396class SinkParser:
397    def __init__(
398        self,
399        store: RDFSink,
400        openFormula: Optional[Formula] = None,
401        thisDoc: str = "",
402        baseURI: Optional[str] = None,
403        genPrefix: str = "",
404        why: Optional[Callable[[], None]] = None,
405        turtle: bool = False,
406    ):
407        """note: namespace names should *not* end in  # ;
408        the  # will get added during qname processing"""
409
410        self._bindings = {}
411        if thisDoc != "":
412            assert ":" in thisDoc, "Document URI not absolute: <%s>" % thisDoc
413            self._bindings[""] = thisDoc + "#"  # default
414
415        self._store = store
416        if genPrefix:
417            # TODO FIXME: there is no function named setGenPrefix
418            store.setGenPrefix(genPrefix)  # type: ignore[attr-defined] # pass it on
419
420        self._thisDoc = thisDoc
421        self.lines = 0  # for error handling
422        self.startOfLine = 0  # For calculating character number
423        self._genPrefix = genPrefix
424        self.keywords = ["a", "this", "bind", "has", "is", "of", "true", "false"]
425        self.keywordsSet = 0  # Then only can others be considered qnames
426        self._anonymousNodes: Dict[str, BNode] = {}
427        # Dict of anon nodes already declared ln: Term
428        self._variables: Dict[str, Variable] = {}
429        self._parentVariables: Dict[str, Variable] = {}
430        self._reason = why  # Why the parser was asked to parse this
431
432        self.turtle = turtle  # raise exception when encountering N3 extensions
433        # Turtle allows single or double quotes around strings, whereas N3
434        # only allows double quotes.
435        self.string_delimiters = ('"', "'") if turtle else ('"',)
436
437        self._reason2: Optional[Callable[..., None]] = None  # Why these triples
438        # was: diag.tracking
439        if tracking:
440            # type error: "BecauseOfData" does not return a value
441            self._reason2 = BecauseOfData(  # type: ignore[func-returns-value]
442                store.newSymbol(thisDoc), because=self._reason
443            )
444
445        self._baseURI: Optional[str]
446        if baseURI:
447            self._baseURI = baseURI
448        else:
449            if thisDoc:
450                self._baseURI = thisDoc
451            else:
452                self._baseURI = None
453
454        assert not self._baseURI or ":" in self._baseURI
455
456        if not self._genPrefix:
457            if self._thisDoc:
458                self._genPrefix = self._thisDoc + "#_g"
459            else:
460                self._genPrefix = uniqueURI()
461
462        self._formula: Optional[Formula]
463        if openFormula is None and not turtle:
464            if self._thisDoc:
465                # TODO FIXME: store.newFormula does not take any arguments
466                self._formula = store.newFormula(thisDoc + "#_formula")  # type: ignore[call-arg]
467            else:
468                self._formula = store.newFormula()
469        else:
470            self._formula = openFormula
471
472        self._context: Optional[Formula] = self._formula
473        self._parentContext: Optional[Formula] = None
474
475    def here(self, i: int) -> str:
476        """String generated from position in file
477
478        This is for repeatability when referring people to bnodes in a document.
479        This has diagnostic uses less formally, as it should point one to which
480        bnode the arbitrary identifier actually is. It gives the
481        line and character number of the '[' charcacter or path character
482        which introduced the blank node. The first blank node is boringly
483        _L1C1. It used to be used only for tracking, but for tests in general
484        it makes the canonical ordering of bnodes repeatable."""
485
486        return "%s_L%iC%i" % (self._genPrefix, self.lines, i - self.startOfLine + 1)
487
488    def formula(self) -> Optional[Formula]:
489        return self._formula
490
491    def loadStream(self, stream: Union[IO[str], IO[bytes]]) -> Optional[Formula]:
492        return self.loadBuf(stream.read())  # Not ideal
493
494    def loadBuf(self, buf: Union[str, bytes]) -> Optional[Formula]:
495        """Parses a buffer and returns its top level formula"""
496        self.startDoc()
497
498        self.feed(buf)
499        return self.endDoc()  # self._formula
500
501    def feed(self, octets: Union[str, bytes]) -> None:
502        """Feed an octet stream to the parser
503
504        if BadSyntax is raised, the string
505        passed in the exception object is the
506        remainder after any statements have been parsed.
507        So if there is more data to feed to the
508        parser, it should be straightforward to recover."""
509
510        if not isinstance(octets, str):
511            s = octets.decode("utf-8")
512            # NB already decoded, so \ufeff
513            if len(s) > 0 and s[0] == codecs.BOM_UTF8.decode("utf-8"):
514                s = s[1:]
515        else:
516            s = octets
517
518        i = 0
519        while i >= 0:
520            j = self.skipSpace(s, i)
521            if j < 0:
522                return
523
524            i = self.directiveOrStatement(s, j)
525            if i < 0:
526                # print("# next char: %s" % s[j])
527                self.BadSyntax(s, j, "expected directive or statement")
528
529    def directiveOrStatement(self, argstr: str, h: int) -> int:
530        i = self.skipSpace(argstr, h)
531        if i < 0:
532            return i  # EOF
533
534        if self.turtle:
535            j = self.sparqlDirective(argstr, i)
536            if j >= 0:
537                return j
538
539        j = self.directive(argstr, i)
540        if j >= 0:
541            return self.checkDot(argstr, j)
542
543        j = self.statement(argstr, i)
544        if j >= 0:
545            return self.checkDot(argstr, j)
546
547        return j
548
549    # @@I18N
550    # _namechars = string.lowercase + string.uppercase + string.digits + '_-'
551
552    def tok(self, tok: str, argstr: str, i: int, colon: bool = False) -> int:
553        """Check for keyword.  Space must have been stripped on entry and
554        we must not be at end of file.
555
556        if colon, then keyword followed by colon is ok
557        (`@prefix:<blah>` is ok, rdf:type shortcut a must be followed by ws)
558        """
559
560        assert tok[0] not in _notNameChars  # not for punctuation
561        if argstr[i] == "@":
562            i += 1
563        else:
564            if tok not in self.keywords:
565                return -1  # No, this has neither keywords declaration nor "@"
566
567        i_plus_len_tok = i + len(tok)
568        if (
569            argstr[i:i_plus_len_tok] == tok
570            and (argstr[i_plus_len_tok] in _notKeywordsChars)
571            or (colon and argstr[i_plus_len_tok] == ":")
572        ):
573            return i_plus_len_tok
574        else:
575            return -1
576
577    def sparqlTok(self, tok: str, argstr: str, i: int) -> int:
578        """Check for SPARQL keyword.  Space must have been stripped on entry
579        and we must not be at end of file.
580        Case insensitive and not preceded by @
581        """
582
583        assert tok[0] not in _notNameChars  # not for punctuation
584
585        len_tok = len(tok)
586        if argstr[i : i + len_tok].lower() == tok.lower() and (
587            argstr[i + len_tok] in _notQNameChars
588        ):
589            i += len_tok
590            return i
591        else:
592            return -1
593
594    def directive(self, argstr: str, i: int) -> int:
595        j = self.skipSpace(argstr, i)
596        if j < 0:
597            return j  # eof
598        res: typing.List[str] = []
599
600        j = self.tok("bind", argstr, i)  # implied "#". Obsolete.
601        if j > 0:
602            self.BadSyntax(argstr, i, "keyword bind is obsolete: use @prefix")
603
604        j = self.tok("keywords", argstr, i)
605        if j > 0:
606            if self.turtle:
607                self.BadSyntax(argstr, i, "Found 'keywords' when in Turtle mode.")
608
609            i = self.commaSeparatedList(argstr, j, res, self.bareWord)
610            if i < 0:
611                self.BadSyntax(
612                    argstr, i, "'@keywords' needs comma separated list of words"
613                )
614            self.setKeywords(res[:])
615            return i
616
617        j = self.tok("forAll", argstr, i)
618        if j > 0:
619            if self.turtle:
620                self.BadSyntax(argstr, i, "Found 'forAll' when in Turtle mode.")
621
622            i = self.commaSeparatedList(argstr, j, res, self.uri_ref2)
623            if i < 0:
624                self.BadSyntax(argstr, i, "Bad variable list after @forAll")
625            for x in res:
626                # self._context.declareUniversal(x)
627                if x not in self._variables or x in self._parentVariables:
628                    # type error: Item "None" of "Optional[Formula]" has no attribute "newUniversal"
629                    self._variables[x] = self._context.newUniversal(x)  # type: ignore[union-attr]
630            return i
631
632        j = self.tok("forSome", argstr, i)
633        if j > 0:
634            if self.turtle:
635                self.BadSyntax(argstr, i, "Found 'forSome' when in Turtle mode.")
636
637            i = self.commaSeparatedList(argstr, j, res, self.uri_ref2)
638            if i < 0:
639                self.BadSyntax(argstr, i, "Bad variable list after @forSome")
640            for x in res:
641                # type error: Item "None" of "Optional[Formula]" has no attribute "declareExistential"
642                self._context.declareExistential(x)  # type: ignore[union-attr]
643            return i
644
645        j = self.tok("prefix", argstr, i, colon=True)  # no implied "#"
646        if j >= 0:
647            t: typing.List[Union[Identifier, Tuple[str, str]]] = []
648            i = self.qname(argstr, j, t)
649            if i < 0:
650                self.BadSyntax(argstr, j, "expected qname after @prefix")
651            j = self.uri_ref2(argstr, i, t)
652            if j < 0:
653                self.BadSyntax(argstr, i, "expected <uriref> after @prefix _qname_")
654            ns: str = self.uriOf(t[1])
655
656            if self._baseURI:
657                ns = join(self._baseURI, ns)
658            elif ":" not in ns:
659                self.BadSyntax(
660                    argstr,
661                    j,
662                    f"With no base URI, cannot use relative URI in @prefix <{ns}>",
663                )
664            assert ":" in ns  # must be absolute
665            self._bindings[t[0][0]] = ns
666            self.bind(t[0][0], hexify(ns))
667            return j
668
669        j = self.tok("base", argstr, i)  # Added 2007/7/7
670        if j >= 0:
671            t = []
672            i = self.uri_ref2(argstr, j, t)
673            if i < 0:
674                self.BadSyntax(argstr, j, "expected <uri> after @base ")
675            ns = self.uriOf(t[0])
676
677            if self._baseURI:
678                ns = join(self._baseURI, ns)
679            else:
680                self.BadSyntax(
681                    argstr,
682                    j,
683                    "With no previous base URI, cannot use "
684                    + "relative URI in @base  <"
685                    + ns
686                    + ">",
687                )
688            assert ":" in ns  # must be absolute
689            self._baseURI = ns
690            return i
691
692        return -1  # Not a directive, could be something else.
693
694    def sparqlDirective(self, argstr: str, i: int) -> int:
695        """
696        turtle and trig support BASE/PREFIX without @ and without
697        terminating .
698        """
699
700        j = self.skipSpace(argstr, i)
701        if j < 0:
702            return j  # eof
703
704        j = self.sparqlTok("PREFIX", argstr, i)
705        if j >= 0:
706            t: typing.List[Any] = []
707            i = self.qname(argstr, j, t)
708            if i < 0:
709                self.BadSyntax(argstr, j, "expected qname after @prefix")
710            j = self.uri_ref2(argstr, i, t)
711            if j < 0:
712                self.BadSyntax(argstr, i, "expected <uriref> after @prefix _qname_")
713            ns = self.uriOf(t[1])
714
715            if self._baseURI:
716                ns = join(self._baseURI, ns)
717            elif ":" not in ns:
718                self.BadSyntax(
719                    argstr,
720                    j,
721                    "With no base URI, cannot use "
722                    + "relative URI in @prefix <"
723                    + ns
724                    + ">",
725                )
726            assert ":" in ns  # must be absolute
727            self._bindings[t[0][0]] = ns
728            self.bind(t[0][0], hexify(ns))
729            return j
730
731        j = self.sparqlTok("BASE", argstr, i)
732        if j >= 0:
733            t = []
734            i = self.uri_ref2(argstr, j, t)
735            if i < 0:
736                self.BadSyntax(argstr, j, "expected <uri> after @base ")
737            ns = self.uriOf(t[0])
738
739            if self._baseURI:
740                ns = join(self._baseURI, ns)
741            else:
742                self.BadSyntax(
743                    argstr,
744                    j,
745                    "With no previous base URI, cannot use "
746                    + "relative URI in @base  <"
747                    + ns
748                    + ">",
749                )
750            assert ":" in ns  # must be absolute
751            self._baseURI = ns
752            return i
753
754        return -1  # Not a directive, could be something else.
755
756    def bind(self, qn: str, uri: bytes) -> None:
757        assert isinstance(uri, bytes), "Any unicode must be %x-encoded already"
758        if qn == "":
759            self._store.setDefaultNamespace(uri)
760        else:
761            self._store.bind(qn, uri)
762
763    def setKeywords(self, k: Optional[typing.List[str]]) -> None:
764        """Takes a list of strings"""
765        if k is None:
766            self.keywordsSet = 0
767        else:
768            self.keywords = k
769            self.keywordsSet = 1
770
771    def startDoc(self) -> None:
772        # was: self._store.startDoc()
773        self._store.startDoc(self._formula)
774
775    def endDoc(self) -> Optional[Formula]:
776        """Signal end of document and stop parsing. returns formula"""
777        self._store.endDoc(self._formula)  # don't canonicalize yet
778        return self._formula
779
780    def makeStatement(self, quadruple) -> None:
781        # $$$$$$$$$$$$$$$$$$$$$
782        # print "# Parser output: ", `quadruple`
783        self._store.makeStatement(quadruple, why=self._reason2)
784
785    def statement(self, argstr: str, i: int) -> int:
786        r: typing.List[Any] = []
787        i = self.object(argstr, i, r)  # Allow literal for subject - extends RDF
788        if i < 0:
789            return i
790
791        j = self.property_list(argstr, i, r[0])
792
793        if j < 0:
794            self.BadSyntax(argstr, i, "expected propertylist")
795        return j
796
797    def subject(self, argstr: str, i: int, res: MutableSequence[Any]) -> int:
798        return self.item(argstr, i, res)
799
800    def verb(self, argstr: str, i: int, res: MutableSequence[Any]) -> int:
801        """has _prop_
802        is _prop_ of
803        a
804        =
805        _prop_
806        >- prop ->
807        <- prop -<
808        _operator_"""
809
810        j = self.skipSpace(argstr, i)
811        if j < 0:
812            return j  # eof
813
814        r: typing.List[Any] = []
815
816        j = self.tok("has", argstr, i)
817        if j >= 0:
818            if self.turtle:
819                self.BadSyntax(argstr, i, "Found 'has' keyword in Turtle mode")
820
821            i = self.prop(argstr, j, r)
822            if i < 0:
823                self.BadSyntax(argstr, j, "expected property after 'has'")
824            res.append(("->", r[0]))
825            return i
826
827        j = self.tok("is", argstr, i)
828        if j >= 0:
829            if self.turtle:
830                self.BadSyntax(argstr, i, "Found 'is' keyword in Turtle mode")
831
832            i = self.prop(argstr, j, r)
833            if i < 0:
834                self.BadSyntax(argstr, j, "expected <property> after 'is'")
835            j = self.skipSpace(argstr, i)
836            if j < 0:
837                self.BadSyntax(
838                    argstr, i, "End of file found, expected property after 'is'"
839                )
840            i = j
841            j = self.tok("of", argstr, i)
842            if j < 0:
843                self.BadSyntax(argstr, i, "expected 'of' after 'is' <prop>")
844            res.append(("<-", r[0]))
845            return j
846
847        j = self.tok("a", argstr, i)
848        if j >= 0:
849            res.append(("->", RDF_type))
850            return j
851
852        if argstr[i : i + 2] == "<=":
853            if self.turtle:
854                self.BadSyntax(argstr, i, "Found '<=' in Turtle mode. ")
855
856            res.append(("<-", self._store.newSymbol(Logic_NS + "implies")))
857            return i + 2
858
859        if argstr[i] == "=":
860            if self.turtle:
861                self.BadSyntax(argstr, i, "Found '=' in Turtle mode")
862            if argstr[i + 1] == ">":
863                res.append(("->", self._store.newSymbol(Logic_NS + "implies")))
864                return i + 2
865            res.append(("->", DAML_sameAs))
866            return i + 1
867
868        if argstr[i : i + 2] == ":=":
869            if self.turtle:
870                self.BadSyntax(argstr, i, "Found ':=' in Turtle mode")
871
872            # patch file relates two formulae, uses this    @@ really?
873            res.append(("->", Logic_NS + "becomes"))
874            return i + 2
875
876        j = self.prop(argstr, i, r)
877        if j >= 0:
878            res.append(("->", r[0]))
879            return j
880
881        if argstr[i : i + 2] == ">-" or argstr[i : i + 2] == "<-":
882            self.BadSyntax(argstr, j, ">- ... -> syntax is obsolete.")
883
884        return -1
885
886    def prop(self, argstr: str, i: int, res: MutableSequence[Any]) -> int:
887        return self.item(argstr, i, res)
888
889    def item(self, argstr: str, i, res: MutableSequence[Any]) -> int:
890        return self.path(argstr, i, res)
891
892    def blankNode(self, uri: Optional[str] = None) -> BNode:
893        return self._store.newBlankNode(self._context, uri, why=self._reason2)
894
895    def path(self, argstr: str, i: int, res: MutableSequence[Any]) -> int:
896        """Parse the path production."""
897        j = self.nodeOrLiteral(argstr, i, res)
898        if j < 0:
899            return j  # nope
900
901        while argstr[j] in {"!", "^"}:  # no spaces, must follow exactly (?)
902            ch = argstr[j]
903            subj = res.pop()
904            obj = self.blankNode(uri=self.here(j))
905            j = self.node(argstr, j + 1, res)
906            if j < 0:
907                self.BadSyntax(argstr, j, "EOF found in middle of path syntax")
908            pred = res.pop()
909            if ch == "^":  # Reverse traverse
910                self.makeStatement((self._context, pred, obj, subj))
911            else:
912                self.makeStatement((self._context, pred, subj, obj))
913            res.append(obj)
914        return j
915
916    def anonymousNode(self, ln: str) -> BNode:
917        """Remember or generate a term for one of these _: anonymous nodes"""
918        term = self._anonymousNodes.get(ln, None)
919        if term is not None:
920            return term
921        term = self._store.newBlankNode(self._context, why=self._reason2)
922        self._anonymousNodes[ln] = term
923        return term
924
925    def node(
926        self,
927        argstr: str,
928        i: int,
929        res: MutableSequence[Any],
930        subjectAlready: Optional[Node] = None,
931    ) -> int:
932        """Parse the <node> production.
933        Space is now skipped once at the beginning
934        instead of in multiple calls to self.skipSpace().
935        """
936        subj: Optional[Node] = subjectAlready
937
938        j = self.skipSpace(argstr, i)
939        if j < 0:
940            return j  # eof
941        i = j
942        ch = argstr[i]  # Quick 1-character checks first:
943
944        if ch == "[":
945            bnodeID = self.here(i)
946            j = self.skipSpace(argstr, i + 1)
947            if j < 0:
948                self.BadSyntax(argstr, i, "EOF after '['")
949            # Hack for "is" binding name to anon node
950            if argstr[j] == "=":
951                if self.turtle:
952                    self.BadSyntax(
953                        argstr, j, "Found '[=' or '[ =' when in turtle mode."
954                    )
955                i = j + 1
956                objs: typing.List[Node] = []
957                j = self.objectList(argstr, i, objs)
958                if j >= 0:
959                    subj = objs[0]
960                    if len(objs) > 1:
961                        for obj in objs:
962                            self.makeStatement((self._context, DAML_sameAs, subj, obj))
963                    j = self.skipSpace(argstr, j)
964                    if j < 0:
965                        self.BadSyntax(
966                            argstr, i, "EOF when objectList expected after [ = "
967                        )
968                    if argstr[j] == ";":
969                        j += 1
970                else:
971                    self.BadSyntax(argstr, i, "objectList expected after [= ")
972
973            if subj is None:
974                subj = self.blankNode(uri=bnodeID)
975
976            i = self.property_list(argstr, j, subj)
977            if i < 0:
978                self.BadSyntax(argstr, j, "property_list expected")
979
980            j = self.skipSpace(argstr, i)
981            if j < 0:
982                self.BadSyntax(
983                    argstr, i, "EOF when ']' expected after [ <propertyList>"
984                )
985            if argstr[j] != "]":
986                self.BadSyntax(argstr, j, "']' expected")
987            res.append(subj)
988            return j + 1
989
990        if not self.turtle and ch == "{":
991            # if self.turtle:
992            #     self.BadSyntax(argstr, i,
993            #                     "found '{' while in Turtle mode, Formulas not supported!")
994            ch2 = argstr[i + 1]
995            if ch2 == "$":
996                # a set
997                i += 1
998                j = i + 1
999                List = []
1000                first_run = True
1001                while 1:
1002                    i = self.skipSpace(argstr, j)
1003                    if i < 0:
1004                        self.BadSyntax(argstr, i, "needed '$}', found end.")
1005                    if argstr[i : i + 2] == "$}":
1006                        j = i + 2
1007                        break
1008
1009                    if not first_run:
1010                        if argstr[i] == ",":
1011                            i += 1
1012                        else:
1013                            self.BadSyntax(argstr, i, "expected: ','")
1014                    else:
1015                        first_run = False
1016
1017                    item: typing.List[Any] = []
1018                    j = self.item(argstr, i, item)  # @@@@@ should be path, was object
1019                    if j < 0:
1020                        self.BadSyntax(argstr, i, "expected item in set or '$}'")
1021                    List.append(self._store.intern(item[0]))
1022                res.append(self._store.newSet(List, self._context))
1023                return j
1024            else:
1025                # parse a formula
1026                j = i + 1
1027                oldParentContext = self._parentContext
1028                self._parentContext = self._context
1029                parentAnonymousNodes = self._anonymousNodes
1030                grandParentVariables = self._parentVariables
1031                self._parentVariables = self._variables
1032                self._anonymousNodes = {}
1033                self._variables = self._variables.copy()
1034                reason2 = self._reason2
1035                self._reason2 = becauseSubexpression
1036                if subj is None:
1037                    # type error: Incompatible types in assignment (expression has type "Formula", variable has type "Optional[Node]")
1038                    subj = self._store.newFormula()  # type: ignore[assignment]
1039                # type error: Incompatible types in assignment (expression has type "Optional[Node]", variable has type "Optional[Formula]")
1040                self._context = subj  # type: ignore[assignment]
1041
1042                while 1:
1043                    i = self.skipSpace(argstr, j)
1044                    if i < 0:
1045                        self.BadSyntax(argstr, i, "needed '}', found end.")
1046
1047                    if argstr[i] == "}":
1048                        j = i + 1
1049                        break
1050
1051                    j = self.directiveOrStatement(argstr, i)
1052                    if j < 0:
1053                        self.BadSyntax(argstr, i, "expected statement or '}'")
1054
1055                self._anonymousNodes = parentAnonymousNodes
1056                self._variables = self._parentVariables
1057                self._parentVariables = grandParentVariables
1058                self._context = self._parentContext
1059                self._reason2 = reason2
1060                self._parentContext = oldParentContext
1061                # type error: Item "Node" of "Optional[Node]" has no attribute "close"
1062                res.append(
1063                    subj.close()  # type: ignore[union-attr]
1064                )  # No use until closed
1065                return j
1066
1067        if ch == "(":
1068            thing_type: Callable[
1069                [typing.List[Any], Optional[Formula]], Union[Set[Any], IdentifiedNode]
1070            ]
1071            thing_type = self._store.newList
1072            ch2 = argstr[i + 1]
1073            if ch2 == "$":
1074                thing_type = self._store.newSet
1075                i += 1
1076            j = i + 1
1077
1078            List = []
1079            while 1:
1080                i = self.skipSpace(argstr, j)
1081                if i < 0:
1082                    self.BadSyntax(argstr, i, "needed ')', found end.")
1083                if argstr[i] == ")":
1084                    j = i + 1
1085                    break
1086
1087                item = []
1088                j = self.item(argstr, i, item)  # @@@@@ should be path, was object
1089                if j < 0:
1090                    self.BadSyntax(argstr, i, "expected item in list or ')'")
1091                List.append(self._store.intern(item[0]))
1092            res.append(thing_type(List, self._context))
1093            return j
1094
1095        j = self.tok("this", argstr, i)  # This context
1096        if j >= 0:
1097            self.BadSyntax(
1098                argstr,
1099                i,
1100                "Keyword 'this' was ancient N3. Now use "
1101                + "@forSome and @forAll keywords.",
1102            )
1103
1104        # booleans
1105        j = self.tok("true", argstr, i)
1106        if j >= 0:
1107            res.append(True)
1108            return j
1109        j = self.tok("false", argstr, i)
1110        if j >= 0:
1111            res.append(False)
1112            return j
1113
1114        if subj is None:  # If this can be a named node, then check for a name.
1115            j = self.uri_ref2(argstr, i, res)
1116            if j >= 0:
1117                return j
1118
1119        return -1
1120
1121    def property_list(self, argstr: str, i: int, subj: Node) -> int:
1122        """Parse property list
1123        Leaves the terminating punctuation in the buffer
1124        """
1125        while 1:
1126            while 1:  # skip repeat ;
1127                j = self.skipSpace(argstr, i)
1128                if j < 0:
1129                    self.BadSyntax(
1130                        argstr, i, "EOF found when expected verb in property list"
1131                    )
1132                if argstr[j] != ";":
1133                    break
1134                i = j + 1
1135
1136            if argstr[j : j + 2] == ":-":
1137                if self.turtle:
1138                    self.BadSyntax(argstr, j, "Found in ':-' in Turtle mode")
1139                i = j + 2
1140                res: typing.List[Any] = []
1141                j = self.node(argstr, i, res, subj)
1142                if j < 0:
1143                    self.BadSyntax(argstr, i, "bad {} or () or [] node after :- ")
1144                i = j
1145                continue
1146            i = j
1147            v: typing.List[Any] = []
1148            j = self.verb(argstr, i, v)
1149            if j <= 0:
1150                return i  # void but valid
1151
1152            objs: typing.List[Any] = []
1153            i = self.objectList(argstr, j, objs)
1154            if i < 0:
1155                self.BadSyntax(argstr, j, "objectList expected")
1156            for obj in objs:
1157                dira, sym = v[0]
1158                if dira == "->":
1159                    self.makeStatement((self._context, sym, subj, obj))
1160                else:
1161                    self.makeStatement((self._context, sym, obj, subj))
1162
1163            j = self.skipSpace(argstr, i)
1164            if j < 0:
1165                self.BadSyntax(argstr, j, "EOF found in list of objects")
1166            if argstr[i] != ";":
1167                return i
1168            i += 1  # skip semicolon and continue
1169
1170    def commaSeparatedList(
1171        self,
1172        argstr: str,
1173        j: int,
1174        res: MutableSequence[Any],
1175        what: Callable[[str, int, MutableSequence[Any]], int],
1176    ) -> int:
1177        """return value: -1 bad syntax; >1 new position in argstr
1178        res has things found appended
1179        """
1180        i = self.skipSpace(argstr, j)
1181        if i < 0:
1182            self.BadSyntax(argstr, i, "EOF found expecting comma sep list")
1183        if argstr[i] == ".":
1184            return j  # empty list is OK
1185        i = what(argstr, i, res)
1186        if i < 0:
1187            return -1
1188
1189        while 1:
1190            j = self.skipSpace(argstr, i)
1191            if j < 0:
1192                return j  # eof
1193            ch = argstr[j]
1194            if ch != ",":
1195                if ch != ".":
1196                    return -1
1197                return j  # Found  but not swallowed "."
1198            i = what(argstr, j + 1, res)
1199            if i < 0:
1200                self.BadSyntax(argstr, i, "bad list content")
1201
1202    def objectList(self, argstr: str, i: int, res: MutableSequence[Any]) -> int:
1203        i = self.object(argstr, i, res)
1204        if i < 0:
1205            return -1
1206        while 1:
1207            j = self.skipSpace(argstr, i)
1208            if j < 0:
1209                self.BadSyntax(argstr, j, "EOF found after object")
1210            if argstr[j] != ",":
1211                return j  # Found something else!
1212            i = self.object(argstr, j + 1, res)
1213            if i < 0:
1214                return i
1215
1216    def checkDot(self, argstr: str, i: int) -> int:
1217        j = self.skipSpace(argstr, i)
1218        if j < 0:
1219            return j  # eof
1220        ch = argstr[j]
1221        if ch == ".":
1222            return j + 1  # skip
1223        if ch == "}":
1224            return j  # don't skip it
1225        if ch == "]":
1226            return j
1227        self.BadSyntax(argstr, j, "expected '.' or '}' or ']' at end of statement")
1228
1229    def uri_ref2(self, argstr: str, i: int, res: MutableSequence[Any]) -> int:
1230        """Generate uri from n3 representation.
1231
1232        Note that the RDF convention of directly concatenating
1233        NS and local name is now used though I prefer inserting a '#'
1234        to make the namesapces look more like what XML folks expect.
1235        """
1236        qn: typing.List[Any] = []
1237        j = self.qname(argstr, i, qn)
1238        if j >= 0:
1239            pfx, ln = qn[0]
1240            if pfx is None:
1241                assert 0, "not used?"
1242                ns = self._baseURI + ADDED_HASH  # type: ignore[unreachable]
1243            else:
1244                try:
1245                    ns = self._bindings[pfx]
1246                except KeyError:
1247                    if pfx == "_":  # Magic prefix 2001/05/30, can be changed
1248                        res.append(self.anonymousNode(ln))
1249                        return j
1250                    if not self.turtle and pfx == "":
1251                        ns = join(self._baseURI or "", "#")
1252                    else:
1253                        self.BadSyntax(argstr, i, 'Prefix "%s:" not bound' % (pfx))
1254            symb = self._store.newSymbol(ns + ln)
1255            res.append(self._variables.get(symb, symb))
1256            return j
1257
1258        i = self.skipSpace(argstr, i)
1259        if i < 0:
1260            return -1
1261
1262        if argstr[i] == "?":
1263            v: typing.List[Any] = []
1264            j = self.variable(argstr, i, v)
1265            if j > 0:  # Forget variables as a class, only in context.
1266                res.append(v[0])
1267                return j
1268            return -1
1269
1270        elif argstr[i] == "<":
1271            st = i + 1
1272            i = argstr.find(">", st)
1273            if i >= 0:
1274                uref = argstr[st:i]  # the join should dealt with "":
1275
1276                # expand unicode escapes
1277                uref = unicodeEscape8.sub(unicodeExpand, uref)
1278                uref = unicodeEscape4.sub(unicodeExpand, uref)
1279
1280                if self._baseURI:
1281                    uref = join(self._baseURI, uref)  # was: uripath.join
1282                else:
1283                    assert (
1284                        ":" in uref
1285                    ), "With no base URI, cannot deal with relative URIs"
1286                if argstr[i - 1] == "#" and not uref[-1:] == "#":
1287                    uref += "#"  # She meant it! Weirdness in urlparse?
1288                symb = self._store.newSymbol(uref)
1289                res.append(self._variables.get(symb, symb))
1290                return i + 1
1291            self.BadSyntax(argstr, j, "unterminated URI reference")
1292
1293        elif self.keywordsSet:
1294            v = []
1295            j = self.bareWord(argstr, i, v)
1296            if j < 0:
1297                return -1  # Forget variables as a class, only in context.
1298            if v[0] in self.keywords:
1299                self.BadSyntax(argstr, i, 'Keyword "%s" not allowed here.' % v[0])
1300            res.append(self._store.newSymbol(self._bindings[""] + v[0]))
1301            return j
1302        else:
1303            return -1
1304
1305    def skipSpace(self, argstr: str, i: int) -> int:
1306        """Skip white space, newlines and comments.
1307        return -1 if EOF, else position of first non-ws character"""
1308
1309        # Most common case is a non-commented line starting with few spaces and tabs.
1310        try:
1311            while True:
1312                ch = argstr[i]
1313                if ch in {" ", "\t"}:
1314                    i += 1
1315                    continue
1316                elif ch not in {"#", "\r", "\n"}:
1317                    return i
1318                break
1319        except IndexError:
1320            return -1
1321
1322        while 1:
1323            m = eol.match(argstr, i)
1324            if m is None:
1325                break
1326            self.lines += 1
1327            self.startOfLine = i = m.end()  # Point to first character unmatched
1328        m = ws.match(argstr, i)
1329        if m is not None:
1330            i = m.end()
1331        m = eof.match(argstr, i)
1332        return i if m is None else -1
1333
1334    def variable(self, argstr: str, i: int, res) -> int:
1335        """?abc -> variable(:abc)"""
1336
1337        j = self.skipSpace(argstr, i)
1338        if j < 0:
1339            return -1
1340
1341        if argstr[j] != "?":
1342            return -1
1343        j += 1
1344        i = j
1345        if argstr[j] in numberChars:
1346            self.BadSyntax(argstr, j, "Variable name can't start with '%s'" % argstr[j])
1347        len_argstr = len(argstr)
1348        while i < len_argstr and argstr[i] not in _notKeywordsChars:
1349            i += 1
1350        if self._parentContext is None:
1351            varURI = self._store.newSymbol(self._baseURI + "#" + argstr[j:i])  # type: ignore[operator]
1352            if varURI not in self._variables:
1353                # type error: Item "None" of "Optional[Formula]" has no attribute "newUniversal"
1354                self._variables[varURI] = self._context.newUniversal(  # type: ignore[union-attr]
1355                    varURI, why=self._reason2
1356                )
1357            res.append(self._variables[varURI])
1358            return i
1359            # @@ was:
1360            # self.BadSyntax(argstr, j,
1361            #     "Can't use ?xxx syntax for variable in outermost level: %s"
1362            #     % argstr[j-1:i])
1363        varURI = self._store.newSymbol(self._baseURI + "#" + argstr[j:i])  # type: ignore[operator]
1364        if varURI not in self._parentVariables:
1365            self._parentVariables[varURI] = self._parentContext.newUniversal(
1366                varURI, why=self._reason2
1367            )
1368        res.append(self._parentVariables[varURI])
1369        return i
1370
1371    def bareWord(self, argstr: str, i: int, res: MutableSequence[Any]) -> int:
1372        """abc -> :abc"""
1373        j = self.skipSpace(argstr, i)
1374        if j < 0:
1375            return -1
1376
1377        if argstr[j] in numberChars or argstr[j] in _notKeywordsChars:
1378            return -1
1379        i = j
1380        len_argstr = len(argstr)
1381        while i < len_argstr and argstr[i] not in _notKeywordsChars:
1382            i += 1
1383        res.append(argstr[j:i])
1384        return i
1385
1386    def qname(
1387        self,
1388        argstr: str,
1389        i: int,
1390        res: MutableSequence[Union[Identifier, Tuple[str, str]]],
1391    ) -> int:
1392        """
1393        xyz:def -> ('xyz', 'def')
1394        If not in keywords and keywordsSet: def -> ('', 'def')
1395        :def -> ('', 'def')
1396        """
1397
1398        i = self.skipSpace(argstr, i)
1399        if i < 0:
1400            return -1
1401
1402        c = argstr[i]
1403        if c in numberCharsPlus:
1404            return -1
1405        len_argstr = len(argstr)
1406        if c not in _notNameChars:
1407            j = i
1408            i += 1
1409
1410            try:
1411                while argstr[i] not in _notNameChars:
1412                    i += 1
1413            except IndexError:
1414                pass  # Very rare.
1415
1416            if argstr[i - 1] == ".":  # qname cannot end with "."
1417                i -= 1
1418                if i == j:
1419                    return -1
1420            ln = argstr[j:i]
1421
1422        else:  # First character is non-alpha
1423            ln = ""  # Was:  None - TBL (why? useful?)
1424
1425        if i < len_argstr and argstr[i] == ":":
1426            pfx = ln
1427            # bnodes names have different rules
1428            if pfx == "_":
1429                allowedChars = _notNameChars
1430            else:
1431                allowedChars = _notQNameChars
1432
1433            i += 1
1434            lastslash = False
1435            start = i
1436            ln = ""
1437            while i < len_argstr:
1438                c = argstr[i]
1439                if c == "\\" and not lastslash:  # Very rare.
1440                    lastslash = True
1441                    if start < i:
1442                        ln += argstr[start:i]
1443                    start = i + 1
1444                elif c not in allowedChars or lastslash:  # Most common case is "a-zA-Z"
1445                    if lastslash:
1446                        if c not in escapeChars:
1447                            raise BadSyntax(
1448                                self._thisDoc,
1449                                self.lines,
1450                                argstr,
1451                                i,
1452                                "illegal escape " + c,
1453                            )
1454                    elif c == "%":  # Very rare.
1455                        if (
1456                            argstr[i + 1] not in hexChars
1457                            or argstr[i + 2] not in hexChars
1458                        ):
1459                            raise BadSyntax(
1460                                self._thisDoc,
1461                                self.lines,
1462                                argstr,
1463                                i,
1464                                "illegal hex escape " + c,
1465                            )
1466                    lastslash = False
1467                else:
1468                    break
1469                i += 1
1470
1471            if lastslash:
1472                raise BadSyntax(
1473                    self._thisDoc, self.lines, argstr, i, "qname cannot end with \\"
1474                )
1475
1476            if argstr[i - 1] == ".":
1477                # localname cannot end in .
1478                if len(ln) == 0 and start == i:
1479                    return -1
1480                i -= 1
1481
1482            if start < i:
1483                ln += argstr[start:i]
1484
1485            res.append((pfx, ln))
1486            return i
1487
1488        else:  # delimiter was not ":"
1489            if ln and self.keywordsSet and ln not in self.keywords:
1490                res.append(("", ln))
1491                return i
1492            return -1
1493
1494    def object(
1495        self,
1496        argstr: str,
1497        i: int,
1498        res: MutableSequence[Any],
1499    ) -> int:
1500        j = self.subject(argstr, i, res)
1501        if j >= 0:
1502            return j
1503        else:
1504            j = self.skipSpace(argstr, i)
1505            if j < 0:
1506                return -1
1507            else:
1508                i = j
1509
1510            ch = argstr[i]
1511            if ch in self.string_delimiters:
1512                ch_three = ch * 3
1513                if argstr[i : i + 3] == ch_three:
1514                    delim = ch_three
1515                    i += 3
1516                else:
1517                    delim = ch
1518                    i += 1
1519
1520                j, s = self.strconst(argstr, i, delim)
1521
1522                res.append(self._store.newLiteral(s))  # type: ignore[call-arg] # TODO FIXME
1523                return j
1524            else:
1525                return -1
1526
1527    def nodeOrLiteral(self, argstr: str, i: int, res: MutableSequence[Any]) -> int:
1528        j = self.node(argstr, i, res)
1529        startline = self.lines  # Remember where for error messages
1530        if j >= 0:
1531            return j
1532        else:
1533            j = self.skipSpace(argstr, i)
1534            if j < 0:
1535                return -1
1536            else:
1537                i = j
1538
1539            ch = argstr[i]
1540            if ch in numberCharsPlus:
1541                m = exponent_syntax.match(argstr, i)
1542                if m:
1543                    j = m.end()
1544                    res.append(sfloat(argstr[i:j]))
1545                    return j
1546
1547                m = decimal_syntax.match(argstr, i)
1548                if m:
1549                    j = m.end()
1550                    res.append(Decimal(argstr[i:j]))
1551                    return j
1552
1553                m = integer_syntax.match(argstr, i)
1554                if m:
1555                    j = m.end()
1556                    res.append(long_type(argstr[i:j]))
1557                    return j
1558
1559                # return -1  ## or fall through?
1560
1561            ch_three = ch * 3
1562            if ch in self.string_delimiters:
1563                if argstr[i : i + 3] == ch_three:
1564                    delim = ch_three
1565                    i += 3
1566                else:
1567                    delim = ch
1568                    i += 1
1569
1570                dt = None
1571                j, s = self.strconst(argstr, i, delim)
1572                lang = None
1573                if argstr[j] == "@":  # Language?
1574                    m = langcode.match(argstr, j + 1)
1575                    if m is None:
1576                        raise BadSyntax(
1577                            self._thisDoc,
1578                            startline,
1579                            argstr,
1580                            i,
1581                            "Bad language code syntax on string " + "literal, after @",
1582                        )
1583                    i = m.end()
1584                    lang = argstr[j + 1 : i]
1585                    j = i
1586                if argstr[j : j + 2] == "^^":
1587                    res2: typing.List[Any] = []
1588                    j = self.uri_ref2(argstr, j + 2, res2)  # Read datatype URI
1589                    dt = res2[0]
1590                res.append(self._store.newLiteral(s, dt, lang))
1591                return j
1592            else:
1593                return -1
1594
1595    def uriOf(self, sym: Union[Identifier, Tuple[str, str]]) -> str:
1596        if isinstance(sym, tuple):
1597            return sym[1]  # old system for --pipe
1598        # return sym.uriref()  # cwm api
1599        return sym
1600
1601    def strconst(self, argstr: str, i: int, delim: str) -> Tuple[int, str]:
1602        """parse an N3 string constant delimited by delim.
1603        return index, val
1604        """
1605        delim1 = delim[0]
1606        delim2, delim3, delim4, delim5 = delim1 * 2, delim1 * 3, delim1 * 4, delim1 * 5
1607
1608        j = i
1609        ustr = ""  # Empty unicode string
1610        startline = self.lines  # Remember where for error messages
1611        len_argstr = len(argstr)
1612        while j < len_argstr:
1613            if argstr[j] == delim1:
1614                if delim == delim1:  # done when delim is " or '
1615                    i = j + 1
1616                    return i, ustr
1617                if (
1618                    delim == delim3
1619                ):  # done when delim is """ or ''' and, respectively ...
1620                    if argstr[j : j + 5] == delim5:  # ... we have "" or '' before
1621                        i = j + 5
1622                        ustr += delim2
1623                        return i, ustr
1624                    if argstr[j : j + 4] == delim4:  # ... we have " or ' before
1625                        i = j + 4
1626                        ustr += delim1
1627                        return i, ustr
1628                    if argstr[j : j + 3] == delim3:  # current " or ' is part of delim
1629                        i = j + 3
1630                        return i, ustr
1631
1632                    # we are inside of the string and current char is " or '
1633                    j += 1
1634                    ustr += delim1
1635                    continue
1636
1637            m = interesting.search(argstr, j)  # was argstr[j:].
1638            # Note for pos param to work, MUST be compiled  ... re bug?
1639            assert m, "Quote expected in string at ^ in %s^%s" % (
1640                argstr[j - 20 : j],
1641                argstr[j : j + 20],
1642            )  # at least need a quote
1643
1644            i = m.start()
1645            try:
1646                ustr += argstr[j:i]
1647            except UnicodeError:
1648                err = ""
1649                for c in argstr[j:i]:
1650                    err = err + (" %02x" % ord(c))
1651                streason = sys.exc_info()[1].__str__()
1652                raise BadSyntax(
1653                    self._thisDoc,
1654                    startline,
1655                    argstr,
1656                    j,
1657                    "Unicode error appending characters"
1658                    + " %s to string, because\n\t%s" % (err, streason),
1659                )
1660
1661            # print "@@@ i = ",i, " j=",j, "m.end=", m.end()
1662
1663            ch = argstr[i]
1664            if ch == delim1:
1665                j = i
1666                continue
1667            elif ch in {'"', "'"} and ch != delim1:
1668                ustr += ch
1669                j = i + 1
1670                continue
1671            elif ch in {"\r", "\n"}:
1672                if delim == delim1:
1673                    raise BadSyntax(
1674                        self._thisDoc,
1675                        startline,
1676                        argstr,
1677                        i,
1678                        "newline found in string literal",
1679                    )
1680                self.lines += 1
1681                ustr += ch
1682                j = i + 1
1683                self.startOfLine = j
1684
1685            elif ch == "\\":
1686                j = i + 1
1687                ch = argstr[j]  # Will be empty if string ends
1688                if not ch:
1689                    raise BadSyntax(
1690                        self._thisDoc,
1691                        startline,
1692                        argstr,
1693                        i,
1694                        "unterminated string literal (2)",
1695                    )
1696                k = "abfrtvn\\\"'".find(ch)
1697                if k >= 0:
1698                    uch = "\a\b\f\r\t\v\n\\\"'"[k]
1699                    ustr += uch
1700                    j += 1
1701                elif ch == "u":
1702                    j, ch = self.uEscape(argstr, j + 1, startline)
1703                    ustr += ch
1704                elif ch == "U":
1705                    j, ch = self.UEscape(argstr, j + 1, startline)
1706                    ustr += ch
1707                else:
1708                    self.BadSyntax(argstr, i, "bad escape")
1709
1710        self.BadSyntax(argstr, i, "unterminated string literal")
1711
1712    def _unicodeEscape(
1713        self,
1714        argstr: str,
1715        i: int,
1716        startline: int,
1717        reg: Pattern[str],
1718        n: int,
1719        prefix: str,
1720    ) -> Tuple[int, str]:
1721        if len(argstr) < i + n:
1722            raise BadSyntax(
1723                self._thisDoc, startline, argstr, i, "unterminated string literal(3)"
1724            )
1725        try:
1726            return i + n, reg.sub(unicodeExpand, "\\" + prefix + argstr[i : i + n])
1727        except Exception:
1728            raise BadSyntax(
1729                self._thisDoc,
1730                startline,
1731                argstr,
1732                i,
1733                "bad string literal hex escape: " + argstr[i : i + n],
1734            )
1735
1736    def uEscape(self, argstr: str, i: int, startline: int) -> Tuple[int, str]:
1737        return self._unicodeEscape(argstr, i, startline, unicodeEscape4, 4, "u")
1738
1739    def UEscape(self, argstr: str, i: int, startline: int) -> Tuple[int, str]:
1740        return self._unicodeEscape(argstr, i, startline, unicodeEscape8, 8, "U")
1741
1742    def BadSyntax(self, argstr: str, i: int, msg: str) -> NoReturn:
1743        raise BadSyntax(self._thisDoc, self.lines, argstr, i, msg)
1744
1745
1746# If we are going to do operators then they should generate
1747# [  is  operator:plus  of (  \1  \2 ) ]
1748
1749
1750class BadSyntax(SyntaxError):  # noqa: N818
1751    def __init__(self, uri: str, lines: int, argstr: str, i: int, why: str):
1752        self._str = argstr.encode("utf-8")  # Better go back to strings for errors
1753        self._i = i
1754        self._why = why
1755        self.lines = lines
1756        self._uri = uri
1757
1758    def __str__(self) -> str:
1759        argstr = self._str
1760        i = self._i
1761        st = 0
1762        if i > 60:
1763            pre = "..."
1764            st = i - 60
1765        else:
1766            pre = ""
1767        if len(argstr) - i > 60:
1768            post = "..."
1769        else:
1770            post = ""
1771
1772        # type error: On Python 3 formatting "b'abc'" with "%s" produces "b'abc'", not "abc"; use "%r" if this is desired behavior
1773        return 'at line %i of <%s>:\nBad syntax (%s) at ^ in:\n"%s%s^%s%s"' % (
1774            self.lines + 1,  # type: ignore[str-bytes-safe]
1775            self._uri,
1776            self._why,
1777            pre,
1778            argstr[st:i],
1779            argstr[i : i + 60],
1780            post,
1781        )
1782
1783    @property
1784    def message(self) -> str:
1785        return str(self)
1786
1787
1788###############################################################################
1789class Formula:
1790    number = 0
1791
1792    def __init__(self, parent: Graph):
1793        self.uuid = uuid4().hex
1794        self.counter = 0
1795        Formula.number += 1
1796        self.number = Formula.number
1797        self.existentials: Dict[str, BNode] = {}
1798        self.universals: Dict[str, BNode] = {}
1799
1800        self.quotedgraph = QuotedGraph(store=parent.store, identifier=self.id())
1801
1802    def __str__(self) -> str:
1803        return "_:Formula%s" % self.number
1804
1805    def id(self) -> BNode:
1806        return BNode("_:Formula%s" % self.number)
1807
1808    def newBlankNode(
1809        self, uri: Optional[str] = None, why: Optional[Any] = None
1810    ) -> BNode:
1811        if uri is None:
1812            self.counter += 1
1813            bn = BNode("f%sb%s" % (self.uuid, self.counter))
1814        else:
1815            bn = BNode(uri.split("#").pop().replace("_", "b"))
1816        return bn
1817
1818    def newUniversal(self, uri: str, why: Optional[Any] = None) -> Variable:
1819        return Variable(uri.split("#").pop())
1820
1821    def declareExistential(self, x: str) -> None:
1822        self.existentials[x] = self.newBlankNode()
1823
1824    def close(self) -> QuotedGraph:
1825        return self.quotedgraph
1826
1827
1828r_hibyte = re.compile(r"([\x80-\xff])")
1829
1830
1831class RDFSink:
1832    def __init__(self, graph: Graph):
1833        self.rootFormula: Optional[Formula] = None
1834        self.uuid = uuid4().hex
1835        self.counter = 0
1836        self.graph = graph
1837
1838    def newFormula(self) -> Formula:
1839        fa = getattr(self.graph.store, "formula_aware", False)
1840        if not fa:
1841            raise ParserError(
1842                "Cannot create formula parser with non-formula-aware store."
1843            )
1844        f = Formula(self.graph)
1845        return f
1846
1847    def newGraph(self, identifier: Identifier) -> Graph:
1848        return Graph(self.graph.store, identifier)
1849
1850    def newSymbol(self, *args: str) -> URIRef:
1851        return URIRef(args[0])
1852
1853    def newBlankNode(
1854        self,
1855        arg: Optional[Union[Formula, Graph, Any]] = None,
1856        uri: Optional[str] = None,
1857        why: Optional[Callable[[], None]] = None,
1858    ) -> BNode:
1859        if isinstance(arg, Formula):
1860            return arg.newBlankNode(uri)
1861        elif isinstance(arg, Graph) or arg is None:
1862            self.counter += 1
1863            bn = BNode("n%sb%s" % (self.uuid, self.counter))
1864        else:
1865            bn = BNode(str(arg[0]).split("#").pop().replace("_", "b"))
1866        return bn
1867
1868    def newLiteral(self, s: str, dt: Optional[URIRef], lang: Optional[str]) -> Literal:
1869        if dt:
1870            return Literal(s, datatype=dt)
1871        else:
1872            return Literal(s, lang=lang)
1873
1874    def newList(self, n: typing.List[Any], f: Optional[Formula]) -> IdentifiedNode:
1875        nil = self.newSymbol("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil")
1876        if not n:
1877            return nil
1878
1879        first = self.newSymbol("http://www.w3.org/1999/02/22-rdf-syntax-ns#first")
1880        rest = self.newSymbol("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest")
1881        af = a = self.newBlankNode(f)
1882
1883        for ne in n[:-1]:
1884            self.makeStatement((f, first, a, ne))
1885            an = self.newBlankNode(f)
1886            self.makeStatement((f, rest, a, an))
1887            a = an
1888        self.makeStatement((f, first, a, n[-1]))
1889        self.makeStatement((f, rest, a, nil))
1890        return af
1891
1892    def newSet(self, *args: _AnyT) -> Set[_AnyT]:
1893        return set(args)
1894
1895    def setDefaultNamespace(self, *args: bytes) -> str:
1896        return ":".join(repr(n) for n in args)
1897
1898    def makeStatement(
1899        self,
1900        quadruple: Tuple[Optional[Union[Formula, Graph]], Node, Node, Node],
1901        why: Optional[Any] = None,
1902    ) -> None:
1903        f, p, s, o = quadruple
1904
1905        if hasattr(p, "formula"):
1906            raise ParserError("Formula used as predicate")
1907
1908        # type error: Argument 1 to "normalise" of "RDFSink" has incompatible type "Union[Formula, Graph, None]"; expected "Optional[Formula]"
1909        s = self.normalise(f, s)  # type: ignore[arg-type]
1910        p = self.normalise(f, p)  # type: ignore[arg-type]
1911        o = self.normalise(f, o)  # type: ignore[arg-type]
1912
1913        if f == self.rootFormula:
1914            # print s, p, o, '.'
1915            self.graph.add((s, p, o))
1916        elif isinstance(f, Formula):
1917            f.quotedgraph.add((s, p, o))
1918        else:
1919            # type error: Item "None" of "Optional[Graph]" has no attribute "add"
1920            f.add((s, p, o))  # type: ignore[union-attr]
1921
1922        # return str(quadruple)
1923
1924    def normalise(
1925        self,
1926        f: Optional[Formula],
1927        n: Union[Tuple[int, str], bool, int, Decimal, sfloat, _AnyT],
1928    ) -> Union[URIRef, Literal, BNode, _AnyT]:
1929        if isinstance(n, tuple):
1930            return URIRef(str(n[1]))
1931
1932        if isinstance(n, bool):
1933            s = Literal(str(n).lower(), datatype=BOOLEAN_DATATYPE)
1934            return s
1935
1936        if isinstance(n, int) or isinstance(n, long_type):
1937            s = Literal(str(n), datatype=INTEGER_DATATYPE)
1938            return s
1939
1940        if isinstance(n, Decimal):
1941            value = str(n)
1942            if value == "-0":
1943                value = "0"
1944            s = Literal(value, datatype=DECIMAL_DATATYPE)
1945            return s
1946
1947        if isinstance(n, sfloat):
1948            s = Literal(str(n), datatype=DOUBLE_DATATYPE)
1949            return s
1950
1951        if isinstance(f, Formula):
1952            if n in f.existentials:
1953                if TYPE_CHECKING:
1954                    assert isinstance(n, URIRef)
1955                return f.existentials[n]
1956
1957        # if isinstance(n, Var):
1958        #    if f.universals.has_key(n):
1959        #       return f.universals[n]
1960        #    f.universals[n] = f.newBlankNode()
1961        #    return f.universals[n]
1962        # type error: Incompatible return value type (got "Union[int, _AnyT]", expected "Union[URIRef, Literal, BNode, _AnyT]")  [return-value]
1963        return n
1964
1965    def intern(self, something: _AnyT) -> _AnyT:
1966        return something
1967
1968    def bind(self, pfx, uri) -> None:
1969        pass  # print pfx, ':', uri
1970
1971    def startDoc(self, formula: Optional[Formula]) -> None:
1972        self.rootFormula = formula
1973
1974    def endDoc(self, formula: Optional[Formula]) -> None:
1975        pass
1976
1977
1978###################################################
1979#
1980#  Utilities
1981#
1982
1983
1984def hexify(ustr: str) -> bytes:
1985    """Use URL encoding to return an ASCII string
1986    corresponding to the given UTF8 string
1987
1988    ```python
1989    >>> hexify("http://example/a b")
1990    b'http://example/a%20b'
1991
1992    ```
1993    """
1994    # s1=ustr.encode('utf-8')
1995    s = ""
1996    for ch in ustr:  # .encode('utf-8'):
1997        if ord(ch) > 126 or ord(ch) < 33:
1998            ch = "%%%02X" % ord(ch)
1999        else:
2000            ch = "%c" % ord(ch)
2001        s = s + ch
2002    return s.encode("latin-1")
2003
2004
2005class TurtleParser(Parser):
2006    """An RDFLib parser for Turtle
2007
2008    See http://www.w3.org/TR/turtle/
2009    """
2010
2011    def __init__(self):
2012        pass
2013
2014    def parse(
2015        self,
2016        source: InputSource,
2017        graph: Graph,
2018        encoding: Optional[str] = "utf-8",
2019        turtle: bool = True,
2020    ) -> None:
2021        if encoding not in [None, "utf-8"]:
2022            raise ParserError(
2023                "N3/Turtle files are always utf-8 encoded, I was passed: %s" % encoding
2024            )
2025
2026        sink = RDFSink(graph)
2027
2028        baseURI = graph.absolutize(source.getPublicId() or source.getSystemId() or "")
2029        p = SinkParser(sink, baseURI=baseURI, turtle=turtle)
2030        # N3 parser prefers str stream
2031        stream = source.getCharacterStream()
2032        if not stream:
2033            stream = source.getByteStream()
2034        p.loadStream(stream)
2035
2036        for prefix, namespace in p._bindings.items():
2037            graph.bind(prefix, namespace)
2038
2039
2040class N3Parser(TurtleParser):
2041    """An RDFLib parser for Notation3
2042
2043    See http://www.w3.org/DesignIssues/Notation3.html
2044    """
2045
2046    def __init__(self):
2047        pass
2048
2049    # type error: Signature of "parse" incompatible with supertype "TurtleParser"
2050    def parse(  # type: ignore[override]
2051        self, source: InputSource, graph: Graph, encoding: Optional[str] = "utf-8"
2052    ) -> None:
2053        # we're currently being handed a Graph, not a ConjunctiveGraph
2054        # context-aware is this implied by formula_aware
2055        ca = getattr(graph.store, "context_aware", False)
2056        fa = getattr(graph.store, "formula_aware", False)
2057        if not ca:
2058            raise ParserError("Cannot parse N3 into non-context-aware store.")
2059        elif not fa:
2060            raise ParserError("Cannot parse N3 into non-formula-aware store.")
2061
2062        conj_graph = Dataset(store=graph.store)
2063        conj_graph.default_context = graph  # TODO: CG __init__ should have a
2064        # default_context arg
2065        # TODO: update N3Processor so that it can use conj_graph as the sink
2066        conj_graph.namespace_manager = graph.namespace_manager
2067
2068        TurtleParser.parse(self, source, conj_graph, encoding, turtle=False)