blob: 285234f6738555384a21544e1b5d7ae638732054 [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át52b90222022-04-27 11:29:54 +020016auto const node_identifier = x3::rule<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át52b90222022-04-27 11:29:54 +020019auto const module_identifier = x3::rule<module_identifier_class, std::string>{"module_identifier"} =
20 ((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{};