regex-pattern-list-matcher.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 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 
27 
28 namespace ndn {
29 
31  shared_ptr<RegexBackrefManager> backrefManager)
32  : RegexMatcher(expr, EXPR_PATTERN_LIST, std::move(backrefManager))
33 {
34  compile();
35 }
36 
37 void
38 RegexPatternListMatcher::compile()
39 {
40  size_t len = m_expr.size();
41  size_t index = 0;
42  size_t subHead = index;
43 
44  while (index < len) {
45  subHead = index;
46 
47  if (!extractPattern(subHead, &index))
48  NDN_THROW(Error("Compile error"));
49  }
50 }
51 
52 bool
53 RegexPatternListMatcher::extractPattern(size_t index, size_t* next)
54 {
55  size_t start = index;
56  size_t end = index;
57  size_t indicator = index;
58 
59  switch (m_expr[index]) {
60  case '(':
61  index++;
62  index = extractSubPattern('(', ')', index);
63  indicator = index;
64  end = extractRepetition(index);
65  if (indicator == end) {
66  auto matcher = make_shared<RegexBackrefMatcher>(m_expr.substr(start, end - start),
68  m_backrefManager->pushRef(matcher);
69  matcher->compile();
70  m_matchers.push_back(std::move(matcher));
71  }
72  else {
73  m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
74  m_backrefManager, indicator - start));
75  }
76  break;
77 
78  case '<':
79  index++;
80  index = extractSubPattern('<', '>', index);
81  indicator = index;
82  end = extractRepetition(index);
83  m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
84  m_backrefManager, indicator - start));
85  break;
86 
87  case '[':
88  index++;
89  index = extractSubPattern('[', ']', index);
90  indicator = index;
91  end = extractRepetition(index);
92  m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
93  m_backrefManager, indicator - start));
94  break;
95 
96  default:
97  NDN_THROW(Error("Unexpected character "s + m_expr[index]));
98  }
99 
100  *next = end;
101  return true;
102 }
103 
104 size_t
105 RegexPatternListMatcher::extractSubPattern(const char left, const char right, size_t index)
106 {
107  size_t lcount = 1;
108  size_t rcount = 0;
109 
110  while (lcount > rcount) {
111  if (index >= m_expr.size())
112  NDN_THROW(Error("Parenthesis mismatch"));
113 
114  if (left == m_expr[index])
115  lcount++;
116 
117  if (right == m_expr[index])
118  rcount++;
119 
120  index++;
121  }
122 
123  return index;
124 }
125 
126 size_t
127 RegexPatternListMatcher::extractRepetition(size_t index)
128 {
129  size_t exprSize = m_expr.size();
130 
131  if (index == exprSize)
132  return index;
133 
134  if (('+' == m_expr[index] || '?' == m_expr[index] || '*' == m_expr[index])) {
135  return ++index;
136  }
137 
138  if ('{' == m_expr[index]) {
139  while ('}' != m_expr[index]) {
140  index++;
141  if (index == exprSize)
142  break;
143  }
144  if (index == exprSize)
145  NDN_THROW(Error("Missing closing brace"));
146  else
147  return ++index;
148  }
149  else {
150  return index;
151  }
152 }
153 
154 } // namespace ndn
shared_ptr< RegexBackrefManager > m_backrefManager
const std::string m_expr
std::vector< shared_ptr< RegexMatcher > > m_matchers
RegexPatternListMatcher(const std::string &expr, shared_ptr< RegexBackrefManager > backrefManager)
#define NDN_THROW(e)
Definition: exception.hpp:56
Definition: data.cpp:25