Skip to content

Commit

Permalink
Implement Verilog Preprocessor. Reworked source reference insfrastruc…
Browse files Browse the repository at this point in the history
…ture. #41

Add type hints to some visitors.
Fixed error message bug
Verilog preprocessor unit tests
  • Loading branch information
amykyta3 committed Sep 11, 2020
1 parent 29a9dc2 commit f415fa6
Show file tree
Hide file tree
Showing 30 changed files with 1,516 additions and 608 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ jobs:
include:
# Run lint
- name: Lint
python: 3.8
install:
- python -m pip install pylint
- python setup.py install
script: pylint --rcfile test/pylint.rc systemrdl

# Run static type checking
- name: Type Check
python: 3.8
install:
- python -m pip install mypy
script: mypy --config-file test/mypy.ini systemrdl
Expand Down
3 changes: 0 additions & 3 deletions docs/api/messages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,3 @@ Message Handling
----------------
.. autoclass:: systemrdl.messages.MessagePrinter
:members:

.. autoclass:: systemrdl.messages.SourceRef
:members:
14 changes: 0 additions & 14 deletions docs/known_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@ SystemRDL ``constraint`` blocks are not implemented yet.




Partial implementation of Verilog Preprocessor
----------------------------------------------

The Verilog-style preprocessor currently only implements ```include`` directive
expansion.
Conditional and macro directives are not implemented yet. (```ifdef``,
```else``, ```define``, etc.)

As a workaround, equivalent preprocessing can be accomplished using the
embedded Perl preprocessor.



No support for non-homogeneous arrays
-------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion systemrdl/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.12.0"
__version__ = "1.13.0"
2 changes: 1 addition & 1 deletion systemrdl/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self, **kwargs: Any):
self.msg = self.env.msg
self.namespace = NamespaceRegistry(self.env) # type: NamespaceRegistry
self.visitor = RootVisitor(self)
self.root = self.visitor.component
self.root = self.visitor.component # type: comp.Root # type: ignore

def define_udp(self, name, valid_type, valid_components=None, default=None):
# type: (str, List[Any], Optional[Set[Type[comp.Component]]], Any) -> None
Expand Down
10 changes: 5 additions & 5 deletions systemrdl/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

if TYPE_CHECKING:
from .core.parameter import Parameter
from .messages import SourceRef
from .source_ref import SourceRefBase

class Component:
"""
Expand Down Expand Up @@ -70,8 +70,8 @@ def __init__(self) -> None:
# Properties applied to this component
self.properties = {} # type: Dict[str, Any]

# SourceRef for the component definition
self.def_src_ref = None # type: Optional['SourceRef']
# Source Ref for the component definition
self.def_src_ref = None # type: Optional['SourceRefBase']

#------------------------------
# Component instantiation
Expand All @@ -91,8 +91,8 @@ def __init__(self) -> None:
#: True if instance type is external. False if internal.
self.external = None # type: bool

# SourceRef for the component instantiation.
self.inst_src_ref = None # type: Optional['SourceRef']
# Source Ref for the component instantiation.
self.inst_src_ref = None # type: Optional['SourceRefBase']

#------------------------------
# List of property names that were assigned via a dynamic property
Expand Down
38 changes: 21 additions & 17 deletions systemrdl/core/BaseVisitor.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from .helpers import get_ID_text

from ..messages import SourceRef
from ..parser.SystemRDLParser import SystemRDLParser
from ..parser.SystemRDLVisitor import SystemRDLVisitor
from ..source_ref import src_ref_from_antlr

from .. import rdltypes

if TYPE_CHECKING:
from ..compiler import RDLCompiler
from antlr4.Token import CommonToken
from antlr4 import ParserRuleContext
from .namespace import TypeNSEntry
from typing import Union, Type

class BaseVisitor(SystemRDLVisitor):

Expand All @@ -32,6 +36,7 @@ def __init__(self, compiler: 'RDLCompiler') -> None:
SystemRDLParser.BOOLEAN_kw : bool
}
def datatype_from_token(self, token):
# type: ('CommonToken') -> Type[Union[int, str, bool, rdltypes.BuiltinEnum, rdltypes.UserEnum, rdltypes.UserStruct]]
"""
Given a SystemRDLParser token, lookup the type
This only includes types under the "data_type" grammar rule
Expand All @@ -44,19 +49,18 @@ def datatype_from_token(self, token):
if typ is None:
self.msg.fatal(
"Type '%s' is not defined" % get_ID_text(token),
SourceRef.from_antlr(token)
src_ref_from_antlr(token)
)

if rdltypes.is_user_enum(typ) or rdltypes.is_user_struct(typ):
return typ
return typ # type: ignore
else:
self.msg.fatal(
"Type '%s' is not a struct or enum" % get_ID_text(token),
SourceRef.from_antlr(token)
src_ref_from_antlr(token)
)

else:
return self._DataType_Map[token.type]
return self._DataType_Map[token.type]

#---------------------------------------------------------------------------
# Keyword passthrough visitors
Expand All @@ -67,35 +71,35 @@ def datatype_from_token(self, token):
# These visitors propagate the original tokens all the way back up to the
# visitor that actually needs to know which keyword was used.

def passthru_kw_token(self, ctx):
def passthru_kw_token(self, ctx: 'ParserRuleContext') -> Any:
if ctx.kw is not None:
return ctx.kw
else:
return self.visitChildren(ctx)

def visitComponent_inst_type(self, ctx: SystemRDLParser.Component_inst_typeContext):
def visitComponent_inst_type(self, ctx: SystemRDLParser.Component_inst_typeContext) -> Any:
return self.passthru_kw_token(ctx)

def visitComponent_type(self, ctx: SystemRDLParser.Component_typeContext):
def visitComponent_type(self, ctx: SystemRDLParser.Component_typeContext) -> Any:
return self.passthru_kw_token(ctx)

def visitComponent_type_primary(self, ctx: SystemRDLParser.Component_type_primaryContext):
def visitComponent_type_primary(self, ctx: SystemRDLParser.Component_type_primaryContext) -> Any:
return self.passthru_kw_token(ctx)

def visitData_type(self, ctx: SystemRDLParser.Data_typeContext):
def visitData_type(self, ctx: SystemRDLParser.Data_typeContext) -> Any:
return self.passthru_kw_token(ctx)

def visitBasic_data_type(self, ctx: SystemRDLParser.Basic_data_typeContext):
def visitBasic_data_type(self, ctx: SystemRDLParser.Basic_data_typeContext) -> Any:
return self.passthru_kw_token(ctx)

def visitProp_keyword(self, ctx: SystemRDLParser.Prop_keywordContext):
def visitProp_keyword(self, ctx: SystemRDLParser.Prop_keywordContext) -> Any:
return self.passthru_kw_token(ctx)

def visitProp_mod(self, ctx: SystemRDLParser.Prop_modContext):
def visitProp_mod(self, ctx: SystemRDLParser.Prop_modContext) -> Any:
return self.passthru_kw_token(ctx)

def visitUdp_data_type(self, ctx: SystemRDLParser.Udp_data_typeContext):
def visitUdp_data_type(self, ctx: SystemRDLParser.Udp_data_typeContext) -> Any:
return self.passthru_kw_token(ctx)

def visitUdp_comp_type(self, ctx: SystemRDLParser.Udp_comp_typeContext):
def visitUdp_comp_type(self, ctx: SystemRDLParser.Udp_comp_typeContext) -> Any:
return self.passthru_kw_token(ctx)

0 comments on commit f415fa6

Please sign in to comment.