Source code

Download source archive ↓

src/rdflib/plugins/sparql/parserutils.py

1"""
2
3NOTE: PyParsing setResultName/__call__ provides a very similar solution to this
4I didn't realise at the time of writing and I will remove a
5lot of this code at some point
6
7Utility classes for creating an abstract-syntax tree out with pyparsing actions
8
9Lets you label and group parts of parser production rules
10
11For example:
12
13# [5] BaseDecl ::= 'BASE' IRIREF
14BaseDecl = Comp('Base', Keyword('BASE') + Param('iri',IRIREF))
15
16After parsing, this gives you back an CompValue object,
17which is a dict/object with the parameters specified.
18So you can access the parameters are attributes or as keys:
19
20baseDecl.iri
21
22Comp lets you set an evalFn that is bound to the eval method of
23the resulting CompValue
24"""
25
26from __future__ import annotations
27
28from collections import OrderedDict
29from types import MethodType
30from typing import (
31    TYPE_CHECKING,
32    Any,
33    Callable,
34    List,
35    Mapping,
36    Optional,
37    Tuple,
38    TypeVar,
39    Union,
40)
41
42from pyparsing import ParserElement, ParseResults, TokenConverter
43
44from rdflib.term import BNode, Identifier, Variable
45
46from .pyparsing_compat import original_text_for
47
48if TYPE_CHECKING:
49    from rdflib.plugins.sparql.sparql import FrozenBindings
50
51
52# This is an alternative
53
54# Comp('Sum')( Param('x')(Number) + '+' + Param('y')(Number) )
55
56
57def value(
58    ctx: FrozenBindings,
59    val: Any,
60    variables: bool = False,
61    errors: bool = False,
62) -> Any:
63    """Utility function for evaluating something...
64
65    Variables will be looked up in the context
66    Normally, non-bound vars is an error,
67    set variables=True to return unbound vars
68
69    Normally, an error raises the error,
70    set errors=True to return error
71    """
72
73    if isinstance(val, Expr):
74        return val.eval(ctx)  # recurse?
75    elif isinstance(val, CompValue):
76        raise Exception("What do I do with this CompValue? %s" % val)
77
78    elif isinstance(val, list):
79        return [value(ctx, x, variables, errors) for x in val]
80
81    elif isinstance(val, (BNode, Variable)):
82        r = ctx.get(val)
83        if isinstance(r, SPARQLError) and not errors:
84            raise r
85        if r is not None:
86            return r
87
88        # not bound
89        if variables:
90            return val
91        else:
92            raise NotBoundError
93
94    elif isinstance(val, ParseResults) and len(val) == 1:
95        return value(ctx, val[0], variables, errors)
96    else:
97        return val
98
99
100class ParamValue:
101    """
102    The result of parsing a Param
103    This just keeps the name/value
104    All cleverness is in the CompValue
105    """
106
107    def __init__(
108        self, name: str, tokenList: Union[List[Any], ParseResults], isList: bool
109    ):
110        self.isList = isList
111        self.name = name
112        if isinstance(tokenList, (list, ParseResults)) and len(tokenList) == 1:
113            tokenList = tokenList[0]
114
115        self.tokenList = tokenList
116
117    def __str__(self) -> str:
118        return "Param(%s, %s)" % (self.name, self.tokenList)
119
120
121class Param(TokenConverter):
122    """
123    A pyparsing token for labelling a part of the parse-tree
124    if isList is true repeat occurrences of ParamList have
125    their values merged in a list
126    """
127
128    def __init__(self, name: str, expr, isList: bool = False):
129        self.isList = isList
130        TokenConverter.__init__(self, expr)
131        self.set_name(name)
132        self.add_parse_action(self.postParse2)
133
134    def postParse2(self, tokenList: Union[List[Any], ParseResults]) -> ParamValue:
135        return ParamValue(self.name, tokenList, self.isList)
136
137
138class ParamList(Param):
139    """
140    A shortcut for a Param with isList=True
141    """
142
143    def __init__(self, name: str, expr):
144        Param.__init__(self, name, expr, True)
145
146
147_ValT = TypeVar("_ValT")
148
149
150class CompValue(OrderedDict):
151    """
152    The result of parsing a Comp
153    Any included Params are available as Dict keys
154    or as attributes
155    """
156
157    def __init__(self, name: str, **values):
158        OrderedDict.__init__(self)
159        self.name = name
160        self.update(values)
161
162    def clone(self) -> CompValue:
163        return CompValue(self.name, **self)
164
165    def __str__(self) -> str:
166        return self.name + "_" + OrderedDict.__str__(self)
167
168    def __repr__(self) -> str:
169        return self.name + "_" + dict.__repr__(self)
170
171    def _value(
172        self, val: _ValT, variables: bool = False, errors: bool = False
173    ) -> Union[_ValT, Any]:
174        if self.ctx is not None:
175            return value(self.ctx, val, variables)
176        else:
177            return val
178
179    def __getitem__(self, a):
180        return self._value(OrderedDict.__getitem__(self, a))
181
182    # type error: Signature of "get" incompatible with supertype "dict"
183    # type error: Signature of "get" incompatible with supertype "Mapping"  [override]
184    def get(self, a, variables: bool = False, errors: bool = False):  # type: ignore[override]
185        return self._value(OrderedDict.get(self, a, a), variables, errors)
186
187    def __getattr__(self, a: str) -> Any:
188        # Hack hack: OrderedDict relies on this
189        if a in ("_OrderedDict__root", "_OrderedDict__end"):
190            raise AttributeError()
191        try:
192            return self[a]
193        except KeyError:
194            # raise AttributeError('no such attribute '+a)
195            return None
196
197    if TYPE_CHECKING:
198        # this is here because properties are dynamically set on CompValue
199        def __setattr__(self, __name: str, __value: Any) -> None: ...
200
201
202class Expr(CompValue):
203    """
204    A CompValue that is evaluatable
205    """
206
207    def __init__(
208        self,
209        name: str,
210        evalfn: Optional[Callable[[Any, Any], Any]] = None,
211        **values,
212    ):
213        super(Expr, self).__init__(name, **values)
214
215        self._evalfn = None
216        if evalfn:
217            self._evalfn = MethodType(evalfn, self)
218
219    def eval(self, ctx: Any = {}) -> Union[SPARQLError, Any]:
220        try:
221            self.ctx: Optional[Union[Mapping, FrozenBindings]] = ctx
222            # type error: "None" not callable
223            return self._evalfn(ctx)  # type: ignore[misc]
224        except SPARQLError as e:
225            return e
226        finally:
227            self.ctx = None
228
229
230class Comp(TokenConverter):
231    """
232    A pyparsing token for grouping together things with a label
233    Any sub-tokens that are not Params will be ignored.
234
235    Returns CompValue / Expr objects - depending on whether evalFn is set.
236    """
237
238    def __init__(self, name: str, expr: ParserElement):
239        self.expr = expr
240        TokenConverter.__init__(self, expr)
241        self.set_name(name)
242        self.evalfn: Optional[Callable[[Any, Any], Any]] = None
243
244    def postParse(
245        self, instring: str, loc: int, tokenList: ParseResults
246    ) -> Union[Expr, CompValue]:
247        res: Union[Expr, CompValue]
248        if self.evalfn:
249            res = Expr(self.name)
250            res._evalfn = MethodType(self.evalfn, res)
251        else:
252            res = CompValue(self.name)
253            if self.name == "ServiceGraphPattern":
254                # Then this must be a service graph pattern and have
255                # already matched.
256                # lets assume there is one, for now, then test for two later.
257                sgp = original_text_for(self.expr)
258                service_string = sgp.search_string(instring)[0][0]
259                res["service_string"] = service_string
260
261        for t in tokenList:
262            if isinstance(t, ParamValue):
263                if t.isList:
264                    if t.name not in res:
265                        res[t.name] = []
266                    res[t.name].append(t.tokenList)
267                else:
268                    res[t.name] = t.tokenList
269                # res.append(t.tokenList)
270            # if isinstance(t,CompValue):
271            #    res.update(t)
272        return res
273
274    def setEvalFn(self, evalfn: Callable[[Any, Any], Any]) -> Comp:
275        self.evalfn = evalfn
276        return self
277
278
279def prettify_parsetree(t: ParseResults, indent: str = "", depth: int = 0) -> str:
280    out: List[str] = []
281    for e in t.as_list():
282        out.append(_prettify_sub_parsetree(e, indent, depth + 1))
283    for k, v in sorted(t.items()):
284        out.append("%s%s- %s:\n" % (indent, "  " * depth, k))
285        out.append(_prettify_sub_parsetree(v, indent, depth + 1))
286    return "".join(out)
287
288
289def _prettify_sub_parsetree(
290    t: Union[Identifier, CompValue, set, list, dict, Tuple, bool, None],
291    indent: str = "",
292    depth: int = 0,
293) -> str:
294    out: List[str] = []
295    if isinstance(t, CompValue):
296        out.append("%s%s> %s:\n" % (indent, "  " * depth, t.name))
297        for k, v in t.items():
298            out.append("%s%s- %s:\n" % (indent, "  " * (depth + 1), k))
299            out.append(_prettify_sub_parsetree(v, indent, depth + 2))
300    elif isinstance(t, dict):
301        for k, v in t.items():
302            out.append("%s%s- %s:\n" % (indent, "  " * (depth + 1), k))
303            out.append(_prettify_sub_parsetree(v, indent, depth + 2))
304    elif isinstance(t, list):
305        for e in t:
306            out.append(_prettify_sub_parsetree(e, indent, depth + 1))
307    else:
308        out.append("%s%s- %r\n" % (indent, "  " * depth, t))
309    return "".join(out)
310
311
312# hurrah for circular imports
313from rdflib.plugins.sparql.sparql import NotBoundError, SPARQLError  # noqa: E402