dbrownell-parserlib 0.1.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.
- {dbrownell_parserlib-0.1.0 → dbrownell_parserlib-0.3.0}/PKG-INFO +1 -1
- {dbrownell_parserlib-0.1.0 → dbrownell_parserlib-0.3.0}/pyproject.toml +2 -1
- dbrownell_parserlib-0.3.0/src/dbrownell_ParserLib/__init__.py +16 -0
- dbrownell_parserlib-0.3.0/src/dbrownell_ParserLib/location.py +74 -0
- dbrownell_parserlib-0.3.0/src/dbrownell_ParserLib/region.py +112 -0
- dbrownell_parserlib-0.1.0/src/dbrownell_ParserLib/UvScaffolding_sample_file.py +0 -11
- dbrownell_parserlib-0.1.0/src/dbrownell_ParserLib/__init__.py +0 -6
- {dbrownell_parserlib-0.1.0 → dbrownell_parserlib-0.3.0}/README.md +0 -0
- {dbrownell_parserlib-0.1.0 → dbrownell_parserlib-0.3.0}/src/dbrownell_ParserLib/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "dbrownell-parserlib"
|
|
3
|
-
version = "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:
|
|
@@ -47,6 +47,7 @@ dev = [
|
|
|
47
47
|
|
|
48
48
|
[tool.pytest.ini_options]
|
|
49
49
|
addopts = "--verbose -vv --capture=no --cov=dbrownell_ParserLib --cov-report html --cov-report term --cov-report xml:coverage.xml --cov-fail-under=95.0"
|
|
50
|
+
python_files = ["tests/*Tests.py"]
|
|
50
51
|
|
|
51
52
|
[tool.ruff]
|
|
52
53
|
line-length = 110
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# noqa: D104
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
from dbrownell_ParserLib.location import Location
|
|
6
|
+
from dbrownell_ParserLib.region import Region
|
|
7
|
+
|
|
8
|
+
# ----------------------------------------------------------------------
|
|
9
|
+
__all__ = [
|
|
10
|
+
"Location",
|
|
11
|
+
"Region",
|
|
12
|
+
"__version__",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
__version__ = version("dbrownell_ParserLib")
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# noqa: D100
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from functools import cached_property
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# ----------------------------------------------------------------------
|
|
7
|
+
@dataclass(frozen=True)
|
|
8
|
+
class Location:
|
|
9
|
+
"""Location within a source file."""
|
|
10
|
+
|
|
11
|
+
# ----------------------------------------------------------------------
|
|
12
|
+
line: int
|
|
13
|
+
column: int
|
|
14
|
+
|
|
15
|
+
# ----------------------------------------------------------------------
|
|
16
|
+
def __post_init__(self) -> None:
|
|
17
|
+
if self.line < 1:
|
|
18
|
+
msg = "Invalid line"
|
|
19
|
+
raise ValueError(msg)
|
|
20
|
+
|
|
21
|
+
if self.column < 1:
|
|
22
|
+
msg = "Invalid column"
|
|
23
|
+
raise ValueError(msg)
|
|
24
|
+
|
|
25
|
+
# ----------------------------------------------------------------------
|
|
26
|
+
def __str__(self) -> str:
|
|
27
|
+
return self._string
|
|
28
|
+
|
|
29
|
+
# ----------------------------------------------------------------------
|
|
30
|
+
@staticmethod
|
|
31
|
+
def Compare(
|
|
32
|
+
this: Location,
|
|
33
|
+
that: Location,
|
|
34
|
+
) -> int:
|
|
35
|
+
"""Compare two `Location` objects."""
|
|
36
|
+
|
|
37
|
+
result = this.line - that.line
|
|
38
|
+
if result != 0:
|
|
39
|
+
return result
|
|
40
|
+
|
|
41
|
+
result = this.column - that.column
|
|
42
|
+
if result != 0:
|
|
43
|
+
return result
|
|
44
|
+
|
|
45
|
+
return 0
|
|
46
|
+
|
|
47
|
+
# ----------------------------------------------------------------------
|
|
48
|
+
def __hash__(self) -> int:
|
|
49
|
+
return hash((self.line, self.column))
|
|
50
|
+
|
|
51
|
+
def __eq__(self, other: object) -> bool:
|
|
52
|
+
return isinstance(other, Location) and self.__class__.Compare(self, other) == 0
|
|
53
|
+
|
|
54
|
+
def __ne__(self, other: object) -> bool:
|
|
55
|
+
return not isinstance(other, Location) or self.__class__.Compare(self, other) != 0
|
|
56
|
+
|
|
57
|
+
def __lt__(self, other: object) -> bool:
|
|
58
|
+
return isinstance(other, Location) and self.__class__.Compare(self, other) < 0
|
|
59
|
+
|
|
60
|
+
def __le__(self, other: object) -> bool:
|
|
61
|
+
return isinstance(other, Location) and self.__class__.Compare(self, other) <= 0
|
|
62
|
+
|
|
63
|
+
def __gt__(self, other: object) -> bool:
|
|
64
|
+
return isinstance(other, Location) and self.__class__.Compare(self, other) > 0
|
|
65
|
+
|
|
66
|
+
def __ge__(self, other: object) -> bool:
|
|
67
|
+
return isinstance(other, Location) and self.__class__.Compare(self, other) >= 0
|
|
68
|
+
|
|
69
|
+
# ----------------------------------------------------------------------
|
|
70
|
+
# ----------------------------------------------------------------------
|
|
71
|
+
# ----------------------------------------------------------------------
|
|
72
|
+
@cached_property
|
|
73
|
+
def _string(self) -> str:
|
|
74
|
+
return f"Ln {self.line} Col {self.column}"
|
|
@@ -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}"
|
|
File without changes
|
|
File without changes
|