| 1 |
""" |
| 2 |
Helpers for making the definition of grammars of the complexity of our |
| 3 |
ADQL grammar a bit easier. |
| 4 |
|
| 5 |
Here, we define the AutoBindingGrammar, which is used in adql.grammar to |
| 6 |
build a grammar with parseActions defined in adql.nodes, where the |
| 7 |
node classes say which symbols they are responsible for. |
| 8 |
""" |
| 9 |
|
| 10 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 11 |
#c |
| 12 |
#c This program is free software, covered by the GNU GPL. See the |
| 13 |
#c COPYING file in the source distribution. |
| 14 |
|
| 15 |
|
| 16 |
from gavo.adql import nodes |
| 17 |
|
| 18 |
|
| 19 |
class SingleBindingDict(dict): |
| 20 |
"""A dictionary that lets you assign each key just once. |
| 21 |
""" |
| 22 |
def __setitem__(self, key, value): |
| 23 |
if key in self: |
| 24 |
raise Exception(f"Trying to rebind key: {key}") |
| 25 |
dict.__setitem__(self, key, value) |
| 26 |
|
| 27 |
|
| 28 |
class AutoBindingGrammar: |
| 29 |
"""A container for pyparsing symbols that receive their parse actions |
| 30 |
from a sequence of "binders" |
| 31 |
|
| 32 |
The binders are passed into the constructor. Most of these will be |
| 33 |
derived from nodes.ADQLNode; but they can also be callables with |
| 34 |
parseActionFor set (see the nodes.symbolAction decorator). Making this more |
| 35 |
generic would take quite a bit of extra thought. Anyway, what I'm looking at |
| 36 |
here on whatever is in nodes: |
| 37 |
|
| 38 |
* type: a name of a symbol that should have nodeClass.fromParseResult |
| 39 |
as a parse action. |
| 40 |
* bindings: as type, but a list of symbol names; when bindings is |
| 41 |
present, type is ignored. |
| 42 |
* parseActionFor: that's a list of symbol names the "node" (which more |
| 43 |
likely is just some function) must be called for as a parse action. |
| 44 |
* collapsible: if set on a node class, the parse action will be |
| 45 |
nodes.autocollapse (that's for transparent nodes I'm eliding out |
| 46 |
of the tree unless they're more than just a single token). |
| 47 |
""" |
| 48 |
# instance variables must be preset on the class level here so |
| 49 |
# __setattr__ doesn't tick them into the symbols dict. |
| 50 |
_actionForSym = None |
| 51 |
_symbols = None |
| 52 |
|
| 53 |
def __init__(self, binders): |
| 54 |
self._actionForSym = SingleBindingDict() |
| 55 |
self._symbols = {} |
| 56 |
|
| 57 |
for binder in binders: |
| 58 |
if hasattr(binder, "type"): |
| 59 |
for binding in getattr(binder, "bindings", [binder.type]): |
| 60 |
if binding: |
| 61 |
self.bind_(binding, binder) |
| 62 |
|
| 63 |
if hasattr(binder, "parseActionFor"): |
| 64 |
for sym in binder.parseActionFor: |
| 65 |
self._actionForSym[sym] = binder |
| 66 |
|
| 67 |
def __setattr__(self, symName, value): |
| 68 |
if hasattr(self, symName): |
| 69 |
object.__setattr__(self, symName, value) |
| 70 |
return |
| 71 |
|
| 72 |
if symName in self._actionForSym: |
| 73 |
value.setParseAction(self._actionForSym[symName]) |
| 74 |
self._symbols[symName] = value |
| 75 |
|
| 76 |
def __getattr__(self, symName): |
| 77 |
if symName not in self._symbols: |
| 78 |
raise AttributeError(f"Symbol {symName} not yet defined.") |
| 79 |
|
| 80 |
return self._symbols[symName] |
| 81 |
|
| 82 |
def bind_(self, symName, nodeClass): |
| 83 |
"""enters nodeClass as a handler for symName. |
| 84 |
|
| 85 |
This also deals with autocollapsining single nodes. |
| 86 |
""" |
| 87 |
if getattr(nodeClass, "collapsible", False): |
| 88 |
self._actionForSym[symName] = \ |
| 89 |
lambda s, pos, toks: nodes.autocollapse(nodeClass, toks) |
| 90 |
else: |
| 91 |
self._actionForSym[symName] = \ |
| 92 |
lambda s, pos, toks: nodeClass.fromParseResult(toks) |
| 93 |
|
| 94 |
def getSymbols_(self): |
| 95 |
"""returns a dict of all symbols defined. |
| 96 |
""" |
| 97 |
return self._symbols |