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