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