|
||||
File indexing completed on 2025-01-31 10:01:56
0001 /*============================================================================= 0002 Copyright (c) 1998-2003 Joel de Guzman 0003 http://spirit.sourceforge.net/ 0004 0005 Distributed under the Boost Software License, Version 1.0. (See accompanying 0006 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 0007 =============================================================================*/ 0008 #if !defined(BOOST_SPIRIT_PARSER_HPP) 0009 #define BOOST_SPIRIT_PARSER_HPP 0010 0011 #include <boost/config.hpp> 0012 #include <boost/type_traits/remove_reference.hpp> 0013 #include <boost/spirit/home/classic/namespace.hpp> 0014 #include <boost/spirit/home/classic/core/scanner/scanner.hpp> 0015 #include <boost/spirit/home/classic/core/nil.hpp> 0016 0017 namespace boost { namespace spirit { 0018 0019 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN 0020 0021 template <typename ParserT, typename ActionT> 0022 class action; // forward declaration 0023 0024 /////////////////////////////////////////////////////////////////////////// 0025 // 0026 // Parser categories 0027 // 0028 // Helper template classes to distinguish different types of 0029 // parsers. The following categories are the most generic. More 0030 // specific types may inherit from these. Each parser has a typedef 0031 // parser_category_t that defines its category. By default, if one 0032 // is not specified, it will inherit from the base parser class 0033 // which typedefs its parser_category_t as plain_parser_category. 0034 // 0035 // - plain parser has nothing special 0036 // - binary parser has subject a and b (e.g. alternative) 0037 // - unary parser has single subject (e.g. kleene star) 0038 // - action parser has an attached action parser 0039 // 0040 /////////////////////////////////////////////////////////////////////////// 0041 struct plain_parser_category {}; 0042 struct binary_parser_category : plain_parser_category {}; 0043 struct unary_parser_category : plain_parser_category {}; 0044 struct action_parser_category : unary_parser_category {}; 0045 0046 /////////////////////////////////////////////////////////////////////////// 0047 // 0048 // parser_result metafunction 0049 // 0050 // Given a scanner type ScannerT and a parser type ParserT, the 0051 // parser_result metafunction provides the actual result of the 0052 // parser. 0053 // 0054 // Usage: 0055 // 0056 // typename parser_result<ParserT, ScannerT>::type 0057 // 0058 /////////////////////////////////////////////////////////////////////////// 0059 template <typename ParserT, typename ScannerT> 0060 struct parser_result 0061 { 0062 typedef typename boost::remove_reference<ParserT>::type parser_type; 0063 typedef typename parser_type::template result<ScannerT>::type type; 0064 }; 0065 0066 /////////////////////////////////////////////////////////////////////////// 0067 // 0068 // parser class 0069 // 0070 // This class is a protocol base class for all parsers. This is 0071 // essentially an interface contract. The parser class does not 0072 // really know how to parse anything but instead relies on the 0073 // template parameter DerivedT (which obviously is assumed to be a 0074 // subclass) to do the actual parsing. 0075 // 0076 // Concrete sub-classes inheriting from parser must have a 0077 // corresponding member function parse(...) compatible with the 0078 // conceptual Interface: 0079 // 0080 // template <typename ScannerT> 0081 // RT parse(ScannerT const& scan) const; 0082 // 0083 // where RT is the desired return type of the parser and ScannerT 0084 // scan is the scanner (see scanner.hpp). 0085 // 0086 // Concrete sub-classes inheriting from parser in most cases need to 0087 // have a nested meta-function result that returns the result type 0088 // of the parser's parse member function, given a scanner type. The 0089 // meta-function has the form: 0090 // 0091 // template <typename ScannerT> 0092 // struct result 0093 // { 0094 // typedef RT type; 0095 // }; 0096 // 0097 // where RT is the desired return type of the parser. This is 0098 // usually, but not always, dependent on the template parameter 0099 // ScannerT. If a parser does not supply a result metafunction, a 0100 // default is provided by the base parser class. 0101 // 0102 // The parser's derived() member function returns a reference to the 0103 // parser as its derived object. 0104 // 0105 // An operator[] is provided. The operator returns a semantic action 0106 // handler (see actions.hpp). 0107 // 0108 // Each parser has a typedef embed_t. This typedef specifies how a 0109 // parser is embedded in a composite (see composite.hpp). By 0110 // default, if one is not specified, the parser will be embedded by 0111 // value. That is, a copy of the parser is placed as a member 0112 // variable of the composite. Most parsers are embedded by value. In 0113 // certain situations however, this is not desirable or possible. 0114 // 0115 /////////////////////////////////////////////////////////////////////////// 0116 template <typename DerivedT> 0117 struct parser 0118 { 0119 typedef DerivedT embed_t; 0120 typedef DerivedT derived_t; 0121 typedef plain_parser_category parser_category_t; 0122 0123 template <typename ScannerT> 0124 struct result 0125 { 0126 typedef typename match_result<ScannerT, nil_t>::type type; 0127 }; 0128 0129 DerivedT& derived() 0130 { 0131 return *static_cast<DerivedT*>(this); 0132 } 0133 0134 DerivedT const& derived() const 0135 { 0136 return *static_cast<DerivedT const*>(this); 0137 } 0138 0139 template <typename ActionT> 0140 action<DerivedT, ActionT> 0141 operator[](ActionT const& actor) const 0142 { 0143 return action<DerivedT, ActionT>(derived(), actor); 0144 } 0145 }; 0146 0147 /////////////////////////////////////////////////////////////////////////// 0148 // 0149 // parse_info 0150 // 0151 // Results returned by the free parse functions: 0152 // 0153 // stop: points to the final parse position (i.e parsing 0154 // processed the input up to this point). 0155 // 0156 // hit: true if parsing is successful. This may be full: 0157 // the parser consumed all the input, or partial: 0158 // the parser consumed only a portion of the input. 0159 // 0160 // full: true when we have a full hit (i.e the parser 0161 // consumed all the input. 0162 // 0163 // length: The number of characters consumed by the parser. 0164 // This is valid only if we have a successful hit 0165 // (either partial or full). 0166 // 0167 /////////////////////////////////////////////////////////////////////////// 0168 template <typename IteratorT = char const*> 0169 struct parse_info 0170 { 0171 IteratorT stop; 0172 bool hit; 0173 bool full; 0174 std::size_t length; 0175 0176 parse_info( 0177 IteratorT const& stop_ = IteratorT(), 0178 bool hit_ = false, 0179 bool full_ = false, 0180 std::size_t length_ = 0) 0181 : stop(stop_) 0182 , hit(hit_) 0183 , full(full_) 0184 , length(length_) {} 0185 0186 template <typename ParseInfoT> 0187 parse_info(ParseInfoT const& pi) 0188 : stop(pi.stop) 0189 , hit(pi.hit) 0190 , full(pi.full) 0191 , length(pi.length) {} 0192 }; 0193 0194 /////////////////////////////////////////////////////////////////////////// 0195 // 0196 // Generic parse function 0197 // 0198 /////////////////////////////////////////////////////////////////////////// 0199 template <typename IteratorT, typename DerivedT> 0200 parse_info<IteratorT> 0201 parse( 0202 IteratorT const& first, 0203 IteratorT const& last, 0204 parser<DerivedT> const& p); 0205 0206 /////////////////////////////////////////////////////////////////////////// 0207 // 0208 // Parse function for null terminated strings 0209 // 0210 /////////////////////////////////////////////////////////////////////////// 0211 template <typename CharT, typename DerivedT> 0212 parse_info<CharT const*> 0213 parse( 0214 CharT const* str, 0215 parser<DerivedT> const& p); 0216 0217 BOOST_SPIRIT_CLASSIC_NAMESPACE_END 0218 0219 }} // namespace BOOST_SPIRIT_CLASSIC_NS 0220 0221 #endif 0222 0223 #include <boost/spirit/home/classic/core/impl/parser.ipp>
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |