diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-16 23:09:57 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-16 23:09:57 -0500 |
commit | a0d49ee85f83348ad694b716b18c7cca604ca3ca (patch) | |
tree | a74a34328f8b709bb7fef0115f8dbad8c7230528 /examples/statemachine/statemachine.py | |
parent | 6c447c4d663d0badddd84976033504c855325ce5 (diff) | |
download | pyparsing-git-a0d49ee85f83348ad694b716b18c7cca604ca3ca.tar.gz |
Added change note re: changes to statemachine example; some code reformat/cleanup/commenting in statemachine.py
Diffstat (limited to 'examples/statemachine/statemachine.py')
-rw-r--r-- | examples/statemachine/statemachine.py | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/examples/statemachine/statemachine.py b/examples/statemachine/statemachine.py index c46e3d0..f4244a5 100644 --- a/examples/statemachine/statemachine.py +++ b/examples/statemachine/statemachine.py @@ -49,6 +49,9 @@ namedStateMachine = (pp.Keyword("statemachine") + ident("name") + ":" def expand_state_definition(source, loc, tokens): + """ + Parse action to convert statemachine to corresponding Python classes and methods + """ indent = " " * (pp.col(loc, source) - 1) statedef = [] @@ -66,9 +69,11 @@ def expand_state_definition(source, loc, tokens): "class %s(object):" % baseStateClass, " def __str__(self):", " return self.__class__.__name__", + " @classmethod", " def states(cls):", " return list(cls.__subclasses__())", + " def next_state(self):", " return self._next_state_class()", ]) @@ -106,6 +111,10 @@ stateMachine.setParseAction(expand_state_definition) def expand_named_state_definition(source, loc, tokens): + """ + Parse action to convert statemachine with named transitions to corresponding Python + classes and methods + """ indent = " " * (pp.col(loc, source) - 1) statedef = [] # build list of states and transitions @@ -152,15 +161,18 @@ def expand_named_state_definition(source, loc, tokens): "class %s(object):" % baseStateClass, " def __str__(self):", " return self.__class__.__name__", + " @classmethod", " def states(cls):", " return list(cls.__subclasses__())", + " @classmethod", " def next_state(cls, name):", " try:", " return cls.tnmap[name]()", " except KeyError:", " raise InvalidTransitionException('%s does not support transition %r'% (cls.__name__, name))", + " def __bad_tn(name):", " def _fn(cls):", " raise InvalidTransitionException('%s does not support transition %r'% (cls.__name__, name))", @@ -194,6 +206,7 @@ def expand_named_state_definition(source, loc, tokens): ) ]) + # define <state>Mixin class for application classes that delegate to the state statedef.extend([ "class {baseStateClass}Mixin:".format(baseStateClass=baseStateClass), " def __init__(self):", @@ -217,7 +230,6 @@ def expand_named_state_definition(source, loc, tokens): " return '{0}: {1}'.format(self.__class__.__name__, self._state)" ]) - return indent + ("\n" + indent).join(statedef) + "\n" namedStateMachine.setParseAction(expand_named_state_definition) |