diff options
author | ptmcg <ptmcg@austin.rr.com> | 2020-12-24 00:30:59 -0600 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2020-12-24 00:30:59 -0600 |
commit | d7fc7e5499276427a4ce2758b3d0a23747d50ce9 (patch) | |
tree | caba495699fc0b5b07026639e897e1d619ca80c4 /pyparsing/actions.py | |
parent | 3abd462a9188e820e69ff8478c999bbf16db05e1 (diff) | |
download | pyparsing-git-d7fc7e5499276427a4ce2758b3d0a23747d50ce9.tar.gz |
Move OnlyOnce out of core.py and into actions.py
Diffstat (limited to 'pyparsing/actions.py')
-rw-r--r-- | pyparsing/actions.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/pyparsing/actions.py b/pyparsing/actions.py index 0c185ab..827c74f 100644 --- a/pyparsing/actions.py +++ b/pyparsing/actions.py @@ -4,6 +4,29 @@ from .exceptions import ParseException from .util import col +class OnlyOnce: + """Wrapper for parse actions, to ensure they are only called once. + """ + + def __init__(self, methodCall): + from .core import _trim_arity + self.callable = _trim_arity(methodCall) + self.called = False + + def __call__(self, s, l, t): + if not self.called: + results = self.callable(s, l, t) + self.called = True + return results + raise ParseException(s, l, "OnlyOnce obj called multiple times w/out reset") + + def reset(self): + """Allow the associated parse action to be called once more. + """ + + self.called = False + + def matchOnlyAtCol(n): """Helper method for defining parse actions that require matching at a specific column in the input text. |