From 91ae87c7ab0c26a387eb42243e50baf0ded30250 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Wed, 13 Feb 2013 23:10:44 +0100 Subject: rewritten string building state machine using a common MicroStateMachine base class * uses label pointers instead of state-enum and switch case (both faster and shorter to write) * common interface for all micro state machines: makes them reusable --- src/parser.h | 76 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 25 deletions(-) (limited to 'src/parser.h') diff --git a/src/parser.h b/src/parser.h index 13b99d4..26cd064 100644 --- a/src/parser.h +++ b/src/parser.h @@ -21,6 +21,7 @@ #include #include +#include "undo.h" #include "sciteco.h" /* TECO uses only lower 7 bits for commands */ @@ -109,36 +110,62 @@ protected: } }; +template +class MicroStateMachine { +protected: + typedef void *MicroState; + MicroState state; + +public: + MicroStateMachine() : state(NULL) {} + + inline void + set(MicroState next) + { + if (next != state) + undo.push_var(state) = next; + } + + virtual inline void + reset(void) + { + set(NULL); + } + + virtual Type input(gchar chr) throw (State::Error) = 0; +}; + +class StringBuildingMachine : public MicroStateMachine { + enum Mode { + MODE_NORMAL, + MODE_UPPER, + MODE_LOWER + } mode; + + bool toctl; + +public: + StringBuildingMachine() : MicroStateMachine(), + mode(MODE_NORMAL), toctl(false) {} + + void + reset(void) + { + MicroStateMachine::reset(); + undo.push_var(mode) = MODE_NORMAL; + undo.push_var(toctl) = false; + } + + gchar *input(gchar chr) throw (State::Error); +}; + /* * Super-class for states accepting string arguments * Opaquely cares about alternative-escape characters, * string building commands and accumulation into a string */ class StateExpectString : public State { - struct Machine { - enum State { - STATE_START, - STATE_ESCAPED, - STATE_LOWER, - STATE_UPPER, - STATE_CTL_E, - STATE_CTL_EQ, - STATE_CTL_EQ_LOCAL, - STATE_CTL_EU, - STATE_CTL_EU_LOCAL - } state; - - enum Mode { - MODE_NORMAL, - MODE_UPPER, - MODE_LOWER - } mode; - - bool toctl; - - Machine() : state(STATE_START), - mode(MODE_NORMAL), toctl(false) {} - } machine; + StringBuildingMachine machine; gint nesting; @@ -151,7 +178,6 @@ public: string_building(_building), last(_last) {} private: - gchar *machine_input(gchar key) throw (Error); State *custom(gchar chr) throw (Error); protected: -- cgit v1.2.3