blob: 71aedb9fd7aa4da40c80fd6029d774104b561be6 [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"
11x3::rule<module_identifier_class, std::string> const module_identifier = "module_identifier";
12x3::rule<module_class, module_> const module = "module";
13x3::rule<node_identifier_class, std::string> const node_identifier = "node_identifier";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020014x3::rule<space_separator_class, x3::unused_type> const space_separator = "a space";
15x3::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
18auto const completing_def =
19 x3::no_skip[x3::eps];
20
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010021auto const node_identifier_def =
22 x3::lexeme[
23 ((x3::alpha | x3::char_("_")) >> *(x3::alnum | x3::char_("_") | x3::char_("-") | x3::char_(".")))
24 ];
25
26auto const module_def =
27 module_identifier >> x3::no_skip[':'] >> !x3::no_skip[x3::space];
28
29auto const module_identifier_def =
30 x3::lexeme[
31 ((x3::alpha | x3::char_("_")) >> *(x3::alnum | x3::char_("_") | x3::char_("-") | x3::char_(".")))
32 ];
33
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020034auto const space_separator_def =
Václav Kubernátb8bad062021-02-04 00:02:45 +010035 x3::omit[x3::no_skip[+x3::space]];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020036
Václav Kubernáte118f002020-05-14 22:54:13 +020037template <typename CoerceTo>
38struct as_type {
39 template <typename...> struct Tag{};
40
41 template <typename ParserType>
42 auto operator[](ParserType p) const {
43 return x3::rule<Tag<CoerceTo, ParserType>, CoerceTo> {"as"} = x3::as_parser(p);
44 }
45};
46
47// The `as` parser creates an ad-hoc x3::rule with the attribute specified with `CoerceTo`.
48// Example usage: as<std::string>[someParser]
49// someParser will have its attribute coerced to std::string
50// https://github.com/boostorg/spirit/issues/530#issuecomment-584836532
51template <typename CoerceTo> const as_type<CoerceTo> as{};
52
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010053BOOST_SPIRIT_DEFINE(node_identifier)
54BOOST_SPIRIT_DEFINE(module)
55BOOST_SPIRIT_DEFINE(module_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020056BOOST_SPIRIT_DEFINE(space_separator)
57BOOST_SPIRIT_DEFINE(completing)