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