Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | #pragma once |
| 9 | #include <boost/spirit/home/x3.hpp> |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 10 | #include <boost/spirit/home/x3/support/ast/position_tagged.hpp> |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 11 | |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 12 | #include <boost/fusion/adapted/struct/adapt_struct.hpp> |
| 13 | #include <boost/fusion/include/adapt_struct.hpp> |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 16 | #include "CTree.hpp" |
Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame^] | 17 | #include "utils.hpp" |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 18 | namespace x3 = boost::spirit::x3; |
| 19 | namespace ascii = boost::spirit::x3::ascii; |
| 20 | |
| 21 | using x3::alpha; |
| 22 | using x3::alnum; |
| 23 | using x3::lit; |
| 24 | using x3::char_; |
| 25 | using x3::_attr; |
| 26 | using x3::lexeme; |
| 27 | using ascii::space; |
| 28 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 29 | struct ParserContext { |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 30 | ParserContext(const CTree& tree); |
| 31 | const CTree& m_tree; |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 32 | std::string m_curPath; |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | struct parser_context_tag; |
| 36 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 37 | struct container_ { |
| 38 | container_() {} |
| 39 | container_(const std::string& name); |
| 40 | |
| 41 | bool operator==(const container_& b) const; |
| 42 | |
| 43 | char m_first = ' '; |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 44 | std::string m_name; |
| 45 | }; |
| 46 | |
| 47 | BOOST_FUSION_ADAPT_STRUCT(container_, m_first, m_name) |
| 48 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 49 | struct path_ { |
| 50 | bool operator==(const path_& b) const; |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 51 | std::vector<container_> m_nodes; |
| 52 | }; |
| 53 | |
| 54 | BOOST_FUSION_ADAPT_STRUCT(path_, m_nodes) |
| 55 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 56 | struct cd_ { |
| 57 | bool operator==(const cd_& b) const; |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 58 | path_ m_path; |
| 59 | }; |
| 60 | |
| 61 | BOOST_FUSION_ADAPT_STRUCT(cd_, m_path) |
| 62 | |
| 63 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 64 | struct container_class { |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 65 | template <typename T, typename Iterator, typename Context> |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 66 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 67 | { |
| 68 | ast.m_name = ast.m_first + ast.m_name; |
| 69 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 70 | const auto& tree = parserContext.m_tree; |
| 71 | |
| 72 | if (tree.isContainer(parserContext.m_curPath, ast.m_name)) { |
| 73 | if (!parserContext.m_curPath.empty()) { |
| 74 | parserContext.m_curPath += '/'; |
| 75 | } |
| 76 | parserContext.m_curPath += ast.m_name; |
| 77 | } else { |
| 78 | throw InvalidNodeException("No container with the name \"" + ast.m_name + "\" in \"" + parserContext.m_curPath + "\""); |
| 79 | } |
| 80 | } |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 81 | }; |
| 82 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 83 | struct path_class { |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 84 | template <typename T, typename Iterator, typename Context> |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 85 | void on_success(Iterator const&, Iterator const&, T&, Context const&) |
| 86 | { |
| 87 | } |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 90 | struct cd_class { |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 91 | template <typename T, typename Iterator, typename Context> |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 92 | void on_success(Iterator const&, Iterator const&, T&, Context const&) |
| 93 | { |
| 94 | } |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 98 | x3::rule<container_class, container_> const container = "container"; |
| 99 | x3::rule<path_class, path_> const path = "path"; |
| 100 | x3::rule<cd_class, cd_> const cd = "cd"; |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 101 | |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 102 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 103 | auto const identifier = |
| 104 | lexeme[ |
| 105 | ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_("."))) |
| 106 | ]; |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 107 | |
| 108 | auto const container_def = |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 109 | identifier; |
| 110 | |
Václav Kubernát | 8cd6342 | 2018-03-19 17:10:13 +0100 | [diff] [blame] | 111 | auto const path_def = |
| 112 | container % '/'; |
| 113 | |
| 114 | auto const cd_def = |
| 115 | lit("cd") >> path >> x3::eoi; |
| 116 | |
| 117 | BOOST_SPIRIT_DEFINE(container) |
| 118 | BOOST_SPIRIT_DEFINE(path) |
| 119 | BOOST_SPIRIT_DEFINE(cd) |