blob: 4911fac901ff0449d0798189b8259f936bbf849f [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
Václav Kubernát624a8872018-03-02 17:28:47 +010014
Václav Kubernátb96eef72018-05-04 19:10:22 +020015InvalidCommandException::~InvalidCommandException() = default;
16
Václav Kubernát43908fb2020-01-02 19:05:51 +010017Parser::Parser(const std::shared_ptr<const Schema> schema, const std::shared_ptr<const DataQuery> dataQuery)
Václav Kubernát48fc3832018-05-28 14:21:22 +020018 : m_schema(schema)
Václav Kubernát43908fb2020-01-02 19:05:51 +010019 , m_dataquery(dataQuery)
Václav Kubernátde598b02020-06-08 17:42:31 +020020 , m_curDir({Scope::Absolute, {}})
Václav Kubernát624a8872018-03-02 17:28:47 +010021{
22}
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át43908fb2020-01-02 19:05:51 +010032 ParserContext ctx(*m_schema, nullptr, 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 ];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020041 bool result = x3::phrase_parse(it, line.end(), grammar, x3::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át43908fb2020-01-02 19:05:51 +010054 ParserContext ctx(*m_schema, m_dataquery, 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 ];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020063 x3::phrase_parse(it, line.end(), grammar, x3::space, parsedCommand);
Václav Kubernát4108e0d2018-10-29 13:32:22 +010064
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
Václav Kubernátcb3af402020-02-12 16:49:17 +010069 auto filtered = filterByPrefix(ctx.m_suggestions, std::string(completionIterator, line.end()));
70 if (filtered.size() == 1) {
Václav Kubernátdb26e9a2020-02-17 18:12:46 +010071 auto suffix = filtered.begin()->m_whenToAdd == Completion::WhenToAdd::Always
72 || filtered.begin()->m_value == std::string{completionIterator, line.end()}
Václav Kubernátcb3af402020-02-12 16:49:17 +010073 ? filtered.begin()->m_suffix
74 : "";
75 return {{filtered.begin()->m_value + suffix}, completionContext};
Václav Kubernát4c325482019-04-11 17:51:55 +020076 }
Václav Kubernátcb3af402020-02-12 16:49:17 +010077
78 std::set<std::string> res;
79 std::transform(filtered.begin(), filtered.end(), std::inserter(res, res.end()), [](auto it) { return it.m_value; });
80 return {res, completionContext};
Václav Kubernát4108e0d2018-10-29 13:32:22 +010081}
82
Václav Kubernát2eaceb82018-10-08 19:56:30 +020083void Parser::changeNode(const dataPath_& name)
Václav Kubernát2a9e1792018-05-28 12:53:48 +020084{
Václav Kubernát37171a12018-08-31 17:01:48 +020085 if (name.m_scope == Scope::Absolute) {
86 m_curDir = name;
87 } else {
88 for (const auto& it : name.m_nodes) {
Václav Kubernátc2bfe492020-06-15 13:19:07 +020089 if (std::holds_alternative<nodeup_>(it.m_suffix)) {
90 if (!m_curDir.m_nodes.empty()) { // Allow going up, when already at root
91 m_curDir.m_nodes.pop_back();
92 }
93 } else {
Václav Kubernát37171a12018-08-31 17:01:48 +020094 m_curDir.m_nodes.push_back(it);
Václav Kubernátc2bfe492020-06-15 13:19:07 +020095 }
Václav Kubernát37171a12018-08-31 17:01:48 +020096 }
Václav Kubernát2a9e1792018-05-28 12:53:48 +020097 }
98}
Václav Kubernát814fa412018-05-25 19:47:18 +020099
Václav Kubernát48fc3832018-05-28 14:21:22 +0200100std::string Parser::currentNode() const
Václav Kubernát2a9e1792018-05-28 12:53:48 +0200101{
Václav Kubernátefcac932020-01-10 15:26:32 +0100102 return pathToDataString(m_curDir, Prefixes::WhenNeeded);
Václav Kubernát2a9e1792018-05-28 12:53:48 +0200103}
Václav Kubernát11afac72018-07-18 14:59:53 +0200104
Václav Kubernát82086872020-04-29 01:09:50 +0200105dataPath_ Parser::currentPath()
Václav Kubernát11afac72018-07-18 14:59:53 +0200106{
Václav Kubernát82086872020-04-29 01:09:50 +0200107 return m_curDir;
Václav Kubernát11afac72018-07-18 14:59:53 +0200108}