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 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 16 | CParser::CParser(const CTree& tree) |
| 17 | : m_tree(tree) |
| 18 | { |
| 19 | } |
| 20 | |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame] | 21 | cd_ CParser::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 | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 23 | cd_ parsedCommand; |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 24 | ParserContext ctx(m_tree, 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)[ |
| 31 | x3::with<x3::error_handler_tag>(std::ref(errorHandler))[cd] |
| 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 | |
| 42 | void CParser::changeNode(const path_& name) |
| 43 | { |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 44 | for (const auto& it : name.m_nodes) { |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 45 | if (it.type() == typeid(nodeup_)) |
| 46 | m_curDir.m_nodes.pop_back(); |
| 47 | else |
| 48 | m_curDir.m_nodes.push_back(it); |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 49 | } |
| 50 | } |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 51 | |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 52 | std::string CParser::currentNode() const |
| 53 | { |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 54 | std::string res; |
| 55 | for (const auto& it : m_curDir.m_nodes) { |
| 56 | res = joinPaths(res, boost::apply_visitor(nodeToString(), it)); |
| 57 | } |
| 58 | |
| 59 | return res; |
Václav Kubernát | 2a9e179 | 2018-05-28 12:53:48 +0200 | [diff] [blame] | 60 | } |
| 61 | |