|
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
|
|
|
>>> a = InBetweenMatch(Match("A", 0), Match("B", 1), Match("C", 2)) |
|
36
|
|
|
>>> b = InBetweenMatch.from_values("A", 0, "B", 1, "C", 2) |
|
37
|
|
|
>>> assert a == b |
|
38
|
|
|
|
|
39
|
|
|
:param begin: The matched string from start pattern. |
|
40
|
|
|
:param begin_pos: The position of the matched begin string. |
|
41
|
|
|
:param inside: The matched string from inside/in-between pattern. |
|
42
|
|
|
:param inside_pos: The position of the matched inside/in-between |
|
43
|
|
|
string. |
|
44
|
|
|
:param end: The matched string from end pattern. |
|
45
|
|
|
:param end_pos: The position of the matched end string. |
|
46
|
|
|
:returns: An InBetweenMatch from the given values. |
|
47
|
|
|
""" |
|
48
|
|
|
return cls(Match(begin, begin_pos), |
|
49
|
|
|
Match(inside, inside_pos), |
|
50
|
|
|
Match(end, end_pos)) |
|
51
|
|
|
|
|
52
|
|
|
@property |
|
53
|
|
|
def begin(self): |
|
54
|
|
|
return self._begin |
|
55
|
|
|
|
|
56
|
|
|
@property |
|
57
|
|
|
def inside(self): |
|
58
|
|
|
return self._inside |
|
59
|
|
|
|
|
60
|
|
|
@property |
|
61
|
|
|
def end(self): |
|
62
|
|
|
return self._end |
|
63
|
|
|
|