Skip to content

Commit b1b676c

Browse files
committed
Handling escaped Verilog identifiers
1 parent 2ac87da commit b1b676c

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

hdl_checker/parsers/verilog_parser.py

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
import logging
2020
import re
21-
from typing import Any, Generator, Iterable
21+
import string
22+
from typing import Any, Generator, Iterable, List, Tuple, Type
2223

2324
from .elements.dependency_spec import (
2425
BaseDependencySpec,
@@ -35,7 +36,10 @@
3536

3637
_logger = logging.getLogger(__name__)
3738

38-
_VERILOG_IDENTIFIER = r"[a-zA-Z_][a-zA-Z0-9_$]+"
39+
_VERILOG_IDENTIFIER = "|".join(
40+
[r"[a-zA-Z_][a-zA-Z0-9_$]+", r"\\[%s]+(?=\s)" % string.printable.replace(" ", "")]
41+
)
42+
3943
_COMMENT = r"(?:\/\*.*?\*\/|//[^(?:\r\n?|\n)]*)"
4044

4145

@@ -55,7 +59,7 @@
5559
"|".join(
5660
[
5761
r"(?P<package>\b{0})\s*::\s*(?:{0}|\*)".format(_VERILOG_IDENTIFIER),
58-
r"\bvirtual\s+class\s+(?P<class>\b{0})".format(
62+
r"(\bvirtual\b)?\s*\bclass\s+(?:static|automatic)?(?P<class>\b{0})".format(
5963
_VERILOG_IDENTIFIER
6064
),
6165
r"(?<=`include\b)\s*\"(?P<include>.*?)\"",
@@ -100,49 +104,38 @@ def _iterDesignUnitMatches(self):
100104
def _getDependencies(self): # type: () -> Iterable[BaseDependencySpec]
101105
text = self.getSourceContent()
102106

103-
for match in _DEPENDENCIES.finditer(text):
104-
include_name = match.groupdict().get("include", None)
105-
106-
# package 'std' seems to be built-in. Need to have a look a this
107-
if include_name is not None:
108-
line_number = text[: match.end()].count("\n")
109-
column_number = len(text[: match.start()].split("\n")[-1])
110-
111-
yield IncludedPath(
112-
owner=self.filename,
113-
name=VerilogIdentifier(include_name),
114-
locations=(Location(line_number, column_number),),
115-
)
116-
117-
# Only SystemVerilog has imports
118-
if self.filetype is FileType.verilog:
119-
continue
120-
121-
name = match.groupdict().get("package", None)
107+
match_groups = [
108+
("include", IncludedPath)
109+
] # type: List[Tuple[str, Type[BaseDependencySpec]]]
122110

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

129-
yield RequiredDesignUnit(
130-
owner=self.filename,
131-
name=VerilogIdentifier(name), # type: ignore
132-
locations=(Location(line_number, column_number),),
133-
)
118+
for match in _DEPENDENCIES.finditer(text):
119+
for match_group, klass in match_groups:
120+
name = match.groupdict().get(match_group, None)
121+
# package 'std' seems to be built-in. Need to have a look a
122+
# this if include_name is not None and include_name != 'std':
123+
if match_group == "package" and name == "std":
124+
continue
134125

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

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

141-
yield RequiredDesignUnit(
133+
yield klass(
142134
owner=self.filename,
143135
name=VerilogIdentifier(name),
144136
locations=(Location(line_number, column_number),),
145137
)
138+
break
146139

147140
def _getDesignUnits(self): # type: () -> Generator[VerilogDesignUnit, None, None]
148141
for match, locations in self._iterDesignUnitMatches():

hdl_checker/tests/test_verilog_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def setUpClass(cls):
7272
" localparam foo::bar = std::randomize(cycles);",
7373
"endmodule",
7474
"",
75-
"package msgPkg;",
75+
"package \\m$gPkg! ;",
7676
" integer errCnt = 0;",
7777
" integer warnCnt = 0;",
7878
"endpackage",
@@ -96,7 +96,7 @@ def test_GetDesignUnits(self):
9696
),
9797
VerilogDesignUnit(
9898
owner=self.source.filename,
99-
name="msgPkg",
99+
name="\\m$gPkg!",
100100
type_=DesignUnitType.package,
101101
locations={(15, 8)},
102102
),

0 commit comments

Comments
 (0)