Failed Conditions
Pull Request — master (#1127)
by Mischa
01:56
created

coalib.parsing.StringProcessing.InBetweenMatch   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %
Metric Value
dl 0
loc 60
rs 10
wmc 7
1
from coalib.misc.Decorators import generate_ordering, generate_repr
2
from coalib.parsing.StringProcessing import Match
3
4
5
@generate_repr("begin", "inside", "end")
6
@generate_ordering("begin", "inside", "end")
7
class InBetweenMatch:
8
    """
9
    Holds information about a match enclosed by two matches.
10
    """
11
12
    def __init__(self, begin, inside, end):
13
        """
14
        Instantiates a new InBetweenMatch.
15
16
        :param begin:  The `Match` of the start pattern.
17
        :param inside: The `Match` between start and end.
18
        :param end:    The `Match` of the end pattern.
19
        """
20
        if begin > inside or inside > end:
21
            raise ValueError("The inside match must be enclosed by the begin "
22
                             "and end match.")
23
24
        self._begin = begin
25
        self._inside = inside
26
        self._end = end
27
28
    @classmethod
29
    def from_values(cls, begin, begin_pos, inside, inside_pos, end, end_pos):
30
        """
31
        Instantiates a new InBetweenMatch from Match values.
32
33
        This function allows to bypass the usage of Match object instantation:
34
35
        >>> InBetweenMatch(Match("A", 0), Match("B", 1), Match("B", 2))
36
37
        can be simplified to:
38
39
        >>> InBetweenMatch.from_values("A", 0, "B", 1, "C", 2)
40
41
        :param begin:      The matched string from start pattern.
42
        :param begin_pos:  The position of the matched begin string.
43
        :param inside:     The matched string from inside/in-between pattern.
44
        :param inside_pos: The position of the matched inside/in-between
45
                           string.
46
        :param end:        The matched string from end pattern.
47
        :param end_pos:    The position of the matched end string.
48
        :returns:          An InBetweenMatch from the given values.
49
        """
50
        return cls(Match(begin, begin_pos),
51
                   Match(inside, inside_pos),
52
                   Match(end, end_pos))
53
54
    @property
55
    def begin(self):
56
        return self._begin
57
58
    @property
59
    def inside(self):
60
        return self._inside
61
62
    @property
63
    def end(self):
64
        return self._end
65