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 | f2e463f | 2018-05-28 15:51:08 +0200 | [diff] [blame] | 8 | #include <experimental/iterator> |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 9 | #include <ostream> |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 10 | #include "parser.hpp" |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 11 | |
| 12 | TooManyArgumentsException::~TooManyArgumentsException() = default; |
| 13 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 14 | InvalidCommandException::~InvalidCommandException() = default; |
| 15 | |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 16 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 17 | Parser::Parser(const Schema& schema) |
| 18 | : m_schema(schema) |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 19 | { |
| 20 | } |
| 21 | |
Václav Kubernát | f2e463f | 2018-05-28 15:51:08 +0200 | [diff] [blame] | 22 | struct nodeToDataString : public boost::static_visitor<std::string> { |
| 23 | std::string operator()(const listElement_& node) const |
| 24 | { |
| 25 | std::ostringstream res; |
| 26 | res << node.m_name + "["; |
| 27 | std::transform(node.m_keys.begin(), node.m_keys.end(), |
| 28 | std::experimental::make_ostream_joiner(res, ' '), |
| 29 | [] (const auto& it) { return it.first + "=" + it.second; }); |
| 30 | res << "]"; |
| 31 | return res.str(); |
| 32 | } |
| 33 | std::string operator()(const nodeup_&) const |
| 34 | { |
| 35 | return ".."; |
| 36 | } |
| 37 | template <class T> |
| 38 | std::string operator()(const T& node) const |
| 39 | { |
| 40 | return node.m_name; |
| 41 | } |
| 42 | }; |
| 43 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 44 | command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream) |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 45 | { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 46 | command_ parsedCommand; |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 47 | ParserContext ctx(m_schema, m_curDir); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 48 | auto it = line.begin(); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 49 | |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 50 | boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream); |
| 51 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 52 | auto grammar = |
| 53 | x3::with<parser_context_tag>(ctx)[ |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 54 | x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command] |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 55 | ]; |
| 56 | 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] | 57 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 58 | if (!result || it != line.end()) { |
| 59 | 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] | 60 | } |
| 61 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 62 | return parsedCommand; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 63 | } |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 64 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 65 | void Parser::changeNode(const path_& name) |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 66 | { |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 67 | for (const auto& it : name.m_nodes) { |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame] | 68 | if (it.type() == typeid(nodeup_)) |
| 69 | m_curDir.m_nodes.pop_back(); |
| 70 | else |
| 71 | m_curDir.m_nodes.push_back(it); |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 72 | } |
| 73 | } |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame] | 74 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 75 | std::string Parser::currentNode() const |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 76 | { |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame] | 77 | std::string res; |
| 78 | for (const auto& it : m_curDir.m_nodes) { |
Václav Kubernát | f2e463f | 2018-05-28 15:51:08 +0200 | [diff] [blame] | 79 | res = joinPaths(res, boost::apply_visitor(nodeToDataString(), it)); |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return res; |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 83 | } |