blob: 420db27ee22fe23785b3e28e3456d38b1766a300 [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{
21}
22
Václav Kubernátb61336d2018-05-28 17:35:03 +020023command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream)
Václav Kubernát624a8872018-03-02 17:28:47 +010024{
Václav Kubernátb61336d2018-05-28 17:35:03 +020025 command_ parsedCommand;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020026 ParserContext ctx(*m_schema, dataPathToSchemaPath(m_curDir));
Václav Kubernát624a8872018-03-02 17:28:47 +010027 auto it = line.begin();
Václav Kubernátd6662962018-03-22 17:41:33 +010028
Václav Kubernát2315c732018-05-16 20:25:55 +020029 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
30
Václav Kubernátb96eef72018-05-04 19:10:22 +020031 auto grammar =
32 x3::with<parser_context_tag>(ctx)[
Václav Kubernátb61336d2018-05-28 17:35:03 +020033 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
Václav Kubernátb96eef72018-05-04 19:10:22 +020034 ];
35 bool result = x3::phrase_parse(it, line.end(), grammar, space, parsedCommand);
Václav Kubernátd6662962018-03-22 17:41:33 +010036
Václav Kubernátb96eef72018-05-04 19:10:22 +020037 if (!result || it != line.end()) {
38 throw InvalidCommandException(std::string(it, line.end()) + " this was left of input");
Václav Kubernát624a8872018-03-02 17:28:47 +010039 }
40
Václav Kubernátd6662962018-03-22 17:41:33 +010041 return parsedCommand;
Václav Kubernát624a8872018-03-02 17:28:47 +010042}
Václav Kubernát2a9e1792018-05-28 12:53:48 +020043
Václav Kubernát4108e0d2018-10-29 13:32:22 +010044std::set<std::string> Parser::completeCommand(const std::string& line, std::ostream& errorStream) const
45{
46 std::set<std::string> completions;
47 command_ parsedCommand;
48 ParserContext ctx(*m_schema, dataPathToSchemaPath(m_curDir));
Václav Kubernátac035d62019-02-18 10:59:08 +010049 ctx.m_completing = true;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010050 auto it = line.begin();
51 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
52
53 auto grammar =
54 x3::with<parser_context_tag>(ctx)[
55 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
56 ];
57 x3::phrase_parse(it, line.end(), grammar, space, parsedCommand);
58
Václav Kubernát4c325482019-04-11 17:51:55 +020059 auto set = filterByPrefix(ctx.m_suggestions, std::string(ctx.m_completionIterator, line.end()));
60 if (set.size() == 1) {
61 return {(*set.begin()) + ctx.m_completionSuffix};
62 }
63 return set;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010064}
65
Václav Kubernát2eaceb82018-10-08 19:56:30 +020066void Parser::changeNode(const dataPath_& name)
Václav Kubernát2a9e1792018-05-28 12:53:48 +020067{
Václav Kubernát37171a12018-08-31 17:01:48 +020068 if (name.m_scope == Scope::Absolute) {
69 m_curDir = name;
70 } else {
71 for (const auto& it : name.m_nodes) {
72 if (it.m_suffix.type() == typeid(nodeup_))
73 m_curDir.m_nodes.pop_back();
74 else
75 m_curDir.m_nodes.push_back(it);
76 }
Václav Kubernát2a9e1792018-05-28 12:53:48 +020077 }
78}
Václav Kubernát814fa412018-05-25 19:47:18 +020079
Václav Kubernát48fc3832018-05-28 14:21:22 +020080std::string Parser::currentNode() const
Václav Kubernát2a9e1792018-05-28 12:53:48 +020081{
Václav Kubernát13e7c652018-08-30 12:51:34 +020082 return "/" + pathToDataString(m_curDir);
Václav Kubernát2a9e1792018-05-28 12:53:48 +020083}
Václav Kubernát11afac72018-07-18 14:59:53 +020084
Václav Kubernát2eaceb82018-10-08 19:56:30 +020085struct getSchemaPathVisitor : boost::static_visitor<schemaPath_> {
86 schemaPath_ operator()(const dataPath_& path) const
87 {
88 return dataPathToSchemaPath(path);
89 }
90
91 schemaPath_ operator()(const schemaPath_& path) const
92 {
93 return path;
94 }
95};
96
Václav Kubernát5c75b252018-10-10 18:33:47 +020097
98std::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 +020099{
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200100 auto pathArg = dataPathToSchemaPath(m_curDir);
101 if (path) {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200102 auto schemaPath = boost::apply_visitor(getSchemaPathVisitor(), *path);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200103 pathArg.m_nodes.insert(pathArg.m_nodes.end(), schemaPath.m_nodes.begin(), schemaPath.m_nodes.end());
104 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200105 return m_schema->childNodes(pathArg, option);
Václav Kubernát11afac72018-07-18 14:59:53 +0200106}