Skip to content

Commit 03c2ae7

Browse files
committed
Replace ParamList with Group
1 parent 246031b commit 03c2ae7

File tree

2 files changed

+74
-80
lines changed

2 files changed

+74
-80
lines changed

rdflib/plugins/sparql/parser.py

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from rdflib.compat import decodeUnicodeEscape
2828

2929
from . import operators as op
30-
from .parserutils import Comp, Param, ParamList
30+
from .parserutils import Comp, Param
3131

3232
# from pyparsing import Keyword as CaseSensitiveKeyword
3333

@@ -427,9 +427,9 @@ def expandCollection(terms):
427427
)
428428

429429
# [45] GraphOrDefault ::= 'DEFAULT' | 'GRAPH'? iri
430-
GraphOrDefault = ParamList("graph", Keyword("DEFAULT")) | Optional(
430+
GraphOrDefault = Group(Param("graph", Keyword("DEFAULT"))) | Optional(
431431
Keyword("GRAPH")
432-
) + ParamList("graph", iri)
432+
) + Group(Param("graph", iri))
433433

434434
# [65] DataBlockValue ::= iri | RDFLiteral | NumericLiteral | BooleanLiteral | 'UNDEF'
435435
DataBlockValue = iri | RDFLiteral | NumericLiteral | BooleanLiteral | Keyword("UNDEF")
@@ -466,11 +466,11 @@ def expandCollection(terms):
466466
# [95] PathNegatedPropertySet ::= PathOneInPropertySet | '(' ( PathOneInPropertySet ( '|' PathOneInPropertySet )* )? ')'
467467
PathNegatedPropertySet = Comp(
468468
"PathNegatedPropertySet",
469-
ParamList("part", PathOneInPropertySet)
469+
Group(Param("part", PathOneInPropertySet))
470470
| "("
471471
+ Optional(
472-
ParamList("part", PathOneInPropertySet)
473-
+ ZeroOrMore("|" + ParamList("part", PathOneInPropertySet))
472+
Group(Param("part", PathOneInPropertySet))
473+
+ ZeroOrMore("|" + Group(Param("part", PathOneInPropertySet)))
474474
)
475475
+ ")",
476476
)
@@ -498,15 +498,16 @@ def expandCollection(terms):
498498
# [90] PathSequence ::= PathEltOrInverse ( '/' PathEltOrInverse )*
499499
PathSequence = Comp(
500500
"PathSequence",
501-
ParamList("part", PathEltOrInverse)
502-
+ ZeroOrMore("/" + ParamList("part", PathEltOrInverse)),
501+
Group(Param("part", PathEltOrInverse))
502+
+ ZeroOrMore("/" + Group(Param("part", PathEltOrInverse))),
503503
)
504504

505505

506506
# [89] PathAlternative ::= PathSequence ( '|' PathSequence )*
507507
PathAlternative = Comp(
508508
"PathAlternative",
509-
ParamList("part", PathSequence) + ZeroOrMore("|" + ParamList("part", PathSequence)),
509+
Group(Param("part", PathSequence))
510+
+ ZeroOrMore("|" + Group(Param("part", PathSequence))),
510511
)
511512

