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.py239
1 files changed, 91 insertions, 148 deletions
diff --git a/pyGHDL/dom/formatting/prettyprint.py b/pyGHDL/dom/formatting/prettyprint.py
index 59fdd485b..1577edca0 100644
--- a/pyGHDL/dom/formatting/prettyprint.py
+++ b/pyGHDL/dom/formatting/prettyprint.py
@@ -2,22 +2,15 @@ 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,
WithDefaultExpression,
)
from pyGHDL import GHDLBaseException
-from pyGHDL.dom.Misc import Document
+from pyGHDL.dom.Misc import Document, Design, Library
from pyGHDL.dom.DesignUnit import (
Entity,
Architecture,
@@ -26,6 +19,7 @@ from pyGHDL.dom.DesignUnit import (
Configuration,
Context,
)
+from pyGHDL.dom.Object import Constant, Signal
from pyGHDL.dom.InterfaceItem import (
GenericConstantInterfaceItem,
PortSignalInterfaceItem,
@@ -33,47 +27,10 @@ from pyGHDL.dom.InterfaceItem import (
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,
)
-StringBuffer = List[str]
-
-DirectionTranslation = {Direction.To: "to", Direction.DownTo: "downto"}
-ModeTranslation = {
- Mode.In: "in",
- Mode.Out: "out",
- Mode.InOut: "inout",
- Mode.Buffer: "buffer",
- Mode.Linkage: "linkage",
-}
-
-UnaryExpressionTranslation = {
- IdentityExpression: " +",
- NegationExpression: " -",
- InverseExpression: "not ",
- AbsoluteExpression: "abs ",
-}
-
-BinaryExpressionTranslation = {
- AdditionExpression: " + ",
- SubtractionExpression: " - ",
- MultiplyExpression: " * ",
- DivisionExpression: " / ",
- ExponentiationExpression: "**",
-}
+StringBuffer = List[str]
@export
@@ -88,33 +45,77 @@ class PrettyPrint:
# def __init__(self):
# self._buffer = []
+ def formatDesign(self, design: Design, level: int = 0) -> StringBuffer:
+ buffer = []
+ prefix = " " * level
+ buffer.append("{prefix}Libraries:".format(prefix=prefix))
+ for library in design.Libraries:
+ for line in self.formatLibrary(library, level + 1):
+ buffer.append(line)
+ buffer.append("{prefix}Documents:".format(prefix=prefix))
+ for document in design.Documents:
+ buffer.append(
+ "{prefix}- Path: '{doc!s}':".format(doc=document.Path, prefix=prefix)
+ )
+ for line in self.formatDocument(document, level + 1):
+ buffer.append(line)
+
+ return buffer
+
+ def formatLibrary(self, library: Library, level: int = 0) -> StringBuffer:
+ buffer = []
+ prefix = " " * level
+ buffer.append("{prefix}Entities:".format(prefix=prefix))
+ for entity in library.Entities:
+ for line in self.formatEntity(entity, level + 1):
+ buffer.append(line)
+ buffer.append("{prefix}Architectures:".format(prefix=prefix))
+ for architecture in library.Architectures:
+ for line in self.formatArchitecture(architecture, level + 1):
+ buffer.append(line)
+ buffer.append("{prefix}Packages:".format(prefix=prefix))
+ for package in library.Packages:
+ for line in self.formatPackage(package, level + 1):
+ buffer.append(line)
+ buffer.append("{prefix}PackageBodies:".format(prefix=prefix))
+ for packageBodies in library.PackageBodies:
+ for line in self.formatPackageBody(packageBodies, level + 1):
+ buffer.append(line)
+ buffer.append("{prefix}Configurations:".format(prefix=prefix))
+ for configuration in library.Configurations:
+ for line in self.formatConfiguration(configuration, level + 1):
+ buffer.append(line)
+ buffer.append("{prefix}Contexts:".format(prefix=prefix))
+ for context in library.Contexts:
+ for line in self.formatContext(context, level + 1):
+ buffer.append(line)
+
+ return buffer
+
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} Entities:".format(prefix=prefix))
+ buffer.append("{prefix}Entities:".format(prefix=prefix))
for entity in document.Entities:
for line in self.formatEntity(entity, level + 1):
buffer.append(line)
- buffer.append("{prefix} Architectures:".format(prefix=prefix))
+ buffer.append("{prefix}Architectures:".format(prefix=prefix))
for architecture in document.Architectures:
for line in self.formatArchitecture(architecture, level + 1):
buffer.append(line)
- buffer.append("{prefix} Packages:".format(prefix=prefix))
+ buffer.append("{prefix}Packages:".format(prefix=prefix))
for package in document.Packages:
for line in self.formatPackage(package, level + 1):
buffer.append(line)
- buffer.append("{prefix} PackageBodies:".format(prefix=prefix))
+ buffer.append("{prefix}PackageBodies:".format(prefix=prefix))
for packageBodies in document.PackageBodies:
for line in self.formatPackageBody(packageBodies, level + 1):
buffer.append(line)
- buffer.append("{prefix} Configurations:".format(prefix=prefix))
+ buffer.append("{prefix}Configurations:".format(prefix=prefix))
for configuration in document.Configurations:
for line in self.formatConfiguration(configuration, level + 1):
buffer.append(line)
- buffer.append("{prefix} Contexts:".format(prefix=prefix))
+ buffer.append("{prefix}Contexts:".format(prefix=prefix))
for context in document.Contexts:
for line in self.formatContext(context, level + 1):
buffer.append(line)
@@ -124,7 +125,7 @@ class PrettyPrint:
def formatEntity(self, entity: Entity, level: int = 0) -> StringBuffer:
buffer = []
prefix = " " * level
- buffer.append("{prefix}- {name}".format(name=entity.Name, prefix=prefix))
+ buffer.append("{prefix}- Name: {name}".format(name=entity.Name, prefix=prefix))
buffer.append("{prefix} Generics:".format(prefix=prefix))
for generic in entity.GenericItems:
for line in self.formatGeneric(generic, level + 1):
@@ -145,7 +146,14 @@ class PrettyPrint:
) -> StringBuffer:
buffer = []
prefix = " " * level
- buffer.append("{prefix}- {name}".format(name=architecture.Name, prefix=prefix))
+ buffer.append(
+ "{prefix}- Name: {name}".format(name=architecture.Name, prefix=prefix)
+ )
+ buffer.append(
+ "{prefix} Entity: {entity}".format(
+ entity=architecture.Entity.SymbolName, prefix=prefix
+ )
+ )
buffer.append("{prefix} Declared:".format(prefix=prefix))
for item in architecture.DeclaredItems:
for line in self.formatDeclaredItems(item, level + 2):
@@ -156,7 +164,7 @@ class PrettyPrint:
def formatPackage(self, package: Package, level: int = 0) -> StringBuffer:
buffer = []
prefix = " " * level
- buffer.append("{prefix}- {name}".format(name=package.Name, prefix=prefix))
+ buffer.append("{prefix}- Name: {name}".format(name=package.Name, prefix=prefix))
buffer.append("{prefix} Declared:".format(prefix=prefix))
for item in package.DeclaredItems:
for line in self.formatDeclaredItems(item, level + 1):
@@ -169,7 +177,9 @@ class PrettyPrint:
) -> StringBuffer:
buffer = []
prefix = " " * level
- buffer.append("{prefix}- {name}".format(name=packageBody.Name, prefix=prefix))
+ buffer.append(
+ "{prefix}- Name: {name}".format(name=packageBody.Name, prefix=prefix)
+ )
buffer.append("{prefix} Declared:".format(prefix=prefix))
for item in packageBody.DeclaredItems:
for line in self.formatDeclaredItems(item, level + 1):
@@ -182,14 +192,16 @@ class PrettyPrint:
) -> StringBuffer:
buffer = []
prefix = " " * level
- buffer.append("{prefix}- {name}".format(name=configuration.Name, prefix=prefix))
+ buffer.append(
+ "{prefix}- Name: {name}".format(name=configuration.Name, prefix=prefix)
+ )
return buffer
def formatContext(self, context: Context, level: int = 0) -> StringBuffer:
buffer = []
prefix = " " * level
- buffer.append("{prefix}- {name}".format(name=context.Name, prefix=prefix))
+ buffer.append("{prefix}- Name: {name}".format(name=context.Name, prefix=prefix))
return buffer
@@ -218,45 +230,18 @@ class PrettyPrint:
) -> StringBuffer:
buffer = []
prefix = " " * level
- subType = generic.SubType
- if isinstance(subType, SimpleSubTypeSymbol):
- buffer.append(
- "{prefix} - {name} : {mode} {type}{initialValue}".format(
- prefix=prefix,
- name=generic.Name,
- mode=ModeTranslation[generic.Mode],
- type=subType.SymbolName,
- initialValue=self.formatInitialValue(generic),
- )
- )
- elif isinstance(subType, ConstrainedSubTypeSymbol):
- buffer.append(
- "{prefix} - {name} : {mode} {type}({constraints}){initialValue}".format(
- prefix=prefix,
- name=generic.Name,
- 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
- ]
- ),
- initialValue=self.formatInitialValue(generic),
- )
- )
- else:
- raise PrettyPrintException(
- "Unhandled constraint kind for generic '{name}'.".format(
- name=generic.Name
- )
+
+ buffer.append(
+ "{prefix} - {name} : {mode!s} {subtypeindication}{initialValue}".format(
+ prefix=prefix,
+ name=generic.Name,
+ mode=generic.Mode,
+ subtypeindication=self.formatSubtypeIndication(
+ generic.SubType, "generic", generic.Name
+ ),
+ initialValue=self.formatInitialValue(generic),
)
+ )
return buffer
@@ -267,10 +252,10 @@ class PrettyPrint:
prefix = " " * level
buffer.append(
- "{prefix} - {name} : {mode} {subtypeindication}{initialValue}".format(
+ "{prefix} - {name} : {mode!s} {subtypeindication}{initialValue}".format(
prefix=prefix,
name=port.Name,
- mode=ModeTranslation[port.Mode],
+ mode=port.Mode,
subtypeindication=self.formatSubtypeIndication(
port.SubType, "port", port.Name
),
@@ -292,7 +277,7 @@ class PrettyPrint:
subtype=self.formatSubtypeIndication(
item.SubType, "constant", item.Name
),
- expr=self.formatExpression(item.DefaultExpression),
+ expr=str(item.DefaultExpression),
)
)
elif isinstance(item, Signal):
@@ -303,9 +288,7 @@ class PrettyPrint:
subtype=self.formatSubtypeIndication(
item.SubType, "signal", item.Name
),
- initValue=" := {expr}".format(
- expr=self.formatExpression(item.DefaultExpression)
- )
+ initValue=" := {expr}".format(expr=str(item.DefaultExpression))
if item.DefaultExpression is not None
else "",
)
@@ -319,16 +302,8 @@ class PrettyPrint:
if isinstance(subTypeIndication, SimpleSubTypeSymbol):
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
- ]
- )
+ ranges = [str(c.Range) for c in subTypeIndication.Constraints]
+ constraints = ", ".join(ranges)
return "{type}({constraints})".format(
type=subTypeIndication.SymbolName, constraints=constraints
@@ -344,36 +319,4 @@ class PrettyPrint:
if item.DefaultExpression is None:
return ""
- return " := {expr}".format(expr=self.formatExpression(item.DefaultExpression))
-
- def formatExpression(self, expression: Expression) -> str:
- if isinstance(expression, SimpleObjectSymbol):
- return "{name}".format(name=expression.SymbolName)
- elif isinstance(expression, IntegerLiteral):
- return "{value}".format(value=expression.Value)
- elif isinstance(expression, FloatingPointLiteral):
- return "{value}".format(value=expression.Value)
- elif isinstance(expression, CharacterLiteral):
- return "'{value}'".format(value=expression.Value)
- elif isinstance(expression, UnaryExpression):
- try:
- operator = UnaryExpressionTranslation[type(expression)]
- except KeyError:
- raise PrettyPrintException("Unhandled operator for unary expression.")
-
- return "{operator}{operand}".format(
- operand=self.formatExpression(expression.Operand), operator=operator
- )
- elif isinstance(expression, BinaryExpression):
- try:
- operator = BinaryExpressionTranslation[type(expression)]
- except KeyError:
- raise PrettyPrintException("Unhandled operator for binary expression.")
-
- return "{left}{operator}{right}".format(
- left=self.formatExpression(expression.LeftOperand),
- right=self.formatExpression(expression.RightOperand),
- operator=operator,
- )
- else:
- raise PrettyPrintException("Unhandled expression kind.")
+ return " := {expr!s}".format(expr=item.DefaultExpression)