Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 24 | #ifndef NDN_UTIL_REGEX_REGEX_PATTERN_LIST_MATCHER_HPP |
| 25 | #define NDN_UTIL_REGEX_REGEX_PATTERN_LIST_MATCHER_HPP |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 27 | #include "../../common.hpp" |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 28 | |
| 29 | #include "regex-matcher.hpp" |
| 30 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 31 | namespace ndn { |
| 32 | |
| 33 | class RegexBackrefManager; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 34 | |
| 35 | class RegexPatternListMatcher : public RegexMatcher |
| 36 | { |
| 37 | public: |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 38 | RegexPatternListMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backrefManager); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 40 | protected: |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 41 | void |
| 42 | compile() override; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 43 | |
| 44 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 45 | bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 46 | extractPattern(size_t index, size_t* next); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 47 | |
| 48 | int |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 49 | extractSubPattern(const char left, const char right, size_t index); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 50 | |
| 51 | int |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 52 | extractRepetition(size_t index); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 53 | }; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 55 | } // namespace ndn |
| 56 | |
| 57 | #include "regex-repeat-matcher.hpp" |
| 58 | #include "regex-backref-matcher.hpp" |
| 59 | |
| 60 | namespace ndn { |
| 61 | |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 62 | inline |
| 63 | RegexPatternListMatcher::RegexPatternListMatcher(const std::string& expr, |
| 64 | shared_ptr<RegexBackrefManager> backrefManager) |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 65 | : RegexMatcher(expr, EXPR_PATTERN_LIST, backrefManager) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 66 | { |
| 67 | compile(); |
| 68 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 69 | |
| 70 | inline void |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 71 | RegexPatternListMatcher::compile() |
| 72 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 73 | size_t len = m_expr.size(); |
| 74 | size_t index = 0; |
| 75 | size_t subHead = index; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 76 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 77 | while (index < len) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 78 | subHead = index; |
| 79 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 80 | if (!extractPattern(subHead, &index)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 81 | BOOST_THROW_EXCEPTION(RegexMatcher::Error("Compile error")); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 85 | inline bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 86 | RegexPatternListMatcher::extractPattern(size_t index, size_t* next) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 87 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 88 | size_t start = index; |
| 89 | size_t end = index; |
| 90 | size_t indicator = index; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 92 | switch (m_expr[index]) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 93 | case '(': |
| 94 | index++; |
| 95 | index = extractSubPattern('(', ')', index); |
| 96 | indicator = index; |
| 97 | end = extractRepetition(index); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 98 | if (indicator == end) { |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 99 | shared_ptr<RegexMatcher> matcher = |
| 100 | make_shared<RegexBackrefMatcher>(m_expr.substr(start, end - start), m_backrefManager); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 101 | m_backrefManager->pushRef(matcher); |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 102 | dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile(); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 104 | m_matchers.push_back(matcher); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 105 | } |
| 106 | else |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 107 | m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start), |
| 108 | m_backrefManager, indicator - start)); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 109 | break; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 111 | case '<': |
| 112 | index++; |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 113 | index = extractSubPattern('<', '>', index); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 114 | indicator = index; |
| 115 | end = extractRepetition(index); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 116 | m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start), |
| 117 | m_backrefManager, indicator - start)); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 118 | break; |
| 119 | |
| 120 | case '[': |
| 121 | index++; |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 122 | index = extractSubPattern('[', ']', index); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 123 | indicator = index; |
| 124 | end = extractRepetition(index); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 125 | m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start), |
| 126 | m_backrefManager, indicator - start)); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 127 | break; |
| 128 | |
| 129 | default: |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 130 | BOOST_THROW_EXCEPTION(RegexMatcher::Error("Unexpected syntax")); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | *next = end; |
| 134 | |
| 135 | return true; |
| 136 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 137 | |
| 138 | inline int |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 139 | RegexPatternListMatcher::extractSubPattern(const char left, const char right, size_t index) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 140 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 141 | size_t lcount = 1; |
| 142 | size_t rcount = 0; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 144 | while (lcount > rcount) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | if (index >= m_expr.size()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 147 | BOOST_THROW_EXCEPTION(RegexMatcher::Error("Parenthesis mismatch")); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 149 | if (left == m_expr[index]) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 150 | lcount++; |
| 151 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 152 | if (right == m_expr[index]) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 153 | rcount++; |
| 154 | |
| 155 | index++; |
| 156 | } |
| 157 | return index; |
| 158 | } |
| 159 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 160 | inline int |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 161 | RegexPatternListMatcher::extractRepetition(size_t index) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 162 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 163 | size_t exprSize = m_expr.size(); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 165 | if (index == exprSize) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 166 | return index; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 167 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 168 | if (('+' == m_expr[index] || '?' == m_expr[index] || '*' == m_expr[index])) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 169 | return ++index; |
| 170 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 171 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 172 | if ('{' == m_expr[index]) { |
| 173 | while ('}' != m_expr[index]) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 174 | index++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 175 | if (index == exprSize) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 176 | break; |
| 177 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 178 | if (index == exprSize) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 179 | BOOST_THROW_EXCEPTION(RegexMatcher::Error("Missing right brace bracket")); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 180 | else |
| 181 | return ++index; |
| 182 | } |
| 183 | else { |
| 184 | return index; |
| 185 | } |
| 186 | } |
| 187 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 188 | } // namespace ndn |
| 189 | |
| 190 | #endif // NDN_UTIL_REGEX_REGEX_PATTERN_LIST_MATCHER_HPP |