| 1 |
""" |
| 2 |
A grammar defined by code embedded in the RD. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
from gavo import base |
| 12 |
from gavo import rscdef |
| 13 |
from gavo.grammars import common |
| 14 |
|
| 15 |
|
| 16 |
class EmbeddedIterator(rscdef.ProcApp): |
| 17 |
"""A definition of an iterator of a grammar. |
| 18 |
|
| 19 |
The code defined here becomes the _iterRows method of a |
| 20 |
grammar.common.RowIterator class. This means that you can |
| 21 |
access self.grammar (the parent grammar; you can use this to transmit |
| 22 |
properties from the RD to your function) and self.sourceToken (whatever |
| 23 |
gets passed to parse()). |
| 24 |
""" |
| 25 |
name_ = "iterator" |
| 26 |
requiredType = "iterator" |
| 27 |
formalArgs = "self" |
| 28 |
|
| 29 |
|
| 30 |
class EmbeddedPargetter(rscdef.ProcApp): |
| 31 |
"""A definition of the parameter getter of an embedded grammar. |
| 32 |
|
| 33 |
The code defined here becomes the getParameters method of the generated |
| 34 |
row iterator. This means that the dictionary returned here becomes |
| 35 |
the input to a parmaker. |
| 36 |
|
| 37 |
If you don't define it, the parameter dict will be empty. |
| 38 |
|
| 39 |
Like the iterators, pargetters see the current source token as |
| 40 |
self.sourceToken, and the grammar as self.grammar. |
| 41 |
|
| 42 |
It is guaranteed that the pargetter is called exactly once before |
| 43 |
the iterator runs. Hence, if you have expensive initialisation |
| 44 |
to do, do it in the pargetter and pass the result to the iterator, |
| 45 |
typically somehow in the sourceToken. |
| 46 |
""" |
| 47 |
name_ = "pargetter" |
| 48 |
requiredType = "pargetter" |
| 49 |
formalArgs = "self" |
| 50 |
|
| 51 |
|
| 52 |
class EmbeddedGrammar(common.Grammar, base.RestrictionMixin): |
| 53 |
"""A Grammar defined by a code application. |
| 54 |
|
| 55 |
To define this grammar, write a ProcApp iterator leading to code yielding |
| 56 |
row dictionaries. The grammar input is available as self.sourceToken; |
| 57 |
for normal grammars within data elements, that would be a fully |
| 58 |
qualified file name. |
| 59 |
|
| 60 |
Grammars can also return one "parameter" dictionary per source (the |
| 61 |
input to a make's parmaker). In an embedded grammar, you can define |
| 62 |
a pargetter to do that. It works like the iterator, except that |
| 63 |
it returns a single dictionary rather than yielding several of them. |
| 64 |
|
| 65 |
This could look like this, when the grammar input is some iterable:: |
| 66 |
|
| 67 |
<embeddedGrammar> |
| 68 |
<iterator> |
| 69 |
<setup> |
| 70 |
<code> |
| 71 |
testData = "a"*1024 |
| 72 |
</code> |
| 73 |
</setup> |
| 74 |
<code> |
| 75 |
for i in self.sourceToken: |
| 76 |
yield {'index': i, 'data': testData} |
| 77 |
</code> |
| 78 |
</iterator> |
| 79 |
</embeddedGrammar> |
| 80 |
|
| 81 |
If you need to raise an error from within an embeddedGrammar, use |
| 82 |
a SourceParseError, somewhat like this:: |
| 83 |
|
| 84 |
raise base.SourceParseError( |
| 85 |
"Bad line", |
| 86 |
offending=inputLine, |
| 87 |
location=str(lineNumber+1), |
| 88 |
source=inputFile.name) |
| 89 |
|
| 90 |
To furnish other exceptions with information on the location |
| 91 |
they occurred at (rather than something like "unknown position -- locator |
| 92 |
missing"), since DaCHS 2.9.3 you can set ``self.location``. As the |
| 93 |
error reporting code includes the sourceToken itself, there is generally |
| 94 |
no need to include it in the location. Make it a string, though. |
| 95 |
""" |
| 96 |
name_ = "embeddedGrammar" |
| 97 |
_iterator = base.StructAttribute("iterator", default=base.Undefined, |
| 98 |
childFactory=EmbeddedIterator, |
| 99 |
description="Code yielding row dictionaries", copyable=True) |
| 100 |
_pargetter = base.StructAttribute("pargetter", default=None, |
| 101 |
childFactory=EmbeddedPargetter, |
| 102 |
description="Code returning a parameter dictionary", copyable=True) |
| 103 |
_isDispatching = base.BooleanAttribute("isDispatching", default=False, |
| 104 |
description="Is this a dispatching grammar (i.e., does the row iterator" |
| 105 |
" return pairs of role, row rather than only rows)?", copyable=True) |
| 106 |
_notify = base.BooleanAttribute("notify", default=False, |
| 107 |
description="Enable notification of begin/end of processing (as" |
| 108 |
" for other grammars; embedded grammars often have odd source" |
| 109 |
" tokens for which you don't want that. Note that the -M option" |
| 110 |
" of gavo imp will only work if you set this to true.", copyable=True) |
| 111 |
|
| 112 |
def onElementComplete(self): |
| 113 |
super().onElementComplete() |
| 114 |
class RowIterator(common.RowIterator): |
| 115 |
_iterRows = self.iterator.compile() |
| 116 |
notify = self.notify |
| 117 |
location = "unknown location (set self.location to see something here)" |
| 118 |
|
| 119 |
def getLocator(self): |
| 120 |
return self.location |
| 121 |
|
| 122 |
if self.pargetter: |
| 123 |
RowIterator.getParameters = self.pargetter.compile() |
| 124 |
|
| 125 |
self.rowIterator = RowIterator |