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 | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 9 | #include "CParser.hpp" |
| 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 | struct nodeToString : public boost::static_visitor<std::string> { |
| 16 | std::string operator()(const nodeup_&) const |
| 17 | { |
| 18 | return ".."; |
| 19 | } |
| 20 | template <class T> |
| 21 | std::string operator()(const T& node) const |
| 22 | { |
| 23 | return node.m_name; |
| 24 | } |
| 25 | }; |
| 26 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 27 | CParser::CParser(const CTree& tree) |
| 28 | : m_tree(tree) |
| 29 | { |
| 30 | } |
| 31 | |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 32 | cd_ CParser::parseCommand(const std::string& line, std::ostream& errorStream) |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 33 | { |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 34 | cd_ parsedCommand; |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame^] | 35 | ParserContext ctx(m_tree, m_curDir); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 36 | auto it = line.begin(); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 37 | |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 38 | boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream); |
| 39 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 40 | auto grammar = |
| 41 | x3::with<parser_context_tag>(ctx)[ |
| 42 | x3::with<x3::error_handler_tag>(std::ref(errorHandler))[cd] |
| 43 | ]; |
| 44 | 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] | 45 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 46 | if (!result || it != line.end()) { |
| 47 | 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] | 48 | } |
| 49 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 50 | return parsedCommand; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 51 | } |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame^] | 52 | |
| 53 | void CParser::changeNode(const path_& name) |
| 54 | { |
| 55 | if (name.m_nodes.empty()) { |
| 56 | m_curDir = ""; |
| 57 | return; |
| 58 | } |
| 59 | for (const auto& it : name.m_nodes) { |
| 60 | const std::string node = boost::apply_visitor(nodeToString(), it); |
| 61 | if (node == "..") { |
| 62 | m_curDir = stripLastNodeFromPath(m_curDir); |
| 63 | } else { |
| 64 | m_curDir = joinPaths(m_curDir, boost::apply_visitor(nodeToString(), it)); |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | } |
| 69 | std::string CParser::currentNode() const |
| 70 | { |
| 71 | return m_curDir; |
| 72 | } |
| 73 | |