source: NonGTP/Boost/boost/spirit/core/parser.hpp @ 857

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