blob: 26e417f71df4b501bda5c00706959ffcd2f50025 [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át48fc3832018-05-28 14:21:22 +02009#include "parser.hpp"
Václav Kubernát624a8872018-03-02 17:28:47 +010010
11TooManyArgumentsException::~TooManyArgumentsException() = default;
12
Václav Kubernátb96eef72018-05-04 19:10:22 +020013InvalidCommandException::~InvalidCommandException() = default;
14
Václav Kubernát2a9e1792018-05-28 12:53:48 +020015
Václav Kubernáta10d8b62018-06-13 17:42:46 +020016Parser::Parser(const std::shared_ptr<const Schema> schema)
Václav Kubernát48fc3832018-05-28 14:21:22 +020017 : m_schema(schema)
Václav Kubernát624a8872018-03-02 17:28:47 +010018{
19}
20
Václav Kubernátb61336d2018-05-28 17:35:03 +020021command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream)
Václav Kubernát624a8872018-03-02 17:28:47 +010022{
Václav Kubernátb61336d2018-05-28 17:35:03 +020023 command_ parsedCommand;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020024 ParserContext ctx(*m_schema, dataPathToSchemaPath(m_curDir));
Václav Kubernát624a8872018-03-02 17:28:47 +010025 auto it = line.begin();
Václav Kubernátd6662962018-03-22 17:41:33 +010026
Václav Kubernát2315c732018-05-16 20:25:55 +020027 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
28
Václav Kubernátb96eef72018-05-04 19:10:22 +020029 auto grammar =
30 x3::with<parser_context_tag>(ctx)[
Václav Kubernátb61336d2018-05-28 17:35:03 +020031 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
Václav Kubernátb96eef72018-05-04 19:10:22 +020032 ];
33 bool result = x3::phrase_parse(it, line.end(), grammar, space, parsedCommand);
Václav Kubernátd6662962018-03-22 17:41:33 +010034
Václav Kubernátb96eef72018-05-04 19:10:22 +020035 if (!result || it != line.end()) {
36 throw InvalidCommandException(std::string(it, line.end()) + " this was left of input");
Václav Kubernát624a8872018-03-02 17:28:47 +010037 }
38
Václav Kubernátd6662962018-03-22 17:41:33 +010039 return parsedCommand;
Václav Kubernát624a8872018-03-02 17:28:47 +010040}
Václav Kubernát2a9e1792018-05-28 12:53:48 +020041
Václav Kubernát4108e0d2018-10-29 13:32:22 +010042std::set<std::string> Parser::completeCommand(const std::string& line, std::ostream& errorStream) const
43{
44 std::set<std::string> completions;
45 command_ parsedCommand;
46 ParserContext ctx(*m_schema, dataPathToSchemaPath(m_curDir));
Václav Kubernátac035d62019-02-18 10:59:08 +010047 ctx.m_completing = true;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010048 auto it = line.begin();
49 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
50
51 auto grammar =
52 x3::with<parser_context_tag>(ctx)[
53 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
54 ];
55 x3::phrase_parse(it, line.end(), grammar, space, parsedCommand);
56
Václav Kubernát4c325482019-04-11 17:51:55 +020057 auto set = filterByPrefix(ctx.m_suggestions, std::string(ctx.m_completionIterator, line.end()));
58 if (set.size() == 1) {
59 return {(*set.begin()) + ctx.m_completionSuffix};
60 }
61 return set;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010062}
63
Václav Kubernát2eaceb82018-10-08 19:56:30 +020064void Parser::changeNode(const dataPath_& name)
Václav Kubernát2a9e1792018-05-28 12:53:48 +020065{
Václav Kubernát37171a12018-08-31 17:01:48 +020066 if (name.m_scope == Scope::Absolute) {
67 m_curDir = name;
68 } else {
69 for (const auto& it : name.m_nodes) {
70 if (it.m_suffix.type() == typeid(nodeup_))
71 m_curDir.m_nodes.pop_back();
72 else
73 m_curDir.m_nodes.push_back(it);
74 }
Václav Kubernát2a9e1792018-05-28 12:53:48 +020075 }
76}
Václav Kubernát814fa412018-05-25 19:47:18 +020077
Václav Kubernát48fc3832018-05-28 14:21:22 +020078std::string Parser::currentNode() const
Václav Kubernát2a9e1792018-05-28 12:53:48 +020079{
Václav Kubernát13e7c652018-08-30 12:51:34 +020080 return "/" + pathToDataString(m_curDir);
Václav Kubernát2a9e1792018-05-28 12:53:48 +020081}
Václav Kubernát11afac72018-07-18 14:59:53 +020082
Václav Kubernát2eaceb82018-10-08 19:56:30 +020083struct getSchemaPathVisitor : boost::static_visitor<schemaPath_> {
84 schemaPath_ operator()(const dataPath_& path) const
85 {
86 return dataPathToSchemaPath(path);
87 }
88
89 schemaPath_ operator()(const schemaPath_& path) const
90 {
91 return path;
92 }
93};
94
Václav Kubernát5c75b252018-10-10 18:33:47 +020095
96std::set<std::string> Parser::availableNodes(const boost::optional<boost::variant<dataPath_, schemaPath_>>& path, const Recursion& option) const
Václav Kubernát11afac72018-07-18 14:59:53 +020097{
Václav Kubernát2eaceb82018-10-08 19:56:30 +020098 auto pathArg = dataPathToSchemaPath(m_curDir);
99 if (path) {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200100 auto schemaPath = boost::apply_visitor(getSchemaPathVisitor(), *path);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200101 pathArg.m_nodes.insert(pathArg.m_nodes.end(), schemaPath.m_nodes.begin(), schemaPath.m_nodes.end());
102 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200103 return m_schema->childNodes(pathArg, option);
Václav Kubernát11afac72018-07-18 14:59:53 +0200104}