blob: c3dc088321bb3754627728d9590babbf20da3a45 [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áta10d8b62018-06-13 17:42:46 +020018Parser::Parser(const std::shared_ptr<const Schema> schema)
Václav Kubernát48fc3832018-05-28 14:21:22 +020019 : m_schema(schema)
Václav Kubernát624a8872018-03-02 17:28:47 +010020{
Václav Kubernát82684cf2020-01-06 13:30:37 +010021 m_curDir.m_scope = Scope::Absolute;
Václav Kubernát624a8872018-03-02 17:28:47 +010022}
23
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010024bool Completions::operator==(const Completions& b) const
25{
26 return this->m_completions == b.m_completions && this->m_contextLength == b.m_contextLength;
27}
28
Václav Kubernátb61336d2018-05-28 17:35:03 +020029command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream)
Václav Kubernát624a8872018-03-02 17:28:47 +010030{
Václav Kubernátb61336d2018-05-28 17:35:03 +020031 command_ parsedCommand;
Václav Kubernát72749c62020-01-03 16:47:34 +010032 ParserContext ctx(*m_schema, m_curDir);
Václav Kubernát624a8872018-03-02 17:28:47 +010033 auto it = line.begin();
Václav Kubernátd6662962018-03-22 17:41:33 +010034
Václav Kubernát2315c732018-05-16 20:25:55 +020035 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
36
Václav Kubernátb96eef72018-05-04 19:10:22 +020037 auto grammar =
38 x3::with<parser_context_tag>(ctx)[
Václav Kubernátb61336d2018-05-28 17:35:03 +020039 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
Václav Kubernátb96eef72018-05-04 19:10:22 +020040 ];
41 bool result = x3::phrase_parse(it, line.end(), grammar, space, parsedCommand);
Václav Kubernátd6662962018-03-22 17:41:33 +010042
Václav Kubernátb96eef72018-05-04 19:10:22 +020043 if (!result || it != line.end()) {
44 throw InvalidCommandException(std::string(it, line.end()) + " this was left of input");
Václav Kubernát624a8872018-03-02 17:28:47 +010045 }
46
Václav Kubernátd6662962018-03-22 17:41:33 +010047 return parsedCommand;
Václav Kubernát624a8872018-03-02 17:28:47 +010048}
Václav Kubernát2a9e1792018-05-28 12:53:48 +020049
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010050Completions Parser::completeCommand(const std::string& line, std::ostream& errorStream) const
Václav Kubernát4108e0d2018-10-29 13:32:22 +010051{
52 std::set<std::string> completions;
53 command_ parsedCommand;
Václav Kubernát72749c62020-01-03 16:47:34 +010054 ParserContext ctx(*m_schema, m_curDir);
Václav Kubernátac035d62019-02-18 10:59:08 +010055 ctx.m_completing = true;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010056 auto it = line.begin();
57 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
58
59 auto grammar =
60 x3::with<parser_context_tag>(ctx)[
61 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
62 ];
63 x3::phrase_parse(it, line.end(), grammar, space, parsedCommand);
64
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010065 auto completionIterator = ctx.m_completionIterator ? *ctx.m_completionIterator : line.end();
66
67 int completionContext = line.end() - completionIterator;
68
69 auto set = filterByPrefix(ctx.m_suggestions, std::string(completionIterator, line.end()));
Václav Kubernát4c325482019-04-11 17:51:55 +020070 if (set.size() == 1) {
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010071 return {{(*set.begin()) + ctx.m_completionSuffix}, completionContext};
Václav Kubernát4c325482019-04-11 17:51:55 +020072 }
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010073 return {set, completionContext};
Václav Kubernát4108e0d2018-10-29 13:32:22 +010074}
75
Václav Kubernát2eaceb82018-10-08 19:56:30 +020076void Parser::changeNode(const dataPath_& name)
Václav Kubernát2a9e1792018-05-28 12:53:48 +020077{
Václav Kubernát37171a12018-08-31 17:01:48 +020078 if (name.m_scope == Scope::Absolute) {
79 m_curDir = name;
80 } else {
81 for (const auto& it : name.m_nodes) {
82 if (it.m_suffix.type() == typeid(nodeup_))
83 m_curDir.m_nodes.pop_back();
84 else
85 m_curDir.m_nodes.push_back(it);
86 }
Václav Kubernát2a9e1792018-05-28 12:53:48 +020087 }
88}
Václav Kubernát814fa412018-05-25 19:47:18 +020089
Václav Kubernát48fc3832018-05-28 14:21:22 +020090std::string Parser::currentNode() const
Václav Kubernát2a9e1792018-05-28 12:53:48 +020091{
Václav Kubernátefcac932020-01-10 15:26:32 +010092 return pathToDataString(m_curDir, Prefixes::WhenNeeded);
Václav Kubernát2a9e1792018-05-28 12:53:48 +020093}
Václav Kubernát11afac72018-07-18 14:59:53 +020094
Václav Kubernát2eaceb82018-10-08 19:56:30 +020095struct getSchemaPathVisitor : boost::static_visitor<schemaPath_> {
96 schemaPath_ operator()(const dataPath_& path) const
97 {
98 return dataPathToSchemaPath(path);
99 }
100
101 schemaPath_ operator()(const schemaPath_& path) const
102 {
103 return path;
104 }
105};
106
Václav Kubernát5c75b252018-10-10 18:33:47 +0200107
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200108std::set<std::string> Parser::availableNodes(const boost::optional<boost::variant<boost::variant<dataPath_, schemaPath_>, module_>>& path, const Recursion& option) const
Václav Kubernát11afac72018-07-18 14:59:53 +0200109{
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200110 auto pathArg = dataPathToSchemaPath(m_curDir);
111 if (path) {
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200112 if (path->type() == typeid(module_)) {
113 return m_schema->moduleNodes(boost::get<module_>(*path), option);
114 }
115
116 auto schemaPath = boost::apply_visitor(getSchemaPathVisitor(), boost::get<boost::variant<dataPath_, schemaPath_>>(*path));
Václav Kubernát1446fe12019-10-02 19:32:51 +0200117 if (schemaPath.m_scope == Scope::Absolute) {
118 pathArg = schemaPath;
119 } else {
120 pathArg.m_nodes.insert(pathArg.m_nodes.end(), schemaPath.m_nodes.begin(), schemaPath.m_nodes.end());
121 }
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200122 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200123 return m_schema->childNodes(pathArg, option);
Václav Kubernát11afac72018-07-18 14:59:53 +0200124}