Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Václav Kubernát <kubernat@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <boost/spirit/home/x3.hpp> |
| 11 | #include "ast_values.hpp" |
| 12 | #include "ast_handlers.hpp" |
| 13 | #include "common_parsers.hpp" |
| 14 | #include "schema.hpp" |
| 15 | namespace x3 = boost::spirit::x3; |
| 16 | |
| 17 | using x3::char_; |
| 18 | using x3::double_; |
| 19 | using x3::int8; |
| 20 | using x3::int16; |
| 21 | using x3::int32; |
| 22 | using x3::int64; |
| 23 | using x3::uint8; |
| 24 | using x3::uint16; |
| 25 | using x3::uint32; |
| 26 | using x3::uint64; |
| 27 | |
| 28 | x3::rule<leaf_data_class, leaf_data_> const leaf_data = "leaf_data"; |
| 29 | x3::rule<leaf_data_enum_class, enum_> const leaf_data_enum = "leaf_data_enum"; |
| 30 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Decimal>, double> const leaf_data_decimal = "leaf_data_decimal"; |
| 31 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Bool>, bool> const leaf_data_bool = "leaf_data_bool"; |
| 32 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Int8>, int8_t> const leaf_data_int8 = "leaf_data_int8"; |
| 33 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Uint8>, uint8_t> const leaf_data_uint8 = "leaf_data_uint8"; |
| 34 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Int16>, int16_t> const leaf_data_int16 = "leaf_data_int16"; |
| 35 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Uint16>, uint16_t> const leaf_data_uint16 = "leaf_data_uint16"; |
| 36 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Int32>, int32_t> const leaf_data_int32 = "leaf_data_int32"; |
| 37 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Uint32>, uint32_t> const leaf_data_uint32 = "leaf_data_uint32"; |
| 38 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Int64>, int64_t> const leaf_data_int64 = "leaf_data_int64"; |
| 39 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Uint64>, uint64_t> const leaf_data_uint64 = "leaf_data_uint64"; |
| 40 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::String>, std::string> const leaf_data_string = "leaf_data_string"; |
| 41 | x3::rule<leaf_data_binary_data_class, std::string> const leaf_data_binary_data = "leaf_data_binary_data"; |
| 42 | x3::rule<leaf_data_base_class<yang::LeafDataTypes::Binary>, binary_> const leaf_data_binary = "leaf_data_binary"; |
| 43 | x3::rule<leaf_data_identityRef_data_class, identityRef_> const leaf_data_identityRef_data = "leaf_data_identityRef_data"; |
| 44 | x3::rule<leaf_data_identityRef_class, identityRef_> const leaf_data_identityRef = "leaf_data_identityRef"; |
| 45 | |
| 46 | x3::rule<createSetSuggestions_class<yang::LeafDataTypes::Enum>, x3::unused_type> const createEnumSuggestions = "createEnumSuggestions"; |
| 47 | x3::rule<createSetSuggestions_class<yang::LeafDataTypes::IdentityRef>, x3::unused_type> const createIdentitySuggestions = "createIdentitySuggestions"; |
| 48 | |
| 49 | auto const createEnumSuggestions_def = |
| 50 | x3::eps; |
| 51 | |
| 52 | auto const leaf_data_enum_def = |
| 53 | createEnumSuggestions >> +char_; |
| 54 | |
| 55 | auto const leaf_data_decimal_def = |
| 56 | double_; |
| 57 | |
| 58 | struct bool_symbol_table : x3::symbols<bool> { |
| 59 | bool_symbol_table() |
| 60 | { |
| 61 | add |
| 62 | ("true", true) |
| 63 | ("false", false); |
| 64 | } |
| 65 | } const bool_rule; |
| 66 | |
| 67 | auto const leaf_data_bool_def = |
| 68 | bool_rule; |
| 69 | auto const leaf_data_int8_def = |
| 70 | int8; |
| 71 | auto const leaf_data_int16_def = |
| 72 | int16; |
| 73 | auto const leaf_data_int32_def = |
| 74 | int32; |
| 75 | auto const leaf_data_int64_def = |
| 76 | int64; |
| 77 | auto const leaf_data_uint8_def = |
| 78 | uint8; |
| 79 | auto const leaf_data_uint16_def = |
| 80 | uint16; |
| 81 | auto const leaf_data_uint32_def = |
| 82 | uint32; |
| 83 | auto const leaf_data_uint64_def = |
| 84 | uint64; |
| 85 | auto const leaf_data_string_def = |
| 86 | '\'' >> *(char_-'\'') >> '\'' | |
| 87 | '\"' >> *(char_-'\"') >> '\"'; |
| 88 | |
| 89 | // This intermediate rule is neccessary for coercing to std::string. |
| 90 | auto const leaf_data_binary_data_def = |
| 91 | +(x3::alnum | char_('+') | char_('/')) >> -char_('=') >> -char_('='); |
| 92 | |
| 93 | auto const leaf_data_binary_def = |
| 94 | leaf_data_binary_data; |
| 95 | |
| 96 | auto const leaf_data_identityRef_data_def = |
| 97 | -module >> node_identifier; |
| 98 | |
| 99 | auto const createIdentitySuggestions_def = |
| 100 | x3::eps; |
| 101 | |
| 102 | auto const leaf_data_identityRef_def = |
| 103 | createIdentitySuggestions >> leaf_data_identityRef_data; |
| 104 | |
| 105 | auto const leaf_data_def = |
| 106 | x3::no_skip[x3::expect[ |
| 107 | leaf_data_enum | |
| 108 | leaf_data_decimal | |
| 109 | leaf_data_bool | |
| 110 | leaf_data_int8 | |
| 111 | leaf_data_int16 | |
| 112 | leaf_data_int32 | |
| 113 | leaf_data_int64 | |
| 114 | leaf_data_uint8 | |
| 115 | leaf_data_uint16 | |
| 116 | leaf_data_uint32 | |
| 117 | leaf_data_uint64 | |
| 118 | leaf_data_binary | |
| 119 | leaf_data_identityRef | |
| 120 | leaf_data_string]]; |
| 121 | |
| 122 | BOOST_SPIRIT_DEFINE(leaf_data) |
| 123 | BOOST_SPIRIT_DEFINE(leaf_data_enum) |
| 124 | BOOST_SPIRIT_DEFINE(leaf_data_decimal) |
| 125 | BOOST_SPIRIT_DEFINE(leaf_data_bool) |
| 126 | BOOST_SPIRIT_DEFINE(leaf_data_int8) |
| 127 | BOOST_SPIRIT_DEFINE(leaf_data_int16) |
| 128 | BOOST_SPIRIT_DEFINE(leaf_data_int32) |
| 129 | BOOST_SPIRIT_DEFINE(leaf_data_int64) |
| 130 | BOOST_SPIRIT_DEFINE(leaf_data_uint8) |
| 131 | BOOST_SPIRIT_DEFINE(leaf_data_uint16) |
| 132 | BOOST_SPIRIT_DEFINE(leaf_data_uint32) |
| 133 | BOOST_SPIRIT_DEFINE(leaf_data_uint64) |
| 134 | BOOST_SPIRIT_DEFINE(leaf_data_string) |
| 135 | BOOST_SPIRIT_DEFINE(leaf_data_binary_data) |
| 136 | BOOST_SPIRIT_DEFINE(leaf_data_binary) |
| 137 | BOOST_SPIRIT_DEFINE(leaf_data_identityRef_data) |
| 138 | BOOST_SPIRIT_DEFINE(leaf_data_identityRef) |
| 139 | BOOST_SPIRIT_DEFINE(createEnumSuggestions) |
| 140 | BOOST_SPIRIT_DEFINE(createIdentitySuggestions) |