Browse Source

[2372] simplification: we actually don't need END_OF_LINE option.

it can be completely local in Start::handle().
JINMEI Tatuya 12 years ago
parent
commit
6e50ccbf96

+ 6 - 4
src/lib/dns/master_lexer.cc

@@ -220,8 +220,7 @@ namespace {
 inline void
 inline void
 adjustOptionsForParen(MasterLexer::Options& options) {
 adjustOptionsForParen(MasterLexer::Options& options) {
     options = static_cast<MasterLexer::Options>(
     options = static_cast<MasterLexer::Options>(
-        options & static_cast<MasterLexer::Options>(
-            ~(MasterLexer::END_OF_LINE | MasterLexer::INITIAL_WS)));
+        options & static_cast<MasterLexer::Options>(~MasterLexer::INITIAL_WS));
 }
 }
 }
 }
 
 
@@ -229,6 +228,7 @@ const State*
 Start::handle(MasterLexer& lexer) const {
 Start::handle(MasterLexer& lexer) const {
     MasterLexer::Options options = getLexerImpl(lexer)->options_;
     MasterLexer::Options options = getLexerImpl(lexer)->options_;
     size_t& paren_count = getLexerImpl(lexer)->paren_count_;
     size_t& paren_count = getLexerImpl(lexer)->paren_count_;
+    bool eol_ok = (paren_count == 0);
     if (paren_count > 0) {
     if (paren_count > 0) {
         adjustOptionsForParen(options);
         adjustOptionsForParen(options);
     }
     }
@@ -255,17 +255,18 @@ Start::handle(MasterLexer& lexer) const {
             continue;
             continue;
         } else if (c == '\n') {
         } else if (c == '\n') {
             getLexerImpl(lexer)->last_was_eol_ = true;
             getLexerImpl(lexer)->last_was_eol_ = true;
-            if ((options & MasterLexer::END_OF_LINE) != 0) {
+            if (eol_ok) {
                 getLexerImpl(lexer)->token_ = Token(Token::END_OF_LINE);
                 getLexerImpl(lexer)->token_ = Token(Token::END_OF_LINE);
                 return (NULL);
                 return (NULL);
             }
             }
         } else if (c == '\r') {
         } else if (c == '\r') {
-            if ((options & MasterLexer::END_OF_LINE) != 0) {
+            if (eol_ok) {
                 return (&CRLF_STATE);
                 return (&CRLF_STATE);
             }
             }
         } else if (c == '(') {
         } else if (c == '(') {
             getLexerImpl(lexer)->last_was_eol_ = false;
             getLexerImpl(lexer)->last_was_eol_ = false;
             adjustOptionsForParen(options);
             adjustOptionsForParen(options);
+            eol_ok = false;
             ++paren_count;
             ++paren_count;
             continue;
             continue;
         } else if (c == ')') {
         } else if (c == ')') {
@@ -275,6 +276,7 @@ Start::handle(MasterLexer& lexer) const {
                 return (NULL);
                 return (NULL);
             }
             }
             if (--paren_count == 0) {
             if (--paren_count == 0) {
+                eol_ok = true;
                 options = getLexerImpl(lexer)->options_;
                 options = getLexerImpl(lexer)->options_;
             }
             }
             continue;
             continue;

+ 1 - 3
src/lib/dns/master_lexer.h

@@ -54,9 +54,7 @@ public:
         NONE = 0, //< No option
         NONE = 0, //< No option
         INITIAL_WS = 1, ///< recognize begin-of-line spaces
         INITIAL_WS = 1, ///< recognize begin-of-line spaces
         QSTRING = INITIAL_WS << 1, ///< recognize quoted string
         QSTRING = INITIAL_WS << 1, ///< recognize quoted string
-        NUMBER = QSTRING << 1, ///< recognize numeric text as integer
-        END_OF_LINE = NUMBER << 1 ///< recognize end of line as a token
-                                 /// (not user-settable)
+        NUMBER = QSTRING << 1 ///< recognize numeric text as integer
     };
     };
 
 
     /// \brief The constructor.
     /// \brief The constructor.

+ 4 - 23
src/lib/dns/tests/master_lexer_state_unittest.cc

