1 /*=============================================================================
2 Copyright (c) 2001-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 #ifndef BOOST_SPIRIT_SYMBOLS_HPP
9 #define BOOST_SPIRIT_SYMBOLS_HPP
11 ///////////////////////////////////////////////////////////////////////////////
14 #include <boost/ref.hpp>
16 #include <boost/spirit/home/classic/namespace.hpp>
17 #include <boost/spirit/home/classic/core/parser.hpp>
18 #include <boost/spirit/home/classic/core/composite/directives.hpp>
20 #include <boost/spirit/home/classic/symbols/symbols_fwd.hpp>
23 ///////////////////////////////////////////////////////////////////////////////
24 namespace boost { namespace spirit {
26 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
28 ///////////////////////////////////////////////////////////////////////////////
32 // This class implements a symbol table. The symbol table holds a
33 // dictionary of symbols where each symbol is a sequence of CharTs.
34 // The template class can work efficiently with 8, 16 and 32 bit
35 // characters. Mutable data of type T is associated with each
38 // The class is a parser. The parse member function returns
39 // additional information in the symbol_match class (see below).
40 // The additional data is a pointer to some data associated with
41 // the matching symbol.
43 // The actual set implementation is supplied by the SetT template
44 // parameter. By default, this uses the tst class (see tst.ipp).
46 // Symbols are added into the symbol table statically using the
49 // sym = a, b, c, d ...;
51 // where sym is a symbol table and a..d are strings. Example:
53 // sym = "pineapple", "orange", "banana", "apple";
55 // Alternatively, symbols may be added dynamically through the
56 // member functor 'add' (see symbol_inserter below). The member
57 // functor 'add' may be attached to a parser as a semantic action
58 // taking in a begin/end pair:
62 // where p is a parser (and sym is a symbol table). On success,
63 // the matching portion of the input is added to the symbol table.
65 // 'add' may also be used to directly initialize data. Examples:
67 // sym.add("hello", 1)("crazy", 2)("world", 3);
69 ///////////////////////////////////////////////////////////////////////////////
70 template <typename T, typename CharT, typename SetT>
73 , public parser<symbols<T, CharT, SetT> >
77 typedef parser<symbols<T, CharT, SetT> > parser_base_t;
78 typedef symbols<T, CharT, SetT> self_t;
79 typedef self_t const& embed_t;
80 typedef T symbol_data_t;
81 typedef boost::reference_wrapper<T> symbol_ref_t;
84 symbols(symbols const& other);
88 operator=(symbols const& other);
90 symbol_inserter<T, SetT> const&
91 operator=(CharT const* str);
93 template <typename ScannerT>
96 typedef typename match_result<ScannerT, symbol_ref_t>::type type;
99 template <typename ScannerT>
100 typename parser_result<self_t, ScannerT>::type
101 parse_main(ScannerT const& scan) const
103 typedef typename ScannerT::iterator_t iterator_t;
104 iterator_t first = scan.first;
105 typename SetT::search_info result = SetT::find(scan);
111 symbol_ref_t(*result.data),
115 return scan.no_match();
118 template <typename ScannerT>
119 typename parser_result<self_t, ScannerT>::type
120 parse(ScannerT const& scan) const
122 typedef typename parser_result<self_t, ScannerT>::type result_t;
123 return impl::implicit_lexeme_parse<result_t>
127 template < typename ScannerT >
128 T* find(ScannerT const& scan) const
129 { return SetT::find(scan).data; }
131 symbol_inserter<T, SetT> const add;
134 ///////////////////////////////////////////////////////////////////////////////
136 // Symbol table utilities
140 // adds a symbol 'sym' (string) to a symbol table 'table' plus an
141 // optional data 'data' associated with the symbol. Returns a pointer to
142 // the data associated with the symbol or NULL if add failed (e.g. when
143 // the symbol is already added before).
147 // finds a symbol 'sym' (string) from a symbol table 'table'. Returns a
148 // pointer to the data associated with the symbol or NULL if not found
150 ///////////////////////////////////////////////////////////////////////////////
151 template <typename T, typename CharT, typename SetT>
152 T* add(symbols<T, CharT, SetT>& table, CharT const* sym, T const& data = T());
154 template <typename T, typename CharT, typename SetT>
155 T* find(symbols<T, CharT, SetT> const& table, CharT const* sym);
157 ///////////////////////////////////////////////////////////////////////////////
159 // symbol_inserter class
161 // The symbols class holds an instance of this class named 'add'.
162 // This can be called directly just like a member function,
163 // passing in a first/last iterator and optional data:
165 // sym.add(first, last, data);
167 // Or, passing in a C string and optional data:
169 // sym.add(c_string, data);
171 // where sym is a symbol table. The 'data' argument is optional.
172 // This may also be used as a semantic action since it conforms
173 // to the action interface (see action.hpp):
177 ///////////////////////////////////////////////////////////////////////////////
178 template <typename T, typename SetT>
179 class symbol_inserter
183 symbol_inserter(SetT& set_)
186 typedef symbol_inserter const & result_type;
188 template <typename IteratorT>
189 symbol_inserter const&
190 operator()(IteratorT first, IteratorT const& last, T const& data = T()) const
192 set.add(first, last, data);
196 template <typename CharT>
197 symbol_inserter const&
198 operator()(CharT const* str, T const& data = T()) const
200 CharT const* last = str;
203 set.add(str, last, data);
207 template <typename CharT>
208 symbol_inserter const&
209 operator,(CharT const* str) const
211 CharT const* last = str;
214 set.add(str, last, T());
223 ///////////////////////////////////////////////////////////////////////////////
224 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
226 }} // namespace BOOST_SPIRIT_CLASSIC_NS
228 #include <boost/spirit/home/classic/symbols/impl/symbols.ipp>