master_lexer_state_unittest.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 MasterLexer::Token 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 MasterLexer::Token& 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. }
  232. TEST_F(MasterLexerStateTest, string) {
  233. // Check with simple strings followed by separate characters
  234. ss << "followed-by-EOL\n";
  235. ss << "followed-by-CR\r";
  236. ss << "followed-by-space ";
  237. ss << "followed-by-tab\t";
  238. ss << "followed-by-comment;this is comment and ignored\n";
  239. ss << "followed-by-paren(closing)";
  240. ss << "followed-by-EOF";
  241. lexer.pushSource(ss);
  242. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  243. s_string.handle(lexer); // recognize str, see \n
  244. EXPECT_FALSE(s_string.wasLastEOL(lexer));
  245. stringTokenCheck("followed-by-EOL", s_string.getToken(lexer));
  246. EXPECT_EQ(s_null, State::start(lexer, common_options)); // skip \n
  247. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  248. s_string.handle(lexer); // recognize str, see \r
  249. stringTokenCheck("followed-by-CR", s_string.getToken(lexer));
  250. EXPECT_EQ(&s_crlf, State::start(lexer, common_options)); // handle \r...
  251. s_crlf.handle(lexer); // ...and skip it
  252. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  253. s_string.handle(lexer); // recognize str, see ' '
  254. stringTokenCheck("followed-by-space", s_string.getToken(lexer));
  255. // skip ' ', then recognize the next string
  256. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  257. s_string.handle(lexer); // recognize str, see \t
  258. stringTokenCheck("followed-by-tab", s_string.getToken(lexer));
  259. // skip \t, then recognize the next string
  260. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  261. s_string.handle(lexer); // recognize str, see comment
  262. stringTokenCheck("followed-by-comment", s_string.getToken(lexer));
  263. EXPECT_EQ(s_null, State::start(lexer, common_options)); // skip \n after it
  264. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  265. s_string.handle(lexer); // recognize str, see '('
  266. stringTokenCheck("followed-by-paren", s_string.getToken(lexer));
  267. EXPECT_EQ(&s_string, State::start(lexer, common_options)); // str in ()
  268. s_string.handle(lexer); // recognize the str, see ')'
  269. stringTokenCheck("closing", s_string.getToken(lexer));
  270. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  271. s_string.handle(lexer); // recognize str, see EOF
  272. stringTokenCheck("followed-by-EOF", s_string.getToken(lexer));
  273. }
  274. TEST_F(MasterLexerStateTest, stringEscape) {
  275. // some of the separate characters should be considered part of the
  276. // string if escaped.
  277. ss << "escaped\\ space ";
  278. ss << "escaped\\\ttab ";
  279. ss << "escaped\\(paren ";
  280. ss << "escaped\\)close ";
  281. ss << "escaped\\;comment ";
  282. ss << "escaped\\\\ backslash "; // second '\' shouldn't escape ' '
  283. lexer.pushSource(ss);
  284. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  285. s_string.handle(lexer); // recognize str, see ' ' at end
  286. stringTokenCheck("escaped\\ space", s_string.getToken(lexer));
  287. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  288. s_string.handle(lexer); // recognize str, see ' ' at end
  289. stringTokenCheck("escaped\\\ttab", 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\\(paren", 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\\)close", 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\\;comment", s_string.getToken(lexer));
  299. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  300. s_string.handle(lexer); // recognize str, see ' ' in mid
  301. stringTokenCheck("escaped\\\\", s_string.getToken(lexer));
  302. // Confirm the word that follows the escaped '\' is correctly recognized.
  303. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  304. s_string.handle(lexer); // recognize str, see ' ' at end
  305. stringTokenCheck("backslash", s_string.getToken(lexer));
  306. }
  307. TEST_F(MasterLexerStateTest, quotedString) {
  308. ss << "\"ignore-quotes\"\n";
  309. ss << "\"quoted string\" "; // space is part of the qstring
  310. // also check other separator characters. note that \r doesn't cause
  311. // UNBALANCED_QUOTES. Not sure if it's intentional, but that's how the
  312. // BIND 9 version works, so we follow it (it should be too minor to matter
  313. // in practice anyway)
  314. ss << "\"quoted()\t\rstring\" ";
  315. ss << "\"escape\\ in quote\" ";
  316. ss << "\"escaped\\\"\" ";
  317. ss << "\"escaped backslash\\\\\" ";
  318. ss << "\"no;comment\"";
  319. lexer.pushSource(ss);
  320. // by default, '"' doesn't have any special meaning and part of string
  321. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  322. s_string.handle(lexer); // recognize str, see \n
  323. stringTokenCheck("\"ignore-quotes\"", s_string.getToken(lexer));
  324. EXPECT_EQ(s_null, State::start(lexer, common_options)); // skip \n after it
  325. EXPECT_TRUE(s_string.wasLastEOL(lexer));
  326. // If QSTRING is specified in option, '"' is regarded as a beginning of
  327. // a quoted string.
  328. const MasterLexer::Options options = common_options | MasterLexer::QSTRING;
  329. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  330. EXPECT_FALSE(s_string.wasLastEOL(lexer)); // EOL is canceled due to '"'
  331. s_qstring.handle(lexer);
  332. stringTokenCheck("quoted string", s_string.getToken(lexer), true);
  333. // Also checks other separator characters within a qstring
  334. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  335. s_qstring.handle(lexer);
  336. stringTokenCheck("quoted()\t\rstring", s_string.getToken(lexer), true);
  337. // escape character mostly doesn't have any effect in the qstring
  338. // processing
  339. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  340. s_qstring.handle(lexer);
  341. stringTokenCheck("escape\\ in quote", s_string.getToken(lexer), true);
  342. // The only exception is the quotation mark itself. Note that the escape
  343. // only works on the quotation mark immediately after it.
  344. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  345. s_qstring.handle(lexer);
  346. stringTokenCheck("escaped\"", s_string.getToken(lexer), true);
  347. // quoted '\' then '"'. Unlike the previous case '"' shouldn't be
  348. // escaped.
  349. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  350. s_qstring.handle(lexer);
  351. stringTokenCheck("escaped backslash\\\\", s_string.getToken(lexer), true);
  352. // ';' has no meaning in a quoted string (not indicating a comment)
  353. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  354. s_qstring.handle(lexer);
  355. stringTokenCheck("no;comment", s_string.getToken(lexer), true);
  356. }
  357. TEST_F(MasterLexerStateTest, brokenQuotedString) {
  358. ss << "\"unbalanced-quote\n";
  359. ss << "\"quoted\\\n\" ";
  360. ss << "\"unclosed quote and EOF";
  361. lexer.pushSource(ss);
  362. // EOL is encountered without closing the quote
  363. const MasterLexer::Options options = common_options | MasterLexer::QSTRING;
  364. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  365. s_qstring.handle(lexer);
  366. ASSERT_EQ(Token::ERROR, s_qstring.getToken(lexer).getType());
  367. EXPECT_EQ(Token::UNBALANCED_QUOTES,
  368. s_qstring.getToken(lexer).getErrorCode());
  369. // We can resume after the error from the '\n'
  370. EXPECT_EQ(s_null, State::start(lexer, options));
  371. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  372. // \n is okay in a quoted string if escaped
  373. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  374. s_qstring.handle(lexer);
  375. stringTokenCheck("quoted\\\n", s_string.getToken(lexer), true);
  376. // EOF is encountered without closing the quote
  377. EXPECT_EQ(&s_qstring, State::start(lexer, options));
  378. s_qstring.handle(lexer);
  379. ASSERT_EQ(Token::ERROR, s_qstring.getToken(lexer).getType());
  380. EXPECT_EQ(Token::UNEXPECTED_END, s_qstring.getToken(lexer).getErrorCode());
  381. // If we continue we'll simply see the EOF
  382. EXPECT_EQ(s_null, State::start(lexer, options));
  383. EXPECT_EQ(Token::END_OF_FILE, s_crlf.getToken(lexer).getType());
  384. }
  385. TEST_F(MasterLexerStateTest, basicNumbers) {
  386. ss << "0 ";
  387. ss << "1 ";
  388. ss << "12345 ";
  389. ss << "4294967295 "; // 2^32-1
  390. ss << "4294967296 "; // Out of range
  391. ss << "340282366920938463463374607431768211456 ";
  392. // Very much out of range (2^128)
  393. ss << "005 "; // Leading zeroes are ignored
  394. ss << "42;asdf\n"; // Number with comment
  395. ss << "37"; // Simple number again, here to make
  396. // sure none of the above messed up
  397. // the tokenizer
  398. lexer.pushSource(ss);
  399. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  400. s_number.handle(lexer);
  401. EXPECT_EQ(0, s_number.getToken(lexer).getNumber());
  402. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  403. s_number.handle(lexer);
  404. EXPECT_EQ(1, s_number.getToken(lexer).getNumber());
  405. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  406. s_number.handle(lexer);
  407. EXPECT_EQ(12345, s_number.getToken(lexer).getNumber());
  408. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  409. s_number.handle(lexer);
  410. EXPECT_EQ(4294967295u, s_number.getToken(lexer).getNumber());
  411. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  412. s_number.handle(lexer);
  413. EXPECT_EQ(Token::NUMBER_RANGE,
  414. s_number.getToken(lexer).getErrorCode());
  415. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  416. s_number.handle(lexer);
  417. EXPECT_EQ(Token::NUMBER_RANGE,
  418. s_number.getToken(lexer).getErrorCode());
  419. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  420. s_number.handle(lexer);
  421. EXPECT_EQ(5, s_number.getToken(lexer).getNumber());
  422. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  423. s_number.handle(lexer);
  424. EXPECT_EQ(42, s_number.getToken(lexer).getNumber());
  425. EXPECT_EQ(s_null, State::start(lexer, common_options));
  426. EXPECT_TRUE(s_crlf.wasLastEOL(lexer));
  427. EXPECT_EQ(Token::END_OF_LINE, s_crlf.getToken(lexer).getType());
  428. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  429. s_number.handle(lexer);
  430. EXPECT_EQ(37, s_number.getToken(lexer).getNumber());
  431. // If we continue we'll simply see the EOF
  432. EXPECT_EQ(s_null, State::start(lexer, options));
  433. EXPECT_EQ(Token::END_OF_FILE, s_crlf.getToken(lexer).getType());
  434. }
  435. // Test tokens that look like (or start out as) numbers,
  436. // but turn out to be strings. Tests include escaped characters.
  437. TEST_F(MasterLexerStateTest, stringNumbers) {
  438. ss << "-1 "; // Negative numbers are interpreted
  439. // as strings (unsigned integers only)
  440. ss << "123abc456 "; // 'Numbers' containing non-digits should
  441. // be interpreted as strings
  442. ss << "123\\456 "; // Numbers containing escaped digits are
  443. // interpreted as strings
  444. ss << "3scaped\\ space ";
  445. ss << "3scaped\\\ttab ";
  446. ss << "3scaped\\(paren ";
  447. ss << "3scaped\\)close ";
  448. ss << "3scaped\\;comment ";
  449. ss << "3scaped\\\\ 8ackslash "; // second '\' shouldn't escape ' '
  450. lexer.pushSource(ss);
  451. EXPECT_EQ(&s_string, State::start(lexer, common_options));
  452. s_string.handle(lexer);
  453. stringTokenCheck("-1", s_string.getToken(lexer), false);
  454. // Starts out as a number, but ends up being a string
  455. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  456. s_number.handle(lexer);
  457. stringTokenCheck("123abc456", s_number.getToken(lexer), false);
  458. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  459. s_number.handle(lexer);
  460. stringTokenCheck("123\\456", s_number.getToken(lexer), false);
  461. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  462. s_number.handle(lexer); // recognize str, see ' ' at end
  463. stringTokenCheck("3scaped\\ space", s_number.getToken(lexer));
  464. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  465. s_number.handle(lexer); // recognize str, see ' ' at end
  466. stringTokenCheck("3scaped\\\ttab", s_number.getToken(lexer));
  467. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  468. s_number.handle(lexer); // recognize str, see ' ' at end
  469. stringTokenCheck("3scaped\\(paren", s_number.getToken(lexer));
  470. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  471. s_number.handle(lexer); // recognize str, see ' ' at end
  472. stringTokenCheck("3scaped\\)close", s_number.getToken(lexer));
  473. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  474. s_number.handle(lexer); // recognize str, see ' ' at end
  475. stringTokenCheck("3scaped\\;comment", s_number.getToken(lexer));
  476. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  477. s_number.handle(lexer); // recognize str, see ' ' in mid
  478. stringTokenCheck("3scaped\\\\", s_number.getToken(lexer));
  479. // Confirm the word that follows the escaped '\' is correctly recognized.
  480. EXPECT_EQ(&s_number, State::start(lexer, common_options));
  481. s_number.handle(lexer); // recognize str, see ' ' at end
  482. stringTokenCheck("8ackslash", s_number.getToken(lexer));
  483. // If we continue we'll simply see the EOF
  484. EXPECT_EQ(s_null, State::start(lexer, options));
  485. EXPECT_EQ(Token::END_OF_FILE, s_crlf.getToken(lexer).getType());
  486. }
  487. } // end anonymous namespace