From 59f000cdcece383efcf03206f0b85d0cc58b0acc Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Thu, 13 Jan 2011 00:44:07 -0500 Subject: [PATCH] Replace Action with Action* so can be polymorphic --- parse.cpp | 15 ++-- parser.cpp | 18 +++-- parser.hpp | 2 +- parserstate.cpp | 171 ++++++++++++++++++++++--------------------- parserstate.hpp | 18 ++--- parsertransition.hpp | 4 +- templates.cpp | 2 +- 7 files changed, 123 insertions(+), 107 deletions(-) diff --git a/parse.cpp b/parse.cpp index b56794c..ce6d9a2 100644 --- a/parse.cpp +++ b/parse.cpp @@ -254,17 +254,22 @@ int vt_parser( struct stripstate *state ) /* feed to vtparse */ for ( size_t i = 0; i < out_index; i++ ) { - std::vector actions = state->parser.input( out_buffer[ i ] ); - for ( std::vector::iterator j = actions.begin(); + std::vector actions = state->parser.input( out_buffer[ i ] ); + for ( std::vector::iterator j = actions.begin(); j != actions.end(); j++ ) { - if ( j->char_present ) { - printf( "%s(0x%02x=%lc) ", j->name.c_str(), j->ch, j->ch ); + Parser::Action *act = *j; + assert( act ); + + if ( act->char_present ) { + printf( "%s(0x%02x=%lc) ", act->name.c_str(), act->ch, act->ch ); } else { - printf( "[%s] ", j->name.c_str() ); + printf( "[%s] ", act->name.c_str() ); } + delete act; + fflush( stdout ); } } diff --git a/parser.cpp b/parser.cpp index f1d6426..bb44442 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1,19 +1,27 @@ #include "parser.hpp" -std::vector Parser::Parser::input( wchar_t ch ) +std::vector Parser::Parser::input( wchar_t ch ) { - std::vector ret; + std::vector ret; Transition tx = state->input( ch ); if ( tx.next_state != NULL ) { - ret.push_back( state->exit() ); + Action *exitact = state->exit(); + if ( exitact ) { + ret.push_back( exitact ); + } } - ret.push_back( tx.action ); + if ( tx.action ) { + ret.push_back( tx.action ); + } if ( tx.next_state != NULL ) { - ret.push_back( tx.next_state->enter() ); + Action *enteract = tx.next_state->enter(); + if ( enteract ) { + ret.push_back( enteract ); + } state = tx.next_state; } diff --git a/parser.hpp b/parser.hpp index a3a7810..efe97b0 100644 --- a/parser.hpp +++ b/parser.hpp @@ -25,7 +25,7 @@ namespace Parser { bool operator=( const Parser & ); ~Parser() {} - std::vector input( wchar_t c ); + std::vector input( wchar_t c ); }; } diff --git a/parserstate.cpp b/parserstate.cpp index eec7f9c..4b385fe 100644 --- a/parserstate.cpp +++ b/parserstate.cpp @@ -9,22 +9,22 @@ Transition State::anywhere_rule( wchar_t ch ) || ((0x80 <= ch) && (ch <= 0x8F)) || ((0x91 <= ch) && (ch <= 0x97)) || (ch == 0x99) || (ch == 0x9A) ) { - return Transition( Execute(), &family->s_Ground ); + return Transition( new Execute(), &family->s_Ground ); } else if ( ch == 0x9C ) { - return Transition( Ignore(), &family->s_Ground ); + return Transition( NULL, &family->s_Ground ); } else if ( ch == 0x1B ) { - return Transition( Ignore(), &family->s_Escape ); + return Transition( NULL, &family->s_Escape ); } else if ( (ch == 0x98) || (ch == 0x9E) || (ch == 0x9F) ) { - return Transition( Ignore(), &family->s_SOS_PM_APC_String ); + return Transition( NULL, &family->s_SOS_PM_APC_String ); } else if ( ch == 0x90 ) { - return Transition( Ignore(), &family->s_DCS_Entry ); + return Transition( NULL, &family->s_DCS_Entry ); } else if ( ch == 0x9D ) { - return Transition( Ignore(), &family->s_OSC_String ); + return Transition( NULL, &family->s_OSC_String ); } else if ( ch == 0x9B ) { - return Transition( Ignore(), &family->s_CSI_Entry ); + return Transition( NULL, &family->s_CSI_Entry ); } - return Transition( Ignore(), NULL ); + return Transition(); } Transition State::input( wchar_t ch ) @@ -38,8 +38,11 @@ Transition State::input( wchar_t ch ) } } - ret.action.char_present = true; - ret.action.ch = ch; + if ( ret.action ) { + ret.action->char_present = true; + ret.action->ch = ch; + } + return ret; } @@ -59,29 +62,29 @@ static bool GLGR ( wchar_t ch ) Transition Ground::input_state_rule( wchar_t ch ) { if ( C0_prime( ch ) ) { - return Transition( Execute(), NULL ); + return Transition( new Execute() ); } if ( GLGR( ch ) ) { - return Transition( Print(), NULL ); + return Transition( new Print() ); } - return Transition( Ignore(), NULL ); + return Transition(); } -Action Escape::enter( void ) +Action *Escape::enter( void ) { - return Clear(); + return new Clear(); } Transition Escape::input_state_rule( wchar_t ch ) { if ( C0_prime( ch ) ) { - return Transition( Execute(), NULL ); + return Transition( new Execute() ); } if ( (0x20 <= ch) && (ch <= 0x2F) ) { - return Transition( Collect(), &family->s_Escape_Intermediate ); + return Transition( new Collect(), &family->s_Escape_Intermediate ); } if ( ( (0x30 <= ch) && (ch <= 0x4F) ) @@ -90,263 +93,263 @@ Transition Escape::input_state_rule( wchar_t ch ) || ( ch == 0x5A ) || ( ch == 0x5C ) || ( (0x60 <= ch) && (ch <= 0x7E) ) ) { - return Transition( Esc_Dispatch(), &family->s_Ground ); + return Transition( new Esc_Dispatch(), &family->s_Ground ); } if ( ch == 0x5B ) { - return Transition( Ignore(), &family->s_CSI_Entry ); + return Transition( NULL, &family->s_CSI_Entry ); } if ( ch == 0x5D ) { - return Transition( Ignore(), &family->s_OSC_String ); + return Transition( NULL, &family->s_OSC_String ); } if ( ch == 0x50 ) { - return Transition( Ignore(), &family->s_DCS_Entry ); + return Transition( NULL, &family->s_DCS_Entry ); } if ( (ch == 0x58) || (ch == 0x5E) || (ch == 0x5F) ) { - return Transition( Ignore(), &family->s_SOS_PM_APC_String ); + return Transition( NULL, &family->s_SOS_PM_APC_String ); } - return Transition( Ignore(), NULL ); + return Transition(); } Transition Escape_Intermediate::input_state_rule( wchar_t ch ) { if ( C0_prime( ch ) ) { - return Transition( Execute(), NULL ); + return Transition( new Execute() ); } if ( (0x20 <= ch) && (ch <= 0x2F) ) { - return Transition( Collect(), NULL ); + return Transition( new Collect() ); } if ( (0x30 <= ch) && (ch <= 0x7E) ) { - return Transition( Esc_Dispatch(), &family->s_Ground ); + return Transition( new Esc_Dispatch(), &family->s_Ground ); } - return Transition( Ignore(), NULL ); + return Transition(); } -Action CSI_Entry::enter( void ) +Action *CSI_Entry::enter( void ) { - return Clear(); + return new Clear(); } Transition CSI_Entry::input_state_rule( wchar_t ch ) { if ( C0_prime( ch ) ) { - return Transition( Execute(), NULL ); + return Transition( new Execute() ); } if ( (0x40 <= ch) && (ch <= 0x7E) ) { - return Transition( CSI_Dispatch(), &family->s_Ground ); + return Transition( new CSI_Dispatch(), &family->s_Ground ); } if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) { - return Transition( Param(), &family->s_CSI_Param ); + return Transition( new Param(), &family->s_CSI_Param ); } if ( (ch <= 0x3C) && (ch <= 0x3F) ) { - return Transition( Collect(), &family->s_CSI_Param ); + return Transition( new Collect(), &family->s_CSI_Param ); } if ( ch == 0x3A ) { - return Transition( Ignore(), &family->s_CSI_Ignore ); + return Transition( NULL, &family->s_CSI_Ignore ); } if ( (0x20 <= ch) && (ch <= 0x2F) ) { - return Transition( Collect(), &family->s_CSI_Intermediate ); + return Transition( new Collect(), &family->s_CSI_Intermediate ); } - return Transition( Ignore(), NULL ); + return Transition(); } Transition CSI_Param::input_state_rule( wchar_t ch ) { if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) { - return Transition( Param(), NULL ); + return Transition( new Param() ); } if ( ( ch == 0x3A ) || ( (0x3C <= ch) && (ch <= 0x3F) ) ) { - return Transition( Ignore(), &family->s_CSI_Ignore ); + return Transition( NULL, &family->s_CSI_Ignore ); } if ( (0x20 <= ch) && (ch <= 0x2F) ) { - return Transition( Collect(), &family->s_CSI_Intermediate ); + return Transition( new Collect(), &family->s_CSI_Intermediate ); } if ( (0x40 <= ch) && (ch <= 0x7E) ) { - return Transition( CSI_Dispatch(), &family->s_Ground ); + return Transition( new CSI_Dispatch(), &family->s_Ground ); } - return Transition( Ignore(), NULL ); + return Transition(); } Transition CSI_Intermediate::input_state_rule( wchar_t ch ) { if ( C0_prime( ch ) ) { - return Transition( Execute(), NULL ); + return Transition( new Execute() ); } if ( (0x20 <= ch) && (ch <= 0x2F) ) { - return Transition( Collect(), NULL ); + return Transition( new Collect() ); } if ( (0x40 <= ch) && (ch <= 0x7E) ) { - return Transition( CSI_Dispatch(), &family->s_Ground ); + return Transition( new CSI_Dispatch(), &family->s_Ground ); } if ( (0x30 <= ch) && (ch <= 0x3F) ) { - return Transition( Ignore(), &family->s_CSI_Ignore ); + return Transition( NULL, &family->s_CSI_Ignore ); } - return Transition( Ignore(), NULL ); + return Transition(); } Transition CSI_Ignore::input_state_rule( wchar_t ch ) { if ( C0_prime( ch ) ) { - return Transition( Execute(), NULL ); + return Transition( new Execute() ); } if ( (0x40 <= ch) && (ch <= 0x7E) ) { - return Transition( Ignore(), &family->s_Ground ); + return Transition( NULL, &family->s_Ground ); } - return Transition( Ignore(), NULL ); + return Transition(); } -Action DCS_Entry::enter( void ) +Action *DCS_Entry::enter( void ) { - return Clear(); + return new Clear(); } Transition DCS_Entry::input_state_rule( wchar_t ch ) { if ( (0x20 <= ch) && (ch <= 0x2F) ) { - return Transition( Collect(), &family->s_DCS_Intermediate ); + return Transition( new Collect(), &family->s_DCS_Intermediate ); } if ( ch == 0x3A ) { - return Transition( Ignore(), &family->s_DCS_Ignore ); + return Transition( NULL, &family->s_DCS_Ignore ); } if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) { - return Transition( Param(), &family->s_DCS_Param ); + return Transition( new Param(), &family->s_DCS_Param ); } if ( (0x3C <= ch) && (ch <= 0x3F) ) { - return Transition( Collect(), &family->s_DCS_Param ); + return Transition( new Collect(), &family->s_DCS_Param ); } if ( (0x40 <= ch) && (ch <= 0x7E) ) { - return Transition( Ignore(), &family->s_DCS_Passthrough ); + return Transition( NULL, &family->s_DCS_Passthrough ); } - return Transition( Ignore(), NULL ); + return Transition(); } Transition DCS_Param::input_state_rule( wchar_t ch ) { if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) { - return Transition( Param(), NULL ); + return Transition( new Param() ); } if ( ( ch == 0x3A ) || ( (0x3C <= ch) && (ch <= 0x3F) ) ) { - return Transition( Ignore(), &family->s_DCS_Ignore ); + return Transition( NULL, &family->s_DCS_Ignore ); } if ( (0x20 <= ch) && (ch <= 0x2F) ) { - return Transition( Collect(), &family->s_DCS_Intermediate ); + return Transition( new Collect(), &family->s_DCS_Intermediate ); } if ( (0x40 <= ch) && (ch <= 0x7E) ) { - return Transition( Ignore(), &family->s_DCS_Passthrough ); + return Transition( NULL, &family->s_DCS_Passthrough ); } - return Transition( Ignore(), NULL ); + return Transition(); } Transition DCS_Intermediate::input_state_rule( wchar_t ch ) { if ( (0x20 <= ch) && (ch <= 0x2F) ) { - return Transition( Collect(), NULL ); + return Transition( new Collect() ); } if ( (0x40 <= ch) && (ch <= 0x7E) ) { - return Transition( Ignore(), &family->s_DCS_Passthrough ); + return Transition( NULL, &family->s_DCS_Passthrough ); } if ( (0x30 <= ch) && (ch <= 0x3F) ) { - return Transition( Ignore(), &family->s_DCS_Ignore ); + return Transition( NULL, &family->s_DCS_Ignore ); } - return Transition( Ignore(), NULL ); + return Transition(); } -Action DCS_Passthrough::enter( void ) +Action *DCS_Passthrough::enter( void ) { - return Hook(); + return new Hook(); } -Action DCS_Passthrough::exit( void ) +Action *DCS_Passthrough::exit( void ) { - return Unhook(); + return new Unhook(); } Transition DCS_Passthrough::input_state_rule( wchar_t ch ) { if ( C0_prime( ch ) || ( (0x20 <= ch) && (ch <= 0x7E) ) ) { - return Transition( Put(), NULL ); + return Transition( new Put() ); } if ( ch == 0x9C ) { - return Transition( Ignore(), &family->s_Ground ); + return Transition( NULL, &family->s_Ground ); } - return Transition( Ignore(), NULL ); + return Transition(); } Transition DCS_Ignore::input_state_rule( wchar_t ch ) { if ( ch == 0x9C ) { - return Transition( Ignore(), &family->s_Ground ); + return Transition( NULL, &family->s_Ground ); } - return Transition( Ignore(), NULL ); + return Transition(); } -Action OSC_String::enter( void ) +Action *OSC_String::enter( void ) { - return OSC_Start(); + return new OSC_Start(); } -Action OSC_String::exit( void ) +Action *OSC_String::exit( void ) { - return OSC_End(); + return new OSC_End(); } Transition OSC_String::input_state_rule( wchar_t ch ) { if ( (0x20 <= ch) && (ch <= 0x7F) ) { - return Transition( OSC_Put(), NULL ); + return Transition( new OSC_Put() ); } if ( (ch == 0x9C) || (ch == 0x07) ) { /* 0x07 is xterm non-ANSI variant */ - return Transition( Ignore(), &family->s_Ground ); + return Transition( NULL, &family->s_Ground ); } - return Transition( Ignore(), NULL ); + return Transition(); } Transition SOS_PM_APC_String::input_state_rule( wchar_t ch ) { if ( ch == 0x9C ) { - return Transition( Ignore(), &family->s_Ground ); + return Transition( NULL, &family->s_Ground ); } - return Transition( Ignore(), NULL ); + return Transition(); } diff --git a/parserstate.hpp b/parserstate.hpp index 3475062..391cae7 100644 --- a/parserstate.hpp +++ b/parserstate.hpp @@ -18,8 +18,8 @@ namespace Parser { public: void setfamily( StateFamily *s_family ) { family = s_family; } Transition input( wchar_t ch ); - virtual Action enter( void ) { return Ignore(); }; - virtual Action exit( void ) { return Ignore(); }; + virtual Action *enter( void ) { return NULL; } + virtual Action *exit( void ) { return NULL; } State() : family( NULL ) {}; virtual ~State() {}; @@ -33,7 +33,7 @@ namespace Parser { }; class Escape : public State { - Action enter( void ); + Action *enter( void ); Transition input_state_rule( wchar_t ch ); }; @@ -42,7 +42,7 @@ namespace Parser { }; class CSI_Entry : public State { - Action enter( void ); + Action *enter( void ); Transition input_state_rule( wchar_t ch ); }; class CSI_Param : public State { @@ -56,7 +56,7 @@ namespace Parser { }; class DCS_Entry : public State { - Action enter( void ); + Action *enter( void ); Transition input_state_rule( wchar_t ch ); }; class DCS_Param : public State { @@ -66,18 +66,18 @@ namespace Parser { Transition input_state_rule( wchar_t ch ); }; class DCS_Passthrough : public State { - Action enter( void ); + Action *enter( void ); Transition input_state_rule( wchar_t ch ); - Action exit( void ); + Action *exit( void ); }; class DCS_Ignore : public State { Transition input_state_rule( wchar_t ch ); }; class OSC_String : public State { - Action enter( void ); + Action *enter( void ); Transition input_state_rule( wchar_t ch ); - Action exit( void ); + Action *exit( void ); }; class SOS_PM_APC_String : public State { Transition input_state_rule( wchar_t ch ); diff --git a/parsertransition.hpp b/parsertransition.hpp index e62bcf7..992795d 100644 --- a/parsertransition.hpp +++ b/parsertransition.hpp @@ -11,7 +11,7 @@ namespace Parser { class Transition { public: - Action action; + Action *action; State *next_state; Transition( const Transition &x ) @@ -26,7 +26,7 @@ namespace Parser { } virtual ~Transition() {} - Transition( Action s_action=Ignore(), State *s_next_state=NULL ) + Transition( Action *s_action=NULL, State *s_next_state=NULL ) : action( s_action ), next_state( s_next_state ) {} }; diff --git a/templates.cpp b/templates.cpp index 2d6a4f3..ec0398b 100644 --- a/templates.cpp +++ b/templates.cpp @@ -2,4 +2,4 @@ #include "parseraction.hpp" -template class std::vector; +template class std::vector;