From fbf05670850752e955aeb33fa87288df41421379 Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Thu, 13 Jan 2011 01:06:16 -0500 Subject: [PATCH] Screen out Ignore actions --- parser.cpp | 29 ++++++++++++++++++----------- parserstate.hpp | 4 ++-- parsertransition.hpp | 4 ++-- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/parser.cpp b/parser.cpp index bb44442..932b3df 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1,5 +1,20 @@ +#include +#include + #include "parser.hpp" +static void append_or_delete( Parser::Action *act, + std::vector&vec ) +{ + assert( act ); + + if ( typeid( *act ) != typeid( Parser::Ignore ) ) { + vec.push_back( act ); + } else { + delete act; + } +} + std::vector Parser::Parser::input( wchar_t ch ) { std::vector ret; @@ -7,21 +22,13 @@ std::vector Parser::Parser::input( wchar_t ch ) Transition tx = state->input( ch ); if ( tx.next_state != NULL ) { - Action *exitact = state->exit(); - if ( exitact ) { - ret.push_back( exitact ); - } + append_or_delete( state->exit(), ret ); } - if ( tx.action ) { - ret.push_back( tx.action ); - } + append_or_delete( tx.action, ret ); if ( tx.next_state != NULL ) { - Action *enteract = tx.next_state->enter(); - if ( enteract ) { - ret.push_back( enteract ); - } + append_or_delete( tx.next_state->enter(), ret ); state = tx.next_state; } diff --git a/parserstate.hpp b/parserstate.hpp index 391cae7..2bfc236 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 NULL; } - virtual Action *exit( void ) { return NULL; } + virtual Action *enter( void ) { return new Ignore; } + virtual Action *exit( void ) { return new Ignore; } State() : family( NULL ) {}; virtual ~State() {}; diff --git a/parsertransition.hpp b/parsertransition.hpp index a1b03df..4d22998 100644 --- a/parsertransition.hpp +++ b/parsertransition.hpp @@ -26,12 +26,12 @@ namespace Parser { } virtual ~Transition() {} - Transition( Action *s_action=new Ignore(), State *s_next_state=NULL ) + Transition( Action *s_action=new Ignore, State *s_next_state=NULL ) : action( s_action ), next_state( s_next_state ) {} Transition( State *s_next_state ) - : action( new Ignore() ), next_state( s_next_state ) + : action( new Ignore ), next_state( s_next_state ) {} }; }