Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 1 | /* |
| 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át | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 8 | #include <ostream> |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 9 | #include "grammars.hpp" |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 10 | #include "parser.hpp" |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 11 | #include "parser_context.hpp" |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 12 | #include "utils.hpp" |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 13 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 14 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 15 | InvalidCommandException::~InvalidCommandException() = default; |
| 16 | |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 17 | Parser::Parser(const std::shared_ptr<const Schema> schema, WritableOps writableOps, const std::shared_ptr<const DataQuery> dataQuery) |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 18 | : m_schema(schema) |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 19 | , m_dataquery(dataQuery) |
Václav Kubernát | de598b0 | 2020-06-08 17:42:31 +0200 | [diff] [blame] | 20 | , m_curDir({Scope::Absolute, {}}) |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 21 | , m_writableOps(writableOps) |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 22 | { |
| 23 | } |
| 24 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 25 | bool 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át | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 30 | command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream) |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 31 | { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 32 | command_ parsedCommand; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 33 | ParserContext ctx(*m_schema, nullptr, m_curDir); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 34 | auto it = line.begin(); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 35 | |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 36 | boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream); |
| 37 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 38 | auto grammar = |
| 39 | x3::with<parser_context_tag>(ctx)[ |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 40 | x3::with<x3::error_handler_tag>(std::ref(errorHandler))[ |
| 41 | x3::with<writableOps_tag>(m_writableOps)[command]] |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 42 | ]; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 43 | bool result = x3::phrase_parse(it, line.end(), grammar, x3::space, parsedCommand); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 44 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 45 | if (!result || it != line.end()) { |
| 46 | throw InvalidCommandException(std::string(it, line.end()) + " this was left of input"); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 49 | return parsedCommand; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 50 | } |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 51 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 52 | Completions Parser::completeCommand(const std::string& line, std::ostream& errorStream) const |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 53 | { |
| 54 | std::set<std::string> completions; |
| 55 | command_ parsedCommand; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 56 | ParserContext ctx(*m_schema, m_dataquery, m_curDir); |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 57 | ctx.m_completing = true; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 58 | 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át | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 63 | x3::with<x3::error_handler_tag>(std::ref(errorHandler))[ |
| 64 | x3::with<writableOps_tag>(m_writableOps)[command]] |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 65 | ]; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 66 | x3::phrase_parse(it, line.end(), grammar, x3::space, parsedCommand); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 67 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 68 | auto completionIterator = ctx.m_completionIterator ? *ctx.m_completionIterator : line.end(); |
| 69 | |
| 70 | int completionContext = line.end() - completionIterator; |
| 71 | |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 72 | auto filtered = filterByPrefix(ctx.m_suggestions, std::string(completionIterator, line.end())); |
| 73 | if (filtered.size() == 1) { |
Václav Kubernát | db26e9a | 2020-02-17 18:12:46 +0100 | [diff] [blame] | 74 | auto suffix = filtered.begin()->m_whenToAdd == Completion::WhenToAdd::Always |
| 75 | || filtered.begin()->m_value == std::string{completionIterator, line.end()} |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 76 | ? filtered.begin()->m_suffix |
| 77 | : ""; |
| 78 | return {{filtered.begin()->m_value + suffix}, completionContext}; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 79 | } |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 80 | |
| 81 | std::set<std::string> res; |
| 82 | std::transform(filtered.begin(), filtered.end(), std::inserter(res, res.end()), [](auto it) { return it.m_value; }); |
| 83 | return {res, completionContext}; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 86 | void Parser::changeNode(const dataPath_& name) |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 87 | { |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 88 | if (name.m_scope == Scope::Absolute) { |
| 89 | m_curDir = name; |
| 90 | } else { |
| 91 | for (const auto& it : name.m_nodes) { |
Václav Kubernát | e781b90 | 2020-06-15 14:35:11 +0200 | [diff] [blame] | 92 | m_curDir.pushFragment(it); |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 93 | } |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 94 | } |
| 95 | } |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame] | 96 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 97 | std::string Parser::currentNode() const |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 98 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 99 | return pathToDataString(m_curDir, Prefixes::WhenNeeded); |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 100 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 101 | |
Václav Kubernát | 8208687 | 2020-04-29 01:09:50 +0200 | [diff] [blame] | 102 | dataPath_ Parser::currentPath() |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 103 | { |
Václav Kubernát | 8208687 | 2020-04-29 01:09:50 +0200 | [diff] [blame] | 104 | return m_curDir; |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 105 | } |