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