lexer_def.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #include "lexer.hpp"
  8. namespace client { namespace lexer
  9. {
  10. template <typename BaseIterator>
  11. conjure_tokens<BaseIterator>::conjure_tokens()
  12. : identifier("[a-zA-Z_][a-zA-Z_0-9]*", token_ids::identifier)
  13. , lit_uint("[0-9]+", token_ids::lit_uint)
  14. , true_or_false("true|false", token_ids::true_or_false)
  15. , add(*this)
  16. {
  17. lex::_pass_type _pass;
  18. this->self = lit_uint | true_or_false;
  19. this->add
  20. ("void")
  21. ("int")
  22. ("if")
  23. ("else")
  24. ("while")
  25. ("return")
  26. ("=", token_ids::assign)
  27. ("\\+=", token_ids::plus_assign)
  28. ("\\-=", token_ids::minus_assign)
  29. ("\\*=", token_ids::times_assign)
  30. ("\\/=", token_ids::divide_assign)
  31. ("%=", token_ids::mod_assign)
  32. ("\\&=", token_ids::bit_and_assign)
  33. ("\\^=", token_ids::bit_xor_assign)
  34. ("\\|=", token_ids::bit_or_assign)
  35. ("<<=", token_ids::shift_left_assign)
  36. (">>=", token_ids::shift_right_assign)
  37. ("\\|\\|", token_ids::logical_or)
  38. ("&&", token_ids::logical_and)
  39. ("\\|", token_ids::bit_or)
  40. ("\\^", token_ids::bit_xor)
  41. ("&", token_ids::bit_and)
  42. ("<<", token_ids::shift_left)
  43. (">>", token_ids::shift_right)
  44. ("==", token_ids::equal)
  45. ("!=", token_ids::not_equal)
  46. ("<", token_ids::less)
  47. ("<=", token_ids::less_equal)
  48. (">", token_ids::greater)
  49. (">=", token_ids::greater_equal)
  50. ("\\+", token_ids::plus)
  51. ("\\-", token_ids::minus)
  52. ("\\*", token_ids::times)
  53. ("\\/", token_ids::divide)
  54. ("%", token_ids::mod)
  55. ("\\+\\+", token_ids::plus_plus)
  56. ("\\-\\-", token_ids::minus_minus)
  57. ("~", token_ids::compl_)
  58. ("!", token_ids::not_)
  59. ;
  60. this->self += lex::char_('(') | ')' | '{' | '}' | ',' | ';';
  61. this->self +=
  62. identifier
  63. | lex::string("(\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/)|(\\/\\/[^\r\n]*)", token_ids::comment)
  64. [
  65. lex::_pass = lex::pass_flags::pass_ignore
  66. ]
  67. | lex::string("[ \t\n\r]+", token_ids::whitespace)
  68. [
  69. lex::_pass = lex::pass_flags::pass_ignore
  70. ]
  71. ;
  72. }
  73. template <typename BaseIterator>
  74. bool conjure_tokens<BaseIterator>::add_(
  75. std::string const& keyword, int id_)
  76. {
  77. // add the token to the lexer
  78. token_ids::type id;
  79. if (id_ == token_ids::invalid)
  80. id = token_ids::type(this->get_next_id());
  81. else
  82. id = token_ids::type(id_);
  83. this->self.add(keyword, id);
  84. // store the mapping for later retrieval
  85. std::pair<typename keyword_map_type::iterator, bool> p =
  86. keywords_.insert(typename keyword_map_type::value_type(keyword, id));
  87. return p.second;
  88. }
  89. }}