| 1 |
""" |
| 2 |
A wrapper around pyparsing. We need this because of the various |
| 3 |
idiosyncrasies pyparsing has had over the years, and also because pyparsing |
| 4 |
is not terribly well suited for the multi-grammar situation we have here. |
| 5 |
|
| 6 |
Hence, whenever you use pyparsing in DaCHS: Use parsetricks instead of |
| 7 |
pyparsing. |
| 8 |
|
| 9 |
This is particularly important on older systems which may have pyparsings |
| 10 |
older than 2.2. There are rather significant changes from 2.2 to 2.4, |
| 11 |
and we fall back to the built-in pyparsing (which is 2.4) if your pyparsing |
| 12 |
is too old. |
| 13 |
""" |
| 14 |
|
| 15 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 16 |
#c |
| 17 |
#c This program is free software, covered by the GNU GPL. See the |
| 18 |
#c COPYING file in the source distribution. |
| 19 |
|
| 20 |
|
| 21 |
# Not checked by pyflakes: (effectively) API file with gratuitous imports |
| 22 |
|
| 23 |
import contextlib |
| 24 |
import os |
| 25 |
import threading |
| 26 |
|
| 27 |
if True: |
| 28 |
from gavo.imp.pyparsing import * |
| 29 |
else: |
| 30 |
import pyparsing |
| 31 |
if pyparsing.__version__<"2.4": |
| 32 |
from gavo.imp.pyparsing import * |
| 33 |
else: |
| 34 |
from pyparsing import * |
| 35 |
|
| 36 |
|
| 37 |
class MatchFirstWithSaneError(MatchFirst): |
| 38 |
# upstream MatchFirst munges the error messages, which sometimes |
| 39 |
# really is distastrous for us. We monkeypatch this until |
| 40 |
# https://github.com/pyparsing/pyparsing/issues/464 is somehow |
| 41 |
# resolved |
| 42 |
def parseImpl(self, instring, loc, doActions=True): |
| 43 |
maxExcLoc = -1 |
| 44 |
maxException = None |
| 45 |
for e in self.exprs: |
| 46 |
try: |
| 47 |
ret = e._parse(instring, loc, doActions) |
| 48 |
return ret |
| 49 |
except ParseException as err: |
| 50 |
if err.loc > maxExcLoc: |
| 51 |
maxException = err |
| 52 |
maxExcLoc = err.loc |
| 53 |
except IndexError: |
| 54 |
if len(instring) > maxExcLoc: |
| 55 |
maxException = ParseException(instring, len(instring), e.errmsg, self) |
| 56 |
maxExcLoc = len(instring) |
| 57 |
|
| 58 |
# only got here if no expression matched, raise exception for match that made it the furthest |
| 59 |
else: |
| 60 |
if maxException is not None: |
| 61 |
raise maxException |
| 62 |
else: |
| 63 |
raise ParseException(instring, loc, "no defined alternatives to match", self) |
| 64 |
|
| 65 |
|
| 66 |
####################### Pyparsing hacks |
| 67 |
# |
| 68 |
# (1) When building grammars, always do so using the pyparsingWhitechars |
| 69 |
# context manager. Building grammars is thread-safe, but different |
| 70 |
# grammars here use different whitespace conventions, so without |
| 71 |
# the c.m., you might get those messed up. |
| 72 |
# |
| 73 |
# (2) When parsing strings, *always* go through pyparseString(grammar, |
| 74 |
# string) and fellow functions whenever your code could run from within |
| 75 |
# the server (i.e., basically always outside of tests). |
| 76 |
# pyparsing is not thread-safe, and thus we'll need to shoehorn some |
| 77 |
# locking on top of it; I don't want to change the pyparsing methods |
| 78 |
# themselves since they may be called very frequently. |
| 79 |
|
| 80 |
ParserElement.enablePackrat() |
| 81 |
|
| 82 |
_PYPARSE_LOCK = threading.RLock() |
| 83 |
|
| 84 |
@contextlib.contextmanager |
| 85 |
def pyparsingWhitechars(whiteChars): |
| 86 |
"""a context manager that serializes pyparsing grammar compilation |
| 87 |
and manages its whitespace chars. |
| 88 |
|
| 89 |
We need different whitespace definitions in some parts of DaCHS. |
| 90 |
(The default used to be " \\t" for a while, so this is what things |
| 91 |
get reset to). |
| 92 |
|
| 93 |
Since whitespace apparently can only be set globally for pyparsing, |
| 94 |
we provide this c.m. Since it is possible that grammars will be |
| 95 |
compiled in threads (e.g., as a side effect of getRD), this is |
| 96 |
protected by a lock. This, in turn, means that this can |
| 97 |
potentially block for a fairly long time. |
| 98 |
|
| 99 |
Bottom line: When compiling pyparsing grammars, *always* set |
| 100 |
the whitespace chars explicitly, and do it through this c.m. |
| 101 |
""" |
| 102 |
_PYPARSE_LOCK.acquire() |
| 103 |
ParserElement.setDefaultWhitespaceChars(whiteChars) |
| 104 |
try: |
| 105 |
yield |
| 106 |
finally: |
| 107 |
ParserElement.setDefaultWhitespaceChars(" \t") |
| 108 |
_PYPARSE_LOCK.release() |
| 109 |
|
| 110 |
|
| 111 |
def pyparseString(grammar, string, **kwargs): |
| 112 |
"""parses a string using a pyparsing grammar thread-safely. |
| 113 |
""" |
| 114 |
with _PYPARSE_LOCK: |
| 115 |
res = grammar.parseString(string, **kwargs) |
| 116 |
ParserElement.resetCache() |
| 117 |
return res |
| 118 |
|
| 119 |
|
| 120 |
def pyparseTransform(grammar, string, **kwargs): |
| 121 |
"""calls grammar's transformString method thread-safely. |
| 122 |
""" |
| 123 |
with _PYPARSE_LOCK: |
| 124 |
return grammar.transformString(string, **kwargs) |