forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_modules.py
executable file
·134 lines (105 loc) · 4.09 KB
/
print_modules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
# Copyright 2017-2020 The Verible Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Print module name, ports, parameters and imports.
Usage: print_modules.py PATH_TO_VERIBLE_VERILOG_SYNTAX \\
VERILOG_FILE [VERILOG_FILE [...]]
This example shows how to use ``verible-verilog-syntax --export_json ...``
output to extract information about module declarations found in System Verilog
source files. Extracted information:
* module name
* module port names
* module parameter names
* module imports
* module header code
"""
import sys
import anytree
import verible_verilog_syntax
def process_file_data(path: str, data: verible_verilog_syntax.SyntaxData):
"""Print information about modules found in SystemVerilog file.
This function uses verible_verilog_syntax.Node methods to find module
declarations and specific tokens containing following information:
* module name
* module port names
* module parameter names
* module imports
* module header code
Args:
path: Path to source file (used only for informational purposes)
data: Parsing results returned by one of VeribleVerilogSyntax' parse_*
methods.
"""
if not data.tree:
return
modules_info = []
# Collect information about each module declaration in the file
for module in data.tree.iter_find_all({"tag": "kModuleDeclaration"}):
module_info = {
"header_text": "",
"name": "",
"ports": [],
"parameters": [],
"imports": [],
}
# Find module header
header = module.find({"tag": "kModuleHeader"})
if not header:
continue
module_info["header_text"] = header.text
# Find module name
name = header.find({"tag": ["SymbolIdentifier", "EscapedIdentifier"]},
iter_=anytree.PreOrderIter)
if not name:
continue
module_info["name"] = name.text
# Get the list of ports
for port in header.iter_find_all({"tag": ["kPortDeclaration", "kPort"]}):
port_id = port.find({"tag": ["SymbolIdentifier", "EscapedIdentifier"]})
module_info["ports"].append(port_id.text)
# Get the list of parameters
for param in header.iter_find_all({"tag": ["kParamDeclaration"]}):
param_id = param.find({"tag": ["SymbolIdentifier", "EscapedIdentifier"]})
module_info["parameters"].append(param_id.text)
# Get the list of imports
for pkg in module.iter_find_all({"tag": ["kPackageImportItem"]}):
module_info["imports"].append(pkg.text)
modules_info.append(module_info)
# Print results
if len(modules_info) > 0:
print(f"\033[1;97;7m{path} \033[0m\n")
def print_entry(key, values):
fmt_values = [f"\033[92m{v}\033[0m" for v in values]
value_part = (f"\n\033[33m// {' '*len(key)}".join(fmt_values)
or "\033[90m-\033[0m")
print(f"\033[33m// \033[93m{key}{value_part}")
for module_info in modules_info:
print_entry("name: ", [module_info["name"]])
print_entry("ports: ", module_info["ports"])
print_entry("parameters: ", module_info["parameters"])
print_entry("imports: ", module_info["imports"])
print(f"\033[97m{module_info['header_text']}\033[0m\n")
def main():
if len(sys.argv) < 3:
print(f"Usage: {sys.argv[0]} PATH_TO_VERIBLE_VERILOG_SYNTAX " +
"VERILOG_FILE [VERILOG_FILE [...]]")
return 1
parser_path = sys.argv[1]
files = sys.argv[2:]
parser = verible_verilog_syntax.VeribleVerilogSyntax(executable=parser_path)
data = parser.parse_files(files)
for file_path, file_data in data.items():
process_file_data(file_path, file_data)
if __name__ == "__main__":
sys.exit(main())