blob: ce6ea01a6c93b3f30935d760217ca71ff1c7d45c [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átb61336d2018-05-28 17:35:03 +020024command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream)
Václav Kubernát624a8872018-03-02 17:28:47 +010025{
Václav Kubernátb61336d2018-05-28 17:35:03 +020026 command_ parsedCommand;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020027 ParserContext ctx(*m_schema, dataPathToSchemaPath(m_curDir));
Václav Kubernát624a8872018-03-02 17:28:47 +010028 auto it = line.begin();
Václav Kubernátd6662962018-03-22 17:41:33 +010029
Václav Kubernát2315c732018-05-16 20:25:55 +020030 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
31
Václav Kubernátb96eef72018-05-04 19:10:22 +020032 auto grammar =
33 x3::with<parser_context_tag>(ctx)[
Václav Kubernátb61336d2018-05-28 17:35:03 +020034 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
Václav Kubernátb96eef72018-05-04 19:10:22 +020035 ];
36 bool result = x3::phrase_parse(it, line.end(), grammar, space, parsedCommand);
Václav Kubernátd6662962018-03-22 17:41:33 +010037
Václav Kubernátb96eef72018-05-04 19:10:22 +020038 if (!result || it != line.end()) {
39 throw InvalidCommandException(std::string(it, line.end()) + " this was left of input");
Václav Kubernát624a8872018-03-02 17:28:47 +010040 }
41
Václav Kubernátd6662962018-03-22 17:41:33 +010042 return parsedCommand;
Václav Kubernát624a8872018-03-02 17:28:47 +010043}
Václav Kubernát2a9e1792018-05-28 12:53:48 +020044
Václav Kubernát4108e0d2018-10-29 13:32:22 +010045std::set<std::string> Parser::completeCommand(const std::string& line, std::ostream& errorStream) const
46{
47 std::set<std::string> completions;
48 command_ parsedCommand;
49 ParserContext ctx(*m_schema, dataPathToSchemaPath(m_curDir));
Václav Kubernátac035d62019-02-18 10:59:08 +010050 ctx.m_completing = true;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010051 auto it = line.begin();
52 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
53
54 auto grammar =
55 x3::with<parser_context_tag>(ctx)[
56 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
57 ];
58 x3::phrase_parse(it, line.end(), grammar, space, parsedCommand);
59
Václav Kubernát4c325482019-04-11 17:51:55 +020060 auto set = filterByPrefix(ctx.m_suggestions, std::string(ctx.m_completionIterator, line.end()));
61 if (set.size() == 1) {
62 return {(*set.begin()) + ctx.m_completionSuffix};
63 }
64 return set;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010065}
66
Václav Kubernát2eaceb82018-10-08 19:56:30 +020067void Parser::changeNode(const dataPath_& name)
Václav Kubernát2a9e1792018-05-28 12:53:48 +020068{
Václav Kubernát37171a12018-08-31 17:01:48 +020069 if (name.m_scope == Scope::Absolute) {
70 m_curDir = name;
71 } else {
72 for (const auto& it : name.m_nodes) {
73 if (it.m_suffix.type() == typeid(nodeup_))
74 m_curDir.m_nodes.pop_back();
75 else
76 m_curDir.m_nodes.push_back(it);
77 }
Václav Kubernát2a9e1792018-05-28 12:53:48 +020078 }
79}
Václav Kubernát814fa412018-05-25 19:47:18 +020080
Václav Kubernát48fc3832018-05-28 14:21:22 +020081std::string Parser::currentNode() const
Václav Kubernát2a9e1792018-05-28 12:53:48 +020082{
Václav Kubernátefcac932020-01-10 15:26:32 +010083 return pathToDataString(m_curDir, Prefixes::WhenNeeded);
Václav Kubernát2a9e1792018-05-28 12:53:48 +020084}
Václav Kubernát11afac72018-07-18 14:59:53 +020085
Václav Kubernát2eaceb82018-10-08 19:56:30 +020086struct getSchemaPathVisitor : boost::static_visitor<schemaPath_> {
87 schemaPath_ operator()(const dataPath_& path) const
88 {
89 return dataPathToSchemaPath(path);
90 }
91
92 schemaPath_ operator()(const schemaPath_& path) const
93 {
94 return path;
95 }
96};
97
Václav Kubernát5c75b252018-10-10 18:33:47 +020098
Václav Kubernát9456b5c2019-10-02 21:14:52 +020099std::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 +0200100{
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200101 auto pathArg = dataPathToSchemaPath(m_curDir);
102 if (path) {
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200103 if (path->type() == typeid(module_)) {
104 return m_schema->moduleNodes(boost::get<module_>(*path), option);
105 }
106
107 auto schemaPath = boost::apply_visitor(getSchemaPathVisitor(), boost::get<boost::variant<dataPath_, schemaPath_>>(*path));
Václav Kubernát1446fe12019-10-02 19:32:51 +0200108 if (schemaPath.m_scope == Scope::Absolute) {
109 pathArg = schemaPath;
110 } else {
111 pathArg.m_nodes.insert(pathArg.m_nodes.end(), schemaPath.m_nodes.begin(), schemaPath.m_nodes.end());
112 }
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200113 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200114 return m_schema->childNodes(pathArg, option);
Václav Kubernát11afac72018-07-18 14:59:53 +0200115}