blob: 195789b0cfa9c08335d2551a4859e6827b947ccf [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át3a823f42020-04-29 23:40:21 +020012#include "utils.hpp"
Václav Kubernát624a8872018-03-02 17:28:47 +010013
14TooManyArgumentsException::~TooManyArgumentsException() = default;
15
Václav Kubernátb96eef72018-05-04 19:10:22 +020016InvalidCommandException::~InvalidCommandException() = default;
17
Václav Kubernát2a9e1792018-05-28 12:53:48 +020018
Václav Kubernát43908fb2020-01-02 19:05:51 +010019Parser::Parser(const std::shared_ptr<const Schema> schema, const std::shared_ptr<const DataQuery> dataQuery)
Václav Kubernát48fc3832018-05-28 14:21:22 +020020 : m_schema(schema)
Václav Kubernát43908fb2020-01-02 19:05:51 +010021 , m_dataquery(dataQuery)
Václav Kubernát624a8872018-03-02 17:28:47 +010022{
Václav Kubernát82684cf2020-01-06 13:30:37 +010023 m_curDir.m_scope = Scope::Absolute;
Václav Kubernát624a8872018-03-02 17:28:47 +010024}
25
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010026bool Completions::operator==(const Completions& b) const
27{
28 return this->m_completions == b.m_completions && this->m_contextLength == b.m_contextLength;
29}
30
Václav Kubernátb61336d2018-05-28 17:35:03 +020031command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream)
Václav Kubernát624a8872018-03-02 17:28:47 +010032{
Václav Kubernátb61336d2018-05-28 17:35:03 +020033 command_ parsedCommand;
Václav Kubernát43908fb2020-01-02 19:05:51 +010034 ParserContext ctx(*m_schema, nullptr, m_curDir);
Václav Kubernát624a8872018-03-02 17:28:47 +010035 auto it = line.begin();
Václav Kubernátd6662962018-03-22 17:41:33 +010036
Václav Kubernát2315c732018-05-16 20:25:55 +020037 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
38
Václav Kubernátb96eef72018-05-04 19:10:22 +020039 auto grammar =
40 x3::with<parser_context_tag>(ctx)[
Václav Kubernátb61336d2018-05-28 17:35:03 +020041 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
Václav Kubernátb96eef72018-05-04 19:10:22 +020042 ];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020043 bool result = x3::phrase_parse(it, line.end(), grammar, x3::space, parsedCommand);
Václav Kubernátd6662962018-03-22 17:41:33 +010044
Václav Kubernátb96eef72018-05-04 19:10:22 +020045 if (!result || it != line.end()) {
46 throw InvalidCommandException(std::string(it, line.end()) + " this was left of input");
Václav Kubernát624a8872018-03-02 17:28:47 +010047 }
48
Václav Kubernátd6662962018-03-22 17:41:33 +010049 return parsedCommand;
Václav Kubernát624a8872018-03-02 17:28:47 +010050}
Václav Kubernát2a9e1792018-05-28 12:53:48 +020051
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010052Completions Parser::completeCommand(const std::string& line, std::ostream& errorStream) const
Václav Kubernát4108e0d2018-10-29 13:32:22 +010053{
54 std::set<std::string> completions;
55 command_ parsedCommand;
Václav Kubernát43908fb2020-01-02 19:05:51 +010056 ParserContext ctx(*m_schema, m_dataquery, m_curDir);
Václav Kubernátac035d62019-02-18 10:59:08 +010057 ctx.m_completing = true;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010058 auto it = line.begin();
59 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
60
61 auto grammar =
62 x3::with<parser_context_tag>(ctx)[
63 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
64 ];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020065 x3::phrase_parse(it, line.end(), grammar, x3::space, parsedCommand);
Václav Kubernát4108e0d2018-10-29 13:32:22 +010066
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010067 auto completionIterator = ctx.m_completionIterator ? *ctx.m_completionIterator : line.end();
68
69 int completionContext = line.end() - completionIterator;
70
Václav Kubernátcb3af402020-02-12 16:49:17 +010071 auto filtered = filterByPrefix(ctx.m_suggestions, std::string(completionIterator, line.end()));
72 if (filtered.size() == 1) {
Václav Kubernátdb26e9a2020-02-17 18:12:46 +010073 auto suffix = filtered.begin()->m_whenToAdd == Completion::WhenToAdd::Always
74 || filtered.begin()->m_value == std::string{completionIterator, line.end()}
Václav Kubernátcb3af402020-02-12 16:49:17 +010075 ? filtered.begin()->m_suffix
76 : "";
77 return {{filtered.begin()->m_value + suffix}, completionContext};
Václav Kubernát4c325482019-04-11 17:51:55 +020078 }
Václav Kubernátcb3af402020-02-12 16:49:17 +010079
80 std::set<std::string> res;
81 std::transform(filtered.begin(), filtered.end(), std::inserter(res, res.end()), [](auto it) { return it.m_value; });
82 return {res, completionContext};
Václav Kubernát4108e0d2018-10-29 13:32:22 +010083}
84
Václav Kubernát2eaceb82018-10-08 19:56:30 +020085void Parser::changeNode(const dataPath_& name)
Václav Kubernát2a9e1792018-05-28 12:53:48 +020086{
Václav Kubernát37171a12018-08-31 17:01:48 +020087 if (name.m_scope == Scope::Absolute) {
88 m_curDir = name;
89 } else {
90 for (const auto& it : name.m_nodes) {
Václav Kubernátb5ca1542020-05-27 01:03:54 +020091 if (std::holds_alternative<nodeup_>(it.m_suffix))
Václav Kubernát37171a12018-08-31 17:01:48 +020092 m_curDir.m_nodes.pop_back();
93 else
94 m_curDir.m_nodes.push_back(it);
95 }
Václav Kubernát2a9e1792018-05-28 12:53:48 +020096 }
97}
Václav Kubernát814fa412018-05-25 19:47:18 +020098
Václav Kubernát48fc3832018-05-28 14:21:22 +020099std::string Parser::currentNode() const
Václav Kubernát2a9e1792018-05-28 12:53:48 +0200100{
Václav Kubernátefcac932020-01-10 15:26:32 +0100101 return pathToDataString(m_curDir, Prefixes::WhenNeeded);
Václav Kubernát2a9e1792018-05-28 12:53:48 +0200102}
Václav Kubernát11afac72018-07-18 14:59:53 +0200103
Václav Kubernát82086872020-04-29 01:09:50 +0200104dataPath_ Parser::currentPath()
Václav Kubernát11afac72018-07-18 14:59:53 +0200105{
Václav Kubernát82086872020-04-29 01:09:50 +0200106 return m_curDir;
Václav Kubernát11afac72018-07-18 14:59:53 +0200107}