diff options
author | ptmcg <ptmcg@austin.rr.com> | 2022-11-06 17:14:59 -0600 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2022-11-06 17:14:59 -0600 |
commit | 764dbdc1970e522f5ffe3bd03a0ddb5d1cfcb798 (patch) | |
tree | 7f52795bf2da70d68bc7889c2170e3a8b96bf0c6 | |
parent | 6aef782b6d347db134bafa66004c60b42e42e427 (diff) | |
download | pyparsing-git-764dbdc1970e522f5ffe3bd03a0ddb5d1cfcb798.tar.gz |
Added new class method `ParserElement.using_each`
-rw-r--r-- | CHANGES | 22 | ||||
-rw-r--r-- | pyparsing/__init__.py | 2 | ||||
-rw-r--r-- | pyparsing/core.py | 12 |
3 files changed, 35 insertions, 1 deletions
@@ -50,6 +50,28 @@ help from Devin J. Pohly in structuring the code to enable this peaceful transit of single-line or multiline quoted strings defined in Python. (Inspired by discussion with Andreas Schörgenhumer in Issue #421.) +- Added new class method `ParserElement.using_each`, to simplify code + that creates a sequence of `Literals`, `Keywords`, or other `ParserElement` + subclasses. + + For instance, to define suppressable punctuation, one would previously + write: + + LPAR, RPAR, LBRACE, RBRACE, SEMI = map(Suppress, "(){};") + + You can now write: + + LPAR, RPAR, LBRACE, RBRACE, SEMI = Suppress.using_each("(){};") + + `using_each` will also accept optional keyword args, which it will + pass through to the class initializer. Here is an expression for + single-letter variable names that might be used in an algebraic + expression: + + algebra_var = MatchFirst( + Char.using_each(string.ascii_lowercase, as_keyword=True) + ) + - Added bool `embed` argument to `ParserElement.create_diagram()`. When passed as True, the resulting diagram will omit the `<DOCTYPE>`, `<HEAD>`, and `<BODY>` tags so that it can be embedded in other diff --git a/pyparsing/__init__.py b/pyparsing/__init__.py index 0c0321d..c27052e 100644 --- a/pyparsing/__init__.py +++ b/pyparsing/__init__.py @@ -121,7 +121,7 @@ class version_info(NamedTuple): __version_info__ = version_info(3, 0, 10, "final", 0) -__version_time__ = "14 Jul 2022 07:55 UTC" +__version_time__ = "06 Nov 2022 23:07 UTC" __version__ = __version_info__.__version__ __versionTime__ = __version_time__ __author__ = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>" diff --git a/pyparsing/core.py b/pyparsing/core.py index c466dbc..a6858bd 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -440,6 +440,18 @@ class ParserElement(ABC): """ ParserElement._literalStringClass = cls + @classmethod + def using_each(cls, seq, **class_kwargs): + """ + Yields a sequence of class(obj, **class_kwargs) for obj in seq. + + Example:: + + LPAR, RPAR, LBRACE, RBRACE, SEMI = Suppress.using_each("(){};") + + """ + yield from (cls(obj, **class_kwargs) for obj in seq) + class DebugActions(NamedTuple): debug_try: typing.Optional[DebugStartAction] debug_match: typing.Optional[DebugSuccessAction] |