1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
6 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
7 Software License, Version 1.0. (See accompanying file
8 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
11 #if !defined(CPP_EXPRESSION_GRAMMAR_HPP_099CD1A4_A6C0_44BE_8F24_0B00F5BE5674_INCLUDED)
12 #define CPP_EXPRESSION_GRAMMAR_HPP_099CD1A4_A6C0_44BE_8F24_0B00F5BE5674_INCLUDED
14 #include <boost/wave/wave_config.hpp>
16 #include <boost/assert.hpp>
17 #include <boost/spirit/include/classic_core.hpp>
18 #include <boost/spirit/include/classic_closure.hpp>
19 #include <boost/spirit/include/classic_if.hpp>
20 #include <boost/spirit/include/classic_assign_actor.hpp>
21 #include <boost/spirit/include/classic_push_back_actor.hpp>
23 #include <boost/spirit/include/phoenix1_functions.hpp>
24 #include <boost/spirit/include/phoenix1_operators.hpp>
25 #include <boost/spirit/include/phoenix1_primitives.hpp>
26 #include <boost/spirit/include/phoenix1_statements.hpp>
27 #include <boost/spirit/include/phoenix1_casts.hpp>
29 #include <boost/wave/token_ids.hpp>
31 #include <boost/wave/cpp_exceptions.hpp>
32 #include <boost/wave/grammars/cpp_expression_grammar_gen.hpp>
33 #include <boost/wave/grammars/cpp_literal_grammar_gen.hpp>
34 #include <boost/wave/grammars/cpp_expression_value.hpp>
35 #include <boost/wave/util/pattern_parser.hpp>
36 #include <boost/wave/util/macro_helpers.hpp>
38 #if !defined(spirit_append_actor)
39 #define spirit_append_actor(actor) boost::spirit::classic::push_back_a(actor)
40 #define spirit_assign_actor(actor) boost::spirit::classic::assign_a(actor)
41 #endif // !defined(spirit_append_actor)
43 // this must occur after all of the includes and before any code appears
44 #ifdef BOOST_HAS_ABI_HEADERS
45 #include BOOST_ABI_PREFIX
48 ///////////////////////////////////////////////////////////////////////////////
50 // Encapsulation of the grammar for evaluation of constant preprocessor
53 ///////////////////////////////////////////////////////////////////////////////
59 ///////////////////////////////////////////////////////////////////////////////
61 // define the closure type used throughout the C++ expression grammar
63 // Throughout this grammar all literal tokens are stored into a
64 // closure_value variables, which converts the types appropriately, where
67 ///////////////////////////////////////////////////////////////////////////////
68 struct cpp_expr_closure
69 : boost::spirit::classic::closure<cpp_expr_closure, closure_value>
74 } // namespace closures
78 ///////////////////////////////////////////////////////////////////////////////
80 // convert the given token value (integer literal) to a unsigned long
82 ///////////////////////////////////////////////////////////////////////////////
83 struct convert_intlit {
85 template <typename ArgT>
88 typedef boost::wave::grammars::closures::closure_value type;
91 template <typename TokenT>
92 boost::wave::grammars::closures::closure_value
93 operator()(TokenT const &token) const
95 typedef boost::wave::grammars::closures::closure_value return_type;
96 bool is_unsigned = false;
97 uint_literal_type ul = intlit_grammar_gen<TokenT>::evaluate(token,
101 return_type(ul) : return_type(static_cast<int_literal_type>(ul));
104 phoenix::function<convert_intlit> const as_intlit;
106 ///////////////////////////////////////////////////////////////////////////////
108 // Convert the given token value (character literal) to a unsigned int
110 ///////////////////////////////////////////////////////////////////////////////
111 struct convert_chlit {
113 template <typename ArgT>
116 typedef boost::wave::grammars::closures::closure_value type;
119 template <typename TokenT>
120 boost::wave::grammars::closures::closure_value
121 operator()(TokenT const &token) const
123 typedef boost::wave::grammars::closures::closure_value return_type;
124 value_error status = error_noerror;
126 // If the literal is a wchar_t and wchar_t is represented by a
127 // signed integral type, then the created value will be signed as
128 // well, otherwise we assume unsigned values.
129 #if BOOST_WAVE_WCHAR_T_SIGNEDNESS == BOOST_WAVE_WCHAR_T_AUTOSELECT
130 if ('L' == token.get_value()[0] && std::numeric_limits<wchar_t>::is_signed)
132 int value = chlit_grammar_gen<int, TokenT>::evaluate(token, status);
133 return return_type(value, status);
135 #elif BOOST_WAVE_WCHAR_T_SIGNEDNESS == BOOST_WAVE_WCHAR_T_FORCE_SIGNED
136 if ('L' == token.get_value()[0])
138 int value = chlit_grammar_gen<int, TokenT>::evaluate(token, status);
139 return return_type(value, status);
143 unsigned int value = chlit_grammar_gen<unsigned int, TokenT>::evaluate(token, status);
144 return return_type(value, status);
147 phoenix::function<convert_chlit> const as_chlit;
149 ////////////////////////////////////////////////////////////////////////////////
151 // Handle the ?: operator with correct type and error propagation
153 ////////////////////////////////////////////////////////////////////////////////
154 struct operator_questionmark {
156 template <typename CondT, typename Arg1T, typename Arg2T>
159 typedef boost::wave::grammars::closures::closure_value type;
162 template <typename CondT, typename Arg1T, typename Arg2T>
163 boost::wave::grammars::closures::closure_value
164 operator()(CondT const &cond, Arg1T &val1, Arg2T const &val2) const
166 return val1.handle_questionmark(cond, val2);
169 phoenix::function<operator_questionmark> const questionmark;
171 ///////////////////////////////////////////////////////////////////////////////
173 // Handle type conversion conserving error conditions
175 ///////////////////////////////////////////////////////////////////////////////
176 struct operator_to_bool {
178 template <typename ArgT>
181 typedef boost::wave::grammars::closures::closure_value type;
184 template <typename ArgT>
185 boost::wave::grammars::closures::closure_value
186 operator()(ArgT &val) const
188 typedef boost::wave::grammars::closures::closure_value return_type;
190 boost::wave::grammars::closures::as_bool(val), val.is_valid());
193 phoenix::function<operator_to_bool> const to_bool;
195 ///////////////////////////////////////////////////////////////////////////////
197 // Handle explicit type conversion
199 ///////////////////////////////////////////////////////////////////////////////
200 struct operator_as_bool {
202 template <typename ArgT>
208 template <typename ArgT>
210 operator()(ArgT &val) const
212 return boost::wave::grammars::closures::as_bool(val);
215 phoenix::function<operator_as_bool> const as_bool;
217 ///////////////////////////////////////////////////////////////////////////////
219 // Handle closure value operators with proper error propagation
221 ///////////////////////////////////////////////////////////////////////////////
222 #define BOOST_WAVE_BINARYOP(op, optok) \
223 struct operator_binary_ ## op { \
225 template <typename Arg1T, typename Arg2T> \
228 typedef boost::wave::grammars::closures::closure_value type; \
231 template <typename Arg1T, typename Arg2T> \
232 boost::wave::grammars::closures::closure_value \
233 operator()(Arg1T &val1, Arg2T &val2) const \
235 return val1 optok val2; \
238 phoenix::function<operator_binary_ ## op> const binary_ ## op \
241 BOOST_WAVE_BINARYOP(and, &&);
242 BOOST_WAVE_BINARYOP(or, ||);
244 BOOST_WAVE_BINARYOP(bitand, &);
245 BOOST_WAVE_BINARYOP(bitor, |);
246 BOOST_WAVE_BINARYOP(bitxor, ^);
248 BOOST_WAVE_BINARYOP(lesseq, <=);
249 BOOST_WAVE_BINARYOP(less, <);
250 BOOST_WAVE_BINARYOP(greater, >);
251 BOOST_WAVE_BINARYOP(greateq, >=);
252 BOOST_WAVE_BINARYOP(eq, ==);
253 BOOST_WAVE_BINARYOP(ne, !=);
255 #undef BOOST_WAVE_BINARYOP
257 ///////////////////////////////////////////////////////////////////////////////
258 #define BOOST_WAVE_UNARYOP(op, optok) \
259 struct operator_unary_ ## op { \
261 template <typename ArgT> \
264 typedef boost::wave::grammars::closures::closure_value type; \
267 template <typename ArgT> \
268 boost::wave::grammars::closures::closure_value \
269 operator()(ArgT &val) const \
274 phoenix::function<operator_unary_ ## op> const unary_ ## op \
277 BOOST_WAVE_UNARYOP(neg, !);
279 #undef BOOST_WAVE_UNARYOP
283 ///////////////////////////////////////////////////////////////////////////////
284 // define, whether the rule's should generate some debug output
285 #define TRACE_CPP_EXPR_GRAMMAR \
286 bool(BOOST_SPIRIT_DEBUG_FLAGS_CPP & BOOST_SPIRIT_DEBUG_FLAGS_CPP_EXPR_GRAMMAR) \
289 struct expression_grammar :
290 public boost::spirit::classic::grammar<
292 closures::cpp_expr_closure::context_t
297 BOOST_SPIRIT_DEBUG_TRACE_GRAMMAR_NAME(*this, "expression_grammar",
298 TRACE_CPP_EXPR_GRAMMAR);
301 // no need for copy constructor/assignment operator
302 expression_grammar(expression_grammar const&);
303 expression_grammar& operator= (expression_grammar const&);
305 template <typename ScannerT>
308 typedef closures::cpp_expr_closure closure_type;
309 typedef boost::spirit::classic::rule<ScannerT, closure_type::context_t> rule_t;
310 typedef boost::spirit::classic::rule<ScannerT> simple_rule_t;
312 simple_rule_t pp_expression;
315 rule_t logical_or_exp, logical_and_exp;
316 rule_t inclusive_or_exp, exclusive_or_exp, and_exp;
317 rule_t cmp_equality, cmp_relational;
319 rule_t add_exp, multiply_exp;
320 rule_t unary_exp, primary_exp, constant;
322 rule_t const_exp_nocalc;
323 rule_t logical_or_exp_nocalc, logical_and_exp_nocalc;
324 rule_t inclusive_or_exp_nocalc, exclusive_or_exp_nocalc, and_exp_nocalc;
325 rule_t cmp_equality_nocalc, cmp_relational_nocalc;
326 rule_t shift_exp_nocalc;
327 rule_t add_exp_nocalc, multiply_exp_nocalc;
328 rule_t unary_exp_nocalc, primary_exp_nocalc, constant_nocalc;
330 boost::spirit::classic::subrule<0, closure_type::context_t> const_exp_subrule;
332 definition(expression_grammar const &self)
334 using namespace boost::spirit::classic;
335 using namespace phoenix;
336 using namespace boost::wave;
337 using boost::wave::util::pattern_p;
340 = const_exp[self.val = arg1]
344 = logical_or_exp[const_exp.val = arg1]
345 >> !(const_exp_subrule =
346 ch_p(T_QUESTION_MARK)
349 const_exp_subrule.val = arg1
354 const_exp_subrule.val =
355 impl::questionmark(const_exp.val,
356 const_exp_subrule.val, arg1)
358 )[const_exp.val = arg1]
362 = logical_and_exp[logical_or_exp.val = arg1]
363 >> *( if_p(impl::as_bool(logical_or_exp.val))
365 // if one of the || operators is true, no more
366 // evaluation is required
367 pattern_p(T_OROR, MainTokenMask)
368 >> logical_and_exp_nocalc
371 impl::to_bool(logical_or_exp.val)
376 pattern_p(T_OROR, MainTokenMask)
380 impl::binary_or(logical_or_exp.val, arg1)
387 = inclusive_or_exp[logical_and_exp.val = arg1]
388 >> *( if_p(impl::as_bool(logical_and_exp.val))
390 pattern_p(T_ANDAND, MainTokenMask)
393 logical_and_exp.val =
394 impl::binary_and(logical_and_exp.val, arg1)
399 // if one of the && operators is false, no more
400 // evaluation is required
401 pattern_p(T_ANDAND, MainTokenMask)
402 >> inclusive_or_exp_nocalc
404 logical_and_exp.val =
405 impl::to_bool(logical_and_exp.val)
412 = exclusive_or_exp[inclusive_or_exp.val = arg1]
413 >> *( pattern_p(T_OR, MainTokenMask)
416 inclusive_or_exp.val =
417 impl::binary_bitor(inclusive_or_exp.val, arg1)
423 = and_exp[exclusive_or_exp.val = arg1]
424 >> *( pattern_p(T_XOR, MainTokenMask)
427 exclusive_or_exp.val =
428 impl::binary_bitxor(exclusive_or_exp.val, arg1)
434 = cmp_equality[and_exp.val = arg1]
435 >> *( pattern_p(T_AND, MainTokenMask)
439 impl::binary_bitand(and_exp.val, arg1)
445 = cmp_relational[cmp_equality.val = arg1]
450 impl::binary_eq(cmp_equality.val, arg1)
452 | pattern_p(T_NOTEQUAL, MainTokenMask)
456 impl::binary_ne(cmp_equality.val, arg1)
462 = shift_exp[cmp_relational.val = arg1]
463 >> *( ch_p(T_LESSEQUAL)
467 impl::binary_lesseq(cmp_relational.val, arg1)
469 | ch_p(T_GREATEREQUAL)
473 impl::binary_greateq(cmp_relational.val, arg1)
479 impl::binary_less(cmp_relational.val, arg1)
485 impl::binary_greater(cmp_relational.val, arg1)
491 = add_exp[shift_exp.val = arg1]
492 >> *( ch_p(T_SHIFTLEFT)
495 shift_exp.val <<= arg1
500 shift_exp.val >>= arg1
506 = multiply_exp[add_exp.val = arg1]
521 = unary_exp[multiply_exp.val = arg1]
525 multiply_exp.val *= arg1
530 multiply_exp.val /= arg1
535 multiply_exp.val %= arg1
541 = primary_exp[unary_exp.val = arg1]
542 | ch_p(T_PLUS) >> unary_exp
546 | ch_p(T_MINUS) >> unary_exp
548 unary_exp.val = -arg1
550 | pattern_p(T_COMPL, MainTokenMask) >> unary_exp
552 unary_exp.val = ~arg1
554 | pattern_p(T_NOT, MainTokenMask) >> unary_exp
556 unary_exp.val = impl::unary_neg(arg1)
561 = constant[primary_exp.val = arg1]
563 >> const_exp[primary_exp.val = arg1]
564 >> ch_p(T_RIGHTPAREN)
570 constant.val = impl::as_intlit(arg1)
574 constant.val = impl::as_intlit(arg1)
578 constant.val = impl::as_chlit(arg1)
582 // here follows the same grammar, but without any embedded
585 = logical_or_exp_nocalc
586 >> !( ch_p(T_QUESTION_MARK)
593 logical_or_exp_nocalc
594 = logical_and_exp_nocalc
595 >> *( pattern_p(T_OROR, MainTokenMask)
596 >> logical_and_exp_nocalc
600 logical_and_exp_nocalc
601 = inclusive_or_exp_nocalc
602 >> *( pattern_p(T_ANDAND, MainTokenMask)
603 >> inclusive_or_exp_nocalc
607 inclusive_or_exp_nocalc
608 = exclusive_or_exp_nocalc
609 >> *( pattern_p(T_OR, MainTokenMask)
610 >> exclusive_or_exp_nocalc
614 exclusive_or_exp_nocalc
616 >> *( pattern_p(T_XOR, MainTokenMask)
622 = cmp_equality_nocalc
623 >> *( pattern_p(T_AND, MainTokenMask)
624 >> cmp_equality_nocalc
629 = cmp_relational_nocalc
631 >> cmp_relational_nocalc
632 | pattern_p(T_NOTEQUAL, MainTokenMask)
633 >> cmp_relational_nocalc
637 cmp_relational_nocalc
639 >> *( ch_p(T_LESSEQUAL)
641 | ch_p(T_GREATEREQUAL)
652 >> *( ch_p(T_SHIFTLEFT)
660 = multiply_exp_nocalc
662 >> multiply_exp_nocalc
664 >> multiply_exp_nocalc
681 | ch_p(T_PLUS) >> unary_exp_nocalc
682 | ch_p(T_MINUS) >> unary_exp_nocalc
683 | pattern_p(T_COMPL, MainTokenMask) >> unary_exp_nocalc
684 | pattern_p(T_NOT, MainTokenMask) >> unary_exp_nocalc
691 >> ch_p(T_RIGHTPAREN)
700 BOOST_SPIRIT_DEBUG_TRACE_RULE(pp_expression, TRACE_CPP_EXPR_GRAMMAR);
701 BOOST_SPIRIT_DEBUG_TRACE_RULE(const_exp, TRACE_CPP_EXPR_GRAMMAR);
702 BOOST_SPIRIT_DEBUG_TRACE_RULE(logical_or_exp, TRACE_CPP_EXPR_GRAMMAR);
703 BOOST_SPIRIT_DEBUG_TRACE_RULE(logical_and_exp, TRACE_CPP_EXPR_GRAMMAR);
704 BOOST_SPIRIT_DEBUG_TRACE_RULE(inclusive_or_exp, TRACE_CPP_EXPR_GRAMMAR);
705 BOOST_SPIRIT_DEBUG_TRACE_RULE(exclusive_or_exp, TRACE_CPP_EXPR_GRAMMAR);
706 BOOST_SPIRIT_DEBUG_TRACE_RULE(and_exp, TRACE_CPP_EXPR_GRAMMAR);
707 BOOST_SPIRIT_DEBUG_TRACE_RULE(cmp_equality, TRACE_CPP_EXPR_GRAMMAR);
708 BOOST_SPIRIT_DEBUG_TRACE_RULE(cmp_relational, TRACE_CPP_EXPR_GRAMMAR);
709 BOOST_SPIRIT_DEBUG_TRACE_RULE(shift_exp, TRACE_CPP_EXPR_GRAMMAR);
710 BOOST_SPIRIT_DEBUG_TRACE_RULE(add_exp, TRACE_CPP_EXPR_GRAMMAR);
711 BOOST_SPIRIT_DEBUG_TRACE_RULE(multiply_exp, TRACE_CPP_EXPR_GRAMMAR);
712 BOOST_SPIRIT_DEBUG_TRACE_RULE(unary_exp, TRACE_CPP_EXPR_GRAMMAR);
713 BOOST_SPIRIT_DEBUG_TRACE_RULE(primary_exp, TRACE_CPP_EXPR_GRAMMAR);
714 BOOST_SPIRIT_DEBUG_TRACE_RULE(constant, TRACE_CPP_EXPR_GRAMMAR);
715 BOOST_SPIRIT_DEBUG_TRACE_RULE(const_exp_subrule, TRACE_CPP_EXPR_GRAMMAR);
717 BOOST_SPIRIT_DEBUG_TRACE_RULE(const_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
718 BOOST_SPIRIT_DEBUG_TRACE_RULE(logical_or_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
719 BOOST_SPIRIT_DEBUG_TRACE_RULE(logical_and_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
720 BOOST_SPIRIT_DEBUG_TRACE_RULE(inclusive_or_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
721 BOOST_SPIRIT_DEBUG_TRACE_RULE(exclusive_or_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
722 BOOST_SPIRIT_DEBUG_TRACE_RULE(and_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
723 BOOST_SPIRIT_DEBUG_TRACE_RULE(cmp_equality_nocalc, TRACE_CPP_EXPR_GRAMMAR);
724 BOOST_SPIRIT_DEBUG_TRACE_RULE(cmp_relational_nocalc, TRACE_CPP_EXPR_GRAMMAR);
725 BOOST_SPIRIT_DEBUG_TRACE_RULE(shift_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
726 BOOST_SPIRIT_DEBUG_TRACE_RULE(add_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
727 BOOST_SPIRIT_DEBUG_TRACE_RULE(multiply_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
728 BOOST_SPIRIT_DEBUG_TRACE_RULE(unary_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
729 BOOST_SPIRIT_DEBUG_TRACE_RULE(primary_exp_nocalc, TRACE_CPP_EXPR_GRAMMAR);
730 BOOST_SPIRIT_DEBUG_TRACE_RULE(constant_nocalc, TRACE_CPP_EXPR_GRAMMAR);
733 // start rule of this grammar
734 simple_rule_t const& start() const
735 { return pp_expression; }
739 ///////////////////////////////////////////////////////////////////////////////
740 #undef TRACE_CPP_EXPR_GRAMMAR
742 ///////////////////////////////////////////////////////////////////////////////
744 // The following function is defined here, to allow the separation of
745 // the compilation of the expression_grammar from the function using it.
747 ///////////////////////////////////////////////////////////////////////////////
749 #if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
750 #define BOOST_WAVE_EXPRGRAMMAR_GEN_INLINE
752 #define BOOST_WAVE_EXPRGRAMMAR_GEN_INLINE inline
755 template <typename TokenT>
756 BOOST_WAVE_EXPRGRAMMAR_GEN_INLINE
758 expression_grammar_gen<TokenT>::evaluate(
759 typename token_sequence_type::const_iterator const &first,
760 typename token_sequence_type::const_iterator const &last,
761 typename token_type::position_type const &act_pos,
762 bool if_block_status, value_error &status)
764 using namespace boost::spirit::classic;
765 using namespace boost::wave;
766 using namespace boost::wave::grammars::closures;
768 using boost::wave::util::impl::as_string;
770 typedef typename token_sequence_type::const_iterator iterator_type;
771 typedef typename token_sequence_type::value_type::string_type string_type;
773 parse_info<iterator_type> hit(first);
774 closure_value result; // expression result
776 #if !defined(BOOST_NO_EXCEPTIONS)
780 expression_grammar g; // expression grammar
781 hit = parse (first, last, g[spirit_assign_actor(result)],
782 ch_p(T_SPACE) | ch_p(T_CCOMMENT) | ch_p(T_CPPCOMMENT));
785 // expression is illformed
786 if (if_block_status) {
787 string_type expression = as_string<string_type>(first, last);
788 if (0 == expression.size())
789 expression = "<empty expression>";
790 BOOST_WAVE_THROW(preprocess_exception, ill_formed_expression,
791 expression.c_str(), act_pos);
795 // as the if_block_status is false no errors will be reported
800 #if !defined(BOOST_NO_EXCEPTIONS)
801 catch (boost::wave::preprocess_exception const& e) {
802 // expression is illformed
803 if (if_block_status) {
804 boost::throw_exception(e);
808 // as the if_block_status is false no errors will be reported
815 // The token list starts with a valid expression, but there remains
816 // something. If the remainder consists out of whitespace only, the
817 // expression is still valid.
818 iterator_type next = hit.stop;
820 while (next != last) {
821 switch (static_cast<unsigned int>(token_id(*next))) {
825 break; // ok continue
829 case T_CPPCOMMENT: // contains newline
830 return as_bool(result); // expression is valid
833 // expression is illformed
834 if (if_block_status) {
835 string_type expression = as_string<string_type>(first, last);
836 if (0 == expression.size())
837 expression = "<empty expression>";
838 BOOST_WAVE_THROW(preprocess_exception, ill_formed_expression,
839 expression.c_str(), act_pos);
843 // as the if_block_status is false no errors will be reported
851 if (error_noerror != result.is_valid()) // division or other error by zero occurred
852 status = result.is_valid();
854 // token sequence is a valid expression
855 return as_bool(result);
858 #undef BOOST_WAVE_EXPRGRAMMAR_GEN_INLINE
860 ///////////////////////////////////////////////////////////////////////////////
861 } // namespace grammars
865 // the suffix header occurs after all of the code
866 #ifdef BOOST_HAS_ABI_HEADERS
867 #include BOOST_ABI_SUFFIX
870 #endif // !defined(CPP_EXPRESSION_GRAMMAR_HPP_099CD1A4_A6C0_44BE_8F24_0B00F5BE5674_INCLUDED)