Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #include "regex-component-set-matcher.hpp" |
| 9 | |
| 10 | #include "../logging.hpp" |
| 11 | |
| 12 | INIT_LOGGER ("RegexComponentSetMatcher"); |
| 13 | |
| 14 | using namespace std; |
| 15 | |
| 16 | namespace ndn |
| 17 | { |
| 18 | RegexComponentSetMatcher::RegexComponentSetMatcher(const string expr, ptr_lib::shared_ptr<RegexBackrefManager> backRefManager) |
| 19 | : RegexMatcher(expr, EXPR_COMPONENT_SET, backRefManager), |
| 20 | m_include(true) |
| 21 | { |
| 22 | // _LOG_TRACE ("Enter RegexComponentSetMatcher Constructor"); |
| 23 | compile(); |
| 24 | // _LOG_TRACE ("Exit RegexComponentSetMatcher Constructor"); |
| 25 | } |
| 26 | |
| 27 | RegexComponentSetMatcher::~RegexComponentSetMatcher() |
| 28 | { |
| 29 | // set<Ptr<RegexComponent> >::iterator it = m_components.begin(); |
| 30 | |
| 31 | // for(; it != m_components.end(); it++) |
| 32 | // delete *it; |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | RegexComponentSetMatcher::compile() |
| 37 | { |
| 38 | // _LOG_TRACE ("Enter RegexComponentSetMatcher::compile"); |
| 39 | |
| 40 | string errMsg = "Error: RegexComponentSetMatcher.compile(): "; |
| 41 | int index = 0; |
| 42 | |
| 43 | |
| 44 | switch(m_expr[0]){ |
| 45 | case '<': |
| 46 | return compileSingleComponent(); |
| 47 | case '[': |
| 48 | { |
| 49 | int lastIndex = m_expr.size() - 1; |
| 50 | if(']' != m_expr[lastIndex]) |
| 51 | throw RegexMatcher::Error(errMsg + " No matched ']' " + m_expr); |
| 52 | |
| 53 | if('^' == m_expr[1]){ |
| 54 | m_include = false; |
| 55 | compileMultipleComponents(2, lastIndex); |
| 56 | } |
| 57 | else |
| 58 | compileMultipleComponents(1, lastIndex); |
| 59 | break; |
| 60 | } |
| 61 | default: |
| 62 | throw RegexMatcher::Error(errMsg + "Parsing error in expr " + m_expr); |
| 63 | } |
| 64 | |
| 65 | // _LOG_TRACE ("Exit RegexComponentSetMatcher::compile"); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | RegexComponentSetMatcher::compileSingleComponent() |
| 70 | { |
| 71 | // _LOG_TRACE ("Enter RegexComponentSetMatcher::compileSingleComponent"); |
| 72 | |
| 73 | string errMsg = "Error: RegexComponentSetMatcher.compileSingleComponent: "; |
| 74 | |
| 75 | int end = extractComponent(1); |
| 76 | |
| 77 | if(m_expr.size() != end) |
| 78 | throw RegexMatcher::Error(errMsg + m_expr); |
| 79 | else{ |
| 80 | // _LOG_DEBUG ("RegexComponentSetMatcher::compileSingleComponent expr: " << m_expr.substr(1, end - 2)); |
| 81 | ptr_lib::shared_ptr<RegexComponentMatcher> component = ptr_lib::make_shared<RegexComponentMatcher>(m_expr.substr(1, end - 2), m_backrefManager); |
| 82 | m_components.insert(component); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | // _LOG_TRACE ("Exit RegexComponentSetMatcher::compileSingleComponent"); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | RegexComponentSetMatcher::compileMultipleComponents(const int start, const int lastIndex) |
| 91 | { |
| 92 | // _LOG_TRACE ("Enter RegexComponentSetMatcher::compileMultipleComponents"); |
| 93 | |
| 94 | string errMsg = "Error: RegexComponentSetMatcher.compileMultipleComponents: "; |
| 95 | |
| 96 | int index = start; |
| 97 | int tmp_index = start; |
| 98 | |
| 99 | while(index < lastIndex){ |
| 100 | if('<' != m_expr[index]) |
| 101 | throw RegexMatcher::Error(errMsg + "Component expr error " + m_expr); |
| 102 | |
| 103 | tmp_index = index + 1; |
| 104 | index = extractComponent(tmp_index); |
| 105 | |
| 106 | ptr_lib::shared_ptr<RegexComponentMatcher> component = ptr_lib::make_shared<RegexComponentMatcher>(m_expr.substr(tmp_index, index - tmp_index - 1), m_backrefManager); |
| 107 | m_components.insert(component); |
| 108 | } |
| 109 | |
| 110 | if(index != lastIndex) |
| 111 | throw RegexMatcher::Error(errMsg + "Not sufficient expr to parse " + m_expr); |
| 112 | |
| 113 | // _LOG_TRACE ("Exit RegexComponentSetMatcher::compileMultipleComponents"); |
| 114 | } |
| 115 | |
| 116 | bool |
| 117 | RegexComponentSetMatcher::match(const Name & name, const int & offset, const int & len) |
| 118 | { |
| 119 | // _LOG_TRACE ("Enter RegexComponentSetMatcher::match"); |
| 120 | |
| 121 | bool matched = false; |
| 122 | |
| 123 | /* componentset only matches one component */ |
| 124 | if(len != 1){ |
| 125 | // _LOG_DEBUG ("Match Fail: ComponentSet matches only one component"); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | set<ptr_lib::shared_ptr<RegexComponentMatcher> >::iterator it = m_components.begin(); |
| 130 | |
| 131 | for(; it != m_components.end(); it++){ |
| 132 | if((*it)->match(name, offset, len)){ |
| 133 | matched = true; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | m_matchResult.clear(); |
| 139 | |
| 140 | if(m_include ? matched : !matched){ |
| 141 | m_matchResult.push_back(name.get(offset)); |
| 142 | return true; |
| 143 | } |
| 144 | else |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | int |
| 149 | RegexComponentSetMatcher::extractComponent(int index) |
| 150 | { |
| 151 | // _LOG_TRACE ("Enter RegexComponentSetMatcher::extractComponent"); |
| 152 | |
| 153 | int lcount = 1; |
| 154 | int rcount = 0; |
| 155 | |
| 156 | while(lcount > rcount){ |
| 157 | switch(m_expr[index]){ |
| 158 | case '<': |
| 159 | lcount++; |
| 160 | break; |
| 161 | |
| 162 | case '>': |
| 163 | rcount++; |
| 164 | break; |
| 165 | |
| 166 | case 0: |
| 167 | throw RegexMatcher::Error("Error: square brackets mismatch"); |
| 168 | break; |
| 169 | } |
| 170 | index++; |
| 171 | |
| 172 | } |
| 173 | |
| 174 | // _LOG_TRACE ("Exit RegexComponentSetMatcher::extractComponent"); |
| 175 | return index; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | }//ndn |