dbrownell-parserlib 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: dbrownell-parserlib
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Functionality useful when creating parsers.
5
5
  Author: Dave Brownell
6
6
  Author-email: Dave Brownell <github@DavidBrownell.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "dbrownell-parserlib"
3
- version = "0.2.0"
3
+ version = "0.3.0"
4
4
  # ^^^^^
5
5
  # Wheel names will be generated according to this value. Do not manually modify this value; instead
6
6
  # update it according to committed changes by running this command from the root of the repository:
@@ -3,11 +3,12 @@
3
3
  from importlib.metadata import version
4
4
 
5
5
  from dbrownell_ParserLib.location import Location
6
-
6
+ from dbrownell_ParserLib.region import Region
7
7
 
8
8
  # ----------------------------------------------------------------------
9
9
  __all__ = [
10
10
  "Location",
11
+ "Region",
11
12
  "__version__",
12
13
  ]
13
14
 
@@ -0,0 +1,112 @@
1
+ # noqa: D100
2
+ import inspect
3
+
4
+ from dataclasses import dataclass
5
+ from functools import cached_property
6
+ from pathlib import Path
7
+ from typing import Self
8
+
9
+ from dbrownell_ParserLib.location import Location
10
+
11
+
12
+ # ----------------------------------------------------------------------
13
+ @dataclass(frozen=True)
14
+ class Region:
15
+ """A region within a source file."""
16
+
17
+ # ----------------------------------------------------------------------
18
+ filename: Path
19
+ begin: Location
20
+ end: Location
21
+
22
+ # ----------------------------------------------------------------------
23
+ @classmethod
24
+ def CreateFromCode(
25
+ cls,
26
+ *,
27
+ callstack_offset: int = 0,
28
+ ) -> Self:
29
+ """Create a `Region` from the current code location."""
30
+
31
+ frame = inspect.stack()[callstack_offset + 1][0]
32
+ location = Location(frame.f_lineno, frame.f_lineno)
33
+
34
+ return cls(Path(frame.f_code.co_filename), location, location)
35
+
36
+ # ----------------------------------------------------------------------
37
+ def __post_init__(self) -> None:
38
+ if self.end < self.begin:
39
+ msg = "Invalid region"
40
+ raise ValueError(msg)
41
+
42
+ # ----------------------------------------------------------------------
43
+ def __str__(self) -> str:
44
+ return self._string
45
+
46
+ # ----------------------------------------------------------------------
47
+ @staticmethod
48
+ def Compare(
49
+ this: Region,
50
+ that: Region,
51
+ ) -> int:
52
+ """Compare two `Region` instances."""
53
+
54
+ if this.filename != that.filename:
55
+ return -1 if this.filename < that.filename else 1
56
+
57
+ result = Location.Compare(this.begin, that.begin)
58
+ if result != 0:
59
+ return result
60
+
61
+ result = Location.Compare(this.end, that.end)
62
+ if result != 0:
63
+ return result
64
+
65
+ return 0
66
+
67
+ # ----------------------------------------------------------------------
68
+ def __hash__(self) -> int:
69
+ return hash((self.filename, self.begin, self.end))
70
+
71
+ def __eq__(self, other: object) -> bool:
72
+ return isinstance(other, Region) and self.__class__.Compare(self, other) == 0
73
+
74
+ def __ne__(self, other: object) -> bool:
75
+ return not isinstance(other, Region) or self.__class__.Compare(self, other) != 0
76
+
77
+ def __lt__(self, other: object) -> bool:
78
+ return isinstance(other, Region) and self.__class__.Compare(self, other) < 0
79
+
80
+ def __le__(self, other: object) -> bool:
81
+ return isinstance(other, Region) and self.__class__.Compare(self, other) <= 0
82
+
83
+ def __gt__(self, other: object) -> bool:
84
+ return isinstance(other, Region) and self.__class__.Compare(self, other) > 0
85
+
86
+ def __ge__(self, other: object) -> bool:
87
+ return isinstance(other, Region) and self.__class__.Compare(self, other) >= 0
88
+
89
+ # ----------------------------------------------------------------------
90
+ def __contains__(
91
+ self,
92
+ location_or_region: Location | Region,
93
+ ) -> bool:
94
+ if isinstance(location_or_region, Location):
95
+ return self.begin <= location_or_region <= self.end
96
+
97
+ region = location_or_region
98
+
99
+ if self.filename != region.filename:
100
+ return False
101
+
102
+ return self.begin <= region.begin and region.end <= self.end
103
+
104
+ # ----------------------------------------------------------------------
105
+ # ----------------------------------------------------------------------
106
+ # ----------------------------------------------------------------------
107
+ @cached_property
108
+ def _string(self) -> str:
109
+ if self.end == self.begin:
110
+ return f"{self.filename.as_posix()}, {self.begin}"
111
+
112
+ return f"{self.filename.as_posix()}, {self.begin} - {self.end}"