|
8 | 8 | import sys
|
9 | 9 |
|
10 | 10 | skip_last_coma = 0
|
| 11 | +keep_comment = 1 |
| 12 | +keep_empty_line = 1 |
11 | 13 | if len(sys.argv) > 1:
|
12 | 14 | skip_last_coma = int(sys.argv[1])
|
13 | 15 |
|
| 16 | +if len(sys.argv) > 2: |
| 17 | + keep_comment = int(sys.argv[2]) |
| 18 | + |
14 | 19 | keywords = []
|
15 | 20 | keywords.extend(["input", "output", "inout", "ref", "parameter", "localparam"])
|
16 | 21 | keywords.extend(["reg", "logic", "wire", "bit", "integer", "int", "string", "type"])
|
|
19 | 24 | patterns = []
|
20 | 25 | patterns.append(re.compile(r'\[[^\[\]]*\]')) # port size, array size
|
21 | 26 | patterns.append(re.compile(r'=.*')) # assignment
|
22 |
| -patterns.append(re.compile(r'//.*')) # // comment |
| 27 | +patterns.append(re.compile(r'//.*') ) |
23 | 28 | patterns.append(re.compile(r'\w+\.\w+')) # interfaces with modport
|
24 | 29 | for kw in keywords: # match keywords
|
25 | 30 | patterns.append(re.compile("\\b%s\\b" % kw))
|
|
32 | 37 | pattern_two_words_no_coma = re.compile(r'^\s*(\w+)\s+(\w+.*)')
|
33 | 38 | pattern_spaces = re.compile(r'\s+')
|
34 | 39 |
|
| 40 | +pattern_inline_comment_kept = re.compile(r'.*\w+.*(//.*)') # comment in port define |
| 41 | +pattern_comment_kept = re.compile(r'\s*(//.*)') # one line comment |
| 42 | + |
35 | 43 | ports = []
|
| 44 | +ports_comments = {} # save comment for every port |
| 45 | +contents = [] # save ports and single line comments |
36 | 46 | wait_to_close_comment = 0
|
37 | 47 | indent_len = -1
|
38 | 48 |
|
|
53 | 63 | else:
|
54 | 64 | wait_to_close_comment = 1
|
55 | 65 | continue
|
| 66 | + # handle port comment |
| 67 | + port_comment = pattern_inline_comment_kept.match(line) |
| 68 | + if port_comment is not None: |
| 69 | + port_comment = port_comment.group(1) |
| 70 | + # handle single line comment |
| 71 | + line_comment = pattern_comment_kept.match(line) |
| 72 | + if line_comment is not None: |
| 73 | + line_comment = line_comment.group(1) |
56 | 74 | # handle all other patterns
|
57 | 75 | for pattern in patterns:
|
58 | 76 | line = pattern.sub(' ', line)
|
|
63 | 81 | # finally, get port names
|
64 | 82 | line = line.strip()
|
65 | 83 | if line != "":
|
66 |
| - ports.extend(line.split(' ')) |
| 84 | + port_names = line.split(' ') |
| 85 | + ports.extend(port_names) |
| 86 | + contents.extend(port_names) |
| 87 | + for port in port_names: |
| 88 | + ports_comments[port] = port_comment |
| 89 | + else: |
| 90 | + # add single line comment to port |
| 91 | + if line_comment is not None: |
| 92 | + contents.append(line_comment) |
67 | 93 |
|
68 | 94 | ports_nb = len(ports)
|
69 | 95 | i = 0
|
70 | 96 | if ports_nb > 0:
|
71 | 97 | max_str_len = len(max(ports, key=len))
|
72 |
| - for port in ports: |
| 98 | + indent_str = " " * indent_len |
| 99 | + for content in contents: |
| 100 | + if len(content) > 0: |
| 101 | + if content[:2] == "//": |
| 102 | + if keep_comment == 1: |
| 103 | + print(f'{indent_str}{content}') |
| 104 | + continue |
| 105 | + else: |
| 106 | + # empty line |
| 107 | + if keep_empty_line == 1: |
| 108 | + print('') |
| 109 | + continue |
| 110 | + port = content |
73 | 111 | skip_coma = skip_last_coma and i == (ports_nb - 1)
|
74 | 112 | space_str = " " * (max_str_len - len(port))
|
75 |
| - indent_str = " " * indent_len |
76 |
| - print("%s.%s%s (%s%s)%s" % ( |
77 |
| - indent_str, port, space_str, port, space_str, (",", "")[skip_coma])) |
| 113 | + output_line_port = "%s.%s%s (%s%s)%s" % ( |
| 114 | + indent_str, port, space_str, port, space_str, (",", "")[skip_coma]) |
| 115 | + if ports_comments.get(port) is not None and keep_comment == 1: |
| 116 | + # add port comment |
| 117 | + output_line = f"{output_line_port} {ports_comments.get(port)}" |
| 118 | + else: |
| 119 | + output_line = output_line_port |
| 120 | + print(output_line) |
78 | 121 | i = i + 1
|
0 commit comments