master_lexer_state_unittest.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dns/master_lexer.h>
  15. #include <dns/master_lexer_inputsource.h>
  16. #include <dns/master_lexer_state.h>
  17. #include <gtest/gtest.h>
  18. #include <sstream>
  19. using namespace isc::dns;
  20. using namespace master_lexer_internal;
  21. namespace {
  22. typedef MasterToken Token; // shortcut
  23. class MasterLexerStateTest : public ::testing::Test {
  24. protected:
  25. MasterLexerStateTest() : common_options(MasterLexer::INITIAL_WS),
  26. s_null(NULL),
  27. s_crlf(State::getInstance(State::CRLF)),
  28. s_string(State::getInstance(State::String)),
  29. s_qstring(State::getInstance(State::QString)),
  30. s_number(State::getInstance(State::Number)),
  31. options(MasterLexer::NONE),
  32. orig_options(options)
  33. {}
  34. // Specify INITIAL_WS as common initial options.
  35. const MasterLexer::Options common_options;
  36. MasterLexer lexer;
  37. const State* const s_null;
  38. const State& s_crlf;
  39. const State& s_string;
  40. const State& s_qstring;
  41. const State& s_number;
  42. std::stringstream ss;
  43. MasterLexer::Options options, orig_options;
  44. };
  45. // Common check for the end-of-file condition.
  46. // Token is set to END_OF_FILE, and the lexer was NOT last eol state.
  47. // Passed state can be any valid one; they are stateless, just providing the
  48. // interface for inspection.
  49. void
  50. eofCheck(const State& state, MasterLexer& lexer) {
  51. EXPECT_EQ(Token::END_OF_FILE, state.getToken(lexer).getType());
  52. EXPECT_FALSE(state.wasLastEOL(lexer));
  53. }
  54. TEST_F(MasterLexerStateTest, startAndEnd) {
  55. // A simple case: the input is empty, so we begin with start and
  56. // are immediately done.
  57. lexer.pushSource(ss);
  58. EXPECT_EQ(s_null, State::start(lexer, common_options));
  59. eofCheck(s_crlf, lexer);
  60. }
  61. TEST_F(MasterLexerStateTest, startToEOL) {
  62. ss << "\n";
  63. lexer.pushSource(ss);
  64. EXPECT_EQ(s_null, State::start(lexer, common_options));
  65. EXPECT_TRUE(s_crlf.wasLastEOL(lexer));
  66. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  67. // The next lexer session will reach EOF. Same eof check should pass.
  68. EXPECT_EQ(s_null, State::start(lexer, common_options));
  69. eofCheck(s_crlf, lexer);
  70. }
  71. TEST_F(MasterLexerStateTest, space) {
  72. // repeat '\t\n' twice (see below), then space after EOL
  73. ss << " \t\n\t\n ";
  74. lexer.pushSource(ss);
  75. // by default space characters and tabs will be ignored. We check this
  76. // twice; at the second iteration, it's a white space at the beginning
  77. // of line, but since we don't specify INITIAL_WS option, it's treated as
  78. // normal space and ignored.
  79. for (size_t i = 0; i < 2; ++i) {
  80. EXPECT_EQ(s_null, State::start(lexer, MasterLexer::NONE));
  81. EXPECT_TRUE(s_crlf.wasLastEOL(lexer));
  82. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  83. }
  84. // Now we specify the INITIAL_WS option. It will be recognized and the
  85. // corresponding token will be returned.
  86. EXPECT_EQ(s_null, State::start(lexer, MasterLexer::INITIAL_WS));
  87. EXPECT_FALSE(s_crlf.wasLastEOL(lexer));
  88. EXPECT_EQ(Token::INITIAL_WS, s_crlf.getToken(lexer).getType());
  89. }
  90. TEST_F(MasterLexerStateTest, parentheses) {
  91. ss << "\n(\na\n )\n "; // 1st \n is to check if 'was EOL' is set to false
  92. lexer.pushSource(ss);
  93. EXPECT_EQ(s_null, State::start(lexer, common_options)); // handle \n
  94. // Now handle '('. It skips \n and recognize 'a' as string
  95. EXPECT_EQ(0, s_crlf.getParenCount(lexer)); // check pre condition
  96. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  97. EXPECT_EQ(1, s_crlf.getParenCount(lexer)); // check post condition
  98. EXPECT_FALSE(s_crlf.wasLastEOL(lexer));
  99. // skip 'a'
  100. s_string.handle(lexer);
  101. // Then handle ')'. '\n' before ')' isn't recognized because
  102. // it's canceled due to the '('. Likewise, the space after the '\n'
  103. // shouldn't be recognized but should be just ignored.
  104. EXPECT_EQ(s_null, State::start(lexer, common_options));
  105. EXPECT_EQ(0, s_crlf.getParenCount(lexer));
  106. // Now, temporarily disabled options are restored: Both EOL and the
  107. // initial WS are recognized
  108. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  109. EXPECT_EQ(s_null, State::start(lexer, common_options));
  110. EXPECT_EQ(Token::INITIAL_WS, s_crlf.getToken(lexer).getType());
  111. }
  112. TEST_F(MasterLexerStateTest, nestedParentheses) {
  113. // This is an unusual, but allowed (in this implementation) case.
  114. ss << "(a(b)\n c)\n ";
  115. lexer.pushSource(ss);
  116. EXPECT_EQ(&s_string, State::start(lexer, common_options)); // consume '('
  117. s_string.handle(lexer); // consume 'a'
  118. EXPECT_EQ(&s_string, State::start(lexer, common_options)); // consume '('
  119. s_string.handle(lexer); // consume 'b'
  120. EXPECT_EQ(2, s_crlf.getParenCount(lexer)); // now the count is 2
  121. // Close the inner most parentheses. count will be decreased, but option
  122. // shouldn't be restored yet, so the intermediate EOL or initial WS won't
  123. // be recognized.
  124. EXPECT_EQ(&s_string, State::start(lexer, common_options)); // consume ')'
  125. s_string.handle(lexer); // consume 'c'
  126. EXPECT_EQ(1, s_crlf.getParenCount(lexer));
  127. // Close the outermost parentheses. count will be reset to 0, and original
  128. // options are restored.
  129. EXPECT_EQ(s_null, State::start(lexer, common_options));
  130. // Now, temporarily disabled options are restored: Both EOL and the
  131. // initial WS are recognized
  132. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  133. EXPECT_EQ(s_null, State::start(lexer, common_options));
  134. EXPECT_EQ(Token::INITIAL_WS, s_crlf.getToken(lexer).getType());
  135. }
  136. TEST_F(MasterLexerStateTest, unbalancedParentheses) {
  137. // Only closing paren is provided. We prepend a \n to check if it's
  138. // correctly canceled after detecting the error.
  139. ss << "\n)";
  140. ss << "(a";
  141. lexer.pushSource(ss);
  142. EXPECT_EQ(s_null, State::start(lexer, common_options)); // consume '\n'
  143. EXPECT_TRUE(s_crlf.wasLastEOL(lexer)); // this \n was remembered
  144. // Now checking ')'. The result should be error, count shouldn't be
  145. // changed. "last EOL" should be canceled.
  146. EXPECT_EQ(0, s_crlf.getParenCount(lexer));
  147. EXPECT_EQ(s_null, State::start(lexer, common_options));
  148. EXPECT_EQ(0, s_crlf.getParenCount(lexer));
  149. ASSERT_EQ(Token::ERROR, s_crlf.getToken(lexer).getType());
  150. EXPECT_EQ(Token::UNBALANCED_PAREN, s_crlf.getToken(lexer).getErrorCode());
  151. EXPECT_FALSE(s_crlf.wasLastEOL(lexer));
  152. // Reach EOF with a dangling open parenthesis.
  153. EXPECT_EQ(&s_string, State::start(lexer, common_options)); // consume '('
  154. s_string.handle(lexer); // consume 'a'
  155. EXPECT_EQ(1, s_crlf.getParenCount(lexer));
  156. EXPECT_EQ(s_null, State::start(lexer, common_options)); // reach EOF
  157. ASSERT_EQ(Token::ERROR, s_crlf.getToken(lexer).getType());
  158. EXPECT_EQ(Token::UNBALANCED_PAREN, s_crlf.getToken(lexer).getErrorCode());
  159. EXPECT_EQ(0, s_crlf.getParenCount(lexer)); // should be reset to 0
  160. }
  161. TEST_F(MasterLexerStateTest, startToComment) {
  162. // Begin with 'start', skip space, then encounter a comment. Skip
  163. // the rest of the line, and recognize the new line. Note that the
  164. // second ';' is simply ignored.
  165. ss << " ;a;\n";
  166. ss << ";a;"; // Likewise, but the comment ends with EOF.
  167. lexer.pushSource(ss);
  168. // Comment ending with EOL
  169. EXPECT_EQ(s_null, State::start(lexer, common_options));
  170. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  171. // Comment ending with EOF
  172. EXPECT_EQ(s_null, State::start(lexer, common_options));
  173. EXPECT_EQ(Token::END_OF_FILE, s_crlf.getToken(lexer).getType());
  174. }
  175. TEST_F(MasterLexerStateTest, commentAfterParen) {
  176. // comment after an opening parenthesis. The code that is tested by
  177. // other tests should also ensure that it works correctly, but we
  178. // check it explicitly.
  179. ss << "( ;this is a comment\na)\n";
  180. lexer.pushSource(ss);
  181. // consume '(', skip comments, consume 'a', then consume ')'
  182. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  183. s_string.handle(lexer);
  184. EXPECT_EQ(s_null, State::start(lexer, common_options));
  185. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  186. }
  187. TEST_F(MasterLexerStateTest, crlf) {
  188. ss << "\r\n"; // case 1
  189. ss << "\r "; // case 2
  190. ss << "\r;comment\na"; // case 3
  191. ss << "\r"; // case 4
  192. lexer.pushSource(ss);
  193. // 1. A sequence of \r, \n is recognized as a single 'end-of-line'
  194. EXPECT_EQ(&s_crlf, State::start(lexer, common_options)); // recognize '\r'
  195. s_crlf.handle(lexer); // recognize '\n'
  196. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  197. EXPECT_TRUE(s_crlf.wasLastEOL(lexer));
  198. // 2. Single '\r' (not followed by \n) is recognized as a single
  199. // 'end-of-line'. then there will be "initial WS"
  200. EXPECT_EQ(&s_crlf, State::start(lexer, common_options)); // recognize '\r'
  201. // see ' ', "unget" it
  202. s_crlf.handle(lexer);
  203. EXPECT_EQ(s_null, State::start(lexer, common_options)); // recognize ' '
  204. EXPECT_EQ(Token::INITIAL_WS, s_crlf.getToken(lexer).getType());
  205. // 3. comment between \r and \n
  206. EXPECT_EQ(&s_crlf, State::start(lexer, common_options)); // recognize '\r'
  207. // skip comments, recognize '\n'
  208. s_crlf.handle(lexer);
  209. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  210. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  211. s_string.handle(lexer); // skip 'a'
  212. // 4. \r then EOF
  213. EXPECT_EQ(&s_crlf, State::start(lexer, common_options)); // recognize '\r'
  214. // see EOF, then "unget" it
  215. s_crlf.handle(lexer);
  216. EXPECT_EQ(s_null, State::start(lexer, common_options)); // recognize EOF
  217. EXPECT_EQ(Token::END_OF_FILE, s_crlf.getToken(lexer).getType());
  218. }
  219. // Commonly used check for string related test cases, checking if the given
  220. // token has expected values.
  221. void
  222. stringTokenCheck(const std::string& expected, const MasterToken& token,
  223. bool quoted = false)
  224. {
  225. EXPECT_EQ(quoted ? Token::QSTRING : Token::STRING, token.getType());
  226. EXPECT_EQ(expected, token.getString());
  227. const std::string actual(token.getStringRegion().beg,
  228. token.getStringRegion().beg +
  229. token.getStringRegion().len);
  230. EXPECT_EQ(expected, actual);
  231. // There should be "hidden" nul-terminator after the string data.
  232. ASSERT_NE(static_cast<const char*>(NULL), token.getStringRegion().beg);
  233. EXPECT_EQ(0, *(token.getStringRegion().beg + token.getStringRegion().len));
  234. }
  235. TEST_F(MasterLexerStateTest, string) {
  236. // Check with simple strings followed by separate characters
  237. ss << "followed-by-EOL\n";
  238. ss << "followed-by-CR\r";
  239. ss << "followed-by-space ";
  240. ss << "followed-by-tab\t";
  241. ss << "followed-by-comment;this is comment and ignored\n";
  242. ss << "followed-by-paren(closing)";
  243. ss << "followed-by-EOF";
  244. lexer.pushSource(ss);
  245. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  246. s_string.handle(lexer); // recognize str, see \n
  247. EXPECT_FALSE(s_string.wasLastEOL(lexer));
  248. stringTokenCheck("followed-by-EOL", s_string.getToken(lexer));
  249. EXPECT_EQ(s_null, State::start(lexer, common_options)); // skip \n
  250. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  251. s_string.handle(lexer); // recognize str, see \r
  252. stringTokenCheck("followed-by-CR", s_string.getToken(lexer));
  253. EXPECT_EQ(&s_crlf, State::start(lexer, common_options)); // handle \r...
  254. s_crlf.handle(lexer); // ...and skip it
  255. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  256. s_string.handle(lexer); // recognize str, see ' '
  257. stringTokenCheck("followed-by-space", s_string.getToken(lexer));
  258. // skip ' ', then recognize the next string
  259. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  260. s_string.handle(lexer); // recognize str, see \t
  261. stringTokenCheck("followed-by-tab", s_string.getToken(lexer));
  262. // skip \t, then recognize the next string
  263. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  264. s_string.handle(lexer); // recognize str, see comment
  265. stringTokenCheck("followed-by-comment", s_string.getToken(lexer));
  266. EXPECT_EQ(s_null, State::start(lexer, common_options)); // skip \n after it
  267. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  268. s_string.handle(lexer); // recognize str, see '('
  269. stringTokenCheck("followed-by-paren", s_string.getToken(lexer));
  270. EXPECT_EQ(&s_string, State::start(lexer, common_options)); // str in ()
  271. s_string.handle(lexer); // recognize the str, see ')'
  272. stringTokenCheck("closing", s_string.getToken(lexer));
  273. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  274. s_string.handle(lexer); // recognize str, see EOF
  275. stringTokenCheck("followed-by-EOF", s_string.getToken(lexer));
  276. }
  277. TEST_F(MasterLexerStateTest, stringEscape) {
  278. // some of the separate characters should be considered part of the
  279. // string if escaped.
  280. ss << "escaped\\ space ";
  281. ss << "escaped\\\ttab ";
  282. ss << "escaped\\(paren ";
  283. ss << "escaped\\)close ";
  284. ss << "escaped\\;comment ";
  285. ss << "escaped\\\\ backslash "; // second '\' shouldn't escape ' '
  286. lexer.pushSource(ss);
  287. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  288. s_string.handle(lexer); // recognize str, see ' ' at end
  289. stringTokenCheck("escaped\\ space", s_string.getToken(lexer));
  290. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  291. s_string.handle(lexer); // recognize str, see ' ' at end
  292. stringTokenCheck("escaped\\\ttab", s_string.getToken(lexer));
  293. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  294. s_string.handle(lexer); // recognize str, see ' ' at end
  295. stringTokenCheck("escaped\\(paren", s_string.getToken(lexer));
  296. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  297. s_string.handle(lexer); // recognize str, see ' ' at end
  298. stringTokenCheck("escaped\\)close", s_string.getToken(lexer));
  299. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  300. s_string.handle(lexer); // recognize str, see ' ' at end
  301. stringTokenCheck("escaped\\;comment", s_string.getToken(lexer));
  302. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  303. s_string.handle(lexer); // recognize str, see ' ' in mid
  304. stringTokenCheck("escaped\\\\", s_string.getToken(lexer));
  305. // Confirm the word that follows the escaped '\' is correctly recognized.
  306. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  307. s_string.handle(lexer); // recognize str, see ' ' at end
  308. stringTokenCheck("backslash", s_string.getToken(lexer));
  309. }
  310. TEST_F(MasterLexerStateTest, quotedString) {
  311. ss << "\"ignore-quotes\"\n";
  312. ss << "\"quoted string\" "; // space is part of the qstring
  313. ss << "\"\" "; // empty quoted string
  314. // also check other separator characters. note that \r doesn't cause
  315. // UNBALANCED_QUOTES. Not sure if it's intentional, but that's how the
  316. // BIND 9 version works, so we follow it (it should be too minor to matter
  317. // in practice anyway)
  318. ss << "\"quoted()\t\rstring\" ";
  319. ss << "\"escape\\ in quote\" ";
  320. ss << "\"escaped\\\"\" ";
  321. ss << "\"escaped backslash\\\\\" ";
  322. ss << "\"no;comment\"";
  323. lexer.pushSource(ss);
  324. // by default, '"' doesn't have any special meaning and part of string
  325. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  326. s_string.handle(lexer); // recognize str, see \n
  327. stringTokenCheck("\"ignore-quotes\"", s_string.getToken(lexer));
  328. EXPECT_EQ(s_null, State::start(lexer, common_options)); // skip \n after it
  329. EXPECT_TRUE(s_string.wasLastEOL(lexer));
  330. // If QSTRING is specified in option, '"' is regarded as a beginning of
  331. // a quoted string.
  332. const MasterLexer::Options options = common_options | MasterLexer::QSTRING;
  333. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  334. EXPECT_FALSE(s_string.wasLastEOL(lexer)); // EOL is canceled due to '"'
  335. s_qstring.handle(lexer);
  336. stringTokenCheck("quoted string", s_string.getToken(lexer), true);
  337. // Empty string is okay as qstring
  338. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  339. s_qstring.handle(lexer);
  340. stringTokenCheck("", s_string.getToken(lexer), true);
  341. // Also checks other separator characters within a qstring
  342. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  343. s_qstring.handle(lexer);
  344. stringTokenCheck("quoted()\t\rstring", s_string.getToken(lexer), true);
  345. // escape character mostly doesn't have any effect in the qstring
  346. // processing
  347. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  348. s_qstring.handle(lexer);
  349. stringTokenCheck("escape\\ in quote", s_string.getToken(lexer), true);
  350. // The only exception is the quotation mark itself. Note that the escape
  351. // only works on the quotation mark immediately after it.
  352. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  353. s_qstring.handle(lexer);
  354. stringTokenCheck("escaped\"", s_string.getToken(lexer), true);
  355. // quoted '\' then '"'. Unlike the previous case '"' shouldn't be
  356. // escaped.
  357. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  358. s_qstring.handle(lexer);
  359. stringTokenCheck("escaped backslash\\\\", s_string.getToken(lexer), true);
  360. // ';' has no meaning in a quoted string (not indicating a comment)
  361. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  362. s_qstring.handle(lexer);
  363. stringTokenCheck("no;comment", s_string.getToken(lexer), true);
  364. }
  365. TEST_F(MasterLexerStateTest, brokenQuotedString) {
  366. ss << "\"unbalanced-quote\n";
  367. ss << "\"quoted\\\n\" ";
  368. ss << "\"unclosed quote and EOF";
  369. lexer.pushSource(ss);
  370. // EOL is encountered without closing the quote
  371. const MasterLexer::Options options = common_options | MasterLexer::QSTRING;
  372. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  373. s_qstring.handle(lexer);
  374. ASSERT_EQ(Token::ERROR, s_qstring.getToken(lexer).getType());
  375. EXPECT_EQ(Token::UNBALANCED_QUOTES,
  376. s_qstring.getToken(lexer).getErrorCode());
  377. // We can resume after the error from the '\n'
  378. EXPECT_EQ(s_null, State::start(lexer, options));
  379. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  380. // \n is okay in a quoted string if escaped
  381. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  382. s_qstring.handle(lexer);
  383. stringTokenCheck("quoted\\\n", s_string.getToken(lexer), true);
  384. // EOF is encountered without closing the quote
  385. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  386. s_qstring.handle(lexer);
  387. ASSERT_EQ(Token::ERROR, s_qstring.getToken(lexer).getType());
  388. EXPECT_EQ(Token::UNEXPECTED_END, s_qstring.getToken(lexer).getErrorCode());
  389. // If we continue we'll simply see the EOF
  390. EXPECT_EQ(s_null, State::start(lexer, options));
  391. EXPECT_EQ(Token::END_OF_FILE, s_crlf.getToken(lexer).getType());
  392. }
  393. TEST_F(MasterLexerStateTest, basicNumbers) {
  394. ss << "0 ";
  395. ss << "1 ";
  396. ss << "12345 ";
  397. ss << "4294967295 "; // 2^32-1
  398. ss << "4294967296 "; // Out of range
  399. ss << "340282366920938463463374607431768211456 ";
  400. // Very much out of range (2^128)
  401. ss << "005 "; // Leading zeroes are ignored
  402. ss << "42;asdf\n"; // Number with comment
  403. ss << "37"; // Simple number again, here to make
  404. // sure none of the above messed up
  405. // the tokenizer
  406. lexer.pushSource(ss);
  407. // Ask the lexer to recognize numbers as well
  408. const MasterLexer::Options options = common_options | MasterLexer::NUMBER;
  409. EXPECT_EQ(&s_number, State::start(lexer, options));
  410. s_number.handle(lexer);
  411. EXPECT_EQ(0, s_number.getToken(lexer).getNumber());
  412. EXPECT_EQ(&s_number, State::start(lexer, options));
  413. s_number.handle(lexer);
  414. EXPECT_EQ(1, s_number.getToken(lexer).getNumber());
  415. EXPECT_EQ(&s_number, State::start(lexer, options));
  416. s_number.handle(lexer);
  417. EXPECT_EQ(12345, s_number.getToken(lexer).getNumber());
  418. EXPECT_EQ(&s_number, State::start(lexer, options));
  419. s_number.handle(lexer);
  420. EXPECT_EQ(4294967295u, s_number.getToken(lexer).getNumber());
  421. EXPECT_EQ(&s_number, State::start(lexer, options));
  422. s_number.handle(lexer);
  423. EXPECT_EQ(Token::NUMBER_OUT_OF_RANGE,
  424. s_number.getToken(lexer).getErrorCode());
  425. EXPECT_EQ(&s_number, State::start(lexer, options));
  426. s_number.handle(lexer);
  427. EXPECT_EQ(Token::NUMBER_OUT_OF_RANGE,
  428. s_number.getToken(lexer).getErrorCode());
  429. EXPECT_EQ(&s_number, State::start(lexer, options));
  430. s_number.handle(lexer);
  431. EXPECT_EQ(5, s_number.getToken(lexer).getNumber());
  432. EXPECT_EQ(&s_number, State::start(lexer, options));
  433. s_number.handle(lexer);
  434. EXPECT_EQ(42, s_number.getToken(lexer).getNumber());
  435. EXPECT_EQ(s_null, State::start(lexer, options));
  436. EXPECT_TRUE(s_crlf.wasLastEOL(lexer));
  437. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  438. EXPECT_EQ(&s_number, State::start(lexer, options));
  439. s_number.handle(lexer);
  440. EXPECT_EQ(37, s_number.getToken(lexer).getNumber());
  441. // If we continue we'll simply see the EOF
  442. EXPECT_EQ(s_null, State::start(lexer, options));
  443. EXPECT_EQ(Token::END_OF_FILE, s_crlf.getToken(lexer).getType());
  444. }
  445. // Test tokens that look like (or start out as) numbers,
  446. // but turn out to be strings. Tests include escaped characters.
  447. TEST_F(MasterLexerStateTest, stringNumbers) {
  448. ss << "123 "; // Should be read as a string if the
  449. // NUMBER option is not given
  450. ss << "-1 "; // Negative numbers are interpreted
  451. // as strings (unsigned integers only)
  452. ss << "123abc456 "; // 'Numbers' containing non-digits should
  453. // be interpreted as strings
  454. ss << "123\\456 "; // Numbers containing escaped digits are
  455. // interpreted as strings
  456. ss << "3scaped\\ space ";
  457. ss << "3scaped\\\ttab ";
  458. ss << "3scaped\\(paren ";
  459. ss << "3scaped\\)close ";
  460. ss << "3scaped\\;comment ";
  461. ss << "3scaped\\\\ 8ackslash "; // second '\' shouldn't escape ' '
  462. lexer.pushSource(ss);
  463. // Note that common_options does not include MasterLexer::NUMBER,
  464. // so the token should be recognized as a string
  465. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  466. s_string.handle(lexer);
  467. stringTokenCheck("123", s_string.getToken(lexer), false);
  468. // Ask the lexer to recognize numbers as well
  469. const MasterLexer::Options options = common_options | MasterLexer::NUMBER;
  470. EXPECT_EQ(&s_string, State::start(lexer, options));
  471. s_string.handle(lexer);
  472. stringTokenCheck("-1", s_string.getToken(lexer), false);
  473. // Starts out as a number, but ends up being a string
  474. EXPECT_EQ(&s_number, State::start(lexer, options));
  475. s_number.handle(lexer);
  476. stringTokenCheck("123abc456", s_number.getToken(lexer), false);
  477. EXPECT_EQ(&s_number, State::start(lexer, options));
  478. s_number.handle(lexer);
  479. stringTokenCheck("123\\456", s_number.getToken(lexer), false);
  480. EXPECT_EQ(&s_number, State::start(lexer, options));
  481. s_number.handle(lexer); // recognize str, see ' ' at end
  482. stringTokenCheck("3scaped\\ space", s_number.getToken(lexer));
  483. EXPECT_EQ(&s_number, State::start(lexer, options));
  484. s_number.handle(lexer); // recognize str, see ' ' at end
  485. stringTokenCheck("3scaped\\\ttab", s_number.getToken(lexer));
  486. EXPECT_EQ(&s_number, State::start(lexer, options));
  487. s_number.handle(lexer); // recognize str, see ' ' at end
  488. stringTokenCheck("3scaped\\(paren", s_number.getToken(lexer));
  489. EXPECT_EQ(&s_number, State::start(lexer, options));
  490. s_number.handle(lexer); // recognize str, see ' ' at end
  491. stringTokenCheck("3scaped\\)close", s_number.getToken(lexer));
  492. EXPECT_EQ(&s_number, State::start(lexer, options));
  493. s_number.handle(lexer); // recognize str, see ' ' at end
  494. stringTokenCheck("3scaped\\;comment", s_number.getToken(lexer));
  495. EXPECT_EQ(&s_number, State::start(lexer, options));
  496. s_number.handle(lexer); // recognize str, see ' ' in mid
  497. stringTokenCheck("3scaped\\\\", s_number.getToken(lexer));
  498. // Confirm the word that follows the escaped '\' is correctly recognized.
  499. EXPECT_EQ(&s_number, State::start(lexer, options));
  500. s_number.handle(lexer); // recognize str, see ' ' at end
  501. stringTokenCheck("8ackslash", s_number.getToken(lexer));
  502. // If we continue we'll simply see the EOF
  503. EXPECT_EQ(s_null, State::start(lexer, options));
  504. EXPECT_EQ(Token::END_OF_FILE, s_crlf.getToken(lexer).getType());
  505. }
  506. } // end anonymous namespace