num_token.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // num_token.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_NUM_TOKEN_HPP
  7. #define BOOST_LEXER_NUM_TOKEN_HPP
  8. #include <boost/config.hpp>
  9. #include "../../consts.hpp" // null_token
  10. #include "../../size_t.hpp"
  11. #include <boost/detail/workaround.hpp>
  12. namespace boost
  13. {
  14. namespace lexer
  15. {
  16. namespace detail
  17. {
  18. template<typename CharT>
  19. struct basic_num_token
  20. {
  21. enum type {BEGIN, REGEX, OREXP, SEQUENCE, SUB, EXPRESSION, REPEAT,
  22. DUP, OR, CHARSET, MACRO, OPENPAREN, CLOSEPAREN, OPT, AOPT,
  23. ZEROORMORE, AZEROORMORE, ONEORMORE, AONEORMORE, REPEATN, AREPEATN,
  24. END};
  25. type _type;
  26. std::size_t _id;
  27. std::size_t _min;
  28. bool _comma;
  29. std::size_t _max;
  30. CharT _macro[max_macro_len + 1];
  31. static const char _precedence_table[END + 1][END + 1];
  32. static const char *_precedence_strings[END + 1];
  33. basic_num_token (const type type_ = BEGIN,
  34. const std::size_t id_ = null_token) :
  35. _type (type_),
  36. _id (id_),
  37. _min (0),
  38. _comma (false),
  39. _max (0)
  40. {
  41. *_macro = 0;
  42. }
  43. basic_num_token &operator = (const basic_num_token &rhs_)
  44. {
  45. _type = rhs_._type;
  46. _id = rhs_._id;
  47. _min = rhs_._min;
  48. _comma = rhs_._comma;
  49. _max = rhs_._max;
  50. if (_type == MACRO)
  51. {
  52. const CharT *read_ = rhs_._macro;
  53. CharT *write_ = _macro;
  54. while (*read_)
  55. {
  56. *write_++ = *read_++;
  57. }
  58. *write_ = 0;
  59. }
  60. return *this;
  61. }
  62. void set (const type type_)
  63. {
  64. _type = type_;
  65. _id = null_token;
  66. }
  67. void set (const type type_, const std::size_t id_)
  68. {
  69. _type = type_;
  70. _id = id_;
  71. }
  72. void min_max (const std::size_t min_, const bool comma_,
  73. const std::size_t max_)
  74. {
  75. _min = min_;
  76. _comma = comma_;
  77. _max = max_;
  78. }
  79. char precedence (const type type_) const
  80. {
  81. return _precedence_table[_type][type_];
  82. }
  83. const char *precedence_string () const
  84. {
  85. return _precedence_strings[_type];
  86. }
  87. };
  88. template<typename CharT>
  89. const char basic_num_token<CharT>::_precedence_table[END + 1][END + 1] = {
  90. // BEG, REG, ORE, SEQ, SUB, EXP, RPT, DUP, | , CHR, MCR, ( , ) , ? , ?? , * , *? , + , +?, {n}?, {n}, END
  91. /*BEGIN*/{' ', '<', '<', '<', '<', '<', '<', ' ', ' ', '<', '<', '<', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  92. /*REGEX*/{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '=', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  93. /*OREXP*/{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '=', '>', '>', ' ', '>', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  94. /* SEQ */{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', ' ', '>', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  95. /* SUB */{' ', ' ', ' ', ' ', ' ', '=', '<', ' ', '>', '<', '<', '<', '>', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  96. /*EXPRE*/{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  97. /* RPT */{' ', ' ', ' ', ' ', ' ', ' ', ' ', '=', '>', '>', '>', '>', '>', '<', '<', '<', '<', '<', '<', '<', '<', '>'},
  98. /*DUPLI*/{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  99. /* | */{' ', ' ', ' ', '=', '<', '<', '<', ' ', ' ', '<', '<', '<', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  100. /*CHARA*/{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>'},
  101. /*MACRO*/{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>'},
  102. /* ( */{' ', '=', '<', '<', '<', '<', '<', ' ', ' ', '<', '<', '<', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  103. /* ) */{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>'},
  104. /* ? */{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', '<', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  105. /* ?? */{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  106. /* * */{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', '<', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  107. /* *? */{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  108. /* + */{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', '<', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  109. /* +? */{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  110. /*{n,m}*/{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', '<', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  111. /*{nm}?*/{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '>'},
  112. /* END */{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}
  113. };
  114. template<typename CharT>
  115. const char *basic_num_token<CharT>::_precedence_strings[END + 1] =
  116. #if BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, BOOST_TESTED_AT(910))
  117. {{"BEGIN"}, {"REGEX"}, {"OREXP"}, {"SEQUENCE"}, {"SUB"}, {"EXPRESSION"},
  118. {"REPEAT"}, {"DUPLICATE"}, {"|"}, {"CHARSET"}, {"MACRO"},
  119. {"("}, {")"}, {"?"}, {"??"}, {"*"}, {"*?"}, {"+"}, {"+?"}, {"{n[,[m]]}"},
  120. {"{n[,[m]]}?"}, {"END"}};
  121. #else
  122. {"BEGIN", "REGEX", "OREXP", "SEQUENCE", "SUB", "EXPRESSION", "REPEAT",
  123. "DUPLICATE", "|", "CHARSET", "MACRO", "(", ")", "?", "??", "*", "*?",
  124. "+", "+?", "{n[,[m]]}", "{n[,[m]]}?", "END"};
  125. #endif
  126. }
  127. }
  128. }
  129. #endif