blob: 9f1844117a68fe8d7fe4313079bfb4b390a78fd3 [file] [log] [blame]
Davide Pesavento19745dc2017-11-05 19:34:31 -05001/* -*- 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#include "regex-pattern-list-matcher.hpp"
25#include "regex-backref-matcher.hpp"
26#include "regex-repeat-matcher.hpp"
27
28namespace ndn {
29
30RegexPatternListMatcher::RegexPatternListMatcher(const std::string& expr,
31 shared_ptr<RegexBackrefManager> backrefManager)
32 : RegexMatcher(expr, EXPR_PATTERN_LIST, std::move(backrefManager))
33{
34 compile();
35}
36
37void
38RegexPatternListMatcher::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 BOOST_THROW_EXCEPTION(Error("Compile error"));
49 }
50}
51
52bool
53RegexPatternListMatcher::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),
67 m_backrefManager);
68 m_backrefManager->pushRef(matcher);
69 matcher->lateCompile();
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 break;
76
77 case '<':
78 index++;
79 index = extractSubPattern('<', '>', index);
80 indicator = index;
81 end = extractRepetition(index);
82 m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
83 m_backrefManager, indicator - start));
84 break;
85
86 case '[':
87 index++;
88 index = extractSubPattern('[', ']', index);
89 indicator = index;
90 end = extractRepetition(index);
91 m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
92 m_backrefManager, indicator - start));
93 break;
94
95 default:
96 BOOST_THROW_EXCEPTION(Error("Unexpected syntax"));
97 }
98
99 *next = end;
100 return true;
101}
102
103size_t
104RegexPatternListMatcher::extractSubPattern(const char left, const char right, size_t index)
105{
106 size_t lcount = 1;
107 size_t rcount = 0;
108
109 while (lcount > rcount) {
110 if (index >= m_expr.size())
111 BOOST_THROW_EXCEPTION(Error("Parenthesis mismatch"));
112
113 if (left == m_expr[index])
114 lcount++;
115
116 if (right == m_expr[index])
117 rcount++;
118
119 index++;
120 }
121
122 return index;
123}
124
125size_t
126RegexPatternListMatcher::extractRepetition(size_t index)
127{
128 size_t exprSize = m_expr.size();
129
130 if (index == exprSize)
131 return index;
132
133 if (('+' == m_expr[index] || '?' == m_expr[index] || '*' == m_expr[index])) {
134 return ++index;
135 }
136
137 if ('{' == m_expr[index]) {
138 while ('}' != m_expr[index]) {
139 index++;
140 if (index == exprSize)
141 break;
142 }
143 if (index == exprSize)
144 BOOST_THROW_EXCEPTION(Error("Missing right brace bracket"));
145 else
146 return ++index;
147 }
148 else {
149 return index;
150 }
151}
152
153} // namespace ndn