1 /*=============================================================================
2 Copyright (c) 1998-2003 Joel de Guzman
3 http://spirit.sourceforge.net/
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #if !defined(BOOST_SPIRIT_PARSER_HPP)
9 #define BOOST_SPIRIT_PARSER_HPP
11 #include <boost/config.hpp>
12 #include <boost/type_traits/remove_reference.hpp>
13 #include <boost/spirit/home/classic/namespace.hpp>
14 #include <boost/spirit/home/classic/core/scanner/scanner.hpp>
15 #include <boost/spirit/home/classic/core/nil.hpp>
17 namespace boost { namespace spirit {
19 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
21 template <typename ParserT, typename ActionT>
22 class action; // forward declaration
24 ///////////////////////////////////////////////////////////////////////////
28 // Helper template classes to distinguish different types of
29 // parsers. The following categories are the most generic. More
30 // specific types may inherit from these. Each parser has a typedef
31 // parser_category_t that defines its category. By default, if one
32 // is not specified, it will inherit from the base parser class
33 // which typedefs its parser_category_t as plain_parser_category.
35 // - plain parser has nothing special
36 // - binary parser has subject a and b (e.g. alternative)
37 // - unary parser has single subject (e.g. kleene star)
38 // - action parser has an attached action parser
40 ///////////////////////////////////////////////////////////////////////////
41 struct plain_parser_category {};
42 struct binary_parser_category : plain_parser_category {};
43 struct unary_parser_category : plain_parser_category {};
44 struct action_parser_category : unary_parser_category {};
46 ///////////////////////////////////////////////////////////////////////////
48 // parser_result metafunction
50 // Given a scanner type ScannerT and a parser type ParserT, the
51 // parser_result metafunction provides the actual result of the
56 // typename parser_result<ParserT, ScannerT>::type
58 ///////////////////////////////////////////////////////////////////////////
59 template <typename ParserT, typename ScannerT>
62 typedef typename boost::remove_reference<ParserT>::type parser_type;
63 typedef typename parser_type::template result<ScannerT>::type type;
66 ///////////////////////////////////////////////////////////////////////////
70 // This class is a protocol base class for all parsers. This is
71 // essentially an interface contract. The parser class does not
72 // really know how to parse anything but instead relies on the
73 // template parameter DerivedT (which obviously is assumed to be a
74 // subclass) to do the actual parsing.
76 // Concrete sub-classes inheriting from parser must have a
77 // corresponding member function parse(...) compatible with the
78 // conceptual Interface:
80 // template <typename ScannerT>
81 // RT parse(ScannerT const& scan) const;
83 // where RT is the desired return type of the parser and ScannerT
84 // scan is the scanner (see scanner.hpp).
86 // Concrete sub-classes inheriting from parser in most cases need to
87 // have a nested meta-function result that returns the result type
88 // of the parser's parse member function, given a scanner type. The
89 // meta-function has the form:
91 // template <typename ScannerT>
97 // where RT is the desired return type of the parser. This is
98 // usually, but not always, dependent on the template parameter
99 // ScannerT. If a parser does not supply a result metafunction, a
100 // default is provided by the base parser class.
102 // The parser's derived() member function returns a reference to the
103 // parser as its derived object.
105 // An operator[] is provided. The operator returns a semantic action
106 // handler (see actions.hpp).
108 // Each parser has a typedef embed_t. This typedef specifies how a
109 // parser is embedded in a composite (see composite.hpp). By
110 // default, if one is not specified, the parser will be embedded by
111 // value. That is, a copy of the parser is placed as a member
112 // variable of the composite. Most parsers are embedded by value. In
113 // certain situations however, this is not desirable or possible.
115 ///////////////////////////////////////////////////////////////////////////
116 template <typename DerivedT>
119 typedef DerivedT embed_t;
120 typedef DerivedT derived_t;
121 typedef plain_parser_category parser_category_t;
123 template <typename ScannerT>
126 typedef typename match_result<ScannerT, nil_t>::type type;
131 return *static_cast<DerivedT*>(this);
134 DerivedT const& derived() const
136 return *static_cast<DerivedT const*>(this);
139 template <typename ActionT>
140 action<DerivedT, ActionT>
141 operator[](ActionT const& actor) const
143 return action<DerivedT, ActionT>(derived(), actor);
147 ///////////////////////////////////////////////////////////////////////////
151 // Results returned by the free parse functions:
153 // stop: points to the final parse position (i.e parsing
154 // processed the input up to this point).
156 // hit: true if parsing is successful. This may be full:
157 // the parser consumed all the input, or partial:
158 // the parser consumed only a portion of the input.
160 // full: true when we have a full hit (i.e the parser
161 // consumed all the input.
163 // length: The number of characters consumed by the parser.
164 // This is valid only if we have a successful hit
165 // (either partial or full).
167 ///////////////////////////////////////////////////////////////////////////
168 template <typename IteratorT = char const*>
177 IteratorT const& stop_ = IteratorT(),
180 std::size_t length_ = 0)
186 template <typename ParseInfoT>
187 parse_info(ParseInfoT const& pi)
191 , length(pi.length) {}
194 ///////////////////////////////////////////////////////////////////////////
196 // Generic parse function
198 ///////////////////////////////////////////////////////////////////////////
199 template <typename IteratorT, typename DerivedT>
200 parse_info<IteratorT>
202 IteratorT const& first,
203 IteratorT const& last,
204 parser<DerivedT> const& p);
206 ///////////////////////////////////////////////////////////////////////////
208 // Parse function for null terminated strings
210 ///////////////////////////////////////////////////////////////////////////
211 template <typename CharT, typename DerivedT>
212 parse_info<CharT const*>
215 parser<DerivedT> const& p);
217 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
219 }} // namespace BOOST_SPIRIT_CLASSIC_NS
223 #include <boost/spirit/home/classic/core/impl/parser.ipp>