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_REGEX_MATCHER_H |
| 16 | #define NDN_UTIL_REGEX_REGEX_MATCHER_H |
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 | #include "../../name.hpp" |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 20 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 21 | namespace ndn { |
| 22 | |
| 23 | class RegexBackrefManager; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 24 | |
| 25 | class RegexMatcher |
| 26 | { |
| 27 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 28 | class Error : public std::runtime_error |
| 29 | { |
| 30 | public: |
| 31 | explicit |
| 32 | Error(const std::string& what) |
| 33 | : std::runtime_error(what) |
| 34 | { |
| 35 | } |
| 36 | }; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 37 | |
| 38 | enum RegexExprType{ |
| 39 | EXPR_TOP, |
| 40 | |
| 41 | EXPR_PATTERNLIST, |
| 42 | |
| 43 | EXPR_REPEAT_PATTERN, |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 45 | EXPR_BACKREF, |
| 46 | EXPR_COMPONENT_SET, |
| 47 | EXPR_COMPONENT, |
| 48 | |
| 49 | EXPR_PSEUDO |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 50 | }; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 52 | RegexMatcher(const std::string& expr, |
| 53 | const RegexExprType& type, |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 54 | shared_ptr<RegexBackrefManager> backrefManager = shared_ptr<RegexBackrefManager>()); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 56 | virtual |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 57 | ~RegexMatcher(); |
| 58 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | virtual bool |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 60 | match(const Name& name, const int& offset, const int& len); |
| 61 | |
| 62 | /** |
| 63 | * @brief get the matched name components |
| 64 | * @returns the matched name components |
| 65 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 66 | const std::vector<name::Component>& |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 67 | getMatchResult() const |
| 68 | { return m_matchResult; } |
| 69 | |
| 70 | const std::string& |
| 71 | getExpr() const |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 72 | { return m_expr; } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 73 | |
| 74 | protected: |
| 75 | /** |
| 76 | * @brief Compile the regular expression to generate the more matchers when necessary |
| 77 | * @returns true if compiling succeeds |
| 78 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 79 | virtual void |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 80 | compile() = 0; |
| 81 | |
| 82 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 83 | bool |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 84 | recursiveMatch(size_t mId, const Name& name, size_t offset, size_t len); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 85 | |
| 86 | |
| 87 | protected: |
| 88 | const std::string m_expr; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 89 | const RegexExprType m_type; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 90 | shared_ptr<RegexBackrefManager> m_backrefManager; |
| 91 | std::vector<shared_ptr<RegexMatcher> > m_matcherList; |
| 92 | std::vector<name::Component> m_matchResult; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 93 | }; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 94 | |
| 95 | } // namespace ndn |
| 96 | |
| 97 | #include "regex-backref-manager.hpp" |
| 98 | |
| 99 | namespace ndn { |
| 100 | |
| 101 | inline |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 102 | RegexMatcher::RegexMatcher(const std::string& expr, |
| 103 | const RegexExprType& type, |
| 104 | shared_ptr<RegexBackrefManager> backrefManager) |
| 105 | : m_expr(expr), |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 106 | m_type(type), |
| 107 | m_backrefManager(backrefManager) |
| 108 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 109 | if (NULL == m_backrefManager) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 110 | m_backrefManager = make_shared<RegexBackrefManager>(); |
| 111 | } |
| 112 | |
| 113 | inline |
| 114 | RegexMatcher::~RegexMatcher() |
| 115 | { |
| 116 | } |
| 117 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 118 | inline bool |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 119 | RegexMatcher::match (const Name& name, const int& offset, const int& len) |
| 120 | { |
| 121 | // _LOG_TRACE ("Enter RegexMatcher::match"); |
| 122 | bool result = false; |
| 123 | |
| 124 | m_matchResult.clear(); |
| 125 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 126 | if (recursiveMatch(0, name, offset, len)) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 127 | { |
| 128 | for(int i = offset; i < offset + len ; i++) |
| 129 | m_matchResult.push_back(name.get(i)); |
| 130 | result = true; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | result = false; |
| 135 | } |
| 136 | |
| 137 | // _LOG_TRACE ("Exit RegexMatcher::match"); |
| 138 | return result; |
| 139 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 140 | |
| 141 | inline bool |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 142 | RegexMatcher::recursiveMatch(size_t mId, const Name& name, size_t offset, size_t len) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 143 | { |
| 144 | // _LOG_TRACE ("Enter RegexMatcher::recursiveMatch"); |
| 145 | |
| 146 | int tried = len; |
| 147 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 148 | if (mId >= m_matcherList.size()) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 149 | return (len != 0 ? false : true); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 150 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 151 | shared_ptr<RegexMatcher> matcher = m_matcherList[mId]; |
| 152 | |
| 153 | while(tried >= 0) |
| 154 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 155 | if (matcher->match(name, offset, tried) && recursiveMatch(mId + 1, name, offset + tried, len - tried)) |
| 156 | return true; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 157 | tried--; |
| 158 | } |
| 159 | |
| 160 | return false; |
| 161 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 162 | |
| 163 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 164 | } // namespace ndn |
| 165 | |
| 166 | |
| 167 | #endif // NDN_UTIL_REGEX_REGEX_MATCHER_H |