Skip to content

Commit

Permalink
Handling escaped Verilog identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
suoto committed Nov 14, 2019
1 parent 2ac87da commit b1b676c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
63 changes: 28 additions & 35 deletions hdl_checker/parsers/verilog_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import logging
import re
from typing import Any, Generator, Iterable
import string
from typing import Any, Generator, Iterable, List, Tuple, Type

from .elements.dependency_spec import (
BaseDependencySpec,
Expand All @@ -35,7 +36,10 @@

_logger = logging.getLogger(__name__)

_VERILOG_IDENTIFIER = r"[a-zA-Z_][a-zA-Z0-9_$]+"
_VERILOG_IDENTIFIER = "|".join(
[r"[a-zA-Z_][a-zA-Z0-9_$]+", r"\\[%s]+(?=\s)" % string.printable.replace(" ", "")]
)

_COMMENT = r"(?:\/\*.*?\*\/|//[^(?:\r\n?|\n)]*)"


Expand All @@ -55,7 +59,7 @@
"|".join(
[
r"(?P<package>\b{0})\s*::\s*(?:{0}|\*)".format(_VERILOG_IDENTIFIER),
r"\bvirtual\s+class\s+(?P<class>\b{0})".format(
r"(\bvirtual\b)?\s*\bclass\s+(?:static|automatic)?(?P<class>\b{0})".format(
_VERILOG_IDENTIFIER
),
r"(?<=`include\b)\s*\"(?P<include>.*?)\"",
Expand Down Expand Up @@ -100,49 +104,38 @@ def _iterDesignUnitMatches(self):
def _getDependencies(self): # type: () -> Iterable[BaseDependencySpec]
text = self.getSourceContent()

for match in _DEPENDENCIES.finditer(text):
include_name = match.groupdict().get("include", None)

# package 'std' seems to be built-in. Need to have a look a this
if include_name is not None:
line_number = text[: match.end()].count("\n")
column_number = len(text[: match.start()].split("\n")[-1])

yield IncludedPath(
owner=self.filename,
name=VerilogIdentifier(include_name),
locations=(Location(line_number, column_number),),
)

# Only SystemVerilog has imports
if self.filetype is FileType.verilog:
continue

name = match.groupdict().get("package", None)
match_groups = [
("include", IncludedPath)
] # type: List[Tuple[str, Type[BaseDependencySpec]]]

# package 'std' seems to be built-in. Need to have a look a this
# if include_name is not None and include_name != 'std':
if name not in (None, "std"):
line_number = text[: match.end()].count("\n")
column_number = len(text[: match.start()].split("\n")[-1])
# Only SystemVerilog has imports or classes
if self.filetype is FileType.systemverilog:
match_groups += [
("package", RequiredDesignUnit),
("class", RequiredDesignUnit),
]

yield RequiredDesignUnit(
owner=self.filename,
name=VerilogIdentifier(name), # type: ignore
locations=(Location(line_number, column_number),),
)
for match in _DEPENDENCIES.finditer(text):
for match_group, klass in match_groups:
name = match.groupdict().get(match_group, None)
# package 'std' seems to be built-in. Need to have a look a
# this if include_name is not None and include_name != 'std':
if match_group == "package" and name == "std":
continue

name = match.groupdict().get("class", None)
# package 'std' seems to be built-in. Need to have a look a this
if name is None:
continue

if name is not None:
line_number = text[: match.end()].count("\n")
column_number = len(text[: match.start()].split("\n")[-1])

yield RequiredDesignUnit(
yield klass(
owner=self.filename,
name=VerilogIdentifier(name),
locations=(Location(line_number, column_number),),
)
break

def _getDesignUnits(self): # type: () -> Generator[VerilogDesignUnit, None, None]
for match, locations in self._iterDesignUnitMatches():
Expand Down
4 changes: 2 additions & 2 deletions hdl_checker/tests/test_verilog_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def setUpClass(cls):
" localparam foo::bar = std::randomize(cycles);",
"endmodule",
"",
"package msgPkg;",
"package \\m$gPkg! ;",
" integer errCnt = 0;",
" integer warnCnt = 0;",
"endpackage",
Expand All @@ -96,7 +96,7 @@ def test_GetDesignUnits(self):
),
VerilogDesignUnit(
owner=self.source.filename,
name="msgPkg",
name="\\m$gPkg!",
type_=DesignUnitType.package,
locations={(15, 8)},
),
Expand Down

0 comments on commit b1b676c

Please sign in to comment.