blob: 14a2755a7a31814cd28a6cd4c84de99edc0642df [file] [log] [blame]
Václav Kubernát9ae8cc42020-03-25 19:17:41 +01001/*
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átd0ea9b22020-04-24 00:44:15 +020011
12// This is a pseudo-parser, that fails if we're not completing a command
Václav Kubernát52b90222022-04-27 11:29:54 +020013auto const completing = x3::rule<completing_class, x3::unused_type>{"completing"} =
Václav Kubernát76bb8c22022-01-19 01:32:31 +010014 x3::eps;
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020015
Václav Kubernátf20f6f92022-04-27 12:13:13 +020016auto const node_identifier = x3::rule<struct node_identifier_class, std::string>{"node_identifier"} =
Václav Kubernát76bb8c22022-01-19 01:32:31 +010017 ((x3::alpha | x3::char_("_")) >> *(x3::alnum | x3::char_("_") | x3::char_("-") | x3::char_(".")));
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010018
Václav Kubernátf20f6f92022-04-27 12:13:13 +020019auto const module_identifier = x3::rule<struct module_identifier_class, std::string>{"module_identifier"} =
Václav Kubernát52b90222022-04-27 11:29:54 +020020 ((x3::alpha | x3::char_("_")) >> *(x3::alnum | x3::char_("_") | x3::char_("-") | x3::char_(".")));
21
22auto const module = x3::rule<module_class, module_>{"module"} =
Václav Kubernát76bb8c22022-01-19 01:32:31 +010023 module_identifier >> ':' >> !x3::space;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010024
Václav Kubernát52b90222022-04-27 11:29:54 +020025auto const space_separator = x3::rule<space_separator_class, x3::unused_type>{"a space"} =
Václav Kubernát76bb8c22022-01-19 01:32:31 +010026 x3::omit[+x3::space];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020027
Václav Kubernáte118f002020-05-14 22:54:13 +020028template <typename CoerceTo>
29struct 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
42template <typename CoerceTo> const as_type<CoerceTo> as{};
Václav Kubernátcd803c82022-04-27 11:57:01 +020043
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 */
51auto 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}