regression_basic_lexer.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2009 Pavel Baranov
  3. //
  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. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/include/lex_lexertl.hpp>
  8. #include <iostream>
  9. #include <string>
  10. using namespace boost::spirit;
  11. using namespace boost::spirit::lex;
  12. typedef const char * base_iterator;
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // Token definition
  15. ///////////////////////////////////////////////////////////////////////////////
  16. template <typename Lexer>
  17. struct position_helper_tokens : lexer<Lexer>
  18. {
  19. position_helper_tokens()
  20. {
  21. // define tokens and associate them with the lexer
  22. eol = "\n";
  23. any = "[^\n]+";
  24. // associate tokens with the lexer
  25. this->self
  26. = eol
  27. | any
  28. ;
  29. }
  30. token_def<> any, eol;
  31. };
  32. int main()
  33. {
  34. // read input from the given file
  35. std::string str ("test");
  36. // token type
  37. typedef lexertl::token<base_iterator, lex::omit, boost::mpl::false_> token_type;
  38. // lexer type
  39. typedef lexertl::actor_lexer<token_type> lexer_type;
  40. // create the lexer object instance needed to invoke the lexical analysis
  41. position_helper_tokens<lexer_type> position_helper_lexer;
  42. // tokenize the given string, all generated tokens are discarded
  43. base_iterator first = str.c_str();
  44. base_iterator last = &first[str.size()];
  45. for(lexer_type::iterator_type i = position_helper_lexer.begin(first, last);
  46. i != position_helper_lexer.end() && (*i).is_valid(); i++ )
  47. {
  48. }
  49. return boost::report_errors();
  50. }