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" |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 11 | |
| 12 | // This is a pseudo-parser, that fails if we're not completing a command |
Václav Kubernát | 52b9022 | 2022-04-27 11:29:54 +0200 | [diff] [blame] | 13 | auto const completing = x3::rule<completing_class, x3::unused_type>{"completing"} = |
Václav Kubernát | 76bb8c2 | 2022-01-19 01:32:31 +0100 | [diff] [blame] | 14 | x3::eps; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 15 | |
Václav Kubernát | f20f6f9 | 2022-04-27 12:13:13 +0200 | [diff] [blame] | 16 | auto const node_identifier = x3::rule<struct node_identifier_class, std::string>{"node_identifier"} = |
Václav Kubernát | 76bb8c2 | 2022-01-19 01:32:31 +0100 | [diff] [blame] | 17 | ((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] | 18 | |
Václav Kubernát | f20f6f9 | 2022-04-27 12:13:13 +0200 | [diff] [blame] | 19 | auto const module_identifier = x3::rule<struct module_identifier_class, std::string>{"module_identifier"} = |
Václav Kubernát | 52b9022 | 2022-04-27 11:29:54 +0200 | [diff] [blame] | 20 | ((x3::alpha | x3::char_("_")) >> *(x3::alnum | x3::char_("_") | x3::char_("-") | x3::char_("."))); |
| 21 | |
| 22 | auto const module = x3::rule<module_class, module_>{"module"} = |
Václav Kubernát | 76bb8c2 | 2022-01-19 01:32:31 +0100 | [diff] [blame] | 23 | module_identifier >> ':' >> !x3::space; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 24 | |
Václav Kubernát | 52b9022 | 2022-04-27 11:29:54 +0200 | [diff] [blame] | 25 | auto const space_separator = x3::rule<space_separator_class, x3::unused_type>{"a space"} = |
Václav Kubernát | 76bb8c2 | 2022-01-19 01:32:31 +0100 | [diff] [blame] | 26 | x3::omit[+x3::space]; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 27 | |
Václav Kubernát | e118f00 | 2020-05-14 22:54:13 +0200 | [diff] [blame] | 28 | template <typename CoerceTo> |
| 29 | struct as_type { |
| 30 | template <typename...> struct Tag{}; |
| 31 | |
| 32 | template <typename ParserType> |
| 33 | auto operator[](ParserType p) const { |
| 34 | return x3::rule<Tag<CoerceTo, ParserType>, CoerceTo> {"as"} = x3::as_parser(p); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | // The `as` parser creates an ad-hoc x3::rule with the attribute specified with `CoerceTo`. |
| 39 | // Example usage: as<std::string>[someParser] |
| 40 | // someParser will have its attribute coerced to std::string |
| 41 | // https://github.com/boostorg/spirit/issues/530#issuecomment-584836532 |
| 42 | template <typename CoerceTo> const as_type<CoerceTo> as{}; |
Václav Kubernát | cd803c8 | 2022-04-27 11:57:01 +0200 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Returns a parser that generates suggestions based on a Completion set. |
| 46 | * Usage: |
| 47 | * const auto suggestSomeStuff = staticSuggestions({"some", "stuff", "yay"}); |
| 48 | * |
| 49 | * You can use this as a standard parser in a Spirit grammar. |
| 50 | */ |
| 51 | auto staticSuggestions(const std::initializer_list<std::string>& strings) |
| 52 | { |
| 53 | std::set<Completion> completions; |
| 54 | std::transform(begin(strings), end(strings), std::inserter(completions, completions.end()), |
| 55 | [](const auto s) { return Completion{s, " "}; }); |
| 56 | return as<x3::unused_type>[x3::eps[([completions](auto& ctx) { |
| 57 | auto& parserContext = x3::get<parser_context_tag>(ctx); |
| 58 | parserContext.m_suggestions = completions; |
| 59 | parserContext.m_completionIterator = _where(ctx).begin(); |
| 60 | })]]; |
| 61 | } |