NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
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  virtual
42  {
43  };
44 
45 protected:
46  virtual void
47  compile();
48 
49 private:
50  bool
51  extractPattern(size_t index, size_t* next);
52 
53  int
54  extractSubPattern(const char left, const char right, size_t index);
55 
56  int
57  extractRepetition(size_t index);
58 
59 private:
60 
61 };
62 
63 } // namespace ndn
64 
65 #include "regex-repeat-matcher.hpp"
67 
68 namespace ndn {
69 
70 inline
72  shared_ptr<RegexBackrefManager> backrefManager)
73  : RegexMatcher(expr, EXPR_PATTERN_LIST, backrefManager)
74 {
75  compile();
76 }
77 
78 inline void
80 {
81  size_t len = m_expr.size();
82  size_t index = 0;
83  size_t subHead = index;
84 
85  while (index < len) {
86  subHead = index;
87 
88  if (!extractPattern(subHead, &index))
89  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Compile error"));
90  }
91 }
92 
93 inline bool
94 RegexPatternListMatcher::extractPattern(size_t index, size_t* next)
95 {
96  size_t start = index;
97  size_t end = index;
98  size_t indicator = index;
99 
100  switch (m_expr[index]) {
101  case '(':
102  index++;
103  index = extractSubPattern('(', ')', index);
104  indicator = index;
105  end = extractRepetition(index);
106  if (indicator == end) {
107  shared_ptr<RegexMatcher> matcher =
108  make_shared<RegexBackrefMatcher>(m_expr.substr(start, end - start), m_backrefManager);
109  m_backrefManager->pushRef(matcher);
110  dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile();
111 
112  m_matchers.push_back(matcher);
113  }
114  else
115  m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
116  m_backrefManager, indicator - start));
117  break;
118 
119  case '<':
120  index++;
121  index = extractSubPattern('<', '>', index);
122  indicator = index;
123  end = extractRepetition(index);
124  m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
125  m_backrefManager, indicator - start));
126  break;
127 
128  case '[':
129  index++;
130  index = extractSubPattern('[', ']', index);
131  indicator = index;
132  end = extractRepetition(index);
133  m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
134  m_backrefManager, indicator - start));
135  break;
136 
137  default:
138  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Unexpected syntax"));
139  }
140 
141  *next = end;
142 
143  return true;
144 }
145 
146 inline int
147 RegexPatternListMatcher::extractSubPattern(const char left, const char right, size_t index)
148 {
149  size_t lcount = 1;
150  size_t rcount = 0;
151 
152  while (lcount > rcount) {
153 
154  if (index >= m_expr.size())
155  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Parenthesis mismatch"));
156 
157  if (left == m_expr[index])
158  lcount++;
159 
160  if (right == m_expr[index])
161  rcount++;
162 
163  index++;
164  }
165  return index;
166 }
167 
168 inline int
169 RegexPatternListMatcher::extractRepetition(size_t index)
170 {
171  size_t exprSize = m_expr.size();
172 
173  if (index == exprSize)
174  return index;
175 
176  if (('+' == m_expr[index] || '?' == m_expr[index] || '*' == m_expr[index])) {
177  return ++index;
178  }
179 
180  if ('{' == m_expr[index]) {
181  while ('}' != m_expr[index]) {
182  index++;
183  if (index == exprSize)
184  break;
185  }
186  if (index == exprSize)
187  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Missing right brace bracket"));
188  else
189  return ++index;
190  }
191  else {
192  return index;
193  }
194 }
195 
196 
197 } // namespace ndn
198 
199 #endif // NDN_UTIL_REGEX_REGEX_PATTERN_LIST_MATCHER_HPP
Copyright (c) 2011-2015 Regents of the University of California.
virtual void compile()
Compile the regular expression to generate the more matchers when necessary.
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