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