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_PATTERN_LIST_MATCHER_HPP |
| 16 | #define NDN_UTIL_REGEX_REGEX_PATTERN_LIST_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 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 22 | namespace ndn { |
| 23 | |
| 24 | class RegexBackrefManager; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 25 | |
| 26 | class RegexPatternListMatcher : public RegexMatcher |
| 27 | { |
| 28 | public: |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 29 | RegexPatternListMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 31 | virtual ~RegexPatternListMatcher(){}; |
| 32 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 33 | protected: |
| 34 | virtual void |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 35 | compile(); |
| 36 | |
| 37 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 38 | bool |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 39 | extractPattern(int index, int* next); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 40 | |
| 41 | int |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 42 | extractSubPattern(const char left, const char right, size_t index); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 43 | |
| 44 | int |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 45 | extractRepetition(size_t index); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | |
| 49 | }; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 51 | } // namespace ndn |
| 52 | |
| 53 | #include "regex-repeat-matcher.hpp" |
| 54 | #include "regex-backref-matcher.hpp" |
| 55 | |
| 56 | namespace ndn { |
| 57 | |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 58 | inline |
| 59 | RegexPatternListMatcher::RegexPatternListMatcher(const std::string& expr, |
| 60 | shared_ptr<RegexBackrefManager> backrefManager) |
| 61 | : RegexMatcher(expr, EXPR_PATTERNLIST, backrefManager) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 62 | { |
| 63 | compile(); |
| 64 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 65 | |
| 66 | inline void |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 67 | RegexPatternListMatcher::compile() |
| 68 | { |
| 69 | const int len = m_expr.size(); |
| 70 | int index = 0; |
| 71 | int subHead = index; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 72 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 73 | while(index < len){ |
| 74 | subHead = index; |
| 75 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 76 | if (!extractPattern(subHead, &index)) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 77 | throw RegexMatcher::Error("RegexPatternListMatcher compile: cannot compile"); |
| 78 | } |
| 79 | } |
| 80 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 81 | inline bool |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 82 | RegexPatternListMatcher::extractPattern(int index, int* next) |
| 83 | { |
| 84 | // std::string errMsg = "Error: RegexPatternListMatcher.ExtractSubPattern(): "; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 86 | const int start = index; |
| 87 | int end = index; |
| 88 | int indicator = index; |
| 89 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 90 | switch (m_expr[index]){ |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 91 | case '(': |
| 92 | index++; |
| 93 | index = extractSubPattern('(', ')', index); |
| 94 | indicator = index; |
| 95 | end = extractRepetition(index); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 96 | if (indicator == end){ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 97 | shared_ptr<RegexMatcher> matcher = |
| 98 | make_shared<RegexBackrefMatcher>(m_expr.substr(start, end - start), m_backrefManager); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 99 | m_backrefManager->pushRef(matcher); |
| 100 | boost::dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile(); |
| 101 | |
| 102 | m_matcherList.push_back(matcher); |
| 103 | } |
| 104 | else |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 105 | m_matcherList.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start), |
| 106 | m_backrefManager, indicator - start)); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 107 | break; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 109 | case '<': |
| 110 | index++; |
| 111 | index = extractSubPattern ('<', '>', index); |
| 112 | indicator = index; |
| 113 | end = extractRepetition(index); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 114 | m_matcherList.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start), |
| 115 | m_backrefManager, indicator - start)); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 116 | break; |
| 117 | |
| 118 | case '[': |
| 119 | index++; |
| 120 | index = extractSubPattern ('[', ']', index); |
| 121 | indicator = index; |
| 122 | end = extractRepetition(index); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 123 | m_matcherList.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start), |
| 124 | m_backrefManager, indicator - start)); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 125 | break; |
| 126 | |
| 127 | default: |
| 128 | throw RegexMatcher::Error("Error: unexpected syntax"); |
| 129 | } |
| 130 | |
| 131 | *next = end; |
| 132 | |
| 133 | return true; |
| 134 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 135 | |
| 136 | inline int |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 137 | RegexPatternListMatcher::extractSubPattern(const char left, const char right, size_t index) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 138 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 139 | size_t lcount = 1; |
| 140 | size_t rcount = 0; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 141 | |
| 142 | while(lcount > rcount){ |
| 143 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 144 | if (index >= m_expr.size()) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 145 | throw RegexMatcher::Error("Error: parenthesis mismatch"); |
| 146 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 147 | if (left == m_expr[index]) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 148 | lcount++; |
| 149 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 150 | if (right == m_expr[index]) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 151 | rcount++; |
| 152 | |
| 153 | index++; |
| 154 | } |
| 155 | return index; |
| 156 | } |
| 157 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 158 | inline int |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 159 | RegexPatternListMatcher::extractRepetition(size_t index) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 160 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 161 | size_t exprSize = m_expr.size(); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 162 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 163 | if (index == exprSize) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 164 | return index; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 165 | |
| 166 | if (('+' == m_expr[index] || '?' == m_expr[index] || '*' == m_expr[index])){ |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 167 | return ++index; |
| 168 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 169 | |
| 170 | if ('{' == m_expr[index]){ |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 171 | while('}' != m_expr[index]){ |
| 172 | index++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 173 | if (index == exprSize) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 174 | break; |
| 175 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 176 | if (index == exprSize) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 177 | throw RegexMatcher::Error(std::string("Error: RegexPatternListMatcher.ExtractRepetition(): ") |
| 178 | + "Missing right brace bracket"); |
| 179 | else |
| 180 | return ++index; |
| 181 | } |
| 182 | else { |
| 183 | return index; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | } // namespace ndn |
| 189 | |
| 190 | #endif // NDN_UTIL_REGEX_REGEX_PATTERN_LIST_MATCHER_HPP |