Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 8 | #ifndef NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP |
| 9 | #define NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 10 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 11 | #include "../../common.hpp" |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 12 | |
| 13 | #include "regex-matcher.hpp" |
| 14 | #include "regex-component-matcher.hpp" |
| 15 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 16 | namespace ndn { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 17 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 18 | class RegexComponentSetMatcher : public RegexMatcher { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 19 | |
| 20 | public: |
| 21 | /** |
| 22 | * @brief Create a RegexComponentSetMatcher matcher from expr |
| 23 | * @param expr The standard regular expression to match a component |
| 24 | * @param exact The flag to provide exact match |
| 25 | * @param backRefNum The starting back reference number |
| 26 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 27 | RegexComponentSetMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 28 | |
| 29 | virtual ~RegexComponentSetMatcher(); |
| 30 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 31 | virtual bool |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 32 | match(const Name& name, const int& offset, const int& len = 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 34 | protected: |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 35 | /** |
| 36 | * @brief Compile the regular expression to generate the more matchers when necessary |
| 37 | * @returns true if compiling succeeds |
| 38 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 39 | virtual void |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 40 | compile(); |
| 41 | |
| 42 | private: |
| 43 | int |
| 44 | extractComponent(int index); |
| 45 | |
| 46 | void |
| 47 | compileSingleComponent(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 48 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 49 | void |
| 50 | compileMultipleComponents(const int start, const int lastIndex); |
| 51 | |
| 52 | private: |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 53 | typedef std::set<shared_ptr<RegexComponentMatcher> > ComponentsSet; |
| 54 | ComponentsSet m_components; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 55 | bool m_include; |
| 56 | }; |
| 57 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 59 | inline |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 60 | RegexComponentSetMatcher::RegexComponentSetMatcher(const std::string& expr, |
| 61 | shared_ptr<RegexBackrefManager> backRefManager) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 62 | : RegexMatcher(expr, EXPR_COMPONENT_SET, backRefManager), |
| 63 | m_include(true) |
| 64 | { |
| 65 | // _LOG_TRACE ("Enter RegexComponentSetMatcher Constructor"); |
| 66 | compile(); |
| 67 | // _LOG_TRACE ("Exit RegexComponentSetMatcher Constructor"); |
| 68 | } |
| 69 | |
| 70 | inline |
| 71 | RegexComponentSetMatcher::~RegexComponentSetMatcher() |
| 72 | { |
| 73 | // ComponentsSet::iterator it = m_components.begin(); |
| 74 | |
| 75 | // for(; it != m_components.end(); it++) |
| 76 | // delete *it; |
| 77 | } |
| 78 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 79 | inline void |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 80 | RegexComponentSetMatcher::compile() |
| 81 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 82 | switch (m_expr[0]){ |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 83 | case '<': |
| 84 | return compileSingleComponent(); |
| 85 | case '[': |
| 86 | { |
| 87 | int lastIndex = m_expr.size() - 1; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 88 | if (']' != m_expr[lastIndex]) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 89 | throw RegexMatcher::Error(std::string("Error: RegexComponentSetMatcher.compile(): ") |
| 90 | + " No matched ']' " + m_expr); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | |
| 92 | if ('^' == m_expr[1]){ |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 93 | m_include = false; |
| 94 | compileMultipleComponents(2, lastIndex); |
| 95 | } |
| 96 | else |
| 97 | compileMultipleComponents(1, lastIndex); |
| 98 | break; |
| 99 | } |
| 100 | default: |
| 101 | throw RegexMatcher::Error(std::string("Error: RegexComponentSetMatcher.compile(): ") |
| 102 | + "Parsing error in expr " + m_expr); |
| 103 | } |
| 104 | } |
| 105 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 106 | inline void |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 107 | RegexComponentSetMatcher::compileSingleComponent() |
| 108 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 109 | size_t end = extractComponent(1); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 110 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 111 | if (m_expr.size() != end) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 112 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 113 | throw RegexMatcher::Error( |
| 114 | std::string("Error: RegexComponentSetMatcher.compileSingleComponent: ") + m_expr); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 115 | } |
| 116 | else |
| 117 | { |
| 118 | shared_ptr<RegexComponentMatcher> component = |
| 119 | make_shared<RegexComponentMatcher>(m_expr.substr(1, end - 2), m_backrefManager); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 120 | |
| 121 | m_components.insert(component); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 125 | inline void |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 126 | RegexComponentSetMatcher::compileMultipleComponents(const int start, const int lastIndex) |
| 127 | { |
| 128 | int index = start; |
| 129 | int tmp_index = start; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 130 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 131 | while(index < lastIndex){ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 132 | if ('<' != m_expr[index]) |
| 133 | throw RegexMatcher::Error( |
| 134 | std::string("Error: RegexComponentSetMatcher.compileMultipleComponents: ") + |
| 135 | "Component expr error " + m_expr); |
| 136 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 137 | tmp_index = index + 1; |
| 138 | index = extractComponent(tmp_index); |
| 139 | |
| 140 | shared_ptr<RegexComponentMatcher> component = |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 141 | make_shared<RegexComponentMatcher>(m_expr.substr(tmp_index, index - tmp_index - 1), |
| 142 | m_backrefManager); |
| 143 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 144 | m_components.insert(component); |
| 145 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | |
| 147 | if (index != lastIndex) |
| 148 | throw RegexMatcher::Error( |
| 149 | std::string("Error: RegexComponentSetMatcher.compileMultipleComponents: ") + |
| 150 | "Not sufficient expr to parse " + m_expr); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 153 | inline bool |
| 154 | RegexComponentSetMatcher::match(const Name& name, const int& offset, const int& len) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 155 | { |
| 156 | bool matched = false; |
| 157 | |
| 158 | /* componentset only matches one component */ |
| 159 | if (len != 1) |
| 160 | { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | for (ComponentsSet::iterator it = m_components.begin(); |
| 165 | it != m_components.end(); |
| 166 | ++it) |
| 167 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 168 | if ((*it)->match(name, offset, len)) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 169 | { |
| 170 | matched = true; |
| 171 | break; |
| 172 | } |
| 173 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 174 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 175 | m_matchResult.clear(); |
| 176 | |
| 177 | if (m_include ? matched : !matched) |
| 178 | { |
| 179 | m_matchResult.push_back(name.get(offset)); |
| 180 | return true; |
| 181 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 182 | else |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 186 | inline int |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 187 | RegexComponentSetMatcher::extractComponent(int index) |
| 188 | { |
| 189 | int lcount = 1; |
| 190 | int rcount = 0; |
| 191 | |
| 192 | while(lcount > rcount){ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 193 | switch (m_expr[index]){ |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 194 | case '<': |
| 195 | lcount++; |
| 196 | break; |
| 197 | |
| 198 | case '>': |
| 199 | rcount++; |
| 200 | break; |
| 201 | |
| 202 | case 0: |
| 203 | throw RegexMatcher::Error("Error: square brackets mismatch"); |
| 204 | break; |
| 205 | } |
| 206 | index++; |
| 207 | |
| 208 | } |
| 209 | |
| 210 | return index; |
| 211 | } |
| 212 | |
| 213 | } // namespace ndn |
| 214 | |
| 215 | #endif // NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP |