Replace Action with Action* so can be polymorphic

This commit is contained in:
Keith Winstein
2011-01-13 00:44:07 -05:00
parent 82ac626320
commit 59f000cdce
7 changed files with 123 additions and 107 deletions
+10 -5
View File
@@ -254,17 +254,22 @@ int vt_parser( struct stripstate *state )
/* feed to vtparse */ /* feed to vtparse */
for ( size_t i = 0; i < out_index; i++ ) { for ( size_t i = 0; i < out_index; i++ ) {
std::vector<Parser::Action> actions = state->parser.input( out_buffer[ i ] ); std::vector<Parser::Action *> actions = state->parser.input( out_buffer[ i ] );
for ( std::vector<Parser::Action>::iterator j = actions.begin(); for ( std::vector<Parser::Action *>::iterator j = actions.begin();
j != actions.end(); j != actions.end();
j++ ) { j++ ) {
if ( j->char_present ) { Parser::Action *act = *j;
printf( "%s(0x%02x=%lc) ", j->name.c_str(), j->ch, j->ch ); assert( act );
if ( act->char_present ) {
printf( "%s(0x%02x=%lc) ", act->name.c_str(), act->ch, act->ch );
} else { } else {
printf( "[%s] ", j->name.c_str() ); printf( "[%s] ", act->name.c_str() );
} }
delete act;
fflush( stdout ); fflush( stdout );
} }
} }
+12 -4
View File
@@ -1,19 +1,27 @@
#include "parser.hpp" #include "parser.hpp"
std::vector<Parser::Action> Parser::Parser::input( wchar_t ch ) std::vector<Parser::Action *> Parser::Parser::input( wchar_t ch )
{ {
std::vector<Action> ret; std::vector<Action *> ret;
Transition tx = state->input( ch ); Transition tx = state->input( ch );
if ( tx.next_state != NULL ) { if ( tx.next_state != NULL ) {
ret.push_back( state->exit() ); Action *exitact = state->exit();
if ( exitact ) {
ret.push_back( exitact );
}
} }
if ( tx.action ) {
ret.push_back( tx.action ); ret.push_back( tx.action );
}
if ( tx.next_state != NULL ) { 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; state = tx.next_state;
} }
+1 -1
View File
@@ -25,7 +25,7 @@ namespace Parser {
bool operator=( const Parser & ); bool operator=( const Parser & );
~Parser() {} ~Parser() {}
std::vector<Action> input( wchar_t c ); std::vector<Action *> input( wchar_t c );
}; };
} }
+87 -84
View File
@@ -9,22 +9,22 @@ Transition State::anywhere_rule( wchar_t ch )
|| ((0x80 <= ch) && (ch <= 0x8F)) || ((0x80 <= ch) && (ch <= 0x8F))
|| ((0x91 <= ch) && (ch <= 0x97)) || ((0x91 <= ch) && (ch <= 0x97))
|| (ch == 0x99) || (ch == 0x9A) ) { || (ch == 0x99) || (ch == 0x9A) ) {
return Transition( Execute(), &family->s_Ground ); return Transition( new Execute(), &family->s_Ground );
} else if ( ch == 0x9C ) { } else if ( ch == 0x9C ) {
return Transition( Ignore(), &family->s_Ground ); return Transition( NULL, &family->s_Ground );
} else if ( ch == 0x1B ) { } else if ( ch == 0x1B ) {
return Transition( Ignore(), &family->s_Escape ); return Transition( NULL, &family->s_Escape );
} else if ( (ch == 0x98) || (ch == 0x9E) || (ch == 0x9F) ) { } 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 ) { } else if ( ch == 0x90 ) {
return Transition( Ignore(), &family->s_DCS_Entry ); return Transition( NULL, &family->s_DCS_Entry );
} else if ( ch == 0x9D ) { } else if ( ch == 0x9D ) {
return Transition( Ignore(), &family->s_OSC_String ); return Transition( NULL, &family->s_OSC_String );
} else if ( ch == 0x9B ) { } 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 ) Transition State::input( wchar_t ch )
@@ -38,8 +38,11 @@ Transition State::input( wchar_t ch )
} }
} }
ret.action.char_present = true; if ( ret.action ) {
ret.action.ch = ch; ret.action->char_present = true;
ret.action->ch = ch;
}
return ret; return ret;
} }
@@ -59,29 +62,29 @@ static bool GLGR ( wchar_t ch )
Transition Ground::input_state_rule( wchar_t ch ) Transition Ground::input_state_rule( wchar_t ch )
{ {
if ( C0_prime( ch ) ) { if ( C0_prime( ch ) ) {
return Transition( Execute(), NULL ); return Transition( new Execute() );
} }
if ( GLGR( ch ) ) { 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 ) Transition Escape::input_state_rule( wchar_t ch )
{ {
if ( C0_prime( ch ) ) { if ( C0_prime( ch ) ) {
return Transition( Execute(), NULL ); return Transition( new Execute() );
} }
if ( (0x20 <= ch) && (ch <= 0x2F) ) { 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) ) if ( ( (0x30 <= ch) && (ch <= 0x4F) )
@@ -90,263 +93,263 @@ Transition Escape::input_state_rule( wchar_t ch )
|| ( ch == 0x5A ) || ( ch == 0x5A )
|| ( ch == 0x5C ) || ( ch == 0x5C )
|| ( (0x60 <= ch) && (ch <= 0x7E) ) ) { || ( (0x60 <= ch) && (ch <= 0x7E) ) ) {
return Transition( Esc_Dispatch(), &family->s_Ground ); return Transition( new Esc_Dispatch(), &family->s_Ground );
} }
if ( ch == 0x5B ) { if ( ch == 0x5B ) {
return Transition( Ignore(), &family->s_CSI_Entry ); return Transition( NULL, &family->s_CSI_Entry );
} }
if ( ch == 0x5D ) { if ( ch == 0x5D ) {
return Transition( Ignore(), &family->s_OSC_String ); return Transition( NULL, &family->s_OSC_String );
} }
if ( ch == 0x50 ) { if ( ch == 0x50 ) {
return Transition( Ignore(), &family->s_DCS_Entry ); return Transition( NULL, &family->s_DCS_Entry );
} }
if ( (ch == 0x58) || (ch == 0x5E) || (ch == 0x5F) ) { 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 ) Transition Escape_Intermediate::input_state_rule( wchar_t ch )
{ {
if ( C0_prime( ch ) ) { if ( C0_prime( ch ) ) {
return Transition( Execute(), NULL ); return Transition( new Execute() );
} }
if ( (0x20 <= ch) && (ch <= 0x2F) ) { if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( Collect(), NULL ); return Transition( new Collect() );
} }
if ( (0x30 <= ch) && (ch <= 0x7E) ) { 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 ) Transition CSI_Entry::input_state_rule( wchar_t ch )
{ {
if ( C0_prime( ch ) ) { if ( C0_prime( ch ) ) {
return Transition( Execute(), NULL ); return Transition( new Execute() );
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { 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) ) if ( ( (0x30 <= ch) && (ch <= 0x39) )
|| ( ch == 0x3B ) ) { || ( ch == 0x3B ) ) {
return Transition( Param(), &family->s_CSI_Param ); return Transition( new Param(), &family->s_CSI_Param );
} }
if ( (ch <= 0x3C) && (ch <= 0x3F) ) { if ( (ch <= 0x3C) && (ch <= 0x3F) ) {
return Transition( Collect(), &family->s_CSI_Param ); return Transition( new Collect(), &family->s_CSI_Param );
} }
if ( ch == 0x3A ) { if ( ch == 0x3A ) {
return Transition( Ignore(), &family->s_CSI_Ignore ); return Transition( NULL, &family->s_CSI_Ignore );
} }
if ( (0x20 <= ch) && (ch <= 0x2F) ) { 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 ) Transition CSI_Param::input_state_rule( wchar_t ch )
{ {
if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) { if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) {
return Transition( Param(), NULL ); return Transition( new Param() );
} }
if ( ( ch == 0x3A ) || ( (0x3C <= ch) && (ch <= 0x3F) ) ) { 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) ) { 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) ) { 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 ) Transition CSI_Intermediate::input_state_rule( wchar_t ch )
{ {
if ( C0_prime( ch ) ) { if ( C0_prime( ch ) ) {
return Transition( Execute(), NULL ); return Transition( new Execute() );
} }
if ( (0x20 <= ch) && (ch <= 0x2F) ) { if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( Collect(), NULL ); return Transition( new Collect() );
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { 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) ) { 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 ) Transition CSI_Ignore::input_state_rule( wchar_t ch )
{ {
if ( C0_prime( ch ) ) { if ( C0_prime( ch ) ) {
return Transition( Execute(), NULL ); return Transition( new Execute() );
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { 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 ) Transition DCS_Entry::input_state_rule( wchar_t ch )
{ {
if ( (0x20 <= ch) && (ch <= 0x2F) ) { if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( Collect(), &family->s_DCS_Intermediate ); return Transition( new Collect(), &family->s_DCS_Intermediate );
} }
if ( ch == 0x3A ) { if ( ch == 0x3A ) {
return Transition( Ignore(), &family->s_DCS_Ignore ); return Transition( NULL, &family->s_DCS_Ignore );
} }
if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) { 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) ) { 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) ) { 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 ) Transition DCS_Param::input_state_rule( wchar_t ch )
{ {
if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) { if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) {
return Transition( Param(), NULL ); return Transition( new Param() );
} }
if ( ( ch == 0x3A ) || ( (0x3C <= ch) && (ch <= 0x3F) ) ) { 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) ) { 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) ) { 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 ) Transition DCS_Intermediate::input_state_rule( wchar_t ch )
{ {
if ( (0x20 <= ch) && (ch <= 0x2F) ) { if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( Collect(), NULL ); return Transition( new Collect() );
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( Ignore(), &family->s_DCS_Passthrough ); return Transition( NULL, &family->s_DCS_Passthrough );
} }
if ( (0x30 <= ch) && (ch <= 0x3F) ) { 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 ) Transition DCS_Passthrough::input_state_rule( wchar_t ch )
{ {
if ( C0_prime( ch ) || ( (0x20 <= ch) && (ch <= 0x7E) ) ) { if ( C0_prime( ch ) || ( (0x20 <= ch) && (ch <= 0x7E) ) ) {
return Transition( Put(), NULL ); return Transition( new Put() );
} }
if ( ch == 0x9C ) { 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 ) Transition DCS_Ignore::input_state_rule( wchar_t ch )
{ {
if ( ch == 0x9C ) { 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 ) Transition OSC_String::input_state_rule( wchar_t ch )
{ {
if ( (0x20 <= ch) && (ch <= 0x7F) ) { 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 */ 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 ) Transition SOS_PM_APC_String::input_state_rule( wchar_t ch )
{ {
if ( ch == 0x9C ) { if ( ch == 0x9C ) {
return Transition( Ignore(), &family->s_Ground ); return Transition( NULL, &family->s_Ground );
} }
return Transition( Ignore(), NULL ); return Transition();
} }
+9 -9
View File
@@ -18,8 +18,8 @@ namespace Parser {
public: public:
void setfamily( StateFamily *s_family ) { family = s_family; } void setfamily( StateFamily *s_family ) { family = s_family; }
Transition input( wchar_t ch ); Transition input( wchar_t ch );
virtual Action enter( void ) { return Ignore(); }; virtual Action *enter( void ) { return NULL; }
virtual Action exit( void ) { return Ignore(); }; virtual Action *exit( void ) { return NULL; }
State() : family( NULL ) {}; State() : family( NULL ) {};
virtual ~State() {}; virtual ~State() {};
@@ -33,7 +33,7 @@ namespace Parser {
}; };
class Escape : public State { class Escape : public State {
Action enter( void ); Action *enter( void );
Transition input_state_rule( wchar_t ch ); Transition input_state_rule( wchar_t ch );
}; };
@@ -42,7 +42,7 @@ namespace Parser {
}; };
class CSI_Entry : public State { class CSI_Entry : public State {
Action enter( void ); Action *enter( void );
Transition input_state_rule( wchar_t ch ); Transition input_state_rule( wchar_t ch );
}; };
class CSI_Param : public State { class CSI_Param : public State {
@@ -56,7 +56,7 @@ namespace Parser {
}; };
class DCS_Entry : public State { class DCS_Entry : public State {
Action enter( void ); Action *enter( void );
Transition input_state_rule( wchar_t ch ); Transition input_state_rule( wchar_t ch );
}; };
class DCS_Param : public State { class DCS_Param : public State {
@@ -66,18 +66,18 @@ namespace Parser {
Transition input_state_rule( wchar_t ch ); Transition input_state_rule( wchar_t ch );
}; };
class DCS_Passthrough : public State { class DCS_Passthrough : public State {
Action enter( void ); Action *enter( void );
Transition input_state_rule( wchar_t ch ); Transition input_state_rule( wchar_t ch );
Action exit( void ); Action *exit( void );
}; };
class DCS_Ignore : public State { class DCS_Ignore : public State {
Transition input_state_rule( wchar_t ch ); Transition input_state_rule( wchar_t ch );
}; };
class OSC_String : public State { class OSC_String : public State {
Action enter( void ); Action *enter( void );
Transition input_state_rule( wchar_t ch ); Transition input_state_rule( wchar_t ch );
Action exit( void ); Action *exit( void );
}; };
class SOS_PM_APC_String : public State { class SOS_PM_APC_String : public State {
Transition input_state_rule( wchar_t ch ); Transition input_state_rule( wchar_t ch );
+2 -2
View File
@@ -11,7 +11,7 @@ namespace Parser {
class Transition class Transition
{ {
public: public:
Action action; Action *action;
State *next_state; State *next_state;
Transition( const Transition &x ) Transition( const Transition &x )
@@ -26,7 +26,7 @@ namespace Parser {
} }
virtual ~Transition() {} 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 ) : action( s_action ), next_state( s_next_state )
{} {}
}; };
+1 -1
View File
@@ -2,4 +2,4 @@
#include "parseraction.hpp" #include "parseraction.hpp"
template class std::vector<Parser::Action>; template class std::vector<Parser::Action *>;