blob: 74ef1ce2485b2d86ce9591e73e939fd55ad7cf57 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento45ab9a92017-11-05 19:34:31 -05002/*
Junxiao Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu5e974202014-01-29 16:59:06 -080022 */
23
Davide Pesavento45ab9a92017-11-05 19:34:31 -050024#ifndef NDN_UTIL_REGEX_REGEX_MATCHER_HPP
25#define NDN_UTIL_REGEX_REGEX_MATCHER_HPP
Yingdi Yu5e974202014-01-29 16:59:06 -080026
Davide Pesavento45ab9a92017-11-05 19:34:31 -050027#include "regex-backref-manager.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080028#include "../../name.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080029
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080030namespace ndn {
31
Yingdi Yu5e974202014-01-29 16:59:06 -080032class RegexMatcher
33{
34public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070035 class Error : public std::runtime_error
36 {
37 public:
Junxiao Shi68b53852018-07-25 13:56:38 -060038 using std::runtime_error::runtime_error;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070039 };
Yingdi Yu5e974202014-01-29 16:59:06 -080040
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070041 enum RegexExprType {
Yingdi Yu5e974202014-01-29 16:59:06 -080042 EXPR_TOP,
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070043 EXPR_PATTERN_LIST,
Yingdi Yu5e974202014-01-29 16:59:06 -080044 EXPR_REPEAT_PATTERN,
Yingdi Yu5e974202014-01-29 16:59:06 -080045 EXPR_BACKREF,
46 EXPR_COMPONENT_SET,
47 EXPR_COMPONENT,
Yingdi Yu5e974202014-01-29 16:59:06 -080048 EXPR_PSEUDO
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049 };
Yingdi Yu5e974202014-01-29 16:59:06 -080050
Davide Pesavento45ab9a92017-11-05 19:34:31 -050051 RegexMatcher(const std::string& expr, const RegexExprType& type,
52 shared_ptr<RegexBackrefManager> backrefManager = nullptr);
Yingdi Yu5e974202014-01-29 16:59:06 -080053
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054 virtual
Davide Pesavento19745dc2017-11-05 19:34:31 -050055 ~RegexMatcher();
Yingdi Yu5e974202014-01-29 16:59:06 -080056
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070057 virtual bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070058 match(const Name& name, size_t offset, size_t len);
Yingdi Yu5e974202014-01-29 16:59:06 -080059
60 /**
61 * @brief get the matched name components
62 * @returns the matched name components
63 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070064 const std::vector<name::Component>&
Yingdi Yu5e974202014-01-29 16:59:06 -080065 getMatchResult() const
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070066 {
67 return m_matchResult;
68 }
Yingdi Yu5e974202014-01-29 16:59:06 -080069
70 const std::string&
71 getExpr() const
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070072 {
73 return m_expr;
74 }
Yingdi Yu5e974202014-01-29 16:59:06 -080075
76protected:
77 /**
78 * @brief Compile the regular expression to generate the more matchers when necessary
Yingdi Yu5e974202014-01-29 16:59:06 -080079 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080 virtual void
Yingdi Yu5e974202014-01-29 16:59:06 -080081 compile() = 0;
82
83private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070084 bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070085 recursiveMatch(size_t matcherNo, const Name& name, size_t offset, size_t len);
Yingdi Yu5e974202014-01-29 16:59:06 -080086
Yingdi Yu5e974202014-01-29 16:59:06 -080087protected:
88 const std::string m_expr;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089 const RegexExprType m_type;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080090 shared_ptr<RegexBackrefManager> m_backrefManager;
Davide Pesavento45ab9a92017-11-05 19:34:31 -050091 std::vector<shared_ptr<RegexMatcher>> m_matchers;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080092 std::vector<name::Component> m_matchResult;
Yingdi Yu5e974202014-01-29 16:59:06 -080093};
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080094
Davide Pesavento19745dc2017-11-05 19:34:31 -050095std::ostream&
96operator<<(std::ostream& os, const RegexMatcher& rm);
Yingdi Yu5e974202014-01-29 16:59:06 -080097
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080098} // namespace ndn
99
Davide Pesavento45ab9a92017-11-05 19:34:31 -0500100#endif // NDN_UTIL_REGEX_REGEX_MATCHER_HPP