PageRenderTime 27ms CodeModel.GetById 10ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/contrib/boost/spirit/home/lex/lexer/string_token_def.hpp

http://pythonocc.googlecode.com/
C++ Header | 123 lines | 87 code | 19 blank | 17 comment | 2 complexity | 6a6954a3ecf3f11f5388bbfc3103d12a MD5 | raw file
  1//  Copyright (c) 2001-2010 Hartmut Kaiser
  2// 
  3//  Distributed under the Boost Software License, Version 1.0. (See accompanying 
  4//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5
  6#if !defined(BOOST_SPIRIT_LEX_STRING_TOKEN_DEF_MAR_28_2007_0722PM)
  7#define BOOST_SPIRIT_LEX_STRING_TOKEN_DEF_MAR_28_2007_0722PM
  8
  9#if defined(_MSC_VER)
 10#pragma once
 11#endif
 12
 13#include <boost/spirit/home/support/common_terminals.hpp>
 14#include <boost/spirit/home/support/string_traits.hpp>
 15#include <boost/spirit/home/lex/domain.hpp>
 16#include <boost/spirit/home/lex/lexer_type.hpp>
 17#include <boost/spirit/home/lex/meta_compiler.hpp>
 18#include <boost/type_traits/add_const.hpp>
 19#include <boost/type_traits/add_reference.hpp>
 20#include <boost/type_traits/remove_const.hpp>
 21#include <boost/fusion/include/vector.hpp>
 22#include <boost/fusion/include/at.hpp>
 23
 24namespace boost { namespace spirit
 25{
 26    ///////////////////////////////////////////////////////////////////////////
 27    // Enablers
 28    ///////////////////////////////////////////////////////////////////////////
 29    template <typename T>
 30    struct use_terminal<lex::domain, T
 31      , typename enable_if<traits::is_string<T> >::type> // enables strings
 32      : mpl::true_ {};
 33
 34    template <typename CharEncoding, typename A0>
 35    struct use_terminal<lex::domain
 36      , terminal_ex<
 37            tag::char_code<tag::string, CharEncoding>   // enables string(str)
 38          , fusion::vector1<A0> >
 39    > : traits::is_string<A0> {};
 40
 41}}
 42
 43namespace boost { namespace spirit { namespace lex
 44{ 
 45    // use string from standard character set by default
 46    using spirit::standard::string_type;
 47    using spirit::standard::string;
 48
 49    ///////////////////////////////////////////////////////////////////////////
 50    //
 51    //  string_token_def 
 52    //      represents a string based token definition
 53    //
 54    ///////////////////////////////////////////////////////////////////////////
 55    template <typename String, typename CharEncoding = char_encoding::standard>
 56    struct string_token_def
 57      : primitive_lexer<string_token_def<String, CharEncoding> >
 58    {
 59        typedef typename
 60            remove_const<typename traits::char_type_of<String>::type>::type
 61        char_type;
 62        typedef std::basic_string<char_type> string_type;
 63
 64        string_token_def(typename add_reference<String>::type str)
 65          : str_(str), id_(std::size_t(~0)) {}
 66
 67        template <typename LexerDef, typename State>
 68        void collect(LexerDef& lexdef, State const& state) const
 69        {
 70            typedef typename LexerDef::id_type id_type;
 71            if (std::size_t(~0) == id_)
 72                id_ = lexdef.get_next_id();
 73            unique_id_ = lexdef.add_token (state.c_str(), str_, id_);
 74        }
 75
 76        template <typename LexerDef>
 77        void add_actions(LexerDef&) const {}
 78
 79        std::size_t id() const { return id_; }
 80        std::size_t unique_id() const { return unique_id_; }
 81
 82        string_type str_;
 83        mutable std::size_t id_;
 84        mutable std::size_t unique_id_;
 85    };
 86
 87    ///////////////////////////////////////////////////////////////////////////
 88    // Lex generators: make_xxx function (objects)
 89    ///////////////////////////////////////////////////////////////////////////
 90    template <typename T, typename Modifiers>
 91    struct make_primitive<T, Modifiers
 92      , typename enable_if<traits::is_string<T> >::type>
 93    {
 94        typedef typename add_const<T>::type const_string;
 95        typedef string_token_def<const_string> result_type;
 96
 97        result_type operator()(
 98            typename add_reference<const_string>::type str, unused_type) const
 99        {
100            return result_type(str);
101        }
102    };
103
104    template <typename Modifiers, typename CharEncoding, typename A0>
105    struct make_primitive<
106        terminal_ex<
107            tag::char_code<tag::string, CharEncoding>
108          , fusion::vector1<A0> >
109      , Modifiers>
110    {
111        typedef typename add_const<A0>::type const_string;
112        typedef string_token_def<const_string, CharEncoding> result_type;
113
114        template <typename Terminal>
115        result_type operator()(Terminal const& term, unused_type) const
116        {
117            return result_type(fusion::at_c<0>(term.args));
118        }
119    };
120
121}}}  // namespace boost::spirit::lex
122
123#endif