Advertisement
Guest User

Untitled

a guest
Oct 26th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <boost/spirit/home/qi.hpp>
  3. #include <vector>
  4. #include <string>
  5.  
  6. namespace qi = boost::spirit::qi;
  7. namespace ascii = boost::spirit::ascii;
  8.  
  9. struct escapedDoubleQuote_ : qi::symbols<char, std::string >
  10. {
  11. escapedDoubleQuote_()
  12. {
  13. add("""", "\"");
  14. }
  15. } escapedDoubleQuote;
  16.  
  17. template<typename Iterator>
  18. struct MyGrammar : qi::grammar<Iterator, std::string()>
  19. {
  20. MyGrammar() : MyGrammar::base_type(escapedField)
  21. {
  22. subField = +(~qi::char_("""));
  23. escapedField =
  24. qi::lexeme[ // or qi::as<std::string> ... both seems to do nothing.
  25. subField
  26. >> -(escapedDoubleQuote >> escapedField)
  27. ];
  28. }
  29.  
  30. qi::rule<Iterator, std::string()> subField;
  31. qi::rule<Iterator, std::string()> escapedField;
  32. };
  33.  
  34. int main()
  35. {
  36. typedef std::string::const_iterator iterator_type;
  37. typedef MyGrammar<iterator_type> parser_type;
  38. parser_type parser;
  39.  
  40. std::string input = "123""456""789";
  41. std::string output;
  42. iterator_type it = input.begin();
  43. bool parsed = parse(
  44. it,
  45. input.cend(),
  46. parser,
  47. output
  48. );
  49.  
  50. if(parsed && it == input.end())
  51. {
  52. std::cout << output << std::endl;
  53. // expected: 123"456"789
  54. // actual : "789
  55. }
  56.  
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement