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