From: JINMEI Tatuya Date: Fri, 2 Nov 2012 23:18:43 +0000 (-0700) Subject: [2372] internal refactoring: pass original options at the time of start X-Git-Tag: trac2487_base~1^2~21^2~22 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a5767f8d4eaa3e94b157fc00211a0d0fc3a4a989;p=thirdparty%2Fkea.git [2372] internal refactoring: pass original options at the time of start this will make the overall interface simpler. --- diff --git a/src/lib/dns/master_lexer.cc b/src/lib/dns/master_lexer.cc index b4bec94cde..5e37eb2640 100644 --- a/src/lib/dns/master_lexer.cc +++ b/src/lib/dns/master_lexer.cc @@ -41,6 +41,7 @@ struct MasterLexer::MasterLexerImpl { std::vector sources_; InputSource* source_; // current source size_t paren_count_; + Options orig_options_; bool last_was_eol_; Token token_; }; @@ -148,16 +149,14 @@ class Start : public State { public: Start() {} virtual const State* handle(MasterLexer& lexer, - MasterLexer::Options& options, - MasterLexer::Options orig_options) const; + MasterLexer::Options& options) const; }; class CRLF : public State { public: CRLF() {} virtual const State* handle(MasterLexer& /*lexer*/, - MasterLexer::Options& /*options*/, - MasterLexer::Options /*orig_options*/) const + MasterLexer::Options& /*options*/) const { return (NULL); } @@ -167,8 +166,7 @@ class String : public State { public: String() {} virtual const State* handle(MasterLexer& /*lexer*/, - MasterLexer::Options& /*options*/, - MasterLexer::Options /*orig_options*/) const + MasterLexer::Options& /*options*/) const { return (NULL); } @@ -203,9 +201,7 @@ cancelOptions(MasterLexer::Options& options, } const State* -Start::handle(MasterLexer& lexer, MasterLexer::Options& options, - MasterLexer::Options orig_options) const -{ +Start::handle(MasterLexer& lexer, MasterLexer::Options& options) const { while (true) { const int c = getLexerImpl(lexer)->source_->getChar(); if (c == InputSource::END_OF_STREAM) { @@ -237,7 +233,7 @@ Start::handle(MasterLexer& lexer, MasterLexer::Options& options, // TBD: unbalanced case --getLexerImpl(lexer)->paren_count_; if (getLexerImpl(lexer)->paren_count_ == 0) { - options = orig_options; + options = getLexerImpl(lexer)->orig_options_; } continue; } else { @@ -247,6 +243,13 @@ Start::handle(MasterLexer& lexer, MasterLexer::Options& options, } } +const State* +State::getStartInstance(MasterLexer& lexer, MasterLexer::Options orig_options) +{ + lexer.impl_->orig_options_ = orig_options; + return (&START_STATE); +} + } // namespace master_lexer_internal } // end of namespace dns diff --git a/src/lib/dns/master_lexer_state.h b/src/lib/dns/master_lexer_state.h index f4be18c10b..4fdc91f303 100644 --- a/src/lib/dns/master_lexer_state.h +++ b/src/lib/dns/master_lexer_state.h @@ -25,17 +25,21 @@ class InputSource; class State { public: + virtual const State* handle(MasterLexer& lexer, + MasterLexer::Options& options) const = 0; + + static const State* getStartInstance(MasterLexer& lexer, + MasterLexer::Options orig_options); + + /// Specific states are basically hidden within the implementation, + /// but we'd like to allow tests to examine them, so we provide + /// a way to get an instance of a specific state. enum ID { Start, ///< TBD CRLF, EatLine, String }; - virtual const State* handle(MasterLexer& lexer, - MasterLexer::Options& options, - MasterLexer::Options orig_options = - MasterLexer::NONE) const = 0; - static const State& getInstance(ID state_id); /// \name Read-only accessors for testing purposes. diff --git a/src/lib/dns/tests/master_lexer_state_unittest.cc b/src/lib/dns/tests/master_lexer_state_unittest.cc index 98f5b30d94..576a9f786a 100644 --- a/src/lib/dns/tests/master_lexer_state_unittest.cc +++ b/src/lib/dns/tests/master_lexer_state_unittest.cc @@ -28,8 +28,11 @@ typedef MasterLexer::Token Token; // shortcut class MasterLexerStateTest : public ::testing::Test { protected: - MasterLexerStateTest() : s_null(NULL), - s_start(State::getInstance(State::Start)), + MasterLexerStateTest() : common_options(MasterLexer::END_OF_LINE | + MasterLexer::INITIAL_WS), + s_null(NULL), + s_start(*State::getStartInstance( + lexer, common_options)), s_crlf(State::getInstance(State::CRLF)), s_string(State::getInstance(State::String)), options(MasterLexer::END_OF_LINE), @@ -37,11 +40,14 @@ protected: { lexer.pushSource(ss); } + + // Specify END_OF_LINE and INITIAL_WS as common initial options. + const MasterLexer::Options common_options; + MasterLexer lexer; const State* const s_null; const State& s_start; const State& s_crlf; const State& s_string; - MasterLexer lexer; std::stringstream ss; MasterLexer::Options options, orig_options; }; @@ -59,20 +65,18 @@ eofCheck(const State& state, MasterLexer& lexer) { TEST_F(MasterLexerStateTest, startAndEnd) { // A simple case: the input is empty, so we begin with start and // are immediately done. - const State* s_next = s_start.handle(lexer, options); - EXPECT_EQ(s_null, s_next); + EXPECT_EQ(s_null, s_start.handle(lexer, options)); eofCheck(s_start, lexer); } TEST_F(MasterLexerStateTest, startToEOL) { ss << "\n"; - const State* s_next = s_start.handle(lexer, options); - EXPECT_EQ(s_null, s_next); + EXPECT_EQ(s_null, s_start.handle(lexer, options)); EXPECT_TRUE(s_start.wasLastEOL(lexer)); EXPECT_EQ(Token::END_OF_LINE, s_start.getToken(lexer).getType()); // The next lexer session will reach EOF. Same eof check should pass. - s_start.handle(lexer, options); + EXPECT_EQ(s_null, s_start.handle(lexer, options)); eofCheck(s_start, lexer); // TBD: EOL after ( @@ -83,11 +87,9 @@ TEST_F(MasterLexerStateTest, space) { // twice; at the second iteration, it's a white space at the beginning // of line, but since we don't specify INITIAL_WS option, it's treated as // normal space and ignored. - const State* s_next; for (size_t i = 0; i < 2; ++i) { ss << " \t\n"; - s_next = s_start.handle(lexer, options); - EXPECT_EQ(s_null, s_next); + EXPECT_EQ(s_null, s_start.handle(lexer, options)); EXPECT_TRUE(s_start.wasLastEOL(lexer)); EXPECT_EQ(Token::END_OF_LINE, s_start.getToken(lexer).getType()); } @@ -96,8 +98,7 @@ TEST_F(MasterLexerStateTest, space) { // corresponding token will be returned. ss << " "; options = MasterLexer::INITIAL_WS; - s_next = s_start.handle(lexer, options); - EXPECT_EQ(s_null, s_next); + EXPECT_EQ(s_null, s_start.handle(lexer, options)); EXPECT_FALSE(s_start.wasLastEOL(lexer)); EXPECT_EQ(Token::INITIAL_WS, s_start.getToken(lexer).getType()); } @@ -105,14 +106,13 @@ TEST_F(MasterLexerStateTest, space) { TEST_F(MasterLexerStateTest, parentheses) { ss << "\n(\na)"; // 1st \n is to check if 'was EOL' is set to false - const State* s_next = s_start.handle(lexer, options); // handle \n - EXPECT_EQ(s_null, s_next); + EXPECT_EQ(s_null, s_start.handle(lexer, options)); // handle \n // Now handle '('. It skips \n and recognize 'a' as strin EXPECT_EQ(0, s_start.getParenCount(lexer)); // check pre condition options = MasterLexer::END_OF_LINE | MasterLexer::INITIAL_WS; - s_next = s_start.handle(lexer, options, options); - EXPECT_EQ(&s_string, s_next); // should recognize 'a' as string + // should recognize 'a' as string + EXPECT_EQ(&s_string, s_start.handle(lexer, options)); // Check post '(' conditions. paren_count should be incremented, and // end-of-line and ws should be canceled at the first open paren @@ -123,8 +123,7 @@ TEST_F(MasterLexerStateTest, parentheses) { // Then handle ')'. eol and init_ws are currently cleared, which will be // set again. - s_next = s_start.handle(lexer, options, (MasterLexer::END_OF_LINE | - MasterLexer::INITIAL_WS)); + EXPECT_EQ(s_null, s_start.handle(lexer, options)); EXPECT_EQ(0, s_start.getParenCount(lexer)); EXPECT_TRUE((options & MasterLexer::END_OF_LINE) != 0); EXPECT_TRUE((options & MasterLexer::INITIAL_WS) != 0); @@ -142,18 +141,14 @@ TEST_F(MasterLexerStateTest, nestedParentheses) { // Close the inner most parentheses. count will be decreased, but option // shouldn't be restored yet. - EXPECT_EQ(&s_string, - s_start.handle(lexer, options, (MasterLexer::END_OF_LINE | - MasterLexer::INITIAL_WS))); + EXPECT_EQ(&s_string, s_start.handle(lexer, options)); EXPECT_EQ(1, s_start.getParenCount(lexer)); EXPECT_TRUE((options & MasterLexer::END_OF_LINE) == 0); EXPECT_TRUE((options & MasterLexer::INITIAL_WS) == 0); // Close the outermost parentheses. count will be reset to 0, and original // options are restored. - EXPECT_EQ(s_null, - s_start.handle(lexer, options, (MasterLexer::END_OF_LINE | - MasterLexer::INITIAL_WS))); + EXPECT_EQ(s_null, s_start.handle(lexer, options)); EXPECT_TRUE((options & MasterLexer::END_OF_LINE) != 0); EXPECT_TRUE((options & MasterLexer::INITIAL_WS) != 0); }