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 | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 12 | |
| 13 | TooManyArgumentsException::~TooManyArgumentsException() = default; |
| 14 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 15 | InvalidCommandException::~InvalidCommandException() = default; |
| 16 | |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 17 | |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 18 | Parser::Parser(const std::shared_ptr<const Schema> schema, const std::shared_ptr<const DataQuery> dataQuery) |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 19 | : m_schema(schema) |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 20 | , m_dataquery(dataQuery) |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 21 | { |
Václav Kubernát | 82684cf | 2020-01-06 13:30:37 +0100 | [diff] [blame] | 22 | m_curDir.m_scope = Scope::Absolute; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 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 | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 40 | x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command] |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 41 | ]; |
| 42 | bool result = x3::phrase_parse(it, line.end(), grammar, space, parsedCommand); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 43 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 44 | if (!result || it != line.end()) { |
| 45 | 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] | 46 | } |
| 47 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 48 | return parsedCommand; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 49 | } |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 50 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 51 | 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] | 52 | { |
| 53 | std::set<std::string> completions; |
| 54 | command_ parsedCommand; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 55 | ParserContext ctx(*m_schema, m_dataquery, m_curDir); |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 56 | ctx.m_completing = true; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 57 | auto it = line.begin(); |
| 58 | boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream); |
| 59 | |
| 60 | auto grammar = |
| 61 | x3::with<parser_context_tag>(ctx)[ |
| 62 | x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command] |
| 63 | ]; |
| 64 | x3::phrase_parse(it, line.end(), grammar, space, parsedCommand); |
| 65 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 66 | auto completionIterator = ctx.m_completionIterator ? *ctx.m_completionIterator : line.end(); |
| 67 | |
| 68 | int completionContext = line.end() - completionIterator; |
| 69 | |
| 70 | auto set = filterByPrefix(ctx.m_suggestions, std::string(completionIterator, line.end())); |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 71 | if (set.size() == 1) { |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 72 | return {{(*set.begin()) + ctx.m_completionSuffix}, completionContext}; |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 73 | } |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 74 | return {set, completionContext}; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 77 | void Parser::changeNode(const dataPath_& name) |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 78 | { |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 79 | if (name.m_scope == Scope::Absolute) { |
| 80 | m_curDir = name; |
| 81 | } else { |
| 82 | for (const auto& it : name.m_nodes) { |
| 83 | if (it.m_suffix.type() == typeid(nodeup_)) |
| 84 | m_curDir.m_nodes.pop_back(); |
| 85 | else |
| 86 | m_curDir.m_nodes.push_back(it); |
| 87 | } |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 88 | } |
| 89 | } |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame] | 90 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 91 | std::string Parser::currentNode() const |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 92 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 93 | return pathToDataString(m_curDir, Prefixes::WhenNeeded); |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 94 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 95 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 96 | struct getSchemaPathVisitor : boost::static_visitor<schemaPath_> { |
| 97 | schemaPath_ operator()(const dataPath_& path) const |
| 98 | { |
| 99 | return dataPathToSchemaPath(path); |
| 100 | } |
| 101 | |
| 102 | schemaPath_ operator()(const schemaPath_& path) const |
| 103 | { |
| 104 | return path; |
| 105 | } |
| 106 | }; |
| 107 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 108 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 109 | std::set<std::string> Parser::availableNodes(const boost::optional<boost::variant<boost::variant<dataPath_, schemaPath_>, module_>>& path, const Recursion& option) const |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 110 | { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 111 | auto pathArg = dataPathToSchemaPath(m_curDir); |
| 112 | if (path) { |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 113 | if (path->type() == typeid(module_)) { |
| 114 | return m_schema->moduleNodes(boost::get<module_>(*path), option); |
| 115 | } |
| 116 | |
| 117 | auto schemaPath = boost::apply_visitor(getSchemaPathVisitor(), boost::get<boost::variant<dataPath_, schemaPath_>>(*path)); |
Václav Kubernát | 1446fe1 | 2019-10-02 19:32:51 +0200 | [diff] [blame] | 118 | if (schemaPath.m_scope == Scope::Absolute) { |
| 119 | pathArg = schemaPath; |
| 120 | } else { |
| 121 | pathArg.m_nodes.insert(pathArg.m_nodes.end(), schemaPath.m_nodes.begin(), schemaPath.m_nodes.end()); |
| 122 | } |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 123 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 124 | return m_schema->childNodes(pathArg, option); |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 125 | } |