distinct_parser_dynamic.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. struct my_grammar: public grammar<my_grammar>
  17. {
  18. template <typename ScannerT>
  19. struct definition
  20. {
  21. typedef rule<ScannerT> rule_t;
  22. // keyword_p for ASN.1
  23. dynamic_distinct_parser<ScannerT> keyword_p;
  24. definition(my_grammar const& self)
  25. : keyword_p(alnum_p | ('-' >> ~ch_p('-'))) // ASN.1 has quite complex naming rules
  26. {
  27. top
  28. =
  29. keyword_p("asn-declare") // use keyword_p instead of std_p
  30. >> !str_p("--")
  31. >> keyword_p("ident")
  32. ;
  33. }
  34. rule_t top;
  35. rule_t const& start() const
  36. {
  37. return top;
  38. }
  39. };
  40. };
  41. int main()
  42. {
  43. my_grammar gram;
  44. parse_info<> info;
  45. info = parse("asn-declare ident", gram, space_p);
  46. BOOST_ASSERT(info.full); // valid input
  47. info = parse("asn-declare--ident", gram, space_p);
  48. BOOST_ASSERT(info.full); // valid input
  49. info = parse("asn-declare-ident", gram, space_p);
  50. BOOST_ASSERT(!info.hit); // invalid input
  51. return exit_success;
  52. }