blob: bb17495d0d6a84e46e502f057c8d0ebd91ad447b [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át48fc3832018-05-28 14:21:22 +02009#include "parser.hpp"
Václav Kubernát624a8872018-03-02 17:28:47 +010010
11TooManyArgumentsException::~TooManyArgumentsException() = default;
12
Václav Kubernátb96eef72018-05-04 19:10:22 +020013InvalidCommandException::~InvalidCommandException() = default;
14
Václav Kubernát2a9e1792018-05-28 12:53:48 +020015
Václav Kubernáta10d8b62018-06-13 17:42:46 +020016Parser::Parser(const std::shared_ptr<const Schema> schema)
Václav Kubernát48fc3832018-05-28 14:21:22 +020017 : m_schema(schema)
Václav Kubernát624a8872018-03-02 17:28:47 +010018{
19}
20
Václav Kubernátb61336d2018-05-28 17:35:03 +020021command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream)
Václav Kubernát624a8872018-03-02 17:28:47 +010022{
Václav Kubernátb61336d2018-05-28 17:35:03 +020023 command_ parsedCommand;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020024 ParserContext ctx(*m_schema, dataPathToSchemaPath(m_curDir));
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)[
Václav Kubernátb61336d2018-05-28 17:35:03 +020031 x3::with<x3::error_handler_tag>(std::ref(errorHandler))[command]
Václav Kubernátb96eef72018-05-04 19:10:22 +020032 ];
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}
Václav Kubernát2a9e1792018-05-28 12:53:48 +020041
Václav Kubernát2eaceb82018-10-08 19:56:30 +020042void Parser::changeNode(const dataPath_& name)
Václav Kubernát2a9e1792018-05-28 12:53:48 +020043{
Václav Kubernát37171a12018-08-31 17:01:48 +020044 if (name.m_scope == Scope::Absolute) {
45 m_curDir = name;
46 } else {
47 for (const auto& it : name.m_nodes) {
48 if (it.m_suffix.type() == typeid(nodeup_))
49 m_curDir.m_nodes.pop_back();
50 else
51 m_curDir.m_nodes.push_back(it);
52 }
Václav Kubernát2a9e1792018-05-28 12:53:48 +020053 }
54}
Václav Kubernát814fa412018-05-25 19:47:18 +020055
Václav Kubernát48fc3832018-05-28 14:21:22 +020056std::string Parser::currentNode() const
Václav Kubernát2a9e1792018-05-28 12:53:48 +020057{
Václav Kubernát13e7c652018-08-30 12:51:34 +020058 return "/" + pathToDataString(m_curDir);
Václav Kubernát2a9e1792018-05-28 12:53:48 +020059}
Václav Kubernát11afac72018-07-18 14:59:53 +020060
Václav Kubernát2eaceb82018-10-08 19:56:30 +020061struct getSchemaPathVisitor : boost::static_visitor<schemaPath_> {
62 schemaPath_ operator()(const dataPath_& path) const
63 {
64 return dataPathToSchemaPath(path);
65 }
66
67 schemaPath_ operator()(const schemaPath_& path) const
68 {
69 return path;
70 }
71};
72
73std::set<std::string> Parser::availableNodes(const boost::optional<dataPath_>& path, const Recursion& option) const
Václav Kubernát11afac72018-07-18 14:59:53 +020074{
Václav Kubernát2eaceb82018-10-08 19:56:30 +020075 auto pathArg = dataPathToSchemaPath(m_curDir);
76 if (path) {
77 auto schemaPath = dataPathToSchemaPath(*path);
78 pathArg.m_nodes.insert(pathArg.m_nodes.end(), schemaPath.m_nodes.begin(), schemaPath.m_nodes.end());
79 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020080 return m_schema->childNodes(pathArg, option);
Václav Kubernát11afac72018-07-18 14:59:53 +020081}