diff options
author | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2021-06-21 22:36:07 +0200 |
---|---|---|
committer | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2021-06-22 12:28:25 +0200 |
commit | 11bd75f611a892af0e20e913a9580d43c23e610b (patch) | |
tree | 2ab0b2d266cad6d7f6369a0afcb7d5bf63a0bfd5 /pyGHDL/dom | |
parent | ad34fac3f4e30f0ff13e1630b42373f31b2918a4 (diff) | |
download | ghdl-11bd75f611a892af0e20e913a9580d43c23e610b.tar.gz ghdl-11bd75f611a892af0e20e913a9580d43c23e610b.tar.bz2 ghdl-11bd75f611a892af0e20e913a9580d43c23e610b.zip |
Print position where exception happened.
Diffstat (limited to 'pyGHDL/dom')
-rw-r--r-- | pyGHDL/dom/Misc.py | 26 | ||||
-rw-r--r-- | pyGHDL/dom/Subprogram.py | 14 | ||||
-rw-r--r-- | pyGHDL/dom/_Translate.py | 83 | ||||
-rw-r--r-- | pyGHDL/dom/_Utils.py | 22 |
4 files changed, 110 insertions, 35 deletions
diff --git a/pyGHDL/dom/Misc.py b/pyGHDL/dom/Misc.py index 8ff62a119..8bea6cf61 100644 --- a/pyGHDL/dom/Misc.py +++ b/pyGHDL/dom/Misc.py @@ -41,13 +41,35 @@ from pyVHDLModel.VHDLModel import ( Alias as VHDLModel_Alias, ) -from pyGHDL.dom._Utils import GetIirKindOfNode -from pyGHDL.dom.Common import DOMException, GHDLMixin __all__ = [] @export +class Position: + _filename: str + _line: int + _column: int + + def __init__(self, filename: str, line: int, column: int): + self._filename = filename + self._line = line + self._column = column + + @property + def Filename(self): + return self._filename + + @property + def Line(self): + return self._line + + @property + def Column(self): + return self._column + + +@export class Alias(VHDLModel_Alias): def __init__(self, aliasName: str): super().__init__(aliasName) diff --git a/pyGHDL/dom/Subprogram.py b/pyGHDL/dom/Subprogram.py index 4fa6b3e6a..420041a52 100644 --- a/pyGHDL/dom/Subprogram.py +++ b/pyGHDL/dom/Subprogram.py @@ -32,11 +32,13 @@ # ============================================================================ from pydecor import export +from pyGHDL.dom._Utils import GetNameOfNode from pyVHDLModel.VHDLModel import ( Function as VHDLModel_Function, Procedure as VHDLModel_Procedure, Expression, ) +from pyGHDL.libghdl._types import Iir @export @@ -44,8 +46,20 @@ class Function(VHDLModel_Function): def __init__(self, functionName: str): super().__init__(functionName) + @classmethod + def parse(cls, node: Iir): + functionName = GetNameOfNode(node) + + return cls(functionName) + @export class Procedure(VHDLModel_Procedure): def __init__(self, procedureName: str): super().__init__(procedureName) + + @classmethod + def parse(cls, node: Iir): + procedureName = GetNameOfNode(node) + + return cls(procedureName) diff --git a/pyGHDL/dom/_Translate.py b/pyGHDL/dom/_Translate.py index ebd650698..84d3448d7 100644 --- a/pyGHDL/dom/_Translate.py +++ b/pyGHDL/dom/_Translate.py @@ -44,9 +44,8 @@ from pyVHDLModel.VHDLModel import ( from pyGHDL.libghdl import utils from pyGHDL.libghdl._types import Iir -from pyGHDL.libghdl.utils import flist_iter from pyGHDL.libghdl.vhdl import nodes -from pyGHDL.dom._Utils import GetNameOfNode, GetIirKindOfNode +from pyGHDL.dom._Utils import GetNameOfNode, GetIirKindOfNode, GetPositionOfNode from pyGHDL.dom.Common import DOMException from pyGHDL.dom.Symbol import ( SimpleObjectOrFunctionCallSymbol, @@ -128,9 +127,16 @@ def GetSubtypeIndicationFromNode(node: Iir, entity: str, name: str) -> SubTypeOr ) ) else: + position = GetPositionOfNode(node) raise DOMException( - "Unknown subtype kind '{kind}' of subtype indication '{indication}' while parsing {entity} '{name}'.".format( - kind=subTypeKind, indication=subTypeIndication, entity=entity, name=name + "Unknown subtype kind '{kind}' of subtype indication '{indication}' while parsing {entity} '{name}' at {file}:{line}:{column}.".format( + kind=subTypeKind, + indication=subTypeIndication, + entity=entity, + name=name, + file=position.Filename, + line=position.Line, + column=position.Column, ) ) @@ -142,7 +148,9 @@ def GetArrayConstraintsFromSubtypeIndication( subTypeIndication: Iir, ) -> List[Constraint]: constraints = [] - for constraint in flist_iter(nodes.Get_Index_Constraint_List(subTypeIndication)): + for constraint in utils.flist_iter( + nodes.Get_Index_Constraint_List(subTypeIndication) + ): constraintKind = GetIirKindOfNode(constraint) if constraintKind == nodes.Iir_Kind.Range_Expression: constraints.append(RangeExpression(GetRangeFromNode(constraint))) @@ -151,11 +159,15 @@ def GetArrayConstraintsFromSubtypeIndication( elif constraintKind == nodes.Iir_Kind.Simple_Name: raise DOMException("[NOT IMPLEMENTED] Subtype as range.") else: + position = GetPositionOfNode(constraint) raise DOMException( - "Unknown constraint kind '{kind}' for constraint '{constraint}' in subtype indication '{indication}'.".format( + "Unknown constraint kind '{kind}' for constraint '{constraint}' in subtype indication '{indication}' at {file}:{line}:{column}.".format( kind=constraintKind, constraint=constraint, indication=subTypeIndication, + file=position.Filename, + line=position.Line, + column=position.Column, ) ) @@ -241,9 +253,15 @@ def GetExpressionFromNode(node: Iir) -> Expression: try: cls = __EXPRESSION_TRANSLATION[kind] except KeyError: + position = GetPositionOfNode(node) raise DOMException( - "Unknown expression kind '{kindName}'({kind}) in expression '{expr}'.".format( - kind=kind, kindName=kind.name, expr=node + "Unknown expression kind '{kindName}'({kind}) in expression '{expr}' at {file}:{line}:{column}.".format( + kind=kind, + kindName=kind.name, + expr=node, + file=position.Filename, + line=position.Line, + column=position.Column, ) ) @@ -263,9 +281,15 @@ def GetGenericsFromChainedNodes(nodeChain: Iir): result.append(genericConstant) else: + position = GetPositionOfNode(generic) raise DOMException( - "Unknown generic kind '{kindName}'({kind}) in generic '{generic}'.".format( - kind=kind, kindName=kind.name, generic=generic + "Unknown generic kind '{kindName}'({kind}) in generic '{generic}' at {file}:{line}:{column}.".format( + kind=kind, + kindName=kind.name, + generic=generic, + file=position.Filename, + line=position.Line, + column=position.Column, ) ) @@ -285,9 +309,15 @@ def GetPortsFromChainedNodes(nodeChain: Iir): result.append(portSignal) else: + position = GetPositionOfNode(port) raise DOMException( - "Unknown port kind '{kindName}'({kind}) in port '{port}'.".format( - kind=kind, kindName=kind.name, port=port + "Unknown port kind '{kindName}'({kind}) in port '{port}' at {file}:{line}:{column}.".format( + kind=kind, + kindName=kind.name, + port=port, + file=position.Filename, + line=position.Line, + column=position.Column, ) ) @@ -313,12 +343,12 @@ def GetDeclaredItemsFromChainedNodes(nodeChain: Iir, entity: str, name: str): elif kind == nodes.Iir_Kind.Subtype_Declaration: result.append(GetSubTypeFromNode(item)) elif kind == nodes.Iir_Kind.Function_Declaration: - result.append(GetFunctionFromNode(item)) + result.append(Function.parse(item)) elif kind == nodes.Iir_Kind.Function_Body: - # functionName = NodeToName(item) + # procedureName = NodeToName(item) print("found function body '{name}'".format(name="????")) elif kind == nodes.Iir_Kind.Procedure_Declaration: - result.append(GetProcedureFromNode(item)) + result.append(Procedure.parse(item)) elif kind == nodes.Iir_Kind.Procedure_Body: # procedureName = NodeToName(item) print("found procedure body '{name}'".format(name="????")) @@ -329,27 +359,22 @@ def GetDeclaredItemsFromChainedNodes(nodeChain: Iir, entity: str, name: str): result.append(Component.parse(item)) else: + position = GetPositionOfNode(item) raise DOMException( - "Unknown declared item kind '{kindName}'({kind}) in {entity} '{name}'.".format( - kind=kind, kindName=kind.name, entity=entity, name=name + "Unknown declared item kind '{kindName}'({kind}) in {entity} '{name}' at {file}:{line}:{column}.".format( + kind=kind, + kindName=kind.name, + entity=entity, + name=name, + file=position.Filename, + line=position.Line, + column=position.Column, ) ) return result -def GetFunctionFromNode(node: Iir): - functionName = GetNameOfNode(node) - - return Function(functionName) - - -def GetProcedureFromNode(node: Iir): - procedureName = GetNameOfNode(node) - - return Procedure(procedureName) - - def GetAliasFromNode(node: Iir): aliasName = GetNameOfNode(node) diff --git a/pyGHDL/dom/_Utils.py b/pyGHDL/dom/_Utils.py index b5cf1fc5c..e0d97f892 100644 --- a/pyGHDL/dom/_Utils.py +++ b/pyGHDL/dom/_Utils.py @@ -30,12 +30,14 @@ # # SPDX-License-Identifier: GPL-2.0-or-later # ============================================================================ +from pyGHDL.libghdl._types import Iir from pydecor import export from pyVHDLModel.VHDLModel import Mode -from pyGHDL.libghdl import LibGHDLException, name_table +from pyGHDL.libghdl import LibGHDLException, name_table, files_map from pyGHDL.libghdl.vhdl import nodes +from pyGHDL.dom.Misc import Position __all__ = [] @@ -50,22 +52,34 @@ __MODE_TRANSLATION = { @export -def GetIirKindOfNode(node) -> nodes.Iir_Kind: +def GetIirKindOfNode(node: Iir) -> nodes.Iir_Kind: kind: int = nodes.Get_Kind(node) return nodes.Iir_Kind(kind) @export -def GetNameOfNode(node) -> str: +def GetNameOfNode(node: Iir) -> str: """Return the python string from node :obj:`node` identifier""" identifier = nodes.Get_Identifier(node) return name_table.Get_Name_Ptr(identifier) @export -def GetModeOfNode(node) -> Mode: +def GetModeOfNode(node: Iir) -> Mode: """Return the mode of a :obj:`port`.""" try: return __MODE_TRANSLATION[nodes.Get_Mode(node)] except KeyError: raise LibGHDLException("Unknown mode.") + + +@export +def GetPositionOfNode(node: Iir) -> Position: + location = nodes.Get_Location(node) + file = files_map.Location_To_File(location) + fileName = name_table.Get_Name_Ptr(file) + # position = files_map.Location_File_To_Pos(location, file) + line = files_map.Location_File_To_Line(location, file) + column = files_map.Location_File_Line_To_Offset(location, file, line) + + return Position(fileName, line, column) |