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 | #include <boost/spirit/home/x3.hpp> |
| 10 | #include "ast_handlers.hpp" |
| 11 | x3::rule<module_identifier_class, std::string> const module_identifier = "module_identifier"; |
| 12 | x3::rule<module_class, module_> const module = "module"; |
| 13 | x3::rule<node_identifier_class, std::string> const node_identifier = "node_identifier"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 14 | x3::rule<space_separator_class, x3::unused_type> const space_separator = "a space"; |
| 15 | x3::rule<completing_class, x3::unused_type> const completing = "completing"; |
| 16 | |
| 17 | // This is a pseudo-parser, that fails if we're not completing a command |
| 18 | auto const completing_def = |
Václav Kubernát | 76bb8c2 | 2022-01-19 01:32:31 +0100 | [diff] [blame] | 19 | x3::eps; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 20 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 21 | auto const node_identifier_def = |
Václav Kubernát | 76bb8c2 | 2022-01-19 01:32:31 +0100 | [diff] [blame] | 22 | ((x3::alpha | x3::char_("_")) >> *(x3::alnum | x3::char_("_") | x3::char_("-") | x3::char_("."))); |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 23 | |
| 24 | auto const module_def = |
Václav Kubernát | 76bb8c2 | 2022-01-19 01:32:31 +0100 | [diff] [blame] | 25 | module_identifier >> ':' >> !x3::space; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 26 | |
| 27 | auto const module_identifier_def = |
Václav Kubernát | 76bb8c2 | 2022-01-19 01:32:31 +0100 | [diff] [blame] | 28 | ((x3::alpha | x3::char_("_")) >> *(x3::alnum | x3::char_("_") | x3::char_("-") | x3::char_("."))); |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 29 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 30 | auto const space_separator_def = |
Václav Kubernát | 76bb8c2 | 2022-01-19 01:32:31 +0100 | [diff] [blame] | 31 | x3::omit[+x3::space]; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 32 | |
Václav Kubernát | e118f00 | 2020-05-14 22:54:13 +0200 | [diff] [blame] | 33 | template <typename CoerceTo> |
| 34 | struct as_type { |
| 35 | template <typename...> struct Tag{}; |
| 36 | |
| 37 | template <typename ParserType> |
| 38 | auto operator[](ParserType p) const { |
| 39 | return x3::rule<Tag<CoerceTo, ParserType>, CoerceTo> {"as"} = x3::as_parser(p); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | // The `as` parser creates an ad-hoc x3::rule with the attribute specified with `CoerceTo`. |
| 44 | // Example usage: as<std::string>[someParser] |
| 45 | // someParser will have its attribute coerced to std::string |
| 46 | // https://github.com/boostorg/spirit/issues/530#issuecomment-584836532 |
| 47 | template <typename CoerceTo> const as_type<CoerceTo> as{}; |
| 48 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 49 | BOOST_SPIRIT_DEFINE(node_identifier) |
| 50 | BOOST_SPIRIT_DEFINE(module) |
| 51 | BOOST_SPIRIT_DEFINE(module_identifier) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 52 | BOOST_SPIRIT_DEFINE(space_separator) |
| 53 | BOOST_SPIRIT_DEFINE(completing) |