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