aboutsummaryrefslogtreecommitdiffstats
path: root/pyGHDL/dom/__init__.py
diff options
context:
space:
mode:
authorPatrick Lehmann <Patrick.Lehmann@plc2.de>2021-06-26 13:48:09 +0200
committerGitHub <noreply@github.com>2021-06-26 13:48:09 +0200
commit111fe055b2f0f3a0225d2553cf739572d691a14d (patch)
tree50d3a874bb78107627a6509fd4054c7fdc96cd25 /pyGHDL/dom/__init__.py
parent15c6de72bc8dd316cb5262e1b5f373ca45b05f68 (diff)
downloadghdl-111fe055b2f0f3a0225d2553cf739572d691a14d.tar.gz
ghdl-111fe055b2f0f3a0225d2553cf739572d691a14d.tar.bz2
ghdl-111fe055b2f0f3a0225d2553cf739572d691a14d.zip
More DOM improvements (#1806)
* First try to handle names. * Reworked names. * Reworked range expressions. * Handle AttributeNames. * Added handling of file declaration and attribute declarations. * Improved error outputs. * Handle protected types. * Make black happy with ugly code. * Handle Null literal and File parameters. * File type and physical type. * Don't fail on reported syntax errors. Catch call errors into libghdl. * Improved Sanity checks for pyGHDL.dom. * Load sourcecode via Python and process in-memory. Fixed testcases. * Added package instantiations and packages with generics. * Added UseClause, AttributeSpecification and PhysicalTypes. * Improved pretty-printing. * Fixed AttributeName in subtype indication. * Get code position of IIR nodes. * Added DOMMixin into all derived classes. * Mark as not yet implemented. * Pinned pyVHDLModel version to v0.10.4. * Removed xfail in LSP test. Bumped requirement of pyVHDLModel to v0.10.4. Fixed some Codacy issues. (cherry picked from commit f64e7ed7c3d69cbf84cd60db8e9b085e90b846cb)
Diffstat (limited to 'pyGHDL/dom/__init__.py')
-rw-r--r--pyGHDL/dom/__init__.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/pyGHDL/dom/__init__.py b/pyGHDL/dom/__init__.py
index b4ffaf4c6..19f23a94b 100644
--- a/pyGHDL/dom/__init__.py
+++ b/pyGHDL/dom/__init__.py
@@ -30,5 +30,78 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later
# ============================================================================
+from pathlib import Path
+
+from pyGHDL import GHDLBaseException
+from pyGHDL.libghdl import files_map, name_table
+
+from pyGHDL.libghdl.vhdl import nodes
+from pydecor import export
+
+from pyGHDL.libghdl._types import Iir
__all__ = []
+
+
+@export
+class Position:
+ """Represents the source code position of a IIR node in a source file."""
+
+ _filename: Path
+ _line: int
+ _column: int
+
+ def __init__(self, filename: Path, line: int, column: int):
+ self._filename = filename
+ self._line = line
+ self._column = column
+
+ @classmethod
+ def parse(cls, node: Iir) -> "Position":
+ """Return the source code position of a IIR node."""
+ if node == nodes.Null_Iir:
+ raise ValueError(
+ "Position.parse(): Parameter 'node' must not be 'Null_iir'."
+ )
+
+ location = nodes.Get_Location(node)
+ file = files_map.Location_To_File(location)
+ fileNameId = files_map.Get_File_Name(file)
+ fileName = name_table.Get_Name_Ptr(fileNameId)
+ line = files_map.Location_File_To_Line(location, file)
+ column = files_map.Location_File_Line_To_Offset(location, file, line)
+
+ return cls(Path(fileName), line, column)
+
+ @property
+ def Filename(self) -> Path:
+ return self._filename
+
+ @property
+ def Line(self) -> int:
+ return self._line
+
+ @property
+ def Column(self) -> int:
+ return self._column
+
+
+@export
+class DOMMixin:
+ _iirNode: Iir
+ _position: Position = None
+
+ def __init__(self, node: Iir):
+ self._iirNode = node
+
+ @property
+ def Position(self) -> Position:
+ if self._position is None:
+ self._position = Position.parse(self._iirNode)
+
+ return self._position
+
+
+@export
+class DOMException(GHDLBaseException):
+ pass