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