parser.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_PARSER_HPP)
  8. #define BOOST_SPIRIT_PARSER_HPP
  9. #include <boost/config.hpp>
  10. #include <boost/type_traits/remove_reference.hpp>
  11. #include <boost/spirit/home/classic/namespace.hpp>
  12. #include <boost/spirit/home/classic/core/scanner/scanner.hpp>
  13. #include <boost/spirit/home/classic/core/nil.hpp>
  14. namespace boost { namespace spirit {
  15. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  16. template <typename ParserT, typename ActionT>
  17. class action; // forward declaration
  18. ///////////////////////////////////////////////////////////////////////////
  19. //
  20. // Parser categories
  21. //
  22. // Helper template classes to distinguish different types of
  23. // parsers. The following categories are the most generic. More
  24. // specific types may inherit from these. Each parser has a typedef
  25. // parser_category_t that defines its category. By default, if one
  26. // is not specified, it will inherit from the base parser class
  27. // which typedefs its parser_category_t as plain_parser_category.
  28. //
  29. // - plain parser has nothing special
  30. // - binary parser has subject a and b (e.g. alternative)
  31. // - unary parser has single subject (e.g. kleene star)
  32. // - action parser has an attached action parser
  33. //
  34. ///////////////////////////////////////////////////////////////////////////
  35. struct plain_parser_category {};
  36. struct binary_parser_category : plain_parser_category {};
  37. struct unary_parser_category : plain_parser_category {};
  38. struct action_parser_category : unary_parser_category {};
  39. ///////////////////////////////////////////////////////////////////////////
  40. //
  41. // parser_result metafunction
  42. //
  43. // Given a scanner type ScannerT and a parser type ParserT, the
  44. // parser_result metafunction provides the actual result of the
  45. // parser.
  46. //
  47. // Usage:
  48. //
  49. // typename parser_result<ParserT, ScannerT>::type
  50. //
  51. ///////////////////////////////////////////////////////////////////////////
  52. template <typename ParserT, typename ScannerT>
  53. struct parser_result
  54. {
  55. typedef typename boost::remove_reference<ParserT>::type parser_type;
  56. typedef typename parser_type::template result<ScannerT>::type type;
  57. };
  58. ///////////////////////////////////////////////////////////////////////////
  59. //
  60. // parser class
  61. //
  62. // This class is a protocol base class for all parsers. This is
  63. // essentially an interface contract. The parser class does not
  64. // really know how to parse anything but instead relies on the
  65. // template parameter DerivedT (which obviously is assumed to be a
  66. // subclass) to do the actual parsing.
  67. //
  68. // Concrete sub-classes inheriting from parser must have a
  69. // corresponding member function parse(...) compatible with the
  70. // conceptual Interface:
  71. //
  72. // template <typename ScannerT>
  73. // RT parse(ScannerT const& scan) const;
  74. //
  75. // where RT is the desired return type of the parser and ScannerT
  76. // scan is the scanner (see scanner.hpp).
  77. //
  78. // Concrete sub-classes inheriting from parser in most cases need to
  79. // have a nested meta-function result that returns the result type
  80. // of the parser's parse member function, given a scanner type. The
  81. // meta-function has the form:
  82. //
  83. // template <typename ScannerT>
  84. // struct result
  85. // {
  86. // typedef RT type;
  87. // };
  88. //
  89. // where RT is the desired return type of the parser. This is
  90. // usually, but not always, dependent on the template parameter
  91. // ScannerT. If a parser does not supply a result metafunction, a
  92. // default is provided by the base parser class.
  93. //
  94. // The parser's derived() member function returns a reference to the
  95. // parser as its derived object.
  96. //
  97. // An operator[] is provided. The operator returns a semantic action
  98. // handler (see actions.hpp).
  99. //
  100. // Each parser has a typedef embed_t. This typedef specifies how a
  101. // parser is embedded in a composite (see composite.hpp). By
  102. // default, if one is not specified, the parser will be embedded by
  103. // value. That is, a copy of the parser is placed as a member
  104. // variable of the composite. Most parsers are embedded by value. In
  105. // certain situations however, this is not desirable or possible.
  106. //
  107. ///////////////////////////////////////////////////////////////////////////
  108. template <typename DerivedT>
  109. struct parser
  110. {
  111. typedef DerivedT embed_t;
  112. typedef DerivedT derived_t;
  113. typedef plain_parser_category parser_category_t;
  114. template <typename ScannerT>
  115. struct result
  116. {
  117. typedef typename match_result<ScannerT, nil_t>::type type;
  118. };
  119. DerivedT& derived()
  120. {
  121. return *static_cast<DerivedT*>(this);
  122. }
  123. DerivedT const& derived() const
  124. {
  125. return *static_cast<DerivedT const*>(this);
  126. }
  127. template <typename ActionT>
  128. action<DerivedT, ActionT>
  129. operator[](ActionT const& actor) const
  130. {
  131. return action<DerivedT, ActionT>(derived(), actor);
  132. }
  133. };
  134. ///////////////////////////////////////////////////////////////////////////
  135. //
  136. // parse_info
  137. //
  138. // Results returned by the free parse functions:
  139. //
  140. // stop: points to the final parse position (i.e parsing
  141. // processed the input up to this point).
  142. //
  143. // hit: true if parsing is successful. This may be full:
  144. // the parser consumed all the input, or partial:
  145. // the parser consumed only a portion of the input.
  146. //
  147. // full: true when we have a full hit (i.e the parser
  148. // consumed all the input.
  149. //
  150. // length: The number of characters consumed by the parser.
  151. // This is valid only if we have a successful hit
  152. // (either partial or full).
  153. //
  154. ///////////////////////////////////////////////////////////////////////////
  155. template <typename IteratorT = char const*>
  156. struct parse_info
  157. {
  158. IteratorT stop;
  159. bool hit;
  160. bool full;
  161. std::size_t length;
  162. parse_info(
  163. IteratorT const& stop_ = IteratorT(),
  164. bool hit_ = false,
  165. bool full_ = false,
  166. std::size_t length_ = 0)
  167. : stop(stop_)
  168. , hit(hit_)
  169. , full(full_)
  170. , length(length_) {}
  171. template <typename ParseInfoT>
  172. parse_info(ParseInfoT const& pi)
  173. : stop(pi.stop)
  174. , hit(pi.hit)
  175. , full(pi.full)
  176. , length(pi.length) {}
  177. };
  178. ///////////////////////////////////////////////////////////////////////////
  179. //
  180. // Generic parse function
  181. //
  182. ///////////////////////////////////////////////////////////////////////////
  183. template <typename IteratorT, typename DerivedT>
  184. parse_info<IteratorT>
  185. parse(
  186. IteratorT const& first,
  187. IteratorT const& last,
  188. parser<DerivedT> const& p);
  189. ///////////////////////////////////////////////////////////////////////////
  190. //
  191. // Parse function for null terminated strings
  192. //
  193. ///////////////////////////////////////////////////////////////////////////
  194. template <typename CharT, typename DerivedT>
  195. parse_info<CharT const*>
  196. parse(
  197. CharT const* str,
  198. parser<DerivedT> const& p);
  199. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  200. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  201. #endif
  202. #include <boost/spirit/home/classic/core/impl/parser.ipp>