Source code

Download source archive ↓

src/rdflib/plugins/sparql/parser.py

1"""
2SPARQL 1.1 Parser
3
4based on pyparsing
5"""
6
7from __future__ import annotations  # noqa: I001
8
9import re
10import sys
11from typing import Any, BinaryIO, List
12from typing import Optional as OptionalType
13from typing import TextIO, Tuple, Union
14
15from pyparsing import CaselessKeyword as Keyword  # watch out :)
16from pyparsing import (
17    Combine,
18    Forward,
19    Group,
20    Literal,
21    OneOrMore,
22    Optional,
23    ParseResults,
24    Regex,
25    Suppress,
26    ZeroOrMore,
27)
28
29import rdflib
30from rdflib.compat import decodeUnicodeEscape
31
32from . import operators as op
33from .pyparsing_compat import DelimitedList, combine_join_kwargs, rest_of_line
34from .parserutils import Comp, CompValue, Param, ParamList
35
36# from pyparsing import Keyword as CaseSensitiveKeyword
37
38
39DEBUG = False
40
41# ---------------- ACTIONS
42
43
44def neg(literal: rdflib.Literal) -> rdflib.Literal:
45    return rdflib.Literal(-literal, datatype=literal.datatype)
46
47
48def setLanguage(terms: Tuple[Any, OptionalType[str]]) -> rdflib.Literal:
49    return rdflib.Literal(terms[0], lang=terms[1])
50
51
52def setDataType(terms: Tuple[Any, OptionalType[str]]) -> rdflib.Literal:
53    return rdflib.Literal(terms[0], datatype=terms[1])
54
55
56def expandTriples(terms: ParseResults) -> List[Any]:
57    """
58    Expand ; and , syntax for repeat predicates, subjects
59    """
60    # import pdb; pdb.set_trace()
61    last_subject, last_predicate = None, None  # Used for ; and ,
62    try:
63        res: List[Any] = []
64        if DEBUG:
65            print("Terms", terms)
66        l_ = len(terms)
67        for i, t in enumerate(terms):
68            if t == ",":
69                res.extend([last_subject, last_predicate])
70            elif t == ";":
71                if i + 1 == len(terms) or terms[i + 1] == ";" or terms[i + 1] == ".":
72                    continue  # this semicolon is spurious
73                res.append(last_subject)
74            elif isinstance(t, list):
75                # BlankNodePropertyList
76                # is this bnode the object of previous triples?
77                if (len(res) % 3) == 2:
78                    res.append(t[0])
79                # is this a single [] ?
80                if len(t) > 1:
81                    res += t  # Don't update last_subject/last_predicate
82                # is this bnode the subject of more triples?
83                if i + 1 < l_ and terms[i + 1] not in [
84                    ".",
85                    ",",
86                    ";",
87                ]:  # term might not be a string
88                    last_subject, last_predicate = t[0], None
89                    res.append(t[0])
90            elif isinstance(t, ParseResults):
91                res += t.as_list()
92            elif t != ".":
93                res.append(t)
94                if (len(res) % 3) == 1:
95                    last_subject = t
96                elif (len(res) % 3) == 2:
97                    last_predicate = t
98            if DEBUG:
99                print(len(res), t)
100        if DEBUG:
101            import json
102
103            print(json.dumps(res, indent=2))
104
105        return res
106        # print res
107        # assert len(res)%3 == 0, \
108        #       "Length of triple-list is not divisible by 3: %d!"%len(res)
109
110        # return [tuple(res[i:i+3]) for i in range(len(res)/3)]
111    except:
112        if DEBUG:
113            import traceback
114
115            traceback.print_exc()
116        raise
117
118
119def expandBNodeTriples(terms: ParseResults) -> List[Any]:
120    """
121    expand [ ?p ?o ] syntax for implicit bnodes
122    """
123    # import pdb; pdb.set_trace()
124    try:
125        if DEBUG:
126            print("Bnode terms", terms)
127            print("1", terms[0])
128            print("2", [rdflib.BNode()] + terms.as_list()[0])
129        return [expandTriples([rdflib.BNode()] + terms.as_list()[0])]
130    except Exception as e:
131        if DEBUG:
132            print(">>>>>>>>", e)
133        raise
134
135
136def expandCollection(terms: ParseResults) -> List[List[Any]]:
137    """
138    expand ( 1 2 3 ) notation for collections
139    """
140    if DEBUG:
141        print("Collection: ", terms)
142
143    res: List[Any] = []
144    other = []
145    for x in terms:
146        if isinstance(x, list):  # is this a [ .. ] ?
147            other += x
148            x = x[0]
149
150        b = rdflib.BNode()
151        if res:
152            res += [res[-3], rdflib.RDF.rest, b, b, rdflib.RDF.first, x]
153        else:
154            res += [b, rdflib.RDF.first, x]
155    res += [b, rdflib.RDF.rest, rdflib.RDF.nil]
156
157    res += other
158
159    if DEBUG:
160        print("CollectionOut", res)
161    return [res]
162
163
164# SPARQL Grammar from http://www.w3.org/TR/sparql11-query/#grammar
165# ------ TERMINALS --------------
166# [139] IRIREF ::= '<' ([^<>"{}|^`\]-[#x00-#x20])* '>'
167IRIREF = Combine(
168    Suppress("<")
169    + Regex(r'[^<>"{}|^`\\%s]*' % "".join("\\x%02X" % i for i in range(33)))
170    + Suppress(">")
171)
172IRIREF.set_parse_action(lambda x: rdflib.URIRef(x[0]))
173
174# [164] P_CHARS_BASE ::= [A-Z] | [a-z] | [#x00C0-#x00D6] | [#x00D8-#x00F6] | [#x00F8-#x02FF] | [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
175
176if sys.maxunicode == 0xFFFF:
177    # this is narrow python build (default on windows/osx)
178    # this means that unicode code points over 0xffff are stored
179    # as several characters, which in turn means that regex character
180    # ranges with these characters do not work.
181    # See
182    # * http://bugs.python.org/issue12729
183    # * http://bugs.python.org/issue12749
184    # * http://bugs.python.org/issue3665
185    #
186    # Here we simple skip the [#x10000-#xEFFFF] part
187    # this means that some SPARQL queries will not parse :(
188    # We *could* generate a new regex with \U00010000|\U00010001 ...
189    # but it would be quite long :)
190    #
191    # in py3.3 this is fixed
192
193    PN_CHARS_BASE_re = "A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD"
194else:
195    # wide python build
196    PN_CHARS_BASE_re = "A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF"
197
198# [165] PN_CHARS_U ::= PN_CHARS_BASE | '_'
199PN_CHARS_U_re = "_" + PN_CHARS_BASE_re
200
201# [167] PN_CHARS ::= PN_CHARS_U | '-' | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040]
202PN_CHARS_re = "\\-0-9\u00B7\u0300-\u036F\u203F-\u2040" + PN_CHARS_U_re
203# PN_CHARS = Regex(u'[%s]'%PN_CHARS_re, flags=re.U)
204
205# [168] PN_PREFIX ::= PN_CHARS_BASE ((PN_CHARS|'.')* PN_CHARS)?
206PN_PREFIX = Regex(
207    "[%s](?:[%s\\.]*[%s])?" % (PN_CHARS_BASE_re, PN_CHARS_re, PN_CHARS_re), flags=re.U
208)
209
210# [140] PNAME_NS ::= PN_PREFIX? ':'
211PNAME_NS = Optional(Param("prefix", PN_PREFIX)) + Suppress(":").leave_whitespace()
212
213# [173] PN_LOCAL_ESC ::= '\' ( '_' | '~' | '.' | '-' | '!' | '$' | '&' | "'" | '(' | ')' | '*' | '+' | ',' | ';' | '=' | '/' | '?' | '#' | '@' | '%' )
214
215PN_LOCAL_ESC_re = "\\\\[_~\\.\\-!$&\"'()*+,;=/?#@%]"
216# PN_LOCAL_ESC = Regex(PN_LOCAL_ESC_re) # regex'd
217# PN_LOCAL_ESC.set_parse_action(lambda x: x[0][1:])
218
219# [172] HEX ::= [0-9] | [A-F] | [a-f]
220# HEX = Regex('[0-9A-Fa-f]') # not needed
221
222# [171] PERCENT ::= '%' HEX HEX
223PERCENT_re = "%[0-9a-fA-F]{2}"
224# PERCENT = Regex(PERCENT_re) # regex'd
225# PERCENT.set_parse_action(lambda x: chr(int(x[0][1:], 16)))
226
227# [170] PLX ::= PERCENT | PN_LOCAL_ESC
228PLX_re = "(%s|%s)" % (PN_LOCAL_ESC_re, PERCENT_re)
229# PLX = PERCENT | PN_LOCAL_ESC # regex'd
230
231
232# [169] PN_LOCAL ::= (PN_CHARS_U | ':' | [0-9] | PLX ) ((PN_CHARS | '.' | ':' | PLX)* (PN_CHARS | ':' | PLX) )?
233
234PN_LOCAL = Regex(
235    """([%(PN_CHARS_U)s:0-9]|%(PLX)s)
236                     (([%(PN_CHARS)s\\.:]|%(PLX)s)*
237                      ([%(PN_CHARS)s:]|%(PLX)s) )?"""
238    % dict(PN_CHARS_U=PN_CHARS_U_re, PN_CHARS=PN_CHARS_re, PLX=PLX_re),
239    flags=re.X | re.UNICODE,
240)
241
242
243# [141] PNAME_LN ::= PNAME_NS PN_LOCAL
244PNAME_LN = PNAME_NS + Param("localname", PN_LOCAL.leave_whitespace())
245
246# [142] BLANK_NODE_LABEL ::= '_:' ( PN_CHARS_U | [0-9] ) ((PN_CHARS|'.')* PN_CHARS)?
247BLANK_NODE_LABEL = Regex(
248    "_:[0-9%s](?:[\\.%s]*[%s])?" % (PN_CHARS_U_re, PN_CHARS_re, PN_CHARS_re),
249    flags=re.U,
250)
251BLANK_NODE_LABEL.set_parse_action(lambda x: rdflib.BNode(x[0][2:]))
252
253
254# [166] VARNAME ::= ( PN_CHARS_U | [0-9] ) ( PN_CHARS_U | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040] )*
255VARNAME = Regex(
256    "[%s0-9][%s0-9\u00B7\u0300-\u036F\u203F-\u2040]*" % (PN_CHARS_U_re, PN_CHARS_U_re),
257    flags=re.U,
258)
259
260# [143] VAR1 ::= '?' VARNAME
261VAR1 = Combine(Suppress("?") + VARNAME)
262
263# [144] VAR2 ::= '$' VARNAME
264VAR2 = Combine(Suppress("$") + VARNAME)
265
266# [145] LANGTAG ::= '@' [a-zA-Z]+ ('-' [a-zA-Z0-9]+)*
267LANGTAG = Combine(Suppress("@") + Regex("[a-zA-Z]+(?:-[a-zA-Z0-9]+)*"))
268
269# [146] INTEGER ::= [0-9]+
270INTEGER = Regex(r"[0-9]+")
271# INTEGER.set_results_name('integer')
272INTEGER.set_parse_action(lambda x: rdflib.Literal(x[0], datatype=rdflib.XSD.integer))
273
274# [155] EXPONENT ::= [eE] [+-]? [0-9]+
275EXPONENT_re = "[eE][+-]?[0-9]+"
276
277# [147] DECIMAL ::= [0-9]* '.' [0-9]+
278DECIMAL = Regex(r"[0-9]*\.[0-9]+")  # (?![eE])
279# DECIMAL.set_results_name('decimal')
280DECIMAL.set_parse_action(lambda x: rdflib.Literal(x[0], datatype=rdflib.XSD.decimal))
281
282# [148] DOUBLE ::= [0-9]+ '.' [0-9]* EXPONENT | '.' ([0-9])+ EXPONENT | ([0-9])+ EXPONENT
283DOUBLE = Regex(r"[0-9]+\.[0-9]*%(e)s|\.([0-9])+%(e)s|[0-9]+%(e)s" % {"e": EXPONENT_re})
284# DOUBLE.set_results_name('double')
285DOUBLE.set_parse_action(lambda x: rdflib.Literal(x[0], datatype=rdflib.XSD.double))
286
287
288# [149] INTEGER_POSITIVE ::= '+' INTEGER
289INTEGER_POSITIVE = Suppress("+") + INTEGER.copy().leave_whitespace()
290INTEGER_POSITIVE.set_parse_action(
291    lambda x: rdflib.Literal("+" + x[0], datatype=rdflib.XSD.integer)
292)
293
294# [150] DECIMAL_POSITIVE ::= '+' DECIMAL
295DECIMAL_POSITIVE = Suppress("+") + DECIMAL.copy().leave_whitespace()
296
297# [151] DOUBLE_POSITIVE ::= '+' DOUBLE
298DOUBLE_POSITIVE = Suppress("+") + DOUBLE.copy().leave_whitespace()
299
300# [152] INTEGER_NEGATIVE ::= '-' INTEGER
301INTEGER_NEGATIVE = Suppress("-") + INTEGER.copy().leave_whitespace()
302INTEGER_NEGATIVE.set_parse_action(lambda x: neg(x[0]))
303
304# [153] DECIMAL_NEGATIVE ::= '-' DECIMAL
305DECIMAL_NEGATIVE = Suppress("-") + DECIMAL.copy().leave_whitespace()
306DECIMAL_NEGATIVE.set_parse_action(lambda x: neg(x[0]))
307
308# [154] DOUBLE_NEGATIVE ::= '-' DOUBLE
309DOUBLE_NEGATIVE = Suppress("-") + DOUBLE.copy().leave_whitespace()
310DOUBLE_NEGATIVE.set_parse_action(lambda x: neg(x[0]))
311
312# [160] ECHAR ::= '\' [tbnrf\"']
313# ECHAR = Regex('\\\\[tbnrf"\']')
314
315
316# [158] STRING_LITERAL_LONG1 ::= "'''" ( ( "'" | "''" )? ( [^'\] | ECHAR ) )* "'''"
317# STRING_LITERAL_LONG1 = Literal("'''") + ( Optional( Literal("'") | "''"
318# ) + ZeroOrMore( ~ Literal("'\\") | ECHAR ) ) + "'''"
319STRING_LITERAL_LONG1 = Regex("'''((?:'|'')?(?:[^'\\\\]|\\\\['ntbrf\\\\]))*'''")
320STRING_LITERAL_LONG1.set_parse_action(
321    lambda x: rdflib.Literal(decodeUnicodeEscape(x[0][3:-3]))
322)
323
324# [159] STRING_LITERAL_LONG2 ::= '"""' ( ( '"' | '""' )? ( [^"\] | ECHAR ) )* '"""'
325# STRING_LITERAL_LONG2 = Literal('"""') + ( Optional( Literal('"') | '""'
326# ) + ZeroOrMore( ~ Literal('"\\') | ECHAR ) ) +  '"""'
327STRING_LITERAL_LONG2 = Regex('"""(?:(?:"|"")?(?:[^"\\\\]|\\\\["ntbrf\\\\]))*"""')
328STRING_LITERAL_LONG2.set_parse_action(
329    lambda x: rdflib.Literal(decodeUnicodeEscape(x[0][3:-3]))
330)
331
332# [156] STRING_LITERAL1 ::= "'" ( ([^#x27#x5C#xA#xD]) | ECHAR )* "'"
333# STRING_LITERAL1 = Literal("'") + ZeroOrMore(
334# Regex(u'[^\u0027\u005C\u000A\u000D]',flags=re.U) | ECHAR ) + "'"
335
336STRING_LITERAL1 = Regex("'(?:[^'\\n\\r\\\\]|\\\\['ntbrf\\\\])*'(?!')", flags=re.U)
337STRING_LITERAL1.set_parse_action(
338    lambda x: rdflib.Literal(decodeUnicodeEscape(x[0][1:-1]))
339)
340
341# [157] STRING_LITERAL2 ::= '"' ( ([^#x22#x5C#xA#xD]) | ECHAR )* '"'
342# STRING_LITERAL2 = Literal('"') + ZeroOrMore (
343# Regex(u'[^\u0022\u005C\u000A\u000D]',flags=re.U) | ECHAR ) + '"'
344
345STRING_LITERAL2 = Regex('"(?:[^"\\n\\r\\\\]|\\\\["ntbrf\\\\])*"(?!")', flags=re.U)
346STRING_LITERAL2.set_parse_action(
347    lambda x: rdflib.Literal(decodeUnicodeEscape(x[0][1:-1]))
348)
349
350# [161] NIL ::= '(' WS* ')'
351NIL = Literal("(") + ")"
352NIL.set_parse_action(lambda x: rdflib.RDF.nil)
353
354# [162] WS ::= #x20 | #x9 | #xD | #xA
355# Not needed?
356# WS = #x20 | #x9 | #xD | #xA
357# [163] ANON ::= '[' WS* ']'
358ANON = Literal("[") + "]"
359ANON.set_parse_action(lambda x: rdflib.BNode())
360
361# A = CaseSensitiveKeyword('a')
362A = Literal("a")
363A.set_parse_action(lambda x: rdflib.RDF.type)
364
365
366# ------ NON-TERMINALS --------------
367
368# [5] BaseDecl ::= 'BASE' IRIREF
369BaseDecl = Comp("Base", Keyword("BASE") + Param("iri", IRIREF))
370
371# [6] PrefixDecl ::= 'PREFIX' PNAME_NS IRIREF
372PrefixDecl = Comp("PrefixDecl", Keyword("PREFIX") + PNAME_NS + Param("iri", IRIREF))
373
374# [4] Prologue ::= ( BaseDecl | PrefixDecl )*
375Prologue = Group(ZeroOrMore(BaseDecl | PrefixDecl))
376
377# [108] Var ::= VAR1 | VAR2
378Var = VAR1 | VAR2
379Var.set_parse_action(lambda x: rdflib.term.Variable(x[0]))
380
381# [137] PrefixedName ::= PNAME_LN | PNAME_NS
382PrefixedName = Comp("pname", PNAME_LN | PNAME_NS)
383
384# [136] iri ::= IRIREF | PrefixedName
385iri = IRIREF | PrefixedName
386
387# [135] String ::= STRING_LITERAL1 | STRING_LITERAL2 | STRING_LITERAL_LONG1 | STRING_LITERAL_LONG2
388String = STRING_LITERAL_LONG1 | STRING_LITERAL_LONG2 | STRING_LITERAL1 | STRING_LITERAL2
389
390# [129] RDFLiteral ::= String ( LANGTAG | ( '^^' iri ) )?
391
392RDFLiteral = Comp(
393    "literal",
394    Param("string", String)
395    + Optional(
396        Param("lang", LANGTAG.leave_whitespace())
397        | Literal("^^").leave_whitespace() + Param("datatype", iri).leave_whitespace()
398    ),
399)
400
401# [132] NumericLiteralPositive ::= INTEGER_POSITIVE | DECIMAL_POSITIVE | DOUBLE_POSITIVE
402NumericLiteralPositive = DOUBLE_POSITIVE | DECIMAL_POSITIVE | INTEGER_POSITIVE
403
404# [133] NumericLiteralNegative ::= INTEGER_NEGATIVE | DECIMAL_NEGATIVE | DOUBLE_NEGATIVE
405NumericLiteralNegative = DOUBLE_NEGATIVE | DECIMAL_NEGATIVE | INTEGER_NEGATIVE
406
407# [131] NumericLiteralUnsigned ::= INTEGER | DECIMAL | DOUBLE
408NumericLiteralUnsigned = DOUBLE | DECIMAL | INTEGER
409
410# [130] NumericLiteral ::= NumericLiteralUnsigned | NumericLiteralPositive | NumericLiteralNegative
411NumericLiteral = (
412    NumericLiteralUnsigned | NumericLiteralPositive | NumericLiteralNegative
413)
414
415# [134] BooleanLiteral ::= 'true' | 'false'
416BooleanLiteral = Keyword("true").set_parse_action(
417    lambda: rdflib.Literal(True)
418) | Keyword("false").set_parse_action(lambda: rdflib.Literal(False))
419
420# [138] BlankNode ::= BLANK_NODE_LABEL | ANON
421BlankNode = BLANK_NODE_LABEL | ANON
422
423# [109] GraphTerm ::= iri | RDFLiteral | NumericLiteral | BooleanLiteral | BlankNode | NIL
424GraphTerm = iri | RDFLiteral | NumericLiteral | BooleanLiteral | BlankNode | NIL
425
426# [106] VarOrTerm ::= Var | GraphTerm
427VarOrTerm = Var | GraphTerm
428
429# [107] VarOrIri ::= Var | iri
430VarOrIri = Var | iri
431
432# [46] GraphRef ::= 'GRAPH' iri
433GraphRef = Keyword("GRAPH") + Param("graphiri", iri)
434
435# [47] GraphRefAll ::= GraphRef | 'DEFAULT' | 'NAMED' | 'ALL'
436GraphRefAll = (
437    GraphRef
438    | Param("graphiri", Keyword("DEFAULT"))
439    | Param("graphiri", Keyword("NAMED"))
440    | Param("graphiri", Keyword("ALL"))
441)
442
443# [45] GraphOrDefault ::= 'DEFAULT' | 'GRAPH'? iri
444GraphOrDefault = ParamList("graph", Keyword("DEFAULT")) | Optional(
445    Keyword("GRAPH")
446) + ParamList("graph", iri)
447
448# [65] DataBlockValue ::= iri | RDFLiteral | NumericLiteral | BooleanLiteral | 'UNDEF'
449DataBlockValue = iri | RDFLiteral | NumericLiteral | BooleanLiteral | Keyword("UNDEF")
450
451# [78] Verb ::= VarOrIri | A
452Verb = VarOrIri | A
453
454
455# [85] VerbSimple ::= Var
456VerbSimple = Var
457
458# [97] Integer ::= INTEGER
459Integer = INTEGER
460
461
462TriplesNode = Forward()
463TriplesNodePath = Forward()
464
465# [104] GraphNode ::= VarOrTerm | TriplesNode
466GraphNode = VarOrTerm | TriplesNode
467
468# [105] GraphNodePath ::= VarOrTerm | TriplesNodePath
469GraphNodePath = VarOrTerm | TriplesNodePath
470
471
472# [93] PathMod ::= '?' | '*' | '+'
473PathMod = Literal("?") | "*" | "+"
474
475# [96] PathOneInPropertySet ::= iri | A | '^' ( iri | A )
476PathOneInPropertySet = iri | A | Comp("InversePath", "^" + (iri | A))
477
478Path = Forward()
479
480# [95] PathNegatedPropertySet ::= PathOneInPropertySet | '(' ( PathOneInPropertySet ( '|' PathOneInPropertySet )* )? ')'
481PathNegatedPropertySet = Comp(
482    "PathNegatedPropertySet",
483    ParamList("part", PathOneInPropertySet)
484    | "("
485    + Optional(
486        ParamList("part", PathOneInPropertySet)
487        + ZeroOrMore("|" + ParamList("part", PathOneInPropertySet))
488    )
489    + ")",
490)
491
492# [94] PathPrimary ::= iri | A | '!' PathNegatedPropertySet | '(' Path ')' | 'DISTINCT' '(' Path ')'
493PathPrimary = (
494    iri
495    | A
496    | Suppress("!") + PathNegatedPropertySet
497    | Suppress("(") + Path + Suppress(")")
498    | Comp("DistinctPath", Keyword("DISTINCT") + "(" + Param("part", Path) + ")")
499)
500
501# [91] PathElt ::= PathPrimary Optional(PathMod)
502PathElt = Comp(
503    "PathElt",
504    Param("part", PathPrimary) + Optional(Param("mod", PathMod.leave_whitespace())),
505)
506
507# [92] PathEltOrInverse ::= PathElt | '^' PathElt
508PathEltOrInverse = PathElt | Suppress("^") + Comp(
509    "PathEltOrInverse", Param("part", PathElt)
510)
511
512# [90] PathSequence ::= PathEltOrInverse ( '/' PathEltOrInverse )*
513PathSequence = Comp(
514    "PathSequence",
515    ParamList("part", PathEltOrInverse)
516    + ZeroOrMore("/" + ParamList("part", PathEltOrInverse)),
517)
518
519
520# [89] PathAlternative ::= PathSequence ( '|' PathSequence )*
521PathAlternative = Comp(
522    "PathAlternative",
523    ParamList("part", PathSequence) + ZeroOrMore("|" + ParamList("part", PathSequence)),
524)
525
526# [88] Path ::= PathAlternative
527Path <<= PathAlternative
528
529# [84] VerbPath ::= Path
530VerbPath = Path
531
532# [87] ObjectPath ::= GraphNodePath
533ObjectPath = GraphNodePath
534
535# [86] ObjectListPath ::= ObjectPath ( ',' ObjectPath )*
536ObjectListPath = ObjectPath + ZeroOrMore("," + ObjectPath)
537
538
539GroupGraphPattern = Forward()
540
541
542# [102] Collection ::= '(' OneOrMore(GraphNode) ')'
543Collection = Suppress("(") + OneOrMore(GraphNode) + Suppress(")")
544Collection.set_parse_action(expandCollection)
545
546# [103] CollectionPath ::= '(' OneOrMore(GraphNodePath) ')'
547CollectionPath = Suppress("(") + OneOrMore(GraphNodePath) + Suppress(")")
548CollectionPath.set_parse_action(expandCollection)
549
550# [80] Object ::= GraphNode
551Object = GraphNode
552
553# [79] ObjectList ::= Object ( ',' Object )*
554ObjectList = Object + ZeroOrMore("," + Object)
555
556# [83] PropertyListPathNotEmpty ::= ( VerbPath | VerbSimple ) ObjectListPath ( ';' ( ( VerbPath | VerbSimple ) ObjectList )? )*
557PropertyListPathNotEmpty = (
558    (VerbPath | VerbSimple)
559    + ObjectListPath
560    + ZeroOrMore(";" + Optional((VerbPath | VerbSimple) + ObjectListPath))
561)
562
563# [82] PropertyListPath ::= Optional(PropertyListPathNotEmpty)
564PropertyListPath = Optional(PropertyListPathNotEmpty)
565
566# [77] PropertyListNotEmpty ::= Verb ObjectList ( ';' ( Verb ObjectList )? )*
567PropertyListNotEmpty = Verb + ObjectList + ZeroOrMore(";" + Optional(Verb + ObjectList))
568
569
570# [76] PropertyList ::= Optional(PropertyListNotEmpty)
571PropertyList = Optional(PropertyListNotEmpty)
572
573# [99] BlankNodePropertyList ::= '[' PropertyListNotEmpty ']'
574BlankNodePropertyList = Group(Suppress("[") + PropertyListNotEmpty + Suppress("]"))
575BlankNodePropertyList.set_parse_action(expandBNodeTriples)
576
577# [101] BlankNodePropertyListPath ::= '[' PropertyListPathNotEmpty ']'
578BlankNodePropertyListPath = Group(
579    Suppress("[") + PropertyListPathNotEmpty + Suppress("]")
580)
581BlankNodePropertyListPath.set_parse_action(expandBNodeTriples)
582
583# [98] TriplesNode ::= Collection | BlankNodePropertyList
584TriplesNode <<= Collection | BlankNodePropertyList
585
586# [100] TriplesNodePath ::= CollectionPath | BlankNodePropertyListPath
587TriplesNodePath <<= CollectionPath | BlankNodePropertyListPath
588
589# [75] TriplesSameSubject ::= VarOrTerm PropertyListNotEmpty | TriplesNode PropertyList
590TriplesSameSubject = VarOrTerm + PropertyListNotEmpty | TriplesNode + PropertyList
591TriplesSameSubject.set_parse_action(expandTriples)
592
593# [52] TriplesTemplate ::= TriplesSameSubject ( '.' TriplesTemplate? )?
594# NOTE: pyparsing.py handling of recursive rules is limited by python's recursion
595# limit.
596# (https://docs.python.org/3/library/sys.html#sys.setrecursionlimit)
597# To accommodate arbitrary amounts of triples this rule is rewritten to not be
598# recursive:
599# [52*] TriplesTemplate ::= TriplesSameSubject ( '.' TriplesSameSubject? )*
600TriplesTemplate = ParamList("triples", TriplesSameSubject) + ZeroOrMore(
601    Suppress(".") + Optional(ParamList("triples", TriplesSameSubject))
602)
603
604# [51] QuadsNotTriples ::= 'GRAPH' VarOrIri '{' Optional(TriplesTemplate) '}'
605QuadsNotTriples = Comp(
606    "QuadsNotTriples",
607    Keyword("GRAPH") + Param("term", VarOrIri) + "{" + Optional(TriplesTemplate) + "}",
608)
609
610# [50] Quads ::= Optional(TriplesTemplate) ( QuadsNotTriples '.'? Optional(TriplesTemplate) )*
611Quads = Comp(
612    "Quads",
613    Optional(TriplesTemplate)
614    + ZeroOrMore(
615        ParamList("quadsNotTriples", QuadsNotTriples)
616        + Optional(Suppress("."))
617        + Optional(TriplesTemplate)
618    ),
619)
620
621# [48] QuadPattern ::= '{' Quads '}'
622QuadPattern = "{" + Param("quads", Quads) + "}"
623
624# [49] QuadData ::= '{' Quads '}'
625QuadData = "{" + Param("quads", Quads) + "}"
626
627# [81] TriplesSameSubjectPath ::= VarOrTerm PropertyListPathNotEmpty | TriplesNodePath PropertyListPath
628TriplesSameSubjectPath = (
629    VarOrTerm + PropertyListPathNotEmpty | TriplesNodePath + PropertyListPath
630)
631TriplesSameSubjectPath.set_parse_action(expandTriples)
632
633# [55] TriplesBlock ::= TriplesSameSubjectPath ( '.' Optional(TriplesBlock) )?
634TriplesBlock = Forward()
635TriplesBlock <<= ParamList("triples", TriplesSameSubjectPath) + Optional(
636    Suppress(".") + Optional(TriplesBlock)
637)
638
639
640# [66] MinusGraphPattern ::= 'MINUS' GroupGraphPattern
641MinusGraphPattern = Comp(
642    "MinusGraphPattern", Keyword("MINUS") + Param("graph", GroupGraphPattern)
643)
644
645# [67] GroupOrUnionGraphPattern ::= GroupGraphPattern ( 'UNION' GroupGraphPattern )*
646GroupOrUnionGraphPattern = Comp(
647    "GroupOrUnionGraphPattern",
648    ParamList("graph", GroupGraphPattern)
649    + ZeroOrMore(Keyword("UNION") + ParamList("graph", GroupGraphPattern)),
650)
651
652
653Expression = Forward()
654
655# [72] ExpressionList ::= NIL | '(' Expression ( ',' Expression )* ')'
656ExpressionList = NIL | Group(Suppress("(") + DelimitedList(Expression) + Suppress(")"))
657
658# [122] RegexExpression ::= 'REGEX' '(' Expression ',' Expression ( ',' Expression )? ')'
659RegexExpression = Comp(
660    "Builtin_REGEX",
661    Keyword("REGEX")
662    + "("
663    + Param("text", Expression)
664    + ","
665    + Param("pattern", Expression)
666    + Optional("," + Param("flags", Expression))
667    + ")",
668)
669RegexExpression.setEvalFn(op.Builtin_REGEX)
670
671# [123] SubstringExpression ::= 'SUBSTR' '(' Expression ',' Expression ( ',' Expression )? ')'
672SubstringExpression = Comp(
673    "Builtin_SUBSTR",
674    Keyword("SUBSTR")
675    + "("
676    + Param("arg", Expression)
677    + ","
678    + Param("start", Expression)
679    + Optional("," + Param("length", Expression))
680    + ")",
681).setEvalFn(op.Builtin_SUBSTR)
682
683# [124] StrReplaceExpression ::= 'REPLACE' '(' Expression ',' Expression ',' Expression ( ',' Expression )? ')'
684StrReplaceExpression = Comp(
685    "Builtin_REPLACE",
686    Keyword("REPLACE")
687    + "("
688    + Param("arg", Expression)
689    + ","
690    + Param("pattern", Expression)
691    + ","
692    + Param("replacement", Expression)
693    + Optional("," + Param("flags", Expression))
694    + ")",
695).setEvalFn(op.Builtin_REPLACE)
696
697# [125] ExistsFunc ::= 'EXISTS' GroupGraphPattern
698ExistsFunc = Comp(
699    "Builtin_EXISTS", Keyword("EXISTS") + Param("graph", GroupGraphPattern)
700).setEvalFn(op.Builtin_EXISTS)
701
702# [126] NotExistsFunc ::= 'NOT' 'EXISTS' GroupGraphPattern
703NotExistsFunc = Comp(
704    "Builtin_NOTEXISTS",
705    Keyword("NOT") + Keyword("EXISTS") + Param("graph", GroupGraphPattern),
706).setEvalFn(op.Builtin_EXISTS)
707
708
709# [127] Aggregate ::= 'COUNT' '(' 'DISTINCT'? ( '*' | Expression ) ')'
710# | 'SUM' '(' Optional('DISTINCT') Expression ')'
711# | 'MIN' '(' Optional('DISTINCT') Expression ')'
712# | 'MAX' '(' Optional('DISTINCT') Expression ')'
713# | 'AVG' '(' Optional('DISTINCT') Expression ')'
714# | 'SAMPLE' '(' Optional('DISTINCT') Expression ')'
715# | 'GROUP_CONCAT' '(' Optional('DISTINCT') Expression ( ';' 'SEPARATOR' '=' String )? ')'
716
717_Distinct = Optional(Keyword("DISTINCT"))
718_AggregateParams = "(" + Param("distinct", _Distinct) + Param("vars", Expression) + ")"
719
720Aggregate = (
721    Comp(
722        "Aggregate_Count",
723        Keyword("COUNT")
724        + "("
725        + Param("distinct", _Distinct)
726        + Param("vars", "*" | Expression)
727        + ")",
728    )
729    | Comp("Aggregate_Sum", Keyword("SUM") + _AggregateParams)
730    | Comp("Aggregate_Min", Keyword("MIN") + _AggregateParams)
731    | Comp("Aggregate_Max", Keyword("MAX") + _AggregateParams)
732    | Comp("Aggregate_Avg", Keyword("AVG") + _AggregateParams)
733    | Comp("Aggregate_Sample", Keyword("SAMPLE") + _AggregateParams)
734    | Comp(
735        "Aggregate_GroupConcat",
736        Keyword("GROUP_CONCAT")
737        + "("
738        + Param("distinct", _Distinct)
739        + Param("vars", Expression)
740        + Optional(";" + Keyword("SEPARATOR") + "=" + Param("separator", String))
741        + ")",
742    )
743)
744
745# [121] BuiltInCall ::= Aggregate
746# | 'STR' '(' + Expression + ')'
747# | 'LANG' '(' + Expression + ')'
748# | 'LANGMATCHES' '(' + Expression + ',' + Expression + ')'
749# | 'DATATYPE' '(' + Expression + ')'
750# | 'BOUND' '(' Var ')'
751# | 'IRI' '(' + Expression + ')'
752# | 'URI' '(' + Expression + ')'
753# | 'BNODE' ( '(' + Expression + ')' | NIL )
754# | 'RAND' NIL
755# | 'ABS' '(' + Expression + ')'
756# | 'CEIL' '(' + Expression + ')'
757# | 'FLOOR' '(' + Expression + ')'
758# | 'ROUND' '(' + Expression + ')'
759# | 'CONCAT' ExpressionList
760# | SubstringExpression
761# | 'STRLEN' '(' + Expression + ')'
762# | StrReplaceExpression
763# | 'UCASE' '(' + Expression + ')'
764# | 'LCASE' '(' + Expression + ')'
765# | 'ENCODE_FOR_URI' '(' + Expression + ')'
766# | 'CONTAINS' '(' + Expression + ',' + Expression + ')'
767# | 'STRSTARTS' '(' + Expression + ',' + Expression + ')'
768# | 'STRENDS' '(' + Expression + ',' + Expression + ')'
769# | 'STRBEFORE' '(' + Expression + ',' + Expression + ')'
770# | 'STRAFTER' '(' + Expression + ',' + Expression + ')'
771# | 'YEAR' '(' + Expression + ')'
772# | 'MONTH' '(' + Expression + ')'
773# | 'DAY' '(' + Expression + ')'
774# | 'HOURS' '(' + Expression + ')'
775# | 'MINUTES' '(' + Expression + ')'
776# | 'SECONDS' '(' + Expression + ')'
777# | 'TIMEZONE' '(' + Expression + ')'
778# | 'TZ' '(' + Expression + ')'
779# | 'NOW' NIL
780# | 'UUID' NIL
781# | 'STRUUID' NIL
782# | 'MD5' '(' + Expression + ')'
783# | 'SHA1' '(' + Expression + ')'
784# | 'SHA256' '(' + Expression + ')'
785# | 'SHA384' '(' + Expression + ')'
786# | 'SHA512' '(' + Expression + ')'
787# | 'COALESCE' ExpressionList
788# | 'IF' '(' Expression ',' Expression ',' Expression ')'
789# | 'STRLANG' '(' + Expression + ',' + Expression + ')'
790# | 'STRDT' '(' + Expression + ',' + Expression + ')'
791# | 'sameTerm' '(' + Expression + ',' + Expression + ')'
792# | 'isIRI' '(' + Expression + ')'
793# | 'isURI' '(' + Expression + ')'
794# | 'isBLANK' '(' + Expression + ')'
795# | 'isLITERAL' '(' + Expression + ')'
796# | 'isNUMERIC' '(' + Expression + ')'
797# | RegexExpression
798# | ExistsFunc
799# | NotExistsFunc
800
801BuiltInCall = (
802    Aggregate
803    | Comp(
804        "Builtin_STR", Keyword("STR") + "(" + Param("arg", Expression) + ")"
805    ).setEvalFn(op.Builtin_STR)
806    | Comp(
807        "Builtin_LANG", Keyword("LANG") + "(" + Param("arg", Expression) + ")"
808    ).setEvalFn(op.Builtin_LANG)
809    | Comp(
810        "Builtin_LANGMATCHES",
811        Keyword("LANGMATCHES")
812        + "("
813        + Param("arg1", Expression)
814        + ","
815        + Param("arg2", Expression)
816        + ")",
817    ).setEvalFn(op.Builtin_LANGMATCHES)
818    | Comp(
819        "Builtin_DATATYPE", Keyword("DATATYPE") + "(" + Param("arg", Expression) + ")"
820    ).setEvalFn(op.Builtin_DATATYPE)
821    | Comp("Builtin_BOUND", Keyword("BOUND") + "(" + Param("arg", Var) + ")").setEvalFn(
822        op.Builtin_BOUND
823    )
824    | Comp(
825        "Builtin_IRI", Keyword("IRI") + "(" + Param("arg", Expression) + ")"
826    ).setEvalFn(op.Builtin_IRI)
827    | Comp(
828        "Builtin_URI", Keyword("URI") + "(" + Param("arg", Expression) + ")"
829    ).setEvalFn(op.Builtin_IRI)
830    | Comp(
831        "Builtin_BNODE", Keyword("BNODE") + ("(" + Param("arg", Expression) + ")" | NIL)
832    ).setEvalFn(op.Builtin_BNODE)
833    | Comp("Builtin_RAND", Keyword("RAND") + NIL).setEvalFn(op.Builtin_RAND)
834    | Comp(
835        "Builtin_ABS", Keyword("ABS") + "(" + Param("arg", Expression) + ")"
836    ).setEvalFn(op.Builtin_ABS)
837    | Comp(
838        "Builtin_CEIL", Keyword("CEIL") + "(" + Param("arg", Expression) + ")"
839    ).setEvalFn(op.Builtin_CEIL)
840    | Comp(
841        "Builtin_FLOOR", Keyword("FLOOR") + "(" + Param("arg", Expression) + ")"
842    ).setEvalFn(op.Builtin_FLOOR)
843    | Comp(
844        "Builtin_ROUND", Keyword("ROUND") + "(" + Param("arg", Expression) + ")"
845    ).setEvalFn(op.Builtin_ROUND)
846    | Comp(
847        "Builtin_CONCAT", Keyword("CONCAT") + Param("arg", ExpressionList)
848    ).setEvalFn(op.Builtin_CONCAT)
849    | SubstringExpression
850    | Comp(
851        "Builtin_STRLEN", Keyword("STRLEN") + "(" + Param("arg", Expression) + ")"
852    ).setEvalFn(op.Builtin_STRLEN)
853    | StrReplaceExpression
854    | Comp(
855        "Builtin_UCASE", Keyword("UCASE") + "(" + Param("arg", Expression) + ")"
856    ).setEvalFn(op.Builtin_UCASE)
857    | Comp(
858        "Builtin_LCASE", Keyword("LCASE") + "(" + Param("arg", Expression) + ")"
859    ).setEvalFn(op.Builtin_LCASE)
860    | Comp(
861        "Builtin_ENCODE_FOR_URI",
862        Keyword("ENCODE_FOR_URI") + "(" + Param("arg", Expression) + ")",
863    ).setEvalFn(op.Builtin_ENCODE_FOR_URI)
864    | Comp(
865        "Builtin_CONTAINS",
866        Keyword("CONTAINS")
867        + "("
868        + Param("arg1", Expression)
869        + ","
870        + Param("arg2", Expression)
871        + ")",
872    ).setEvalFn(op.Builtin_CONTAINS)
873    | Comp(
874        "Builtin_STRSTARTS",
875        Keyword("STRSTARTS")
876        + "("
877        + Param("arg1", Expression)
878        + ","
879        + Param("arg2", Expression)
880        + ")",
881    ).setEvalFn(op.Builtin_STRSTARTS)
882    | Comp(
883        "Builtin_STRENDS",
884        Keyword("STRENDS")
885        + "("
886        + Param("arg1", Expression)
887        + ","
888        + Param("arg2", Expression)
889        + ")",
890    ).setEvalFn(op.Builtin_STRENDS)
891    | Comp(
892        "Builtin_STRBEFORE",
893        Keyword("STRBEFORE")
894        + "("
895        + Param("arg1", Expression)
896        + ","
897        + Param("arg2", Expression)
898        + ")",
899    ).setEvalFn(op.Builtin_STRBEFORE)
900    | Comp(
901        "Builtin_STRAFTER",
902        Keyword("STRAFTER")
903        + "("
904        + Param("arg1", Expression)
905        + ","
906        + Param("arg2", Expression)
907        + ")",
908    ).setEvalFn(op.Builtin_STRAFTER)
909    | Comp(
910        "Builtin_YEAR", Keyword("YEAR") + "(" + Param("arg", Expression) + ")"
911    ).setEvalFn(op.Builtin_YEAR)
912    | Comp(
913        "Builtin_MONTH", Keyword("MONTH") + "(" + Param("arg", Expression) + ")"
914    ).setEvalFn(op.Builtin_MONTH)
915    | Comp(
916        "Builtin_DAY", Keyword("DAY") + "(" + Param("arg", Expression) + ")"
917    ).setEvalFn(op.Builtin_DAY)
918    | Comp(
919        "Builtin_HOURS", Keyword("HOURS") + "(" + Param("arg", Expression) + ")"
920    ).setEvalFn(op.Builtin_HOURS)
921    | Comp(
922        "Builtin_MINUTES", Keyword("MINUTES") + "(" + Param("arg", Expression) + ")"
923    ).setEvalFn(op.Builtin_MINUTES)
924    | Comp(
925        "Builtin_SECONDS", Keyword("SECONDS") + "(" + Param("arg", Expression) + ")"
926    ).setEvalFn(op.Builtin_SECONDS)
927    | Comp(
928        "Builtin_TIMEZONE", Keyword("TIMEZONE") + "(" + Param("arg", Expression) + ")"
929    ).setEvalFn(op.Builtin_TIMEZONE)
930    | Comp(
931        "Builtin_TZ", Keyword("TZ") + "(" + Param("arg", Expression) + ")"
932    ).setEvalFn(op.Builtin_TZ)
933    | Comp("Builtin_NOW", Keyword("NOW") + NIL).setEvalFn(op.Builtin_NOW)
934    | Comp("Builtin_UUID", Keyword("UUID") + NIL).setEvalFn(op.Builtin_UUID)
935    | Comp("Builtin_STRUUID", Keyword("STRUUID") + NIL).setEvalFn(op.Builtin_STRUUID)
936    | Comp(
937        "Builtin_MD5", Keyword("MD5") + "(" + Param("arg", Expression) + ")"
938    ).setEvalFn(op.Builtin_MD5)
939    | Comp(
940        "Builtin_SHA1", Keyword("SHA1") + "(" + Param("arg", Expression) + ")"
941    ).setEvalFn(op.Builtin_SHA1)
942    | Comp(
943        "Builtin_SHA256", Keyword("SHA256") + "(" + Param("arg", Expression) + ")"
944    ).setEvalFn(op.Builtin_SHA256)
945    | Comp(
946        "Builtin_SHA384", Keyword("SHA384") + "(" + Param("arg", Expression) + ")"
947    ).setEvalFn(op.Builtin_SHA384)
948    | Comp(
949        "Builtin_SHA512", Keyword("SHA512") + "(" + Param("arg", Expression) + ")"
950    ).setEvalFn(op.Builtin_SHA512)
951    | Comp(
952        "Builtin_COALESCE", Keyword("COALESCE") + Param("arg", ExpressionList)
953    ).setEvalFn(op.Builtin_COALESCE)
954    | Comp(
955        "Builtin_IF",
956        Keyword("IF")
957        + "("
958        + Param("arg1", Expression)
959        + ","
960        + Param("arg2", Expression)
961        + ","
962        + Param("arg3", Expression)
963        + ")",
964    ).setEvalFn(op.Builtin_IF)
965    | Comp(
966        "Builtin_STRLANG",
967        Keyword("STRLANG")
968        + "("
969        + Param("arg1", Expression)
970        + ","
971        + Param("arg2", Expression)
972        + ")",
973    ).setEvalFn(op.Builtin_STRLANG)
974    | Comp(
975        "Builtin_STRDT",
976        Keyword("STRDT")
977        + "("
978        + Param("arg1", Expression)
979        + ","
980        + Param("arg2", Expression)
981        + ")",
982    ).setEvalFn(op.Builtin_STRDT)
983    | Comp(
984        "Builtin_sameTerm",
985        Keyword("sameTerm")
986        + "("
987        + Param("arg1", Expression)
988        + ","
989        + Param("arg2", Expression)
990        + ")",
991    ).setEvalFn(op.Builtin_sameTerm)
992    | Comp(
993        "Builtin_isIRI", Keyword("isIRI") + "(" + Param("arg", Expression) + ")"
994    ).setEvalFn(op.Builtin_isIRI)
995    | Comp(
996        "Builtin_isURI", Keyword("isURI") + "(" + Param("arg", Expression) + ")"
997    ).setEvalFn(op.Builtin_isIRI)
998    | Comp(
999        "Builtin_isBLANK", Keyword("isBLANK") + "(" + Param("arg", Expression) + ")"
1000    ).setEvalFn(op.Builtin_isBLANK)
1001    | Comp(
1002        "Builtin_isLITERAL", Keyword("isLITERAL") + "(" + Param("arg", Expression) + ")"
1003    ).setEvalFn(op.Builtin_isLITERAL)
1004    | Comp(
1005        "Builtin_isNUMERIC", Keyword("isNUMERIC") + "(" + Param("arg", Expression) + ")"
1006    ).setEvalFn(op.Builtin_isNUMERIC)
1007    | RegexExpression
1008    | ExistsFunc
1009    | NotExistsFunc
1010)
1011
1012# [71] ArgList ::= NIL | '(' 'DISTINCT'? Expression ( ',' Expression )* ')'
1013ArgList = (
1014    NIL
1015    | "("
1016    + Param("distinct", _Distinct)
1017    + DelimitedList(ParamList("expr", Expression))
1018    + ")"
1019)
1020
1021# [128] iriOrFunction ::= iri Optional(ArgList)
1022iriOrFunction = (
1023    Comp("Function", Param("iri", iri) + ArgList).setEvalFn(op.Function)
1024) | iri
1025
1026# [70] FunctionCall ::= iri ArgList
1027FunctionCall = Comp("Function", Param("iri", iri) + ArgList).setEvalFn(op.Function)
1028
1029
1030# [120] BrackettedExpression ::= '(' Expression ')'
1031BrackettedExpression = Suppress("(") + Expression + Suppress(")")
1032
1033# [119] PrimaryExpression ::= BrackettedExpression | BuiltInCall | iriOrFunction | RDFLiteral | NumericLiteral | BooleanLiteral | Var
1034PrimaryExpression = (
1035    BrackettedExpression
1036    | BuiltInCall
1037    | iriOrFunction
1038    | RDFLiteral
1039    | NumericLiteral
1040    | BooleanLiteral
1041    | Var
1042)
1043
1044# [118] UnaryExpression ::= '!' PrimaryExpression
1045# | '+' PrimaryExpression
1046# | '-' PrimaryExpression
1047# | PrimaryExpression
1048UnaryExpression = (
1049    Comp("UnaryNot", "!" + Param("expr", PrimaryExpression)).setEvalFn(op.UnaryNot)
1050    | Comp("UnaryPlus", "+" + Param("expr", PrimaryExpression)).setEvalFn(op.UnaryPlus)
1051    | Comp("UnaryMinus", "-" + Param("expr", PrimaryExpression)).setEvalFn(
1052        op.UnaryMinus
1053    )
1054    | PrimaryExpression
1055)
1056
1057# [117] MultiplicativeExpression ::= UnaryExpression ( '*' UnaryExpression | '/' UnaryExpression )*
1058MultiplicativeExpression = Comp(
1059    "MultiplicativeExpression",
1060    Param("expr", UnaryExpression)
1061    + ZeroOrMore(
1062        ParamList("op", "*") + ParamList("other", UnaryExpression)
1063        | ParamList("op", "/") + ParamList("other", UnaryExpression)
1064    ),
1065).setEvalFn(op.MultiplicativeExpression)
1066
1067# [116] AdditiveExpression ::= MultiplicativeExpression ( '+' MultiplicativeExpression | '-' MultiplicativeExpression | ( NumericLiteralPositive | NumericLiteralNegative ) ( ( '*' UnaryExpression ) | ( '/' UnaryExpression ) )* )*
1068
1069# NOTE: The second part of this production is there because:
1070# "In signed numbers, no white space is allowed between the sign and the number. The AdditiveExpression grammar rule allows for this by covering the two cases of an expression followed by a signed number. These produce an addition or subtraction of the unsigned number as appropriate."
1071
1072# Here (I think) this is not necessary since pyparsing doesn't separate
1073# tokenizing and parsing
1074
1075
1076AdditiveExpression = Comp(
1077    "AdditiveExpression",
1078    Param("expr", MultiplicativeExpression)
1079    + ZeroOrMore(
1080        ParamList("op", "+") + ParamList("other", MultiplicativeExpression)
1081        | ParamList("op", "-") + ParamList("other", MultiplicativeExpression)
1082    ),
1083).setEvalFn(op.AdditiveExpression)
1084
1085
1086# [115] NumericExpression ::= AdditiveExpression
1087NumericExpression = AdditiveExpression
1088
1089# [114] RelationalExpression ::= NumericExpression ( '=' NumericExpression | '!=' NumericExpression | '<' NumericExpression | '>' NumericExpression | '<=' NumericExpression | '>=' NumericExpression | 'IN' ExpressionList | 'NOT' 'IN' ExpressionList )?
1090RelationalExpression = Comp(
1091    "RelationalExpression",
1092    Param("expr", NumericExpression)
1093    + Optional(
1094        Param("op", "=") + Param("other", NumericExpression)
1095        | Param("op", "!=") + Param("other", NumericExpression)
1096        | Param("op", "<") + Param("other", NumericExpression)
1097        | Param("op", ">") + Param("other", NumericExpression)
1098        | Param("op", "<=") + Param("other", NumericExpression)
1099        | Param("op", ">=") + Param("other", NumericExpression)
1100        | Param("op", Keyword("IN")) + Param("other", ExpressionList)
1101        | Param(
1102            "op",
1103            Combine(
1104                Keyword("NOT") + Keyword("IN"),
1105                adjacent=False,
1106                **combine_join_kwargs(" "),
1107            ),
1108        )
1109        + Param("other", ExpressionList)
1110    ),
1111).setEvalFn(op.RelationalExpression)
1112
1113
1114# [113] ValueLogical ::= RelationalExpression
1115ValueLogical = RelationalExpression
1116
1117# [112] ConditionalAndExpression ::= ValueLogical ( '&&' ValueLogical )*
1118ConditionalAndExpression = Comp(
1119    "ConditionalAndExpression",
1120    Param("expr", ValueLogical) + ZeroOrMore("&&" + ParamList("other", ValueLogical)),
1121).setEvalFn(op.ConditionalAndExpression)
1122
1123# [111] ConditionalOrExpression ::= ConditionalAndExpression ( '||' ConditionalAndExpression )*
1124ConditionalOrExpression = Comp(
1125    "ConditionalOrExpression",
1126    Param("expr", ConditionalAndExpression)
1127    + ZeroOrMore("||" + ParamList("other", ConditionalAndExpression)),
1128).setEvalFn(op.ConditionalOrExpression)
1129
1130# [110] Expression ::= ConditionalOrExpression
1131Expression <<= ConditionalOrExpression
1132
1133
1134# [69] Constraint ::= BrackettedExpression | BuiltInCall | FunctionCall
1135Constraint = BrackettedExpression | BuiltInCall | FunctionCall
1136
1137# [68] Filter ::= 'FILTER' Constraint
1138Filter = Comp("Filter", Keyword("FILTER") + Param("expr", Constraint))
1139
1140
1141# [16] SourceSelector ::= iri
1142SourceSelector = iri
1143
1144# [14] DefaultGraphClause ::= SourceSelector
1145DefaultGraphClause = SourceSelector
1146
1147# [15] NamedGraphClause ::= 'NAMED' SourceSelector
1148NamedGraphClause = Keyword("NAMED") + Param("named", SourceSelector)
1149
1150# [13] DatasetClause ::= 'FROM' ( DefaultGraphClause | NamedGraphClause )
1151DatasetClause = Comp(
1152    "DatasetClause",
1153    Keyword("FROM") + (Param("default", DefaultGraphClause) | NamedGraphClause),
1154)
1155
1156# [20] GroupCondition ::= BuiltInCall | FunctionCall | '(' Expression ( 'AS' Var )? ')' | Var
1157GroupCondition = (
1158    BuiltInCall
1159    | FunctionCall
1160    | Comp(
1161        "GroupAs",
1162        "("
1163        + Param("expr", Expression)
1164        + Optional(Keyword("AS") + Param("var", Var))
1165        + ")",
1166    )
1167    | Var
1168)
1169
1170# [19] GroupClause ::= 'GROUP' 'BY' GroupCondition+
1171GroupClause = Comp(
1172    "GroupClause",
1173    Keyword("GROUP")
1174    + Keyword("BY")
1175    + OneOrMore(ParamList("condition", GroupCondition)),
1176)
1177
1178
1179_Silent = Optional(Param("silent", Keyword("SILENT")))
1180
1181# [31] Load ::= 'LOAD' 'SILENT'? iri ( 'INTO' GraphRef )?
1182Load = Comp(
1183    "Load",
1184    Keyword("LOAD")
1185    + _Silent
1186    + Param("iri", iri)
1187    + Optional(Keyword("INTO") + GraphRef),
1188)
1189
1190# [32] Clear ::= 'CLEAR' 'SILENT'? GraphRefAll
1191Clear = Comp("Clear", Keyword("CLEAR") + _Silent + GraphRefAll)
1192
1193# [33] Drop ::= 'DROP' _Silent GraphRefAll
1194Drop = Comp("Drop", Keyword("DROP") + _Silent + GraphRefAll)
1195
1196# [34] Create ::= 'CREATE' _Silent GraphRef
1197Create = Comp("Create", Keyword("CREATE") + _Silent + GraphRef)
1198
1199# [35] Add ::= 'ADD' _Silent GraphOrDefault 'TO' GraphOrDefault
1200Add = Comp(
1201    "Add", Keyword("ADD") + _Silent + GraphOrDefault + Keyword("TO") + GraphOrDefault
1202)
1203
1204# [36] Move ::= 'MOVE' _Silent GraphOrDefault 'TO' GraphOrDefault
1205Move = Comp(
1206    "Move", Keyword("MOVE") + _Silent + GraphOrDefault + Keyword("TO") + GraphOrDefault
1207)
1208
1209# [37] Copy ::= 'COPY' _Silent GraphOrDefault 'TO' GraphOrDefault
1210Copy = Comp(
1211    "Copy", Keyword("COPY") + _Silent + GraphOrDefault + Keyword("TO") + GraphOrDefault
1212)
1213
1214# [38] InsertData ::= 'INSERT DATA' QuadData
1215InsertData = Comp("InsertData", Keyword("INSERT") + Keyword("DATA") + QuadData)
1216
1217# [39] DeleteData ::= 'DELETE DATA' QuadData
1218DeleteData = Comp("DeleteData", Keyword("DELETE") + Keyword("DATA") + QuadData)
1219
1220# [40] DeleteWhere ::= 'DELETE WHERE' QuadPattern
1221DeleteWhere = Comp("DeleteWhere", Keyword("DELETE") + Keyword("WHERE") + QuadPattern)
1222
1223# [42] DeleteClause ::= 'DELETE' QuadPattern
1224DeleteClause = Comp("DeleteClause", Keyword("DELETE") + QuadPattern)
1225
1226# [43] InsertClause ::= 'INSERT' QuadPattern
1227InsertClause = Comp("InsertClause", Keyword("INSERT") + QuadPattern)
1228
1229# [44] UsingClause ::= 'USING' ( iri | 'NAMED' iri )
1230UsingClause = Comp(
1231    "UsingClause",
1232    Keyword("USING") + (Param("default", iri) | Keyword("NAMED") + Param("named", iri)),
1233)
1234
1235# [41] Modify ::= ( 'WITH' iri )? ( DeleteClause Optional(InsertClause) | InsertClause ) ZeroOrMore(UsingClause) 'WHERE' GroupGraphPattern
1236Modify = Comp(
1237    "Modify",
1238    Optional(Keyword("WITH") + Param("withClause", iri))
1239    + (
1240        Param("delete", DeleteClause) + Optional(Param("insert", InsertClause))
1241        | Param("insert", InsertClause)
1242    )
1243    + ZeroOrMore(ParamList("using", UsingClause))
1244    + Keyword("WHERE")
1245    + Param("where", GroupGraphPattern),
1246)
1247
1248
1249# [30] Update1 ::= Load | Clear | Drop | Add | Move | Copy | Create | InsertData | DeleteData | DeleteWhere | Modify
1250Update1 = (
1251    Load
1252    | Clear
1253    | Drop
1254    | Add
1255    | Move
1256    | Copy
1257    | Create
1258    | InsertData
1259    | DeleteData
1260    | DeleteWhere
1261    | Modify
1262)
1263
1264
1265# [63] InlineDataOneVar ::= Var '{' ZeroOrMore(DataBlockValue) '}'
1266InlineDataOneVar = (
1267    ParamList("var", Var) + "{" + ZeroOrMore(ParamList("value", DataBlockValue)) + "}"
1268)
1269
1270# [64] InlineDataFull ::= ( NIL | '(' ZeroOrMore(Var) ')' ) '{' ( '(' ZeroOrMore(DataBlockValue) ')' | NIL )* '}'
1271InlineDataFull = (
1272    (NIL | "(" + ZeroOrMore(ParamList("var", Var)) + ")")
1273    + "{"
1274    + ZeroOrMore(
1275        ParamList(
1276            "value",
1277            Group(Suppress("(") + ZeroOrMore(DataBlockValue) + Suppress(")") | NIL),
1278        )
1279    )
1280    + "}"
1281)
1282
1283# [62] DataBlock ::= InlineDataOneVar | InlineDataFull
1284DataBlock = InlineDataOneVar | InlineDataFull
1285
1286
1287# [28] ValuesClause ::= ( 'VALUES' DataBlock )?
1288ValuesClause = Optional(
1289    Param("valuesClause", Comp("ValuesClause", Keyword("VALUES") + DataBlock))
1290)
1291
1292
1293# [74] ConstructTriples ::= TriplesSameSubject ( '.' Optional(ConstructTriples) )?
1294ConstructTriples = Forward()
1295ConstructTriples <<= ParamList("template", TriplesSameSubject) + Optional(
1296    Suppress(".") + Optional(ConstructTriples)
1297)
1298
1299# [73] ConstructTemplate ::= '{' Optional(ConstructTriples) '}'
1300ConstructTemplate = Suppress("{") + Optional(ConstructTriples) + Suppress("}")
1301
1302
1303# [57] OptionalGraphPattern ::= 'OPTIONAL' GroupGraphPattern
1304OptionalGraphPattern = Comp(
1305    "OptionalGraphPattern", Keyword("OPTIONAL") + Param("graph", GroupGraphPattern)
1306)
1307
1308# [58] GraphGraphPattern ::= 'GRAPH' VarOrIri GroupGraphPattern
1309GraphGraphPattern = Comp(
1310    "GraphGraphPattern",
1311    Keyword("GRAPH") + Param("term", VarOrIri) + Param("graph", GroupGraphPattern),
1312)
1313
1314# [59] ServiceGraphPattern ::= 'SERVICE' _Silent VarOrIri GroupGraphPattern
1315ServiceGraphPattern = Comp(
1316    "ServiceGraphPattern",
1317    Keyword("SERVICE")
1318    + _Silent
1319    + Param("term", VarOrIri)
1320    + Param("graph", GroupGraphPattern),
1321)
1322
1323# [60] Bind ::= 'BIND' '(' Expression 'AS' Var ')'
1324Bind = Comp(
1325    "Bind",
1326    Keyword("BIND")
1327    + "("
1328    + Param("expr", Expression)
1329    + Keyword("AS")
1330    + Param("var", Var)
1331    + ")",
1332)
1333
1334# [61] InlineData ::= 'VALUES' DataBlock
1335InlineData = Comp("InlineData", Keyword("VALUES") + DataBlock)
1336
1337# [56] GraphPatternNotTriples ::= GroupOrUnionGraphPattern | OptionalGraphPattern | MinusGraphPattern | GraphGraphPattern | ServiceGraphPattern | Filter | Bind | InlineData
1338GraphPatternNotTriples = (
1339    GroupOrUnionGraphPattern
1340    | OptionalGraphPattern
1341    | MinusGraphPattern
1342    | GraphGraphPattern
1343    | ServiceGraphPattern
1344    | Filter
1345    | Bind
1346    | InlineData
1347)
1348
1349# [54] GroupGraphPatternSub ::= Optional(TriplesBlock) ( GraphPatternNotTriples '.'? Optional(TriplesBlock) )*
1350GroupGraphPatternSub = Comp(
1351    "GroupGraphPatternSub",
1352    Optional(ParamList("part", Comp("TriplesBlock", TriplesBlock)))
1353    + ZeroOrMore(
1354        ParamList("part", GraphPatternNotTriples)
1355        + Optional(".")
1356        + Optional(ParamList("part", Comp("TriplesBlock", TriplesBlock)))
1357    ),
1358)
1359
1360
1361# ----------------
1362# [22] HavingCondition ::= Constraint
1363HavingCondition = Constraint
1364
1365# [21] HavingClause ::= 'HAVING' HavingCondition+
1366HavingClause = Comp(
1367    "HavingClause",
1368    Keyword("HAVING") + OneOrMore(ParamList("condition", HavingCondition)),
1369)
1370
1371# [24] OrderCondition ::= ( ( 'ASC' | 'DESC' ) BrackettedExpression )
1372# | ( Constraint | Var )
1373OrderCondition = Comp(
1374    "OrderCondition",
1375    Param("order", Keyword("ASC") | Keyword("DESC"))
1376    + Param("expr", BrackettedExpression)
1377    | Param("expr", Constraint | Var),
1378)
1379
1380# [23] OrderClause ::= 'ORDER' 'BY' OneOrMore(OrderCondition)
1381OrderClause = Comp(
1382    "OrderClause",
1383    Keyword("ORDER")
1384    + Keyword("BY")
1385    + OneOrMore(ParamList("condition", OrderCondition)),
1386)
1387
1388# [26] LimitClause ::= 'LIMIT' INTEGER
1389LimitClause = Keyword("LIMIT") + Param("limit", INTEGER)
1390
1391# [27] OffsetClause ::= 'OFFSET' INTEGER
1392OffsetClause = Keyword("OFFSET") + Param("offset", INTEGER)
1393
1394# [25] LimitOffsetClauses ::= LimitClause Optional(OffsetClause) | OffsetClause Optional(LimitClause)
1395LimitOffsetClauses = Comp(
1396    "LimitOffsetClauses",
1397    LimitClause + Optional(OffsetClause) | OffsetClause + Optional(LimitClause),
1398)
1399
1400# [18] SolutionModifier ::= GroupClause? HavingClause? OrderClause? LimitOffsetClauses?
1401SolutionModifier = (
1402    Optional(Param("groupby", GroupClause))
1403    + Optional(Param("having", HavingClause))
1404    + Optional(Param("orderby", OrderClause))
1405    + Optional(Param("limitoffset", LimitOffsetClauses))
1406)
1407
1408
1409# [9] SelectClause ::= 'SELECT' ( 'DISTINCT' | 'REDUCED' )? ( ( Var | ( '(' Expression 'AS' Var ')' ) )+ | '*' )
1410SelectClause = (
1411    Keyword("SELECT")
1412    + Optional(Param("modifier", Keyword("DISTINCT") | Keyword("REDUCED")))
1413    + (
1414        OneOrMore(
1415            ParamList(
1416                "projection",
1417                Comp(
1418                    "vars",
1419                    Param("var", Var)
1420                    | (
1421                        Literal("(")
1422                        + Param("expr", Expression)
1423                        + Keyword("AS")
1424                        + Param("evar", Var)
1425                        + ")"
1426                    ),
1427                ),
1428            )
1429        )
1430        | "*"
1431    )
1432)
1433
1434# [17] WhereClause ::= 'WHERE'? GroupGraphPattern
1435WhereClause = Optional(Keyword("WHERE")) + Param("where", GroupGraphPattern)
1436
1437# [8] SubSelect ::= SelectClause WhereClause SolutionModifier ValuesClause
1438SubSelect = Comp(
1439    "SubSelect", SelectClause + WhereClause + SolutionModifier + ValuesClause
1440)
1441
1442# [53] GroupGraphPattern ::= '{' ( SubSelect | GroupGraphPatternSub ) '}'
1443GroupGraphPattern <<= Suppress("{") + (SubSelect | GroupGraphPatternSub) + Suppress("}")
1444
1445# [7] SelectQuery ::= SelectClause DatasetClause* WhereClause SolutionModifier
1446SelectQuery = Comp(
1447    "SelectQuery",
1448    SelectClause
1449    + ZeroOrMore(ParamList("datasetClause", DatasetClause))
1450    + WhereClause
1451    + SolutionModifier
1452    + ValuesClause,
1453)
1454
1455# [10] ConstructQuery ::= 'CONSTRUCT' ( ConstructTemplate DatasetClause* WhereClause SolutionModifier | DatasetClause* 'WHERE' '{' TriplesTemplate? '}' SolutionModifier )
1456# NOTE: The CONSTRUCT WHERE alternative has unnecessarily many Comp/Param pairs
1457# to allow it to through the same algebra translation process
1458ConstructQuery = Comp(
1459    "ConstructQuery",
1460    Keyword("CONSTRUCT")
1461    + (
1462        ConstructTemplate
1463        + ZeroOrMore(ParamList("datasetClause", DatasetClause))
1464        + WhereClause
1465        + SolutionModifier
1466        + ValuesClause
1467        | ZeroOrMore(ParamList("datasetClause", DatasetClause))
1468        + Keyword("WHERE")
1469        + "{"
1470        + Optional(
1471            Param(
1472                "where",
1473                Comp(
1474                    "FakeGroupGraphPatten",
1475                    ParamList("part", Comp("TriplesBlock", TriplesTemplate)),
1476                ),
1477            )
1478        )
1479        + "}"
1480        + SolutionModifier
1481        + ValuesClause
1482    ),
1483)
1484
1485# [12] AskQuery ::= 'ASK' DatasetClause* WhereClause SolutionModifier
1486AskQuery = Comp(
1487    "AskQuery",
1488    Keyword("ASK")
1489    + ZeroOrMore(ParamList("datasetClause", DatasetClause))
1490    + WhereClause
1491    + SolutionModifier
1492    + ValuesClause,
1493)
1494
1495# [11] DescribeQuery ::= 'DESCRIBE' ( VarOrIri+ | '*' ) DatasetClause* WhereClause? SolutionModifier
1496DescribeQuery = Comp(
1497    "DescribeQuery",
1498    Keyword("DESCRIBE")
1499    + (OneOrMore(ParamList("var", VarOrIri)) | "*")
1500    + ZeroOrMore(ParamList("datasetClause", DatasetClause))
1501    + Optional(WhereClause)
1502    + SolutionModifier
1503    + ValuesClause,
1504)
1505
1506# [29] Update ::= Prologue ( Update1 ( ';' Update )? )?
1507Update = Forward()
1508Update <<= ParamList("prologue", Prologue) + Optional(
1509    ParamList("request", Update1) + Optional(";" + Update)
1510)
1511
1512
1513# [2] Query ::= Prologue
1514# ( SelectQuery | ConstructQuery | DescribeQuery | AskQuery )
1515# ValuesClause
1516# NOTE: ValuesClause was moved to individual queries
1517Query = Prologue + (SelectQuery | ConstructQuery | DescribeQuery | AskQuery)
1518
1519# [3] UpdateUnit ::= Update
1520UpdateUnit = Comp("Update", Update)
1521
1522# [1] QueryUnit ::= Query
1523QueryUnit = Query
1524
1525QueryUnit.ignore("#" + rest_of_line)
1526UpdateUnit.ignore("#" + rest_of_line)
1527
1528
1529expandUnicodeEscapes_re: re.Pattern = re.compile(
1530    r"\\u([0-9a-f]{4}(?:[0-9a-f]{4})?)", flags=re.I
1531)
1532
1533
1534def expandUnicodeEscapes(q: str) -> str:
1535    r"""
1536    The syntax of the SPARQL Query Language is expressed over code points in Unicode [UNICODE]. The encoding is always UTF-8 [RFC3629].
1537    Unicode code points may also be expressed using an \ uXXXX (U+0 to U+FFFF) or \ UXXXXXXXX syntax (for U+10000 onwards) where X is a hexadecimal digit [0-9A-F]
1538    """
1539
1540    def expand(m: re.Match) -> str:
1541        try:
1542            return chr(int(m.group(1), 16))
1543        except (ValueError, OverflowError) as e:
1544            raise ValueError("Invalid unicode code point: " + m.group(1)) from e
1545
1546    return expandUnicodeEscapes_re.sub(expand, q)
1547
1548
1549def parseQuery(q: Union[str, bytes, TextIO, BinaryIO]) -> ParseResults:
1550    if hasattr(q, "read"):
1551        q = q.read()
1552    if isinstance(q, bytes):
1553        q = q.decode("utf-8")
1554
1555    q = expandUnicodeEscapes(q)
1556    return Query.parse_string(q, parse_all=True)
1557
1558
1559def parseUpdate(q: Union[str, bytes, TextIO, BinaryIO]) -> CompValue:
1560    if hasattr(q, "read"):
1561        q = q.read()
1562
1563    if isinstance(q, bytes):
1564        q = q.decode("utf-8")
1565
1566    q = expandUnicodeEscapes(q)
1567    return UpdateUnit.parse_string(q, parse_all=True)[0]