Advertisement
Guest User

testing.cc

a guest
Dec 12th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <boost/spirit/home/x3.hpp>
  2.  
  3. #include <string>
  4. #include <iostream>
  5. #include <ios>
  6.  
  7. namespace x3 = boost::spirit::x3;
  8. namespace ascii = boost::spirit::x3::ascii;
  9.  
  10. using ascii::char_;
  11. using x3::lexeme;
  12. using x3::_val;
  13. using x3::_attr;
  14.  
  15. auto const append = [](auto& ctx) {
  16.     _val(ctx) += _attr(ctx);
  17. };
  18.  
  19. auto const backtick_code_point_foo = lexeme[
  20.     '`' >> *char_("\x60\x6f")[append] >> '`'
  21. ];
  22.  
  23. auto const backtick_ascii_foo = lexeme[
  24.     '`' >> *char_("fo")[append] >> '`'
  25. ];
  26.  
  27. class name_ascii_class;
  28.  
  29. typedef x3::rule<name_ascii_class, std::string> name_ascii_type;
  30. name_ascii_type const name_ascii = "name_ascii";
  31.  
  32. auto const name_ascii_def =
  33.     backtick_ascii_foo
  34. ;
  35.  
  36. class name_code_point_class;
  37.  
  38. typedef x3::rule<name_code_point_class, std::string> name_code_point_type;
  39. name_code_point_type const name_code_point = "name_code_point";
  40.  
  41. auto const name_code_point_def =
  42.     backtick_code_point_foo
  43. ;
  44.  
  45. BOOST_SPIRIT_DEFINE(name_ascii, name_code_point);
  46.  
  47. int main(int argc, const char** argv) {
  48.     namespace x3 = boost::spirit::x3;
  49.  
  50.     typedef std::pair<std::string, std::string> test_data_t;
  51.  
  52.     std::string data("`foo`");
  53.  
  54.     bool result = false;
  55.  
  56.     std::string parsed;
  57.     std::cout << "Parsing `foo` with name_code_point parser: ";
  58.     result = x3::phrase_parse(data.begin(),
  59.                               data.end(),
  60.                               name_code_point,
  61.                               x3::ascii::space,
  62.                               parsed);
  63.     std::cout << result << std::endl;
  64.     std::cout << "Parsing `foo` with name_ascii parser: ";
  65.     result = x3::phrase_parse(data.begin(),
  66.                               data.end(),
  67.                               name_ascii,
  68.                               x3::ascii::space,
  69.                               parsed);
  70.     std::cout << result << std::endl;
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement