Make each state a singleton instead of constructing anew each time

This commit is contained in:
Keith Winstein
2011-01-09 04:16:07 -05:00
parent f2ceffc5d9
commit 2f70137df0
4 changed files with 106 additions and 40 deletions
+3 -1
View File
@@ -6,14 +6,16 @@
#include "parsertransition.hpp" #include "parsertransition.hpp"
#include "parseraction.hpp" #include "parseraction.hpp"
#include "parserstate.hpp" #include "parserstate.hpp"
#include "parserstatefamily.hpp"
namespace Parser { namespace Parser {
class Parser { class Parser {
private: private:
StateFamily family;
State *state; State *state;
public: public:
Parser() : state( NULL ) {} Parser() : family(), state( NULL ) {}
Parser( const Parser & ); Parser( const Parser & );
bool operator=( const Parser & ); bool operator=( const Parser & );
void input( wchar_t c ); /* should return list of actions */ void input( wchar_t c ); /* should return list of actions */
+40 -39
View File
@@ -1,4 +1,5 @@
#include "parserstate.hpp" #include "parserstate.hpp"
#include "parserstatefamily.hpp"
using namespace Parser; using namespace Parser;
@@ -8,19 +9,19 @@ 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(), new Ground() ); return Transition( Execute(), &family->s_Ground );
} else if ( ch == 0x9C ) { } else if ( ch == 0x9C ) {
return Transition( Ignore(), new Ground() ); return Transition( Ignore(), &family->s_Ground );
} else if ( ch == 0x1B ) { } else if ( ch == 0x1B ) {
return Transition( Ignore(), new Escape() ); return Transition( Ignore(), &family->s_Escape );
} else if ( (ch == 0x98) || (ch == 0x9E) || (ch == 0x9F) ) { } else if ( (ch == 0x98) || (ch == 0x9E) || (ch == 0x9F) ) {
return Transition( Ignore(), new SOS_PM_APC_String() ); return Transition( Ignore(), &family->s_SOS_PM_APC_String );
} else if ( ch == 0x90 ) { } else if ( ch == 0x90 ) {
return Transition( Ignore(), new DCS_Entry() ); return Transition( Ignore(), &family->s_DCS_Entry );
} else if ( ch == 0x9D ) { } else if ( ch == 0x9D ) {
return Transition( Ignore(), new OSC_String() ); return Transition( Ignore(), &family->s_OSC_String );
} else if ( ch == 0x9B ) { } else if ( ch == 0x9B ) {
return Transition( Ignore(), new CSI_Entry() ); return Transition( Ignore(), &family->s_CSI_Entry );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -78,7 +79,7 @@ Transition Escape::input_state_rule( wchar_t ch )
} }
if ( (0x20 <= ch) && (ch <= 0x2F) ) { if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( Collect(), new Escape_Intermediate() ); return Transition( Collect(), &family->s_Escape_Intermediate );
} }
if ( ( (0x30 <= ch) && (ch <= 0x4F) ) if ( ( (0x30 <= ch) && (ch <= 0x4F) )
@@ -87,23 +88,23 @@ 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(), new Ground() ); return Transition( Esc_Dispatch(), &family->s_Ground );
} }
if ( ch == 0x5B ) { if ( ch == 0x5B ) {
return Transition( Ignore(), new CSI_Entry() ); return Transition( Ignore(), &family->s_CSI_Entry );
} }
if ( ch == 0x5D ) { if ( ch == 0x5D ) {
return Transition( Ignore(), new OSC_String() ); return Transition( Ignore(), &family->s_OSC_String );
} }
if ( ch == 0x50 ) { if ( ch == 0x50 ) {
return Transition( Ignore(), new DCS_Entry() ); return Transition( Ignore(), &family->s_DCS_Entry );
} }
if ( (ch == 0x58) || (ch == 0x5E) || (ch == 0x5F) ) { if ( (ch == 0x58) || (ch == 0x5E) || (ch == 0x5F) ) {
return Transition( Ignore(), new SOS_PM_APC_String() ); return Transition( Ignore(), &family->s_SOS_PM_APC_String );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -120,7 +121,7 @@ Transition Escape_Intermediate::input_state_rule( wchar_t ch )
} }
if ( (0x30 <= ch) && (ch <= 0x7E) ) { if ( (0x30 <= ch) && (ch <= 0x7E) ) {
return Transition( Esc_Dispatch(), new Ground() ); return Transition( Esc_Dispatch(), &family->s_Ground );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -138,24 +139,24 @@ Transition CSI_Entry::input_state_rule( wchar_t ch )
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( CSI_Dispatch(), new Ground() ); return Transition( CSI_Dispatch(), &family->s_Ground );
} }
if ( ( (0x30 <= ch) && (ch <= 0x39) ) if ( ( (0x30 <= ch) && (ch <= 0x39) )
|| ( ch == 0x3B ) ) { || ( ch == 0x3B ) ) {
return Transition( Param(), new CSI_Param() ); return Transition( Param(), &family->s_CSI_Param );
} }
if ( (ch <= 0x3C) && (ch <= 0x3F) ) { if ( (ch <= 0x3C) && (ch <= 0x3F) ) {
return Transition( Collect(), new CSI_Param() ); return Transition( Collect(), &family->s_CSI_Param );
} }
if ( ch == 0x3A ) { if ( ch == 0x3A ) {
return Transition( Ignore(), new CSI_Ignore() ); return Transition( Ignore(), &family->s_CSI_Ignore );
} }
if ( (0x20 <= ch) && (ch <= 0x2F) ) { if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( Collect(), new CSI_Intermediate() ); return Transition( Collect(), &family->s_CSI_Intermediate );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -168,15 +169,15 @@ Transition CSI_Param::input_state_rule( wchar_t ch )
} }
if ( ( ch == 0x3A ) || ( (0x3C <= ch) && (ch <= 0x3F) ) ) { if ( ( ch == 0x3A ) || ( (0x3C <= ch) && (ch <= 0x3F) ) ) {
return Transition( Ignore(), new CSI_Ignore() ); return Transition( Ignore(), &family->s_CSI_Ignore );
} }
if ( (0x20 <= ch) && (ch <= 0x2F) ) { if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( Collect(), new CSI_Intermediate() ); return Transition( Collect(), &family->s_CSI_Intermediate );
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( CSI_Dispatch(), new Ground() ); return Transition( CSI_Dispatch(), &family->s_Ground );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -193,11 +194,11 @@ Transition CSI_Intermediate::input_state_rule( wchar_t ch )
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( CSI_Dispatch(), new Ground() ); return Transition( CSI_Dispatch(), &family->s_Ground );
} }
if ( (0x30 <= ch) && (ch <= 0x3F) ) { if ( (0x30 <= ch) && (ch <= 0x3F) ) {
return Transition( Ignore(), new CSI_Ignore() ); return Transition( Ignore(), &family->s_CSI_Ignore );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -210,7 +211,7 @@ Transition CSI_Ignore::input_state_rule( wchar_t ch )
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( Ignore(), new Ground() ); return Transition( Ignore(), &family->s_Ground );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -224,23 +225,23 @@ Action DCS_Entry::enter( void )
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(), new DCS_Intermediate() ); return Transition( Collect(), &family->s_DCS_Intermediate );
} }
if ( ch == 0x3A ) { if ( ch == 0x3A ) {
return Transition( Ignore(), new DCS_Ignore() ); return Transition( Ignore(), &family->s_DCS_Ignore );
} }
if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) { if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) {
return Transition( Param(), new DCS_Param() ); return Transition( Param(), &family->s_DCS_Param );
} }
if ( (0x3C <= ch) && (ch <= 0x3F) ) { if ( (0x3C <= ch) && (ch <= 0x3F) ) {
return Transition( Collect(), new DCS_Param() ); return Transition( Collect(), &family->s_DCS_Param );
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( Ignore(), new DCS_Passthrough() ); return Transition( Ignore(), &family->s_DCS_Passthrough );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -253,15 +254,15 @@ Transition DCS_Param::input_state_rule( wchar_t ch )
} }
if ( ( ch == 0x3A ) || ( (0x3C <= ch) && (ch <= 0x3F) ) ) { if ( ( ch == 0x3A ) || ( (0x3C <= ch) && (ch <= 0x3F) ) ) {
return Transition( Ignore(), new DCS_Ignore() ); return Transition( Ignore(), &family->s_DCS_Ignore );
} }
if ( (0x20 <= ch) && (ch <= 0x2F) ) { if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( Collect(), new DCS_Intermediate() ); return Transition( Collect(), &family->s_DCS_Intermediate );
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( Ignore(), new DCS_Passthrough() ); return Transition( Ignore(), &family->s_DCS_Passthrough );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -274,11 +275,11 @@ Transition DCS_Intermediate::input_state_rule( wchar_t ch )
} }
if ( (0x40 <= ch) && (ch <= 0x7E) ) { if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( Ignore(), new DCS_Passthrough() ); return Transition( Ignore(), &family->s_DCS_Passthrough );
} }
if ( (0x30 <= ch) && (ch <= 0x3F) ) { if ( (0x30 <= ch) && (ch <= 0x3F) ) {
return Transition( Ignore(), new DCS_Ignore() ); return Transition( Ignore(), &family->s_DCS_Ignore );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -301,7 +302,7 @@ Transition DCS_Passthrough::input_state_rule( wchar_t ch )
} }
if ( ch == 0x9C ) { if ( ch == 0x9C ) {
return Transition( Ignore(), new Ground() ); return Transition( Ignore(), &family->s_Ground );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -310,7 +311,7 @@ Transition DCS_Passthrough::input_state_rule( wchar_t ch )
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(), new Ground() ); return Transition( Ignore(), &family->s_Ground );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -333,7 +334,7 @@ Transition OSC_String::input_state_rule( wchar_t ch )
} }
if ( ch == 0x9C ) { if ( ch == 0x9C ) {
return Transition( Ignore(), new Ground() ); return Transition( Ignore(), &family->s_Ground );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
@@ -342,7 +343,7 @@ Transition OSC_String::input_state_rule( wchar_t ch )
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(), new Ground() ); return Transition( Ignore(), &family->s_Ground );
} }
return Transition( Ignore(), NULL ); return Transition( Ignore(), NULL );
+9
View File
@@ -4,10 +4,13 @@
#include "parsertransition.hpp" #include "parsertransition.hpp"
namespace Parser { namespace Parser {
class StateFamily;
class State class State
{ {
protected: protected:
virtual Transition input_state_rule( wchar_t ch ) = 0; virtual Transition input_state_rule( wchar_t ch ) = 0;
StateFamily *family;
private: private:
virtual Action enter( void ) { return Ignore(); }; virtual Action enter( void ) { return Ignore(); };
@@ -17,7 +20,13 @@ namespace Parser {
Transition anywhere_rule( wchar_t ch ); Transition anywhere_rule( wchar_t ch );
public: public:
void setfamily( StateFamily *s_family ) { family = s_family; }
State() : family( NULL ) {};
virtual ~State() {}; virtual ~State() {};
State( const State & );
bool operator=( const State & );
}; };
class Ground : public State { class Ground : public State {
+54
View File
@@ -0,0 +1,54 @@
#ifndef PARSERSTATEFAMILY_HPP
#define PARSERSTATEFAMILY_HPP
#include "parserstate.hpp"
namespace Parser {
class StateFamily
{
public:
Ground s_Ground;
Escape s_Escape;
Escape_Intermediate s_Escape_Intermediate;
CSI_Entry s_CSI_Entry;
CSI_Param s_CSI_Param;
CSI_Intermediate s_CSI_Intermediate;
CSI_Ignore s_CSI_Ignore;
DCS_Entry s_DCS_Entry;
DCS_Param s_DCS_Param;
DCS_Intermediate s_DCS_Intermediate;
DCS_Passthrough s_DCS_Passthrough;
DCS_Ignore s_DCS_Ignore;
OSC_String s_OSC_String;
SOS_PM_APC_String s_SOS_PM_APC_String;
StateFamily()
: s_Ground(), s_Escape(), s_Escape_Intermediate(),
s_CSI_Entry(), s_CSI_Param(), s_CSI_Intermediate(), s_CSI_Ignore(),
s_DCS_Entry(), s_DCS_Param(), s_DCS_Intermediate(),
s_DCS_Passthrough(), s_DCS_Ignore(),
s_OSC_String(), s_SOS_PM_APC_String()
{
s_Ground.setfamily( this );
s_Escape.setfamily( this );
s_Escape_Intermediate.setfamily( this );
s_CSI_Entry.setfamily( this );
s_CSI_Param.setfamily( this );
s_CSI_Intermediate.setfamily( this );
s_CSI_Ignore.setfamily( this );
s_DCS_Entry.setfamily( this );
s_DCS_Param.setfamily( this );
s_DCS_Intermediate.setfamily( this );
s_DCS_Passthrough.setfamily( this );
s_DCS_Ignore.setfamily( this );
s_OSC_String.setfamily( this );
s_SOS_PM_APC_String.setfamily( this );
}
};
}
#endif