string_token_id.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/detail/lightweight_test.hpp>
  6. #include <boost/config/warning_disable.hpp>
  7. #include <boost/spirit/include/lex_lexertl.hpp>
  8. #include <boost/spirit/include/qi_parse.hpp>
  9. #include <boost/spirit/include/qi_operator.hpp>
  10. #include <boost/spirit/include/qi_action.hpp>
  11. #include <boost/spirit/include/qi_grammar.hpp>
  12. #include <boost/spirit/include/phoenix_operator.hpp>
  13. #include <iostream>
  14. #include <string>
  15. namespace qi = boost::spirit::qi;
  16. namespace lex = boost::spirit::lex;
  17. enum tokenids
  18. {
  19. IDWORD = lex::min_token_id,
  20. IDCHAR,
  21. IDANY
  22. };
  23. template <typename Lexer>
  24. struct word_count_tokens : lex::lexer<Lexer>
  25. {
  26. word_count_tokens()
  27. {
  28. this->self.add_pattern
  29. ("TEST", "A")
  30. ;
  31. this->self =
  32. lex::string("{TEST}", IDWORD)
  33. | lex::char_('a', IDCHAR)
  34. | lex::string(".", IDANY)
  35. ;
  36. }
  37. };
  38. template <typename Iterator>
  39. struct word_count_grammar : qi::grammar<Iterator>
  40. {
  41. template <typename TokenDef>
  42. word_count_grammar(TokenDef const&)
  43. : word_count_grammar::base_type(start)
  44. , w(0), c(0), a(0)
  45. {
  46. using boost::phoenix::ref;
  47. using qi::token;
  48. start = *( token(IDWORD) [++ref(w)]
  49. | token(IDCHAR) [++ref(c)]
  50. | token(IDANY) [++ref(a)]
  51. )
  52. ;
  53. }
  54. std::size_t w, c, a;
  55. qi::rule<Iterator> start;
  56. };
  57. int main()
  58. {
  59. typedef lex::lexertl::token<
  60. const char*, boost::mpl::vector<std::string>
  61. > token_type;
  62. typedef lex::lexertl::lexer<token_type> lexer_type;
  63. typedef word_count_tokens<lexer_type>::iterator_type iterator_type;
  64. word_count_tokens<lexer_type> word_count; // Our lexer
  65. word_count_grammar<iterator_type> g (word_count); // Our parser
  66. std::string str ("AaBCD");
  67. char const* first = str.c_str();
  68. char const* last = &first[str.size()];
  69. BOOST_TEST(lex::tokenize_and_parse(first, last, word_count, g));
  70. BOOST_TEST(g.w == 1 && g.c == 1 && g.a == 3);
  71. return boost::report_errors();
  72. }