blob: 794cc254c1d8527d284fa477fd8124c9dc96c921 [file] [log] [blame]
Václav Kubernátd6662962018-03-22 17:41:33 +01001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
Václav Kubernát2315c732018-05-16 20:25:55 +02008#include <ostream>
Václav Kubernát509ce652019-05-29 19:46:44 +02009#include "grammars.hpp"
Václav Kubernát48fc3832018-05-28 14:21:22 +020010#include "parser.hpp"
Václav Kubernát509ce652019-05-29 19:46:44 +020011#include "parser_context.hpp"
Václav Kubernát624a8872018-03-02 17:28:47 +010012
13TooManyArgumentsException::~TooManyArgumentsException() = default;
14
Václav Kubernátb96eef72018-05-04 19:10:22 +020015InvalidCommandException::~InvalidCommandException() = default;
16
Václav Kubernát2a9e1792018-05-28 12:53:48 +020017
Václav Kubernát43908fb2020-01-02 19:05:51 +010018Parser::Parser(const std::shared_ptr<const Schema> schema, const std::shared_ptr<const DataQuery> dataQuery)
Václav Kubernát48fc3832018-05-28 14:21:22 +020019 : m_schema(schema)
Václav Kubernát43908fb2020-01-02 19:05:51 +010020 , m_dataquery(dataQuery)
Václav Kubernát624a8872018-03-02 17:28:47 +010021{
Václav Kubernát82684cf2020-01-06 13:30:37 +010022 m_curDir.m_scope = Scope::Absolute;
Václav Kubernát624a8872018-03-02 17:28:47 +010023}
24
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010025bool Completions::operator==(const Completions& b) const
26{
27 return this->m_completions == b.m_completions && this->m_contextLength == b.m_contextLength;
28}
29
Václav Kubernátb61336d2018-05-28 17:35:03 +020030command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream)
Václav Kubernát624a8872018-03-02 17:28:47 +010031{
Václav Kubernátb61336d2018-05-28 17:35:03 +020032 command_ parsedCommand;
Václav Kubernát43908fb2020-01-02 19:05:51 +010033 ParserContext ctx(*m_schema, nullptr, m_curDir);
Václav Kubernát624a8872018-03-02 17:28:47 +010034 auto it = line.begin();
Václav Kubernátd6662962018-03-22 17:41:33 +010035
Václav Kubernát2315c732018-05-16 20:25:55 +020036 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
37
Václav Kubernátb96eef72018-05-04 19:10:22 +020038 auto grammar =
39 x3::with<parser_context_tag>(ctx)[
Václav Kubernátb61336d2018-05-28 17:35:03 +020040 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
Václav Kubernátb96eef72018-05-04 19:10:22 +020041 ];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020042 bool result = x3::phrase_parse(it, line.end(), grammar, x3::space, parsedCommand);
Václav Kubernátd6662962018-03-22 17:41:33 +010043
Václav Kubernátb96eef72018-05-04 19:10:22 +020044 if (!result || it != line.end()) {
45 throw InvalidCommandException(std::string(it, line.end()) + " this was left of input");
Václav Kubernát624a8872018-03-02 17:28:47 +010046 }
47
Václav Kubernátd6662962018-03-22 17:41:33 +010048 return parsedCommand;
Václav Kubernát624a8872018-03-02 17:28:47 +010049}
Václav Kubernát2a9e1792018-05-28 12:53:48 +020050
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010051Completions Parser::completeCommand(const std::string& line, std::ostream& errorStream) const
Václav Kubernát4108e0d2018-10-29 13:32:22 +010052{
53 std::set<std::string> completions;
54 command_ parsedCommand;
Václav Kubernát43908fb2020-01-02 19:05:51 +010055 ParserContext ctx(*m_schema, m_dataquery, m_curDir);
Václav Kubernátac035d62019-02-18 10:59:08 +010056 ctx.m_completing = true;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010057 auto it = line.begin();
58 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
59
60 auto grammar =
61 x3::with<parser_context_tag>(ctx)[
62 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
63 ];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020064 x3::phrase_parse(it, line.end(), grammar, x3::space, parsedCommand);
Václav Kubernát4108e0d2018-10-29 13:32:22 +010065
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010066 auto completionIterator = ctx.m_completionIterator ? *ctx.m_completionIterator : line.end();
67
68 int completionContext = line.end() - completionIterator;
69
Václav Kubernátcb3af402020-02-12 16:49:17 +010070 auto filtered = filterByPrefix(ctx.m_suggestions, std::string(completionIterator, line.end()));
71 if (filtered.size() == 1) {
Václav Kubernátdb26e9a2020-02-17 18:12:46 +010072 auto suffix = filtered.begin()->m_whenToAdd == Completion::WhenToAdd::Always
73 || filtered.begin()->m_value == std::string{completionIterator, line.end()}
Václav Kubernátcb3af402020-02-12 16:49:17 +010074 ? filtered.begin()->m_suffix
75 : "";
76 return {{filtered.begin()->m_value + suffix}, completionContext};
Václav Kubernát4c325482019-04-11 17:51:55 +020077 }
Václav Kubernátcb3af402020-02-12 16:49:17 +010078
79 std::set<std::string> res;
80 std::transform(filtered.begin(), filtered.end(), std::inserter(res, res.end()), [](auto it) { return it.m_value; });
81 return {res, completionContext};
Václav Kubernát4108e0d2018-10-29 13:32:22 +010082}
83
Václav Kubernát2eaceb82018-10-08 19:56:30 +020084void Parser::changeNode(const dataPath_& name)
Václav Kubernát2a9e1792018-05-28 12:53:48 +020085{
Václav Kubernát37171a12018-08-31 17:01:48 +020086 if (name.m_scope == Scope::Absolute) {
87 m_curDir = name;
88 } else {
89 for (const auto& it : name.m_nodes) {
90 if (it.m_suffix.type() == typeid(nodeup_))
91 m_curDir.m_nodes.pop_back();
92 else
93 m_curDir.m_nodes.push_back(it);
94 }
Václav Kubernát2a9e1792018-05-28 12:53:48 +020095 }
96}
Václav Kubernát814fa412018-05-25 19:47:18 +020097
Václav Kubernát48fc3832018-05-28 14:21:22 +020098std::string Parser::currentNode() const
Václav Kubernát2a9e1792018-05-28 12:53:48 +020099{
Václav Kubernátefcac932020-01-10 15:26:32 +0100100 return pathToDataString(m_curDir, Prefixes::WhenNeeded);
Václav Kubernát2a9e1792018-05-28 12:53:48 +0200101}
Václav Kubernát11afac72018-07-18 14:59:53 +0200102
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200103struct getSchemaPathVisitor : boost::static_visitor<schemaPath_> {
104 schemaPath_ operator()(const dataPath_& path) const
105 {
106 return dataPathToSchemaPath(path);
107 }
108
109 schemaPath_ operator()(const schemaPath_& path) const
110 {
111 return path;
112 }
Václav Kubernátbeaa8aa2020-04-29 22:39:34 +0200113
114 [[noreturn]] schemaPath_ operator()([[maybe_unused]] const module_& path) const
115 {
116 throw std::logic_error("getSchemaPathVisitor: Tried getting a schema path from a module");
117 }
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200118};
119
Václav Kubernát5c75b252018-10-10 18:33:47 +0200120
Václav Kubernátbeaa8aa2020-04-29 22:39:34 +0200121std::set<std::string> Parser::availableNodes(const boost::optional<boost::variant<dataPath_, schemaPath_, module_>>& path, const Recursion& option) const
Václav Kubernát11afac72018-07-18 14:59:53 +0200122{
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200123 auto pathArg = dataPathToSchemaPath(m_curDir);
124 if (path) {
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200125 if (path->type() == typeid(module_)) {
126 return m_schema->moduleNodes(boost::get<module_>(*path), option);
127 }
128
Václav Kubernátbeaa8aa2020-04-29 22:39:34 +0200129 auto schemaPath = boost::apply_visitor(getSchemaPathVisitor(), *path);
Václav Kubernát1446fe12019-10-02 19:32:51 +0200130 if (schemaPath.m_scope == Scope::Absolute) {
131 pathArg = schemaPath;
132 } else {
133 pathArg.m_nodes.insert(pathArg.m_nodes.end(), schemaPath.m_nodes.begin(), schemaPath.m_nodes.end());
134 }
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200135 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200136 return m_schema->childNodes(pathArg, option);
Václav Kubernát11afac72018-07-18 14:59:53 +0200137}