9
3

re_tokeniser.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // tokeniser.hpp
  2. // Copyright (c) 2007-2009 Ben Hanson (http://www.benhanson.net/)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_LEXER_RE_TOKENISER_HPP
  7. #define BOOST_LEXER_RE_TOKENISER_HPP
  8. // memcpy()
  9. #include <cstring>
  10. #include <map>
  11. #include "num_token.hpp"
  12. #include "../../runtime_error.hpp"
  13. #include "../../size_t.hpp"
  14. #include <sstream>
  15. #include "../../string_token.hpp"
  16. #include "re_tokeniser_helper.hpp"
  17. namespace boost
  18. {
  19. namespace lexer
  20. {
  21. namespace detail
  22. {
  23. template<typename CharT>
  24. class basic_re_tokeniser
  25. {
  26. public:
  27. typedef basic_num_token<CharT> num_token;
  28. typedef basic_re_tokeniser_state<CharT> state;
  29. typedef basic_string_token<CharT> string_token;
  30. typedef typename string_token::string string;
  31. typedef std::map<string_token, std::size_t> token_map;
  32. typedef std::pair<string_token, std::size_t> token_pair;
  33. static void next (state &state_, token_map &map_, num_token &token_)
  34. {
  35. CharT ch_ = 0;
  36. bool eos_ = state_.next (ch_);
  37. token_.min_max (0, false, 0);
  38. while (!eos_ && ch_ == '"')
  39. {
  40. state_._in_string ^= 1;
  41. eos_ = state_.next (ch_);
  42. }
  43. if (eos_)
  44. {
  45. if (state_._in_string)
  46. {
  47. throw runtime_error ("Unexpected end of regex "
  48. "(missing '\"').");
  49. }
  50. if (state_._paren_count)
  51. {
  52. throw runtime_error ("Unexpected end of regex "
  53. "(missing ')').");
  54. }
  55. token_.set (num_token::END, null_token);
  56. }
  57. else
  58. {
  59. if (ch_ == '\\')
  60. {
  61. // Even if we are in a string, respect escape sequences...
  62. escape (state_, map_, token_);
  63. }
  64. else if (state_._in_string)
  65. {
  66. // All other meta characters lose their special meaning
  67. // inside a string.
  68. create_charset_token (string (1, ch_), false, map_, token_);
  69. }
  70. else
  71. {
  72. // Not an escape sequence and not inside a string, so
  73. // check for meta characters.
  74. switch (ch_)
  75. {
  76. case '(':
  77. token_.set (num_token::OPENPAREN, null_token);
  78. ++state_._paren_count;
  79. read_options (state_);
  80. break;
  81. case ')':
  82. --state_._paren_count;
  83. if (state_._paren_count < 0)
  84. {
  85. std::ostringstream ss_;
  86. ss_ << "Number of open parenthesis < 0 at index " <<
  87. state_.index () - 1 << '.';
  88. throw runtime_error (ss_.str ().c_str ());
  89. }
  90. token_.set (num_token::CLOSEPAREN, null_token);
  91. if (!state_._flags_stack.empty ())
  92. {
  93. state_._flags = state_._flags_stack.top ();
  94. state_._flags_stack.pop ();
  95. }
  96. break;
  97. case '?':
  98. if (!state_.eos () && *state_._curr == '?')
  99. {
  100. token_.set (num_token::AOPT, null_token);
  101. state_.increment ();
  102. }
  103. else
  104. {
  105. token_.set (num_token::OPT, null_token);
  106. }
  107. break;
  108. case '*':
  109. if (!state_.eos () && *state_._curr == '?')
  110. {
  111. token_.set (num_token::AZEROORMORE, null_token);
  112. state_.increment ();
  113. }
  114. else
  115. {
  116. token_.set (num_token::ZEROORMORE, null_token);
  117. }
  118. break;
  119. case '+':
  120. if (!state_.eos () && *state_._curr == '?')
  121. {
  122. token_.set (num_token::AONEORMORE, null_token);
  123. state_.increment ();
  124. }
  125. else
  126. {
  127. token_.set (num_token::ONEORMORE, null_token);
  128. }
  129. break;
  130. case '{':
  131. open_curly (state_, token_);
  132. break;
  133. case '|':
  134. token_.set (num_token::OR, null_token);
  135. break;
  136. case '^':
  137. if (state_._curr - 1 == state_._start)
  138. {
  139. token_.set (num_token::CHARSET, bol_token);
  140. state_._seen_BOL_assertion = true;
  141. }
  142. else
  143. {
  144. create_charset_token (string (1, ch_), false,
  145. map_, token_);
  146. }
  147. break;
  148. case '$':
  149. if (state_._curr == state_._end)
  150. {
  151. token_.set (num_token::CHARSET, eol_token);
  152. state_._seen_EOL_assertion = true;
  153. }
  154. else
  155. {
  156. create_charset_token (string (1, ch_), false,
  157. map_, token_);
  158. }
  159. break;
  160. case '.':
  161. {
  162. string dot_;
  163. if (state_._flags & dot_not_newline)
  164. {
  165. dot_ = '\n';
  166. }
  167. create_charset_token (dot_, true, map_, token_);
  168. break;
  169. }
  170. case '[':
  171. {
  172. charset (state_, map_, token_);
  173. break;
  174. }
  175. case '/':
  176. throw runtime_error("Lookahead ('/') is not supported yet.");
  177. break;
  178. default:
  179. if ((state_._flags & icase) &&
  180. (std::isupper (ch_, state_._locale) ||
  181. std::islower (ch_, state_._locale)))
  182. {
  183. CharT upper_ = std::toupper (ch_, state_._locale);
  184. CharT lower_ = std::tolower (ch_, state_._locale);
  185. string str_ (1, upper_);
  186. str_ += lower_;
  187. create_charset_token (str_, false, map_, token_);
  188. }
  189. else
  190. {
  191. create_charset_token (string (1, ch_), false,
  192. map_, token_);
  193. }
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. private:
  200. typedef basic_re_tokeniser_helper<CharT> tokeniser_helper;
  201. static void read_options (state &state_)
  202. {
  203. if (!state_.eos () && *state_._curr == '?')
  204. {
  205. CharT ch_ = 0;
  206. bool eos_ = false;
  207. bool negate_ = false;
  208. state_.increment ();
  209. eos_ = state_.next (ch_);
  210. state_._flags_stack.push (state_._flags);
  211. while (!eos_ && ch_ != ':')
  212. {
  213. switch (ch_)
  214. {
  215. case '-':
  216. negate_ ^= 1;
  217. break;
  218. case 'i':
  219. if (negate_)
  220. {
  221. state_._flags = static_cast<regex_flags>
  222. (state_._flags & ~icase);
  223. }
  224. else
  225. {
  226. state_._flags = static_cast<regex_flags>
  227. (state_._flags | icase);
  228. }
  229. negate_ = false;
  230. break;
  231. case 's':
  232. if (negate_)
  233. {
  234. state_._flags = static_cast<regex_flags>
  235. (state_._flags | dot_not_newline);
  236. }
  237. else
  238. {
  239. state_._flags = static_cast<regex_flags>
  240. (state_._flags & ~dot_not_newline);
  241. }
  242. negate_ = false;
  243. break;
  244. default:
  245. {
  246. std::ostringstream ss_;
  247. ss_ << "Unknown option at index " <<
  248. state_.index () - 1 << '.';
  249. throw runtime_error (ss_.str ().c_str ());
  250. }
  251. }
  252. eos_ = state_.next (ch_);
  253. }
  254. // End of string handler will handle early termination
  255. }
  256. else if (!state_._flags_stack.empty ())
  257. {
  258. state_._flags_stack.push (state_._flags);
  259. }
  260. }
  261. static void escape (state &state_, token_map &map_, num_token &token_)
  262. {
  263. CharT ch_ = 0;
  264. std::size_t str_len_ = 0;
  265. const CharT *str_ = tokeniser_helper::escape_sequence (state_,
  266. ch_, str_len_);
  267. if (str_)
  268. {
  269. state state2_ (str_ + 1, str_ + str_len_, state_._flags,
  270. state_._locale);
  271. charset (state2_, map_, token_);
  272. }
  273. else
  274. {
  275. create_charset_token (string (1, ch_), false, map_, token_);
  276. }
  277. }
  278. static void charset (state &state_, token_map &map_, num_token &token_)
  279. {
  280. string chars_;
  281. bool negated_ = false;
  282. tokeniser_helper::charset (state_, chars_, negated_);
  283. create_charset_token (chars_, negated_, map_, token_);
  284. }
  285. static void create_charset_token (const string &charset_,
  286. const bool negated_, token_map &map_, num_token &token_)
  287. {
  288. std::size_t id_ = null_token;
  289. string_token stok_ (negated_, charset_);
  290. stok_.remove_duplicates ();
  291. stok_.normalise ();
  292. typename token_map::const_iterator iter_ = map_.find (stok_);
  293. if (iter_ == map_.end ())
  294. {
  295. id_ = map_.size ();
  296. map_.insert (token_pair (stok_, id_));
  297. }
  298. else
  299. {
  300. id_ = iter_->second;
  301. }
  302. token_.set (num_token::CHARSET, id_);
  303. }
  304. static void open_curly (state &state_, num_token &token_)
  305. {
  306. if (state_.eos ())
  307. {
  308. throw runtime_error ("Unexpected end of regex "
  309. "(missing '}').");
  310. }
  311. else if (*state_._curr >= '0' && *state_._curr <= '9')
  312. {
  313. repeat_n (state_, token_);
  314. if (!state_.eos () && *state_._curr == '?')
  315. {
  316. token_._type = num_token::AREPEATN;
  317. state_.increment ();
  318. }
  319. }
  320. else
  321. {
  322. macro (state_, token_);
  323. }
  324. }
  325. // SYNTAX:
  326. // {n[,[n]]}
  327. // SEMANTIC RULES:
  328. // {0} - INVALID (throw exception)
  329. // {0,} = *
  330. // {0,0} - INVALID (throw exception)
  331. // {0,1} = ?
  332. // {1,} = +
  333. // {min,max} where min == max - {min}
  334. // {min,max} where max < min - INVALID (throw exception)
  335. static void repeat_n (state &state_, num_token &token_)
  336. {
  337. CharT ch_ = 0;
  338. bool eos_ = state_.next (ch_);
  339. while (!eos_ && ch_ >= '0' && ch_ <= '9')
  340. {
  341. token_._min *= 10;
  342. token_._min += ch_ - '0';
  343. eos_ = state_.next (ch_);
  344. }
  345. if (eos_)
  346. {
  347. throw runtime_error ("Unexpected end of regex "
  348. "(missing '}').");
  349. }
  350. bool min_max_ = false;
  351. bool repeatn_ = true;
  352. token_._comma = ch_ == ',';
  353. if (token_._comma)
  354. {
  355. eos_ = state_.next (ch_);
  356. if (eos_)
  357. {
  358. throw runtime_error ("Unexpected end of regex "
  359. "(missing '}').");
  360. }
  361. if (ch_ == '}')
  362. {
  363. // Small optimisation: Check for '*' equivalency.
  364. if (token_._min == 0)
  365. {
  366. token_.set (num_token::ZEROORMORE, null_token);
  367. repeatn_ = false;
  368. }
  369. // Small optimisation: Check for '+' equivalency.
  370. else if (token_._min == 1)
  371. {
  372. token_.set (num_token::ONEORMORE, null_token);
  373. repeatn_ = false;
  374. }
  375. }
  376. else
  377. {
  378. if (ch_ < '0' || ch_ > '9')
  379. {
  380. std::ostringstream ss_;
  381. ss_ << "Missing '}' at index " <<
  382. state_.index () - 1 << '.';
  383. throw runtime_error (ss_.str ().c_str ());
  384. }
  385. min_max_ = true;
  386. do
  387. {
  388. token_._max *= 10;
  389. token_._max += ch_ - '0';
  390. eos_ = state_.next (ch_);
  391. } while (!eos_ && ch_ >= '0' && ch_ <= '9');
  392. if (eos_)
  393. {
  394. throw runtime_error ("Unexpected end of regex "
  395. "(missing '}').");
  396. }
  397. // Small optimisation: Check for '?' equivalency.
  398. if (token_._min == 0 && token_._max == 1)
  399. {
  400. token_.set (num_token::OPT, null_token);
  401. repeatn_ = false;
  402. }
  403. // Small optimisation: if min == max, then min.
  404. else if (token_._min == token_._max)
  405. {
  406. token_._comma = false;
  407. min_max_ = false;
  408. token_._max = 0;
  409. }
  410. }
  411. }
  412. if (ch_ != '}')
  413. {
  414. std::ostringstream ss_;
  415. ss_ << "Missing '}' at index " << state_.index () - 1 << '.';
  416. throw runtime_error (ss_.str ().c_str ());
  417. }
  418. if (repeatn_)
  419. {
  420. // SEMANTIC VALIDATION follows:
  421. // NOTE: {0,} has already become *
  422. // therefore we don't check for a comma.
  423. if (token_._min == 0 && token_._max == 0)
  424. {
  425. std::ostringstream ss_;
  426. ss_ << "Cannot have exactly zero repeats preceding index " <<
  427. state_.index () << '.';
  428. throw runtime_error (ss_.str ().c_str ());
  429. }
  430. if (min_max_ && token_._max < token_._min)
  431. {
  432. std::ostringstream ss_;
  433. ss_ << "Max less than min preceding index " <<
  434. state_.index () << '.';
  435. throw runtime_error (ss_.str ().c_str ());
  436. }
  437. token_.set (num_token::REPEATN, null_token);
  438. }
  439. }
  440. static void macro (state &state_, num_token &token_)
  441. {
  442. CharT ch_ = 0;
  443. bool eos_ = false;
  444. const CharT *start_ = state_._curr;
  445. state_.next (ch_);
  446. if (ch_ != '_' && !(ch_ >= 'A' && ch_ <= 'Z') &&
  447. !(ch_ >= 'a' && ch_ <= 'z'))
  448. {
  449. std::ostringstream ss_;
  450. ss_ << "Invalid MACRO name at index " <<
  451. state_.index () - 1 << '.';
  452. throw runtime_error (ss_.str ().c_str ());
  453. }
  454. do
  455. {
  456. eos_ = state_.next (ch_);
  457. if (eos_)
  458. {
  459. throw runtime_error ("Unexpected end of regex "
  460. "(missing '}').");
  461. }
  462. } while (ch_ == '_' || ch_ == '-' || (ch_ >= 'A' && ch_ <= 'Z') ||
  463. (ch_ >= 'a' && ch_ <= 'z') || (ch_ >= '0' && ch_ <= '9'));
  464. if (ch_ != '}')
  465. {
  466. std::ostringstream ss_;
  467. ss_ << "Missing '}' at index " << state_.index () - 1 << '.';
  468. throw runtime_error (ss_.str ().c_str ());
  469. }
  470. std::size_t len_ = state_._curr - 1 - start_;
  471. if (len_ > max_macro_len)
  472. {
  473. std::basic_stringstream<CharT> ss_;
  474. std::ostringstream os_;
  475. os_ << "MACRO name '";
  476. while (len_)
  477. {
  478. os_ << ss_.narrow (*start_++, ' ');
  479. --len_;
  480. }
  481. os_ << "' too long.";
  482. throw runtime_error (os_.str ());
  483. }
  484. token_.set (num_token::MACRO, null_token);
  485. // Some systems have memcpy in namespace std.
  486. using namespace std;
  487. memcpy (token_._macro, start_, len_ * sizeof (CharT));
  488. token_._macro[len_] = 0;
  489. }
  490. };
  491. }
  492. }
  493. }
  494. #endif