NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
regex-pattern-list-matcher.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
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.
20  *
21  * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22  */
23 
24 #ifndef NDN_UTIL_REGEX_REGEX_PATTERN_LIST_MATCHER_HPP
25 #define NDN_UTIL_REGEX_REGEX_PATTERN_LIST_MATCHER_HPP
26 
27 #include "../../common.hpp"
28 
29 #include "regex-matcher.hpp"
30 
31 namespace ndn {
32 
33 class RegexBackrefManager;
34 
36 {
37 public:
38  RegexPatternListMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backrefManager);
39 
40 protected:
41  void
42  compile() override;
43 
44 private:
45  bool
46  extractPattern(size_t index, size_t* next);
47 
48  int
49  extractSubPattern(const char left, const char right, size_t index);
50 
51  int
52  extractRepetition(size_t index);
53 };
54 
55 } // namespace ndn
56 
57 #include "regex-repeat-matcher.hpp"
59 
60 namespace ndn {
61 
62 inline
64  shared_ptr<RegexBackrefManager> backrefManager)
65  : RegexMatcher(expr, EXPR_PATTERN_LIST, backrefManager)
66 {
67  compile();
68 }
69 
70 inline void
72 {
73  size_t len = m_expr.size();
74  size_t index = 0;
75  size_t subHead = index;
76 
77  while (index < len) {
78  subHead = index;
79 
80  if (!extractPattern(subHead, &index))
81  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Compile error"));
82  }
83 }
84 
85 inline bool
86 RegexPatternListMatcher::extractPattern(size_t index, size_t* next)
87 {
88  size_t start = index;
89  size_t end = index;
90  size_t indicator = index;
91 
92  switch (m_expr[index]) {
93  case '(':
94  index++;
95  index = extractSubPattern('(', ')', index);
96  indicator = index;
97  end = extractRepetition(index);
98  if (indicator == end) {
99  shared_ptr<RegexMatcher> matcher =
100  make_shared<RegexBackrefMatcher>(m_expr.substr(start, end - start), m_backrefManager);
101  m_backrefManager->pushRef(matcher);
102  dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile();
103 
104  m_matchers.push_back(matcher);
105  }
106  else
107  m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
108  m_backrefManager, indicator - start));
109  break;
110 
111  case '<':
112  index++;
113  index = extractSubPattern('<', '>', index);
114  indicator = index;
115  end = extractRepetition(index);
116  m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
117  m_backrefManager, indicator - start));
118  break;
119 
120  case '[':
121  index++;
122  index = extractSubPattern('[', ']', index);
123  indicator = index;
124  end = extractRepetition(index);
125  m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
126  m_backrefManager, indicator - start));
127  break;
128 
129  default:
130  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Unexpected syntax"));
131  }
132 
133  *next = end;
134 
135  return true;
136 }
137 
138 inline int
139 RegexPatternListMatcher::extractSubPattern(const char left, const char right, size_t index)
140 {
141  size_t lcount = 1;
142  size_t rcount = 0;
143 
144  while (lcount > rcount) {
145 
146  if (index >= m_expr.size())
147  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Parenthesis mismatch"));
148 
149  if (left == m_expr[index])
150  lcount++;
151 
152  if (right == m_expr[index])
153  rcount++;
154 
155  index++;
156  }
157  return index;
158 }
159 
160 inline int
161 RegexPatternListMatcher::extractRepetition(size_t index)
162 {
163  size_t exprSize = m_expr.size();
164 
165  if (index == exprSize)
166  return index;
167 
168  if (('+' == m_expr[index] || '?' == m_expr[index] || '*' == m_expr[index])) {
169  return ++index;
170  }
171 
172  if ('{' == m_expr[index]) {
173  while ('}' != m_expr[index]) {
174  index++;
175  if (index == exprSize)
176  break;
177  }
178  if (index == exprSize)
179  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Missing right brace bracket"));
180  else
181  return ++index;
182  }
183  else {
184  return index;
185  }
186 }
187 
188 } // namespace ndn
189 
190 #endif // NDN_UTIL_REGEX_REGEX_PATTERN_LIST_MATCHER_HPP
Copyright (c) 2011-2015 Regents of the University of California.
shared_ptr< RegexBackrefManager > m_backrefManager
RegexPatternListMatcher(const std::string &expr, shared_ptr< RegexBackrefManager > backrefManager)
std::vector< shared_ptr< RegexMatcher > > m_matchers
const std::string m_expr
void compile() override
Compile the regular expression to generate the more matchers when necessary.