blob: b5838fa0233b4ccd5c870a83da3652d3230aec3f [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu5e974202014-01-29 16:59:06 -08002/**
Qiuhan Ding699725d2015-05-24 01:41:09 -07003 * Copyright (c) 2013-2015 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.
Yingdi Yu5e974202014-01-29 16:59:06 -080020 */
21
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080022#ifndef NDN_UTIL_REGEX_REGEX_MATCHER_H
23#define NDN_UTIL_REGEX_REGEX_MATCHER_H
Yingdi Yu5e974202014-01-29 16:59:06 -080024
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080025#include "../../common.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080026#include "../../name.hpp"
Qiuhan Ding699725d2015-05-24 01:41:09 -070027#include "regex-backref-manager.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080028
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080029namespace ndn {
30
Yingdi Yu5e974202014-01-29 16:59:06 -080031class RegexMatcher
32{
33public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070034 class Error : public std::runtime_error
35 {
36 public:
37 explicit
38 Error(const std::string& what)
39 : std::runtime_error(what)
40 {
41 }
42 };
Yingdi Yu5e974202014-01-29 16:59:06 -080043
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070044 enum RegexExprType {
Yingdi Yu5e974202014-01-29 16:59:06 -080045 EXPR_TOP,
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070046 EXPR_PATTERN_LIST,
Yingdi Yu5e974202014-01-29 16:59:06 -080047 EXPR_REPEAT_PATTERN,
Yingdi Yu5e974202014-01-29 16:59:06 -080048 EXPR_BACKREF,
49 EXPR_COMPONENT_SET,
50 EXPR_COMPONENT,
Yingdi Yu5e974202014-01-29 16:59:06 -080051 EXPR_PSEUDO
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052 };
Yingdi Yu5e974202014-01-29 16:59:06 -080053
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054 RegexMatcher(const std::string& expr,
55 const RegexExprType& type,
Qiuhan Ding699725d2015-05-24 01:41:09 -070056 shared_ptr<RegexBackrefManager> backrefManager = nullptr);
Yingdi Yu5e974202014-01-29 16:59:06 -080057
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058 virtual
Yingdi Yu5e974202014-01-29 16:59:06 -080059 ~RegexMatcher();
60
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061 virtual bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070062 match(const Name& name, size_t offset, size_t len);
Yingdi Yu5e974202014-01-29 16:59:06 -080063
64 /**
65 * @brief get the matched name components
66 * @returns the matched name components
67 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070068 const std::vector<name::Component>&
Yingdi Yu5e974202014-01-29 16:59:06 -080069 getMatchResult() const
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070070 {
71 return m_matchResult;
72 }
Yingdi Yu5e974202014-01-29 16:59:06 -080073
74 const std::string&
75 getExpr() const
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070076 {
77 return m_expr;
78 }
Yingdi Yu5e974202014-01-29 16:59:06 -080079
80protected:
81 /**
82 * @brief Compile the regular expression to generate the more matchers when necessary
83 * @returns true if compiling succeeds
84 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070085 virtual void
Yingdi Yu5e974202014-01-29 16:59:06 -080086 compile() = 0;
87
88private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089 bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070090 recursiveMatch(size_t matcherNo, const Name& name, size_t offset, size_t len);
Yingdi Yu5e974202014-01-29 16:59:06 -080091
92
93protected:
94 const std::string m_expr;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095 const RegexExprType m_type;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080096 shared_ptr<RegexBackrefManager> m_backrefManager;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070097 std::vector<shared_ptr<RegexMatcher> > m_matchers;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080098 std::vector<name::Component> m_matchResult;
Yingdi Yu5e974202014-01-29 16:59:06 -080099};
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800100
Alexander Afanasyev90164962014-03-06 08:29:59 +0000101inline std::ostream&
102operator<<(std::ostream& os, const RegexMatcher& regex)
103{
104 os << regex.getExpr();
105 return os;
106}
107
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800108} // namespace ndn
109
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800110#endif // NDN_UTIL_REGEX_REGEX_MATCHER_H