@@ -28,20 +28,19 @@ typedef MasterLexer::Token Token; // shortcut
 
 
 class MasterLexerStateTest : public ::testing::Test {
 class MasterLexerStateTest : public ::testing::Test {
 protected:
 protected:
-    MasterLexerStateTest() : common_options(MasterLexer::END_OF_LINE |
-                                            MasterLexer::INITIAL_WS),
+    MasterLexerStateTest() : common_options(MasterLexer::INITIAL_WS),
                              s_null(NULL),
                              s_null(NULL),
                              s_start(*State::getStartInstance(
                              s_start(*State::getStartInstance(
                                          lexer, common_options)),
                                          lexer, common_options)),
                              s_crlf(State::getInstance(State::CRLF)),
                              s_crlf(State::getInstance(State::CRLF)),
                              s_string(State::getInstance(State::String)),
                              s_string(State::getInstance(State::String)),
-                             options(MasterLexer::END_OF_LINE),
+                             options(MasterLexer::NONE),
                              orig_options(options)
                              orig_options(options)
     {
     {
         lexer.pushSource(ss);
         lexer.pushSource(ss);
     }
     }
 
 
-    // Specify END_OF_LINE and INITIAL_WS as common initial options.
+    // Specify INITIAL_WS as common initial options.
     const MasterLexer::Options common_options;
     const MasterLexer::Options common_options;
     MasterLexer lexer;
     MasterLexer lexer;
     const State* const s_null;
     const State* const s_null;
@@ -81,23 +80,12 @@ TEST_F(MasterLexerStateTest, startToEOL) {
     eofCheck(s_start, lexer);
     eofCheck(s_start, lexer);
 }
 }
 
 
-TEST_F(MasterLexerStateTest, continuedInitialWS) {
-    // Unusual, probably impossible case in our expected usage.  We'll see
-    // an initial space after newline, but we didn't request recognizing
-    // the new line (so it'll be just skipped).
-    ss << "\n ";
-    options = MasterLexer::INITIAL_WS;
-    const State* state = State::getStartInstance(lexer, options);
-    EXPECT_EQ(s_null, state->handle(lexer));
-    EXPECT_EQ(Token::INITIAL_WS, s_start.getToken(lexer).getType());
-}
-
 TEST_F(MasterLexerStateTest, space) {
 TEST_F(MasterLexerStateTest, space) {
     // by default space characters and tabs will be ignored.  We check this
     // by default space characters and tabs will be ignored.  We check this
     // twice; at the second iteration, it's a white space at the beginning
     // 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
     // of line, but since we don't specify INITIAL_WS option, it's treated as
     // normal space and ignored.
     // normal space and ignored.
-    State::getStartInstance(lexer, MasterLexer::END_OF_LINE);
+    State::getStartInstance(lexer, MasterLexer::NONE);
     for (size_t i = 0; i < 2; ++i) {
     for (size_t i = 0; i < 2; ++i) {
         ss << " \t\n";
         ss << " \t\n";
         EXPECT_EQ(s_null, s_start.handle(lexer));
         EXPECT_EQ(s_null, s_start.handle(lexer));
@@ -122,7 +110,6 @@ TEST_F(MasterLexerStateTest, parentheses) {
 
 
     // Now handle '('.  It skips \n and recognize 'a' as string
     // Now handle '('.  It skips \n and recognize 'a' as string
     EXPECT_EQ(0, s_start.getParenCount(lexer)); // check pre condition
     EXPECT_EQ(0, s_start.getParenCount(lexer)); // check pre condition
-    options = MasterLexer::END_OF_LINE | MasterLexer::INITIAL_WS;
     EXPECT_EQ(&s_string, s_start.handle(lexer));
     EXPECT_EQ(&s_string, s_start.handle(lexer));
     EXPECT_EQ(1, s_start.getParenCount(lexer)); // check post condition
     EXPECT_EQ(1, s_start.getParenCount(lexer)); // check post condition
     EXPECT_FALSE(s_start.wasLastEOL(lexer));
     EXPECT_FALSE(s_start.wasLastEOL(lexer));
@@ -232,12 +219,6 @@ TEST_F(MasterLexerStateTest, crlf) {
     EXPECT_EQ(Token::END_OF_LINE, s_start.getToken(lexer).getType());
     EXPECT_EQ(Token::END_OF_LINE, s_start.getToken(lexer).getType());
     EXPECT_TRUE(s_start.wasLastEOL(lexer));
     EXPECT_TRUE(s_start.wasLastEOL(lexer));
 
 
-    // If EOL isn't expected to be recognized, \r is just ignored.
-    ss << "\r\na";
-    State::getStartInstance(lexer, MasterLexer::NONE);
-    EXPECT_EQ(&s_string, s_start.handle(lexer));
-    s_string.handle(lexer);     // skip
-
     // Single '\r' (not followed by \n) is recognized as a single 'end-of-line'
     // Single '\r' (not followed by \n) is recognized as a single 'end-of-line'
     ss << "\r ";                // then there will be "initial WS"
     ss << "\r ";                // then there will be "initial WS"
     State::getStartInstance(lexer, common_options); // specify usual options
     State::getStartInstance(lexer, common_options); // specify usual options