1 /*============================================================================= 2 Copyright (c) 2001-2015 Joel de Guzman 3 4 Distributed under the Boost Software License, Version 1.0. (See accompanying 5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 =============================================================================*/ 7 #if !defined(BOOST_SPIRIT_X3_REPR_REXPR_DEF_HPP) 8 #define BOOST_SPIRIT_X3_REPR_REXPR_DEF_HPP 9 10 #include "ast.hpp" 11 #include "ast_adapted.hpp" 12 #include "error_handler.hpp" 13 #include "rexpr.hpp" 14 15 #include <boost/spirit/home/x3.hpp> 16 #include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp> 17 18 namespace rexpr { namespace parser 19 { 20 namespace x3 = boost::spirit::x3; 21 namespace ascii = boost::spirit::x3::ascii; 22 23 using x3::lit; 24 using x3::lexeme; 25 26 using ascii::char_; 27 using ascii::string; 28 29 /////////////////////////////////////////////////////////////////////////// 30 // Rule IDs 31 /////////////////////////////////////////////////////////////////////////// 32 33 struct rexpr_value_class; 34 struct rexpr_key_value_class; 35 struct rexpr_inner_class; 36 37 /////////////////////////////////////////////////////////////////////////// 38 // Rules 39 /////////////////////////////////////////////////////////////////////////// 40 41 x3::rule<rexpr_value_class, ast::rexpr_value> const 42 rexpr_value = "rexpr_value"; 43 44 x3::rule<rexpr_key_value_class, ast::rexpr_key_value> const 45 rexpr_key_value = "rexpr_key_value"; 46 47 x3::rule<rexpr_inner_class, ast::rexpr> const 48 rexpr_inner = "rexpr"; 49 50 rexpr_type const rexpr = "rexpr"; 51 52 /////////////////////////////////////////////////////////////////////////// 53 // Grammar 54 /////////////////////////////////////////////////////////////////////////// 55 56 auto const quoted_string = 57 lexeme['"' >> *(char_ - '"') >> '"']; 58 59 auto const rexpr_value_def = 60 quoted_string | rexpr_inner; 61 62 auto const rexpr_key_value_def = 63 quoted_string > '=' > rexpr_value; 64 65 auto const rexpr_inner_def = 66 '{' > *rexpr_key_value > '}'; 67 68 auto const rexpr_def = rexpr_inner_def; 69 70 BOOST_SPIRIT_DEFINE(rexpr_value, rexpr, rexpr_inner, rexpr_key_value); 71 72 /////////////////////////////////////////////////////////////////////////// 73 // Annotation and Error handling 74 /////////////////////////////////////////////////////////////////////////// 75 76 // We want these to be annotated with the iterator position. 77 struct rexpr_value_class : x3::annotate_on_success {}; 78 struct rexpr_key_value_class : x3::annotate_on_success {}; 79 struct rexpr_inner_class : x3::annotate_on_success {}; 80 81 // We want error-handling only for the start (outermost) rexpr 82 // rexpr is the same as rexpr_inner but without error-handling (see error_handler.hpp) 83 struct rexpr_class : x3::annotate_on_success, error_handler_base {}; 84 }} 85 86 namespace rexpr 87 { rexpr()88 parser::rexpr_type const& rexpr() 89 { 90 return parser::rexpr; 91 } 92 } 93 94 #endif 95