aboutsummaryrefslogtreecommitdiffstats
path: root/pyGHDL/dom/formatting/prettyprint.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyGHDL/dom/formatting/prettyprint.py')
-rw-r--r--pyGHDL/dom/formatting/prettyprint.py201
1 files changed, 137 insertions, 64 deletions
diff --git a/pyGHDL/dom/formatting/prettyprint.py b/pyGHDL/dom/formatting/prettyprint.py
index a64f2a4f5..387706cac 100644
--- a/pyGHDL/dom/formatting/prettyprint.py
+++ b/pyGHDL/dom/formatting/prettyprint.py
@@ -3,53 +3,83 @@ from typing import List, Union
from pydecor import export
from pyGHDL.dom.Object import Constant, Signal
-from pyVHDLModel.VHDLModel import GenericInterfaceItem, Expression, Direction, Mode, NamedEntity, PortInterfaceItem, BinaryExpression, IdentityExpression, \
- UnaryExpression
+from pyVHDLModel.VHDLModel import (
+ GenericInterfaceItem,
+ Expression,
+ Direction,
+ Mode,
+ NamedEntity,
+ PortInterfaceItem,
+ BinaryExpression,
+ IdentityExpression,
+ UnaryExpression,
+)
from pyGHDL import GHDLBaseException
from pyGHDL.dom.Misc import Document
-from pyGHDL.dom.DesignUnit import Entity, Architecture, Package, PackageBody, Configuration, Context
-from pyGHDL.dom.InterfaceItem import GenericConstantInterfaceItem, PortSignalInterfaceItem
-from pyGHDL.dom.Symbol import SimpleSubTypeSymbol, ConstrainedSubTypeSymbol, SimpleObjectSymbol
+from pyGHDL.dom.DesignUnit import (
+ Entity,
+ Architecture,
+ Package,
+ PackageBody,
+ Configuration,
+ Context,
+)
+from pyGHDL.dom.InterfaceItem import (
+ GenericConstantInterfaceItem,
+ PortSignalInterfaceItem,
+)
+from pyGHDL.dom.Symbol import (
+ SimpleSubTypeSymbol,
+ ConstrainedSubTypeSymbol,
+ SimpleObjectSymbol,
+)
from pyGHDL.dom.Literal import IntegerLiteral, CharacterLiteral, FloatingPointLiteral
-from pyGHDL.dom.Expression import SubtractionExpression, AdditionExpression, MultiplyExpression, DivisionExpression, InverseExpression, AbsoluteExpression, \
- NegationExpression, ExponentiationExpression
+from pyGHDL.dom.Expression import (
+ SubtractionExpression,
+ AdditionExpression,
+ MultiplyExpression,
+ DivisionExpression,
+ InverseExpression,
+ AbsoluteExpression,
+ NegationExpression,
+ ExponentiationExpression,
+)
StringBuffer = List[str]
-DirectionTranslation = {
- Direction.To: "to",
- Direction.DownTo: "downto"
-}
+DirectionTranslation = {Direction.To: "to", Direction.DownTo: "downto"}
ModeTranslation = {
- Mode.In: "in",
- Mode.Out: "out",
- Mode.InOut: "inout",
- Mode.Buffer: "buffer",
- Mode.Linkage: "linkage"
+ Mode.In: "in",
+ Mode.Out: "out",
+ Mode.InOut: "inout",
+ Mode.Buffer: "buffer",
+ Mode.Linkage: "linkage",
}
UnaryExpressionTranslation = {
- IdentityExpression: " +",
- NegationExpression: " -",
- InverseExpression: "not ",
- AbsoluteExpression: "abs ",
+ IdentityExpression: " +",
+ NegationExpression: " -",
+ InverseExpression: "not ",
+ AbsoluteExpression: "abs ",
}
BinaryExpressionTranslation = {
- AdditionExpression: " + ",
- SubtractionExpression: " - ",
- MultiplyExpression: " * ",
- DivisionExpression: " / ",
- ExponentiationExpression: "**"
+ AdditionExpression: " + ",
+ SubtractionExpression: " - ",
+ MultiplyExpression: " * ",
+ DivisionExpression: " / ",
+ ExponentiationExpression: "**",
}
+
@export
class PrettyPrintException(GHDLBaseException):
pass
+
@export
class PrettyPrint:
# _buffer: StringBuffer
@@ -60,30 +90,32 @@ class PrettyPrint:
def formatDocument(self, document: Document, level: int = 0) -> StringBuffer:
buffer = []
prefix = " " * level
- buffer.append("{prefix}Document '{doc!s}':".format(doc=document.Path, prefix=prefix))
+ buffer.append(
+ "{prefix}Document '{doc!s}':".format(doc=document.Path, prefix=prefix)
+ )
buffer.append("{prefix} Entities:".format(prefix=prefix))
for entity in document.Entities:
- for line in self.formatEntity(entity, level+1):
+ for line in self.formatEntity(entity, level + 1):
buffer.append(line)
buffer.append("{prefix} Architectures:".format(prefix=prefix))
for architecture in document.Architectures:
- for line in self.formatArchitecture(architecture, level+1):
+ for line in self.formatArchitecture(architecture, level + 1):
buffer.append(line)
buffer.append("{prefix} Packages:".format(prefix=prefix))
for package in document.Packages:
- for line in self.formatPackage(package, level+1):
+ for line in self.formatPackage(package, level + 1):
buffer.append(line)
buffer.append("{prefix} PackageBodies:".format(prefix=prefix))
for packageBodies in document.PackageBodies:
- for line in self.formatPackageBody(packageBodies, level+1):
+ for line in self.formatPackageBody(packageBodies, level + 1):
buffer.append(line)
buffer.append("{prefix} Configurations:".format(prefix=prefix))
for configuration in document.Configurations:
- for line in self.formatConfiguration(configuration, level+1):
+ for line in self.formatConfiguration(configuration, level + 1):
buffer.append(line)
buffer.append("{prefix} Contexts:".format(prefix=prefix))
for context in document.Contexts:
- for line in self.formatContext(context, level+1):
+ for line in self.formatContext(context, level + 1):
buffer.append(line)
return buffer
@@ -107,7 +139,9 @@ class PrettyPrint:
return buffer
- def formatArchitecture(self, architecture: Architecture, level: int = 0) -> StringBuffer:
+ def formatArchitecture(
+ self, architecture: Architecture, level: int = 0
+ ) -> StringBuffer:
buffer = []
prefix = " " * level
buffer.append("{prefix}- {name}".format(name=architecture.Name, prefix=prefix))
@@ -129,7 +163,9 @@ class PrettyPrint:
return buffer
- def formatPackageBody(self, packageBody: PackageBody, level: int = 0) -> StringBuffer:
+ def formatPackageBody(
+ self, packageBody: PackageBody, level: int = 0
+ ) -> StringBuffer:
buffer = []
prefix = " " * level
buffer.append("{prefix}- {name}".format(name=packageBody.Name, prefix=prefix))
@@ -140,7 +176,9 @@ class PrettyPrint:
return buffer
- def formatConfiguration(self, configuration: Configuration, level: int = 0) -> StringBuffer:
+ def formatConfiguration(
+ self, configuration: Configuration, level: int = 0
+ ) -> StringBuffer:
buffer = []
prefix = " " * level
buffer.append("{prefix}- {name}".format(name=configuration.Name, prefix=prefix))
@@ -154,19 +192,29 @@ class PrettyPrint:
return buffer
- def formatGeneric(self, generic: Union[NamedEntity, GenericInterfaceItem], level: int = 0) -> StringBuffer:
+ def formatGeneric(
+ self, generic: Union[NamedEntity, GenericInterfaceItem], level: int = 0
+ ) -> StringBuffer:
if isinstance(generic, GenericConstantInterfaceItem):
return self.formatGenericConstant(generic, level)
else:
- raise PrettyPrintException("Unhandled generic kind for generic '{name}'.".format(name=generic.Name))
+ raise PrettyPrintException(
+ "Unhandled generic kind for generic '{name}'.".format(name=generic.Name)
+ )
- def formatPort(self, port: Union[NamedEntity, PortInterfaceItem], level: int = 0) -> StringBuffer:
+ def formatPort(
+ self, port: Union[NamedEntity, PortInterfaceItem], level: int = 0
+ ) -> StringBuffer:
if isinstance(port, PortSignalInterfaceItem):
return self.formatPortSignal(port, level)
else:
- raise PrettyPrintException("Unhandled port kind for port '{name}'.".format(name=port.Name))
+ raise PrettyPrintException(
+ "Unhandled port kind for port '{name}'.".format(name=port.Name)
+ )
- def formatGenericConstant(self, generic: GenericConstantInterfaceItem, level: int = 0) -> StringBuffer:
+ def formatGenericConstant(
+ self, generic: GenericConstantInterfaceItem, level: int = 0
+ ) -> StringBuffer:
buffer = []
prefix = " " * level
subType = generic.SubType
@@ -176,7 +224,7 @@ class PrettyPrint:
prefix=prefix,
name=generic.Name,
mode=ModeTranslation[generic.Mode],
- type=subType.SymbolName
+ type=subType.SymbolName,
)
)
elif isinstance(subType, ConstrainedSubTypeSymbol):
@@ -187,19 +235,31 @@ class PrettyPrint:
mode=ModeTranslation[generic.Mode],
type=subType.SymbolName,
constraints=", ".join(
- ["{left} {dir} {right}".format(
- left=self.formatExpression(constraint.Range.LeftBound),
- right=self.formatExpression(constraint.Range.RightBound),
- dir=DirectionTranslation[constraint.Range.Direction])
- for constraint in subType.Constraints])
+ [
+ "{left} {dir} {right}".format(
+ left=self.formatExpression(constraint.Range.LeftBound),
+ right=self.formatExpression(
+ constraint.Range.RightBound
+ ),
+ dir=DirectionTranslation[constraint.Range.Direction],
+ )
+ for constraint in subType.Constraints
+ ]
+ ),
)
)
else:
- raise PrettyPrintException("Unhandled constraint kind for generic '{name}'.".format(name=generic.Name))
+ raise PrettyPrintException(
+ "Unhandled constraint kind for generic '{name}'.".format(
+ name=generic.Name
+ )
+ )
return buffer
- def formatPortSignal(self, port: PortSignalInterfaceItem, level: int = 0) -> StringBuffer:
+ def formatPortSignal(
+ self, port: PortSignalInterfaceItem, level: int = 0
+ ) -> StringBuffer:
buffer = []
prefix = " " * level
@@ -208,7 +268,9 @@ class PrettyPrint:
prefix=prefix,
name=port.Name,
mode=ModeTranslation[port.Mode],
- subtypeindication=self.formatSubtypeIndication(port.SubType, "port", port.Name)
+ subtypeindication=self.formatSubtypeIndication(
+ port.SubType, "port", port.Name
+ ),
)
)
@@ -223,8 +285,10 @@ class PrettyPrint:
"{prefix}- constant {name} : {subtype} := {expr}".format(
prefix=prefix,
name=item.Name,
- subtype=self.formatSubtypeIndication(item.SubType, "constant", item.Name),
- expr=self.formatExpression(item.DefaultExpression)
+ subtype=self.formatSubtypeIndication(
+ item.SubType, "constant", item.Name
+ ),
+ expr=self.formatExpression(item.DefaultExpression),
)
)
elif isinstance(item, Signal):
@@ -232,10 +296,14 @@ class PrettyPrint:
"{prefix}- signal {name} : {subtype}{initValue}".format(
prefix=prefix,
name=item.Name,
- subtype=self.formatSubtypeIndication(item.SubType, "signal", item.Name),
+ subtype=self.formatSubtypeIndication(
+ item.SubType, "signal", item.Name
+ ),
initValue=" := {expr}".format(
expr=self.formatExpression(item.DefaultExpression)
- ) if item.DefaultExpression is not None else ""
+ )
+ if item.DefaultExpression is not None
+ else "",
)
)
else:
@@ -248,19 +316,25 @@ class PrettyPrint:
return "{type}".format(type=subTypeIndication.SymbolName)
elif isinstance(subTypeIndication, ConstrainedSubTypeSymbol):
constraints = ", ".join(
- ["{left} {dir} {right}".format(
- left=self.formatExpression(constraint.Range.LeftBound),
- right=self.formatExpression(constraint.Range.RightBound),
- dir=DirectionTranslation[constraint.Range.Direction]
- ) for constraint in subTypeIndication.Constraints]
+ [
+ "{left} {dir} {right}".format(
+ left=self.formatExpression(constraint.Range.LeftBound),
+ right=self.formatExpression(constraint.Range.RightBound),
+ dir=DirectionTranslation[constraint.Range.Direction],
+ )
+ for constraint in subTypeIndication.Constraints
+ ]
)
return "{type}({constraints})".format(
- type=subTypeIndication.SymbolName,
- constraints=constraints
+ type=subTypeIndication.SymbolName, constraints=constraints
)
else:
- raise PrettyPrintException("Unhandled constraint kind for {entity} '{name}'.".format(entity=entity, name=name))
+ raise PrettyPrintException(
+ "Unhandled constraint kind for {entity} '{name}'.".format(
+ entity=entity, name=name
+ )
+ )
def formatExpression(self, expression: Expression) -> str:
if isinstance(expression, SimpleObjectSymbol):
@@ -278,8 +352,7 @@ class PrettyPrint:
raise PrettyPrintException("Unhandled operator for unary expression.")
return "{operator}{operand}".format(
- operand=self.formatExpression(expression.Operand),
- operator=operator
+ operand=self.formatExpression(expression.Operand), operator=operator
)
elif isinstance(expression, BinaryExpression):
try:
@@ -290,7 +363,7 @@ class PrettyPrint:
return "{left}{operator}{right}".format(
left=self.formatExpression(expression.LeftOperand),
right=self.formatExpression(expression.RightOperand),
- operator=operator
+ operator=operator,
)
else:
raise PrettyPrintException("Unhandled expression kind.")