OSSIA
Open Scenario System for Interactive Application
value_parse_impl.hpp
1 #pragma once
2 
3 #ifndef Q_MOC_RUN
4 //#define BOOST_SPIRIT_DEBUG
5 // see https://svn.boost.org/trac/boost/ticket/11875
6 #if defined(_GLIBCXX_DEBUG)
7 #define BOOST_PHOENIX_USING_LIBCPP
8 #endif
9 #include <ossia/network/base/name_validation.hpp>
10 #include <ossia/network/value/value.hpp>
11 
12 #include <boost/fusion/adapted.hpp>
13 #include <boost/fusion/adapted/std_array.hpp>
14 #include <boost/spirit/home/x3.hpp>
15 #endif
16 BOOST_FUSION_ADAPT_STRUCT(ossia::impulse)
17 namespace ossia::detail::parse
18 {
19 using namespace boost::fusion;
20 namespace x3 = boost::spirit::x3;
21 
22 using x3::char_;
23 using x3::int_;
24 using x3::real_parser;
25 using x3::skip;
26 
27 struct BoolParse_map : x3::symbols<bool>
28 {
29  BoolParse_map()
30  {
31  add("bool: true", true)("bool: false", false)("bool: 1", true)("bool: 0", false);
32  }
33 };
34 
35 const x3::rule<class value_, ossia::value> value_ = "value";
36 const x3::rule<class o_impulse_, ossia::impulse> o_impulse_ = "impulse";
37 const x3::rule<class o_str_, std::string> o_str_ = "str";
38 const x3::rule<class o_vec2_, std::array<float, 2>> o_vec2_ = "vec2";
39 const x3::rule<class o_vec3_, std::array<float, 3>> o_vec3_ = "vec3";
40 const x3::rule<class o_vec4_, std::array<float, 4>> o_vec4_ = "vec4";
41 const x3::rule<class o_list_, std::vector<ossia::value>> o_list_ = "list";
42 
43 struct EscapedChar : x3::symbols<const char>
44 {
45  EscapedChar() { add("\\\"", '\"'); }
46 };
47 using float_p = real_parser<float, x3::real_policies<float>>;
48 
49 const auto o_impulse__def = x3::lit("impulse")[([](auto&) { return ossia::impulse{}; })];
50 const auto o_str__def
51  = "string: \"" >> (x3::lexeme[*(EscapedChar() | (char_ - '"'))]) >> '"';
52 const auto o_vec2__def = "vec2f: [" >> float_p() >> "," >> float_p() >> "]";
53 const auto o_vec3__def
54  = "vec3f: [" >> float_p() >> "," >> float_p() >> "," >> float_p() >> "]";
55 const auto o_vec4__def = "vec4f: [" >> float_p() >> "," >> float_p() >> "," >> float_p()
56  >> "," >> float_p() >> "]";
57 const auto o_list__def = "list: [" >> *(value_ % x3::lit(",")) >> "]";
58 
59 BOOST_SPIRIT_DEFINE(o_impulse_)
60 BOOST_SPIRIT_DEFINE(o_str_)
61 BOOST_SPIRIT_DEFINE(o_vec2_)
62 BOOST_SPIRIT_DEFINE(o_vec3_)
63 BOOST_SPIRIT_DEFINE(o_vec4_)
64 BOOST_SPIRIT_DEFINE(o_list_)
65 
66 const auto value__def = ("float: " >> float_p()) | ("char: '" >> (char_ - "'") >> "'")
67  | ("int: " >> int_) | BoolParse_map{} | o_impulse_ | o_str_
68  | o_vec2_ | o_vec3_ | o_vec4_ | o_list_;
69 
70 BOOST_SPIRIT_DEFINE(value_)
71 
72 const x3::rule<class address_, std::string> address_ = "address";
73 const auto address__def = x3::lexeme[(
74  +("/" >> +x3::char_(std::string{ossia::net::name_characters()})) | "/")];
75 BOOST_SPIRIT_DEFINE(address_)
76 
77 const x3::rule<class preset_pair_, std::pair<std::string, ossia::value>> preset_pair_
78  = "preset_pair";
79 const auto preset_pair__def
80  = x3::lexeme[+x3::char_(std::string{ossia::net::path_characters()}) >> x3::lit("\t")]
81  >> value_;
82 BOOST_SPIRIT_DEFINE(preset_pair_)
83 
84 const x3::rule<class preset_, std::vector<std::pair<std::string, ossia::value>>> preset_
85  = "preset";
86 const auto preset__def = *(preset_pair_ % x3::lexeme[x3::eol]);
87 BOOST_SPIRIT_DEFINE(preset_)
88 }