distinct_parser.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*=============================================================================
  2. Copyright (c) 2003 Vaclav Vesely
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #include <boost/assert.hpp>
  9. #include <iostream>
  10. #include <boost/cstdlib.hpp>
  11. #include <boost/spirit/include/classic_core.hpp>
  12. #include <boost/spirit/include/classic_distinct.hpp>
  13. using namespace std;
  14. using namespace boost;
  15. using namespace BOOST_SPIRIT_CLASSIC_NS;
  16. // keyword_p for C++
  17. // (for basic usage instead of std_p)
  18. const distinct_parser<> keyword_p("0-9a-zA-Z_");
  19. // keyword_d for C++
  20. // (for mor intricate usage, for example together with symbol tables)
  21. const distinct_directive<> keyword_d("0-9a-zA-Z_");
  22. struct my_grammar: public grammar<my_grammar>
  23. {
  24. template <typename ScannerT>
  25. struct definition
  26. {
  27. typedef rule<ScannerT> rule_t;
  28. definition(my_grammar const& self)
  29. {
  30. top
  31. =
  32. keyword_p("declare") // use keyword_p instead of std_p
  33. >> !ch_p(':')
  34. >> keyword_d[str_p("ident")] // use keyword_d
  35. ;
  36. }
  37. rule_t top;
  38. rule_t const& start() const
  39. {
  40. return top;
  41. }
  42. };
  43. };
  44. int main()
  45. {
  46. my_grammar gram;
  47. parse_info<> info;
  48. info = parse("declare ident", gram, space_p);
  49. BOOST_ASSERT(info.full); // valid input
  50. info = parse("declare: ident", gram, space_p);
  51. BOOST_ASSERT(info.full); // valid input
  52. info = parse("declareident", gram, space_p);
  53. BOOST_ASSERT(!info.hit); // invalid input
  54. return exit_success;
  55. }