blob: d37e37e3c6e954bafdf64d41c54aa45fc9488133 [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át28cf3362020-06-29 17:52:51 +020017Parser::Parser(const std::shared_ptr<const Schema> schema, WritableOps writableOps, 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át28cf3362020-06-29 17:52:51 +020021 , m_writableOps(writableOps)
Václav Kubernát624a8872018-03-02 17:28:47 +010022{
23}
24
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010025bool Completions::operator==(const Completions& b) const
26{
27 return this->m_completions == b.m_completions && this->m_contextLength == b.m_contextLength;
28}
29
Václav Kubernátb61336d2018-05-28 17:35:03 +020030command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream)
Václav Kubernát624a8872018-03-02 17:28:47 +010031{
Václav Kubernátb61336d2018-05-28 17:35:03 +020032 command_ parsedCommand;
Václav Kubernát43908fb2020-01-02 19:05:51 +010033 ParserContext ctx(*m_schema, nullptr, m_curDir);
Václav Kubernát624a8872018-03-02 17:28:47 +010034 auto it = line.begin();
Václav Kubernátd6662962018-03-22 17:41:33 +010035
Václav Kubernát2315c732018-05-16 20:25:55 +020036 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
37
Václav Kubernátb96eef72018-05-04 19:10:22 +020038 auto grammar =
39 x3::with<parser_context_tag>(ctx)[
Václav Kubernát28cf3362020-06-29 17:52:51 +020040 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[
41 x3::with<writableOps_tag>(m_writableOps)[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)[
Václav Kubernát28cf3362020-06-29 17:52:51 +020063 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[
64 x3::with<writableOps_tag>(m_writableOps)[command]]
Václav Kubernát4108e0d2018-10-29 13:32:22 +010065 ];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020066 x3::phrase_parse(it, line.end(), grammar, x3::space, parsedCommand);
Václav Kubernát4108e0d2018-10-29 13:32:22 +010067
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010068 auto completionIterator = ctx.m_completionIterator ? *ctx.m_completionIterator : line.end();
69
70 int completionContext = line.end() - completionIterator;
71
Václav Kubernátcb3af402020-02-12 16:49:17 +010072 auto filtered = filterByPrefix(ctx.m_suggestions, std::string(completionIterator, line.end()));
73 if (filtered.size() == 1) {
Václav Kubernátdb26e9a2020-02-17 18:12:46 +010074 auto suffix = filtered.begin()->m_whenToAdd == Completion::WhenToAdd::Always
75 || filtered.begin()->m_value == std::string{completionIterator, line.end()}
Václav Kubernátcb3af402020-02-12 16:49:17 +010076 ? filtered.begin()->m_suffix
77 : "";
Václav Kubernátd7d793c2020-11-26 03:15:48 +010078 return {.m_completions = {filtered.begin()->m_value + suffix}, .m_contextLength = completionContext};
Václav Kubernát4c325482019-04-11 17:51:55 +020079 }
Václav Kubernátcb3af402020-02-12 16:49:17 +010080
81 std::set<std::string> res;
82 std::transform(filtered.begin(), filtered.end(), std::inserter(res, res.end()), [](auto it) { return it.m_value; });
Václav Kubernátd7d793c2020-11-26 03:15:48 +010083 return {.m_completions = res, .m_contextLength = completionContext};
Václav Kubernát4108e0d2018-10-29 13:32:22 +010084}
85
Václav Kubernát2eaceb82018-10-08 19:56:30 +020086void Parser::changeNode(const dataPath_& name)
Václav Kubernát2a9e1792018-05-28 12:53:48 +020087{
Václav Kubernát37171a12018-08-31 17:01:48 +020088 if (name.m_scope == Scope::Absolute) {
89 m_curDir = name;
90 } else {
91 for (const auto& it : name.m_nodes) {
Václav Kubernáte781b902020-06-15 14:35:11 +020092 m_curDir.pushFragment(it);
Václav Kubernát37171a12018-08-31 17:01:48 +020093 }
Václav Kubernát2a9e1792018-05-28 12:53:48 +020094 }
95}
Václav Kubernát814fa412018-05-25 19:47:18 +020096
Václav Kubernát48fc3832018-05-28 14:21:22 +020097std::string Parser::currentNode() const
Václav Kubernát2a9e1792018-05-28 12:53:48 +020098{
Václav Kubernátefcac932020-01-10 15:26:32 +010099 return pathToDataString(m_curDir, Prefixes::WhenNeeded);
Václav Kubernát2a9e1792018-05-28 12:53:48 +0200100}
Václav Kubernát11afac72018-07-18 14:59:53 +0200101
Václav Kubernát82086872020-04-29 01:09:50 +0200102dataPath_ Parser::currentPath()
Václav Kubernát11afac72018-07-18 14:59:53 +0200103{
Václav Kubernát82086872020-04-29 01:09:50 +0200104 return m_curDir;
Václav Kubernát11afac72018-07-18 14:59:53 +0200105}