blob: c6a42a606114e616d7ab9bc82f610ce30df7db15 [file] [log] [blame]
Václav Kubernátd6662962018-03-22 17:41:33 +01001/*
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át2315c732018-05-16 20:25:55 +02008#include <ostream>
Václav Kubernát624a8872018-03-02 17:28:47 +01009#include "CParser.hpp"
10
11TooManyArgumentsException::~TooManyArgumentsException() = default;
12
Václav Kubernátb96eef72018-05-04 19:10:22 +020013InvalidCommandException::~InvalidCommandException() = default;
14
Václav Kubernát624a8872018-03-02 17:28:47 +010015CParser::CParser(const CTree& tree)
16 : m_tree(tree)
17{
18}
19
20
Václav Kubernát2315c732018-05-16 20:25:55 +020021cd_ CParser::parseCommand(const std::string& line, std::ostream& errorStream)
Václav Kubernát624a8872018-03-02 17:28:47 +010022{
Václav Kubernátd6662962018-03-22 17:41:33 +010023 cd_ parsedCommand;
24 ParserContext ctx(m_tree);
Václav Kubernát624a8872018-03-02 17:28:47 +010025 auto it = line.begin();
Václav Kubernátd6662962018-03-22 17:41:33 +010026
Václav Kubernát2315c732018-05-16 20:25:55 +020027 boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
28
Václav Kubernátb96eef72018-05-04 19:10:22 +020029 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átd6662962018-03-22 17:41:33 +010034
Václav Kubernátb96eef72018-05-04 19:10:22 +020035 if (!result || it != line.end()) {
36 throw InvalidCommandException(std::string(it, line.end()) + " this was left of input");
Václav Kubernát624a8872018-03-02 17:28:47 +010037 }
38
Václav Kubernátd6662962018-03-22 17:41:33 +010039 return parsedCommand;
Václav Kubernát624a8872018-03-02 17:28:47 +010040}