512513
# [88] Path ::= PathAlternative
@@ -583,8 +584,8 @@ def expandCollection(terms):
583584
# To accommodate arbitrary amounts of triples this rule is rewritten to not be
584585
# recursive:
585586
# [52*] TriplesTemplate ::= TriplesSameSubject ( '.' TriplesSameSubject? )*
586-
TriplesTemplate = ParamList("triples", TriplesSameSubject) + ZeroOrMore(
587-
Suppress(".") + Optional(ParamList("triples", TriplesSameSubject))
587+
TriplesTemplate = Group(Param("triples", TriplesSameSubject)) + ZeroOrMore(
588+
Suppress(".") + Optional(Group(Param("triples", TriplesSameSubject)))
588589
)
589590

590591
# [51] QuadsNotTriples ::= 'GRAPH' VarOrIri '{' Optional(TriplesTemplate) '}'
@@ -598,7 +599,7 @@ def expandCollection(terms):
598599
"Quads",
599600
Optional(TriplesTemplate)
600601
+ ZeroOrMore(
601-
ParamList("quadsNotTriples", QuadsNotTriples)
602+
Group(Param("quadsNotTriples", QuadsNotTriples))
602603
+ Optional(Suppress("."))
603604
+ Optional(TriplesTemplate)
604605
),
@@ -618,7 +619,7 @@ def expandCollection(terms):
618619

619620
# [55] TriplesBlock ::= TriplesSameSubjectPath ( '.' Optional(TriplesBlock) )?
620621
TriplesBlock = Forward()
621-
TriplesBlock <<= ParamList("triples", TriplesSameSubjectPath) + Optional(
622+
TriplesBlock <<= Group(Param("triples", TriplesSameSubjectPath)) + Optional(
622623
Suppress(".") + Optional(TriplesBlock)
623624
)
624625

@@ -631,8 +632,8 @@ def expandCollection(terms):
631632
# [67] GroupOrUnionGraphPattern ::= GroupGraphPattern ( 'UNION' GroupGraphPattern )*
632633
GroupOrUnionGraphPattern = Comp(
633634
"GroupOrUnionGraphPattern",
634-
ParamList("graph", GroupGraphPattern)
635-
+ ZeroOrMore(Keyword("UNION") + ParamList("graph", GroupGraphPattern)),
635+
Group(Param("graph", GroupGraphPattern))
636+
+ ZeroOrMore(Keyword("UNION") + Group(Param("graph", GroupGraphPattern))),
636637
)
637638

638639

@@ -1000,7 +1001,7 @@ def expandCollection(terms):
10001001
NIL
10011002
| "("
10021003
+ Param("distinct", _Distinct)
1003-
+ delimitedList(ParamList("expr", Expression))
1004+
+ delimitedList(Group(Param("expr", Expression)))
10041005
+ ")"
10051006
)
10061007

@@ -1045,8 +1046,8 @@ def expandCollection(terms):
10451046
"MultiplicativeExpression",
10461047
Param("expr", UnaryExpression)
10471048
+ ZeroOrMore(
1048-
ParamList("op", "*") + ParamList("other", UnaryExpression)
1049-
| ParamList("op", "/") + ParamList("other", UnaryExpression)
1049+
Group(Param("op", "*")) + Group(Param("other", UnaryExpression))
1050+
| Group(Param("op", "/")) + Group(Param("other", UnaryExpression))
10501051
),
10511052
).setEvalFn(op.MultiplicativeExpression)
10521053

@@ -1063,8 +1064,8 @@ def expandCollection(terms):
10631064
"AdditiveExpression",
10641065
Param("expr", MultiplicativeExpression)
10651066
+ ZeroOrMore(
1066-
ParamList("op", "+") + ParamList("other", MultiplicativeExpression)
1067-
| ParamList("op", "-") + ParamList("other", MultiplicativeExpression)
1067+
Group(Param("op", "+")) + Group(Param("other", MultiplicativeExpression))
1068+
| Group(Param("op", "-")) + Group(Param("other", MultiplicativeExpression))
10681069
),
10691070
).setEvalFn(op.AdditiveExpression)
10701071

@@ -1099,14 +1100,15 @@ def expandCollection(terms):
10991100
# [112] ConditionalAndExpression ::= ValueLogical ( '&&' ValueLogical )*
11001101
ConditionalAndExpression = Comp(
11011102
"ConditionalAndExpression",
1102-
Param("expr", ValueLogical) + ZeroOrMore("&&" + ParamList("other", ValueLogical)),
1103+
Param("expr", ValueLogical)
1104+
+ ZeroOrMore("&&" + Group(Param("other", ValueLogical))),
11031105
).setEvalFn(op.ConditionalAndExpression)
11041106

11051107
# [111] ConditionalOrExpression ::= ConditionalAndExpression ( '||' ConditionalAndExpression )*
11061108
ConditionalOrExpression = Comp(
11071109
"ConditionalOrExpression",
11081110
Param("expr", ConditionalAndExpression)
1109-
+ ZeroOrMore("||" + ParamList("other", ConditionalAndExpression)),
1111+
+ ZeroOrMore("||" + Group(Param("other", ConditionalAndExpression))),
11101112
).setEvalFn(op.ConditionalOrExpression)
11111113

11121114
# [110] Expression ::= ConditionalOrExpression
@@ -1154,7 +1156,7 @@ def expandCollection(terms):
11541156
"GroupClause",
11551157
Keyword("GROUP")
11561158
+ Keyword("BY")
1157-
+ OneOrMore(ParamList("condition", GroupCondition)),
1159+
+ OneOrMore(Group(Param("condition", GroupCondition))),
11581160
)
11591161

11601162

@@ -1222,7 +1224,7 @@ def expandCollection(terms):
12221224
Param("delete", DeleteClause) + Optional(Param("insert", InsertClause))
12231225
| Param("insert", InsertClause)
12241226
)
1225-
+ ZeroOrMore(ParamList("using", UsingClause))
1227+
+ ZeroOrMore(Group(Param("using", UsingClause)))
12261228
+ Keyword("WHERE")
12271229
+ Param("where", GroupGraphPattern),
12281230
)
@@ -1246,17 +1248,22 @@ def expandCollection(terms):
12461248

12471249
# [63] InlineDataOneVar ::= Var '{' ZeroOrMore(DataBlockValue) '}'
12481250
InlineDataOneVar = (
1249-
ParamList("var", Var) + "{" + ZeroOrMore(ParamList("value", DataBlockValue)) + "}"
1251+
Group(Param("var", Var))
1252+
+ "{"
1253+
+ ZeroOrMore(Group(Param("value", DataBlockValue)))
1254+
+ "}"
12501255
)
12511256

12521257
# [64] InlineDataFull ::= ( NIL | '(' ZeroOrMore(Var) ')' ) '{' ( '(' ZeroOrMore(DataBlockValue) ')' | NIL )* '}'
12531258
InlineDataFull = (
1254-
(NIL | "(" + ZeroOrMore(ParamList("var", Var)) + ")")
1259+
(NIL | "(" + ZeroOrMore(Group(Param("var", Var))) + ")")
12551260
+ "{"
12561261
+ ZeroOrMore(
1257-
ParamList(
1258-
"value",
1259-
Group(Suppress("(") + ZeroOrMore(DataBlockValue) + Suppress(")") | NIL),
1262+
Group(
1263+
Param(
1264+
"value",
1265+
Group(Suppress("(") + ZeroOrMore(DataBlockValue) + Suppress(")") | NIL),
1266+
)
12601267
)
12611268
)
12621269
+ "}"
@@ -1274,7 +1281,7 @@ def expandCollection(terms):
12741281

12751282
# [74] ConstructTriples ::= TriplesSameSubject ( '.' Optional(ConstructTriples) )?
12761283
ConstructTriples = Forward()
1277-
ConstructTriples <<= ParamList("template", TriplesSameSubject) + Optional(
1284+
ConstructTriples <<= Group(Param("template", TriplesSameSubject)) + Optional(
12781285
Suppress(".") + Optional(ConstructTriples)
12791286
)
12801287

@@ -1331,11 +1338,11 @@ def expandCollection(terms):
13311338
# [54] GroupGraphPatternSub ::= Optional(TriplesBlock) ( GraphPatternNotTriples '.'? Optional(TriplesBlock) )*
13321339
GroupGraphPatternSub = Comp(
13331340
"GroupGraphPatternSub",
1334-
Optional(ParamList("part", Comp("TriplesBlock", TriplesBlock)))
1341+
Optional(Group(Param("part", Comp("TriplesBlock", TriplesBlock))))
13351342
+ ZeroOrMore(
1336-
ParamList("part", GraphPatternNotTriples)
1343+
Group(Param("part", GraphPatternNotTriples))
13371344
+ Optional(".")
1338-
+ Optional(ParamList("part", Comp("TriplesBlock", TriplesBlock)))
1345+
+ Optional(Group(Param("part", Comp("TriplesBlock", TriplesBlock))))
13391346
),
13401347
)
13411348

@@ -1347,7 +1354,7 @@ def expandCollection(terms):
13471354
# [21] HavingClause ::= 'HAVING' HavingCondition+
13481355
HavingClause = Comp(
13491356
"HavingClause",
1350-
Keyword("HAVING") + OneOrMore(ParamList("condition", HavingCondition)),
1357+
Keyword("HAVING") + OneOrMore(Group(Param("condition", HavingCondition))),
13511358
)
13521359

13531360
# [24] OrderCondition ::= ( ( 'ASC' | 'DESC' ) BrackettedExpression )
@@ -1364,7 +1371,7 @@ def expandCollection(terms):
13641371
"OrderClause",
13651372
Keyword("ORDER")
13661373
+ Keyword("BY")
1367-
+ OneOrMore(ParamList("condition", OrderCondition)),
1374+
+ OneOrMore(Group(Param("condition", OrderCondition))),
13681375
)
13691376

13701377
# [26] LimitClause ::= 'LIMIT' INTEGER
@@ -1394,19 +1401,21 @@ def expandCollection(terms):
13941401
+ Optional(Param("modifier", Keyword("DISTINCT") | Keyword("REDUCED")))
13951402
+ (
13961403
OneOrMore(
1397-
ParamList(
1398-
"projection",
1399-
Comp(
1400-
"vars",
1401-
Param("var", Var)
1402-
| (
1403-
Literal("(")
1404-
+ Param("expr", Expression)
1405-
+ Keyword("AS")
1406-
+ Param("evar", Var)
1407-
+ ")"
1404+
Group(
1405+
Param(
1406+
"projection",
1407+
Comp(
1408+
"vars",
1409+
Param("var", Var)
1410+
| (
1411+
Literal("(")
1412+
+ Param("expr", Expression)
1413+
+ Keyword("AS")
1414+
+ Param("evar", Var)
1415+
+ ")"
1416+
),
14081417
),
1409-
),
1418+
)
14101419
)
14111420
)
14121421
| "*"
@@ -1428,7 +1437,7 @@ def expandCollection(terms):
14281437
SelectQuery = Comp(
14291438
"SelectQuery",
14301439
SelectClause
1431-
+ ZeroOrMore(ParamList("datasetClause", DatasetClause))
1440+
+ ZeroOrMore(Group(Param("datasetClause", DatasetClause)))
14321441
+ WhereClause
14331442
+ SolutionModifier
14341443
+ ValuesClause,
@@ -1442,19 +1451,19 @@ def expandCollection(terms):
14421451
Keyword("CONSTRUCT")
14431452
+ (
14441453
ConstructTemplate
1445-
+ ZeroOrMore(ParamList("datasetClause", DatasetClause))
1454+
+ ZeroOrMore(Group(Param("datasetClause", DatasetClause)))
14461455
+ WhereClause
14471456
+ SolutionModifier
14481457
+ ValuesClause
1449-
| ZeroOrMore(ParamList("datasetClause", DatasetClause))
1458+
| ZeroOrMore(Group(Param("datasetClause", DatasetClause)))
14501459
+ Keyword("WHERE")
14511460
+ "{"
14521461
+ Optional(
14531462
Param(
14541463
"where",
14551464
Comp(
14561465
"FakeGroupGraphPatten",
1457-
ParamList("part", Comp("TriplesBlock", TriplesTemplate)),
1466+
Group(Param("part", Comp("TriplesBlock", TriplesTemplate))),
14581467
),
14591468
)
14601469
)
@@ -1478,7 +1487,7 @@ def expandCollection(terms):
14781487
DescribeQuery = Comp(
14791488
"DescribeQuery",
14801489
Keyword("DESCRIBE")
1481-
+ (OneOrMore(ParamList("var", VarOrIri)) | "*")
1490+
+ (OneOrMore(Group(Param("var", VarOrIri))) | "*")
14821491
+ Param("datasetClause", ZeroOrMore(DatasetClause))
14831492
+ Optional(WhereClause)
14841493
+ SolutionModifier
@@ -1487,8 +1496,8 @@ def expandCollection(terms):
14871496

14881497
# [29] Update ::= Prologue ( Update1 ( ';' Update )? )?
14891498
Update = Forward()
1490-
Update <<= ParamList("prologue", Prologue) + Optional(
1491-
ParamList("request", Update1) + Optional(";" + Update)
1499+
Update <<= Group(Param("prologue", Prologue)) + Optional(
1500+
Group(Param("request", Update1)) + Optional(";" + Update)
14921501
)
14931502

14941503

rdflib/plugins/sparql/parserutils.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ class ParamValue(object):
9494
All cleverness is in the CompValue
9595
"""
9696

97-
def __init__(self, name, tokenList, isList):
98-
self.isList = isList
97+
def __init__(self, name, tokenList):
9998
self.name = name
10099
if isinstance(tokenList, (list, ParseResults)) and len(tokenList) == 1:
101100
tokenList = tokenList[0]
@@ -109,27 +108,15 @@ def __str__(self):
109108
class Param(TokenConverter):
110109
"""
111110
A pyparsing token for labelling a part of the parse-tree
112-
if isList is true repeat occurrences of ParamList have
113-
their values merged in a list
114111
"""
115112

116-
def __init__(self, name, expr, isList=False):
117-
self.isList = isList
113+
def __init__(self, name, expr):
118114
TokenConverter.__init__(self, expr)
119115
self.setName(name)
120116
self.addParseAction(self.postParse2)
121117

122118
def postParse2(self, tokenList):
123-
return ParamValue(self.name, tokenList, self.isList)
124-
125-
126-
class ParamList(Param):
127-
"""
128-
A shortcut for a Param with isList=True
129-
"""
130-
131-
def __init__(self, name, expr):
132-
Param.__init__(self, name, expr, True)
119+
return ParamValue(self.name, tokenList)
133120

134121

135122
class plist(list):
@@ -241,16 +228,14 @@ def postParse(self, instring, loc, tokenList):
241228
res["service_string"] = service_string
242229

243230
for t in tokenList:
244-
if isinstance(t, ParamValue):
245-
if t.isList:
246-
if t.name not in res:
247-
res[t.name] = plist()
248-
res[t.name].append(t.tokenList)
249-
else:
250-
res[t.name] = t.tokenList
251-
# res.append(t.tokenList)
252-
# if isinstance(t,CompValue):
253-
# res.update(t)
231+
if isinstance(t, ParseResults):
232+
for i in t:
233+
if isinstance(i, ParamValue):
234+
if i.name not in res:
235+
res[i.name] = plist()
236+
res[i.name].append(i.tokenList)
237+
elif isinstance(t, ParamValue):
238+
res[t.name] = t.tokenList
254239
return res
255240

256241
def setEvalFn(self, evalfn):

0 commit comments

Comments
 (0)