gavo.utils.parsetricks module

A wrapper around pyparsing. We need this because of the various idiosyncrasies pyparsing has had over the years, and also because pyparsing is not terribly well suited for the multi-grammar situation we have here.

Hence, whenever you use pyparsing in DaCHS: Use parsetricks instead of pyparsing.

This is particularly important on older systems which may have pyparsings older than 2.2. There are rather significant changes from 2.2 to 2.4, and we fall back to the built-in pyparsing (which is 2.4) if your pyparsing is too old.

class gavo.utils.parsetricks.MatchFirstWithSaneError(exprs: Iterable[ParserElement], savelist: bool = False)[source]

Bases: MatchFirst

parseImpl(instring, loc, doActions=True)[source]
gavo.utils.parsetricks.pyparseString(grammar, string, **kwargs)[source]

parses a string using a pyparsing grammar thread-safely.

gavo.utils.parsetricks.pyparseTransform(grammar, string, **kwargs)[source]

calls grammar’s transformString method thread-safely.

gavo.utils.parsetricks.pyparsingWhitechars(whiteChars)[source]

a context manager that serializes pyparsing grammar compilation and manages its whitespace chars.

We need different whitespace definitions in some parts of DaCHS. (The default used to be “ t” for a while, so this is what things get reset to).

Since whitespace apparently can only be set globally for pyparsing, we provide this c.m. Since it is possible that grammars will be compiled in threads (e.g., as a side effect of getRD), this is protected by a lock. This, in turn, means that this can potentially block for a fairly long time.

Bottom line: When compiling pyparsing grammars, always set the whitespace chars explicitly, and do it through this c.m.