blob: 46d85275fc326011d56881e347d14e6447b204ac [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento45ab9a92017-11-05 19:34:31 -05002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu5e974202014-01-29 16:59:06 -080022 */
23
Davide Pesavento45ab9a92017-11-05 19:34:31 -050024#ifndef NDN_UTIL_REGEX_REGEX_COMPONENT_SET_MATCHER_HPP
25#define NDN_UTIL_REGEX_REGEX_COMPONENT_SET_MATCHER_HPP
Yingdi Yu5e974202014-01-29 16:59:06 -080026
Yingdi Yu5e974202014-01-29 16:59:06 -080027#include "regex-component-matcher.hpp"
Davide Pesavento45ab9a92017-11-05 19:34:31 -050028#include "regex-matcher.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080029
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030#include <set>
31
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080032namespace ndn {
Yingdi Yu5e974202014-01-29 16:59:06 -080033
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070034class RegexComponentSetMatcher : public RegexMatcher
35{
Yingdi Yu5e974202014-01-29 16:59:06 -080036public:
37 /**
38 * @brief Create a RegexComponentSetMatcher matcher from expr
39 * @param expr The standard regular expression to match a component
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070040 * @param backrefManager Shared pointer to back-reference manager
Yingdi Yu5e974202014-01-29 16:59:06 -080041 */
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070042 RegexComponentSetMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backrefManager);
Yingdi Yu5e974202014-01-29 16:59:06 -080043
Davide Pesavento45ab9a92017-11-05 19:34:31 -050044 bool
45 match(const Name& name, size_t offset, size_t len = 1) override;
Yingdi Yu5e974202014-01-29 16:59:06 -080046
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047protected:
Yingdi Yu5e974202014-01-29 16:59:06 -080048 /**
49 * @brief Compile the regular expression to generate the more matchers when necessary
Yingdi Yu5e974202014-01-29 16:59:06 -080050 */
Davide Pesavento45ab9a92017-11-05 19:34:31 -050051 void
52 compile() override;
Yingdi Yu5e974202014-01-29 16:59:06 -080053
54private:
Yingdi Yu5e974202014-01-29 16:59:06 -080055 void
56 compileSingleComponent();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070057
Yingdi Yu5e974202014-01-29 16:59:06 -080058 void
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070059 compileMultipleComponents(size_t start, size_t lastIndex);
Yingdi Yu5e974202014-01-29 16:59:06 -080060
Davide Pesavento45ab9a92017-11-05 19:34:31 -050061 size_t
62 extractComponent(size_t index) const;
63
Yingdi Yu5e974202014-01-29 16:59:06 -080064private:
Davide Pesavento45ab9a92017-11-05 19:34:31 -050065 std::set<shared_ptr<RegexComponentMatcher>> m_components;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070066 bool m_isInclusion;
Yingdi Yu5e974202014-01-29 16:59:06 -080067};
68
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080069inline
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070RegexComponentSetMatcher::RegexComponentSetMatcher(const std::string& expr,
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070071 shared_ptr<RegexBackrefManager> backrefManager)
Davide Pesavento45ab9a92017-11-05 19:34:31 -050072 : RegexMatcher(expr, EXPR_COMPONENT_SET, std::move(backrefManager))
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070073 , m_isInclusion(true)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080074{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080075 compile();
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080076}
77
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078inline void
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080079RegexComponentSetMatcher::compile()
80{
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070081 if (m_expr.size() < 2)
Davide Pesavento45ab9a92017-11-05 19:34:31 -050082 BOOST_THROW_EXCEPTION(Error("Regexp compile error (cannot parse " + m_expr + ")"));
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070083
84 switch (m_expr[0]) {
Davide Pesavento45ab9a92017-11-05 19:34:31 -050085 case '<':
86 return compileSingleComponent();
87 case '[': {
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070088 size_t lastIndex = m_expr.size() - 1;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089 if (']' != m_expr[lastIndex])
Davide Pesavento45ab9a92017-11-05 19:34:31 -050090 BOOST_THROW_EXCEPTION(Error("Regexp compile error (no matching ']' in " + m_expr + ")"));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070092 if ('^' == m_expr[1]) {
93 m_isInclusion = false;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080094 compileMultipleComponents(2, lastIndex);
95 }
96 else
97 compileMultipleComponents(1, lastIndex);
98 break;
99 }
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500100 default:
101 BOOST_THROW_EXCEPTION(Error("Regexp compile error (cannot parse " + m_expr + ")"));
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800102 }
103}
104
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700105inline void
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800106RegexComponentSetMatcher::compileSingleComponent()
107{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700108 size_t end = extractComponent(1);
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700109 if (m_expr.size() != end)
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500110 BOOST_THROW_EXCEPTION(Error("Component expr error " + m_expr));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500112 m_components.insert(make_shared<RegexComponentMatcher>(m_expr.substr(1, end - 2),
113 m_backrefManager));
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800114}
115
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700116inline void
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700117RegexComponentSetMatcher::compileMultipleComponents(size_t start, size_t lastIndex)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800118{
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700119 size_t index = start;
120 size_t tempIndex = start;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700121
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700122 while (index < lastIndex) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 if ('<' != m_expr[index])
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500124 BOOST_THROW_EXCEPTION(Error("Component expr error " + m_expr));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700126 tempIndex = index + 1;
127 index = extractComponent(tempIndex);
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500128 m_components.insert(make_shared<RegexComponentMatcher>(m_expr.substr(tempIndex, index - tempIndex - 1),
129 m_backrefManager));
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800130 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700131
132 if (index != lastIndex)
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500133 BOOST_THROW_EXCEPTION(Error("Not sufficient expr to parse " + m_expr));
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800134}
135
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136inline bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700137RegexComponentSetMatcher::match(const Name& name, size_t offset, size_t len)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800138{
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500139 // componentset only matches one component
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800140 if (len != 1)
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500141 return false;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800142
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500143 bool isMatched = false;
144 for (const auto& comp : m_components) {
145 if (comp->match(name, offset, len)) {
146 isMatched = true;
147 break;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800148 }
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500149 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700150
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800151 m_matchResult.clear();
152
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500153 if (m_isInclusion ? isMatched : !isMatched) {
154 m_matchResult.push_back(name.get(offset));
155 return true;
156 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700157 else
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800158 return false;
159}
160
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700161inline size_t
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500162RegexComponentSetMatcher::extractComponent(size_t index) const
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800163{
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700164 size_t lcount = 1;
165 size_t rcount = 0;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800166
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700167 while (lcount > rcount) {
168 switch (m_expr[index]) {
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500169 case '<':
170 lcount++;
171 break;
172 case '>':
173 rcount++;
174 break;
175 case 0:
176 BOOST_THROW_EXCEPTION(Error("Square brackets mismatch"));
177 break;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800178 }
179 index++;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800180 }
181
182 return index;
183}
184
185} // namespace ndn
186
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500187#endif // NDN_UTIL_REGEX_REGEX_COMPONENT_SET_MATCHER_HPP