grammar.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef META_HS_GRAMMAR_HPP
  2. #define META_HS_GRAMMAR_HPP
  3. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2012.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <token.hpp>
  8. #include <semantic.hpp>
  9. #include <boost/metaparse/middle_of.hpp>
  10. #include <boost/metaparse/transform.hpp>
  11. #include <boost/metaparse/sequence.hpp>
  12. #include <boost/metaparse/last_of.hpp>
  13. #include <boost/metaparse/one_of.hpp>
  14. #include <boost/metaparse/foldl_reject_incomplete_start_with_parser.hpp>
  15. #include <boost/metaparse/foldr_start_with_parser.hpp>
  16. #include <boost/metaparse/entire_input.hpp>
  17. #include <boost/metaparse/build_parser.hpp>
  18. namespace grammar
  19. {
  20. /*
  21. * The grammar
  22. *
  23. * definition ::= token::name+ token::define expression
  24. * expression ::= cmp_exp
  25. * cmp_exp ::= plus_exp (token::cmp plus_exp)*
  26. * plus_exp ::= mult_exp ((token::plus | token::minus) mult_exp)*
  27. * mult_exp ::= application ((token::mult | token::div) application)*
  28. * application ::= single_exp+
  29. * single_exp ::= if_exp | token::name | token::int_ | bracket_exp
  30. * if_exp ::= token::if_ expression token::then expression token::else_ expression
  31. * bracket_exp ::= token::open_bracket expression token::close_bracket
  32. */
  33. struct expression;
  34. typedef
  35. boost::metaparse::middle_of<
  36. token::open_bracket,
  37. expression,
  38. token::close_bracket
  39. >
  40. bracket_exp;
  41. typedef
  42. boost::metaparse::transform<
  43. boost::metaparse::sequence<
  44. boost::metaparse::last_of<token::if_, expression>,
  45. boost::metaparse::last_of<token::then, expression>,
  46. boost::metaparse::last_of<token::else_, expression>
  47. >,
  48. semantic::if_
  49. >
  50. if_exp;
  51. typedef
  52. boost::metaparse::one_of<
  53. if_exp,
  54. boost::metaparse::transform<token::name, semantic::ref>,
  55. boost::metaparse::transform<token::int_, semantic::value>,
  56. bracket_exp
  57. >
  58. single_exp;
  59. typedef
  60. boost::metaparse::foldl_reject_incomplete_start_with_parser<
  61. single_exp,
  62. single_exp,
  63. semantic::application
  64. >
  65. application;
  66. typedef
  67. boost::metaparse::foldl_reject_incomplete_start_with_parser<
  68. boost::metaparse::sequence<
  69. boost::metaparse::one_of<token::mult, token::div>,
  70. application
  71. >,
  72. application,
  73. semantic::binary_op
  74. >
  75. mult_exp;
  76. typedef
  77. boost::metaparse::foldl_reject_incomplete_start_with_parser<
  78. boost::metaparse::sequence<
  79. boost::metaparse::one_of<token::plus, token::minus>,
  80. mult_exp
  81. >,
  82. mult_exp,
  83. semantic::binary_op
  84. >
  85. plus_exp;
  86. typedef
  87. boost::metaparse::foldl_reject_incomplete_start_with_parser<
  88. boost::metaparse::sequence<token::cmp, plus_exp>,
  89. plus_exp,
  90. semantic::binary_op
  91. >
  92. cmp_exp;
  93. struct expression : cmp_exp {};
  94. typedef
  95. boost::metaparse::transform<
  96. boost::metaparse::sequence<
  97. token::name,
  98. boost::metaparse::foldr_start_with_parser<
  99. token::name,
  100. boost::metaparse::last_of<token::define, expression>,
  101. semantic::lambda
  102. >
  103. >,
  104. semantic::pair
  105. >
  106. definition;
  107. typedef
  108. boost::metaparse::build_parser<
  109. boost::metaparse::entire_input<definition>
  110. >
  111. def_parser;
  112. }
  113. #endif