/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. #if !defined(BOOST_SPIRIT_LEX_STRING_TOKEN_DEF_MAR_28_2007_0722PM)
  6. #define BOOST_SPIRIT_LEX_STRING_TOKEN_DEF_MAR_28_2007_0722PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/support/common_terminals.hpp>
  11. #include <boost/spirit/home/support/string_traits.hpp>
  12. #include <boost/spirit/home/lex/domain.hpp>
  13. #include <boost/spirit/home/lex/lexer_type.hpp>
  14. #include <boost/spirit/home/lex/meta_compiler.hpp>
  15. #include <boost/type_traits/add_const.hpp>
  16. #include <boost/type_traits/add_reference.hpp>
  17. #include <boost/type_traits/remove_const.hpp>
  18. #include <boost/fusion/include/vector.hpp>
  19. #include <boost/fusion/include/at.hpp>
  20. namespace boost { namespace spirit
  21. {
  22. ///////////////////////////////////////////////////////////////////////////
  23. // Enablers
  24. ///////////////////////////////////////////////////////////////////////////
  25. template <typename T>
  26. struct use_terminal<lex::domain, T
  27. , typename enable_if<traits::is_string<T> >::type> // enables strings
  28. : mpl::true_ {};
  29. template <typename CharEncoding, typename A0>
  30. struct use_terminal<lex::domain
  31. , terminal_ex<
  32. tag::char_code<tag::string, CharEncoding> // enables string(str)
  33. , fusion::vector1<A0> >
  34. > : traits::is_string<A0> {};
  35. }}
  36. namespace boost { namespace spirit { namespace lex
  37. {
  38. // use string from standard character set by default
  39. using spirit::standard::string_type;
  40. using spirit::standard::string;
  41. ///////////////////////////////////////////////////////////////////////////
  42. //
  43. // string_token_def
  44. // represents a string based token definition
  45. //
  46. ///////////////////////////////////////////////////////////////////////////
  47. template <typename String, typename CharEncoding = char_encoding::standard>
  48. struct string_token_def
  49. : primitive_lexer<string_token_def<String, CharEncoding> >
  50. {
  51. typedef typename
  52. remove_const<typename traits::char_type_of<String>::type>::type
  53. char_type;
  54. typedef std::basic_string<char_type> string_type;
  55. string_token_def(typename add_reference<String>::type str)
  56. : str_(str), id_(std::size_t(~0)) {}
  57. template <typename LexerDef, typename State>
  58. void collect(LexerDef& lexdef, State const& state) const
  59. {
  60. typedef typename LexerDef::id_type id_type;
  61. if (std::size_t(~0) == id_)
  62. id_ = lexdef.get_next_id();
  63. unique_id_ = lexdef.add_token (state.c_str(), str_, id_);
  64. }
  65. template <typename LexerDef>
  66. void add_actions(LexerDef&) const {}
  67. std::size_t id() const { return id_; }
  68. std::size_t unique_id() const { return unique_id_; }
  69. string_type str_;
  70. mutable std::size_t id_;
  71. mutable std::size_t unique_id_;
  72. };
  73. ///////////////////////////////////////////////////////////////////////////
  74. // Lex generators: make_xxx function (objects)
  75. ///////////////////////////////////////////////////////////////////////////
  76. template <typename T, typename Modifiers>
  77. struct make_primitive<T, Modifiers
  78. , typename enable_if<traits::is_string<T> >::type>
  79. {
  80. typedef typename add_const<T>::type const_string;
  81. typedef string_token_def<const_string> result_type;
  82. result_type operator()(
  83. typename add_reference<const_string>::type str, unused_type) const
  84. {
  85. return result_type(str);
  86. }
  87. };
  88. template <typename Modifiers, typename CharEncoding, typename A0>
  89. struct make_primitive<
  90. terminal_ex<
  91. tag::char_code<tag::string, CharEncoding>
  92. , fusion::vector1<A0> >
  93. , Modifiers>
  94. {
  95. typedef typename add_const<A0>::type const_string;
  96. typedef string_token_def<const_string, CharEncoding> result_type;
  97. template <typename Terminal>
  98. result_type operator()(Terminal const& term, unused_type) const
  99. {
  100. return result_type(fusion::at_c<0>(term.args));
  101. }
  102. };
  103. }}} // namespace boost::spirit::lex
  104. #endif