def _extractPattern(self, index, next):
        """
        :param int index:
        :param Array<int> next: Update next[0]
        :rtype: bool
        """
        start = index
        end = index
        indicator = index

        if self._expr[index] == '(':
            index += 1
            index = self._extractSubPattern('(', ')', index)
            indicator = index
            end = self._extractRepetition(index)
            if indicator == end:
                matcher = NdnRegexBackrefMatcher(self._expr[start:end],
                                                 self._backrefManager)
                self._backrefManager.pushRef(matcher)
                matcher.lateCompile()

                self._matchers.append(matcher)
            else:
                self._matchers.append(
                    NdnRegexRepeatMatcher(self._expr[start:end],
                                          self._backrefManager,
                                          indicator - start))
        elif self._expr[index] == '<':
            index += 1
            index = self._extractSubPattern('<', '>', index)
            indicator = index
            end = self._extractRepetition(index)
            self._matchers.append(
                NdnRegexRepeatMatcher(self._expr[start:end],
                                      self._backrefManager, indicator - start))
        elif self._expr[index] == '[':
            index += 1
            index = self._extractSubPattern('[', ']', index)
            indicator = index
            end = self._extractRepetition(index)
            self._matchers.append(
                NdnRegexRepeatMatcher(self._expr[start:end],
                                      self._backrefManager, indicator - start))
        else:
            raise NdnRegexMatcherBase.Error("Unexpected syntax")

        next[0] = end

        return True
Ejemplo n.º 2
0
    def _compile(self):
        """
        Compile the regular expression to generate more matchers when necessary.
        """
        if '(' == self._expr[0]:
            matcher = NdnRegexBackrefMatcher(self._expr[0:self._indicator],
                                             self._backrefManager)
            self._backrefManager.pushRef(matcher)
            matcher.lateCompile()
        else:
            matcher = NdnRegexComponentSetMatcher(
                self._expr[0:self._indicator], self._backrefManager)

        self._matchers.append(matcher)

        self._parseRepetition()
    def _extractPattern(self, index, next):
        """
        :param int index:
        :param Array<int> next: Update next[0]
        :rtype: bool
        """
        start = index
        end = index
        indicator = index

        if self._expr[index] == '(':
            index += 1
            index = self._extractSubPattern('(', ')', index)
            indicator = index
            end = self._extractRepetition(index)
            if indicator == end:
                matcher = NdnRegexBackrefMatcher(
                  self._expr[start : end], self._backrefManager)
                self._backrefManager.pushRef(matcher)
                matcher.lateCompile()

                self._matchers.append(matcher)
            else:
                self._matchers.append(NdnRegexRepeatMatcher
                  (self._expr[start : end], self._backrefManager,
                   indicator - start))
        elif self._expr[index] == '<':
            index += 1
            index = self._extractSubPattern('<', '>', index)
            indicator = index
            end = self._extractRepetition(index)
            self._matchers.append(NdnRegexRepeatMatcher
              (self._expr[start : end], self._backrefManager, indicator - start))
        elif self._expr[index] == '[':
            index += 1
            index = self._extractSubPattern('[', ']', index)
            indicator = index
            end = self._extractRepetition(index)
            self._matchers.append(NdnRegexRepeatMatcher
              (self._expr[start : end], self._backrefManager, indicator - start))
        else:
            raise NdnRegexMatcherBase.Error("Unexpected syntax")

        next[0] = end

        return True
Ejemplo n.º 4
0
    def test_backref_matcher(self):
        backRef = NdnRegexBackrefManager()
        cm = NdnRegexBackrefMatcher("(<a><b>)", backRef)
        backRef.pushRef(cm)
        cm.lateCompile()
        res = cm.match(Name("/a/b/c"), 0, 2)
        self.assertEqual(True, res)
        self.assertEqual(2, len(cm.getMatchResult()))
        self.assertEqual("a", cm.getMatchResult()[0].toEscapedString())
        self.assertEqual("b", cm.getMatchResult()[1].toEscapedString())
        self.assertEqual(1, backRef.size())

        backRef = NdnRegexBackrefManager()
        cm = NdnRegexBackrefMatcher("(<a>(<b>))", backRef)
        backRef.pushRef(cm)
        cm.lateCompile()
        res = cm.match(Name("/a/b/c"), 0, 2)
        self.assertEqual(True, res)
        self.assertEqual(2, len(cm.getMatchResult()))
        self.assertEqual("a", cm.getMatchResult()[0].toEscapedString())
        self.assertEqual("b", cm.getMatchResult()[1].toEscapedString())
        self.assertEqual(2, backRef.size())
        self.assertEqual("a",
          backRef.getBackref(0).getMatchResult()[0].toEscapedString())
        self.assertEqual("b",
          backRef.getBackref(0).getMatchResult()[1].toEscapedString())
        self.assertEqual("b",
          backRef.getBackref(1).getMatchResult()[0].toEscapedString())