hdl-parser 0.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hdl_parser/Templates/__init__.py +23 -0
- hdl_parser/Templates/instance_template +10 -0
- hdl_parser/Templates/instance_with_param_template +19 -0
- hdl_parser/__init__.py +46 -0
- hdl_parser/_datamodel.py +72 -0
- hdl_parser/_gen_templates.py +94 -0
- hdl_parser/_sv/__init__.py +21 -0
- hdl_parser/_sv/lexer.py +478 -0
- hdl_parser/_sv/parser.py +258 -0
- hdl_parser/_sv/token.py +39 -0
- hdl_parser/_vhdl/__init__.py +21 -0
- hdl_parser/_vhdl/lexer.py +260 -0
- hdl_parser/_vhdl/parser.py +168 -0
- hdl_parser/_vhdl/token.py +33 -0
- hdl_parser/cli.py +192 -0
- hdl_parser/datamodel.py +139 -0
- hdl_parser/filelistparser.py +103 -0
- hdl_parser/parser.py +95 -0
- hdl_parser-0.6.0.dist-info/METADATA +74 -0
- hdl_parser-0.6.0.dist-info/RECORD +23 -0
- hdl_parser-0.6.0.dist-info/WHEEL +4 -0
- hdl_parser-0.6.0.dist-info/entry_points.txt +5 -0
- hdl_parser-0.6.0.dist-info/licenses/LICENSE +21 -0
hdl_parser/_sv/lexer.py
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2025 ericsmacedo
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
# furnished to do so, subject to the following conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
# copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
# SOFTWARE.
|
|
22
|
+
|
|
23
|
+
import logging
|
|
24
|
+
import re
|
|
25
|
+
|
|
26
|
+
from pygments.lexer import ExtendedRegexLexer, LexerContext, bygroups, default, include, words
|
|
27
|
+
from pygments.token import (
|
|
28
|
+
Comment,
|
|
29
|
+
Error,
|
|
30
|
+
Keyword,
|
|
31
|
+
Name,
|
|
32
|
+
Number,
|
|
33
|
+
Operator,
|
|
34
|
+
Punctuation,
|
|
35
|
+
String,
|
|
36
|
+
Token,
|
|
37
|
+
Whitespace,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
from .token import IFDEF, Module, Port
|
|
41
|
+
|
|
42
|
+
# Create a LOGGER for this module
|
|
43
|
+
LOGGER = logging.getLogger(__name__)
|
|
44
|
+
|
|
45
|
+
punctuation = (r"[()\[\],.;\'$]", Punctuation)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
preproc = (r"`(ifdef|ifndef|else|endif|define|undef)\b\s*?\w+", Comment.Preproc)
|
|
49
|
+
|
|
50
|
+
# fmt: off
|
|
51
|
+
builtin_tup = (
|
|
52
|
+
"$exit", "$finish", "$stop", "$realtime", "$stime", "$time", "$printtimescale",
|
|
53
|
+
"$timeformat", "$bitstoreal", "$bitstoshortreal", "$cast", "$itor", "$realtobits",
|
|
54
|
+
"$rtoi", "$shortrealtobits", "$signed", "$unsigned", "$bits", "$isunbounded",
|
|
55
|
+
"$typename", "$dimensions", "$high", "$increment", "$left", "$low", "$right",
|
|
56
|
+
"$size", "$unpacked_dimensions", "$acos", "$acosh", "$asin", "$asinh", "$atan",
|
|
57
|
+
"$atan2", "$atanh", "$ceil", "$clog2", "$cos", "$cosh", "$exp", "$floor", "$hypot",
|
|
58
|
+
"$ln", "$log10", "$pow", "$sin", "$sinh", "$sqrt", "$tan", "$tanh",
|
|
59
|
+
"$countbits", "$countones", "$isunknown", "$onehot", "$onehot0", "$info", "$error",
|
|
60
|
+
"$fatal", "$warning", "$assertcontrol", "$assertfailoff", "$assertfailon",
|
|
61
|
+
"$assertkill", "$assertnonvacuouson", "$assertoff", "$asserton", "$assertpassoff",
|
|
62
|
+
"$assertpasson", "$assertvacuousoff", "$changed", "$changed_gclk",
|
|
63
|
+
"$changing_gclk", "$falling_gclk", "$fell", "$fell_gclk", "$future_gclk", "$past",
|
|
64
|
+
"$past_gclk", "$rising_gclk", "$rose", "$rose_gclk", "$sampled", "$stable",
|
|
65
|
+
"$stable_gclk", "$steady_gclk", "$coverage_control", "$coverage_get",
|
|
66
|
+
"$coverage_get_max", "$coverage_merge", "$coverage_save", "$get_coverage",
|
|
67
|
+
"$load_coverage_db", "$set_coverage_db_name",
|
|
68
|
+
"$dist_chi_square", "$dist_erlang", "$dist_exponential", "$dist_normal",
|
|
69
|
+
"$dist_poisson", "$dist_t", "$dist_uniform", "$random", "$q_add", "$q_exam",
|
|
70
|
+
"$q_full", "$q_initialize", "$q_remove", "$async$and$array", "$async$and$plane",
|
|
71
|
+
"$async$nand$array", "$async$nand$plane", "$async$nor$array", "$async$nor$plane",
|
|
72
|
+
"$async$or$array", "$async$or$plane", "$sync$and$array", "$sync$and$plane",
|
|
73
|
+
"$sync$nand$array", "$sync$nand$plane", "$sync$nor$array", "$sync$nor$plane",
|
|
74
|
+
"$sync$or$array", "$sync$or$plane",
|
|
75
|
+
"$system", "$display", "$displayb", "$displayh", "$displayo", "$monitor",
|
|
76
|
+
"$monitorb", "$monitorh", "$monitoro", "$monitoroff", "$monitoron", "$strobe",
|
|
77
|
+
"$strobeb", "$strobeh", "$strobeo", "$write", "$writeb", "$writeh", "$writeo",
|
|
78
|
+
"$fclose", "$fdisplay", "$fdisplayb", "$fdisplayh", "$fdisplayo", "$feof",
|
|
79
|
+
"$ferror", "$fflush", "$fgetc", "$fgets", "$fmonitor", "$fmonitorb", "$fmonitorh",
|
|
80
|
+
"$fmonitoro", "$fopen", "$fread", "$fscanf", "$fseek", "$fstrobe", "$fstrobeb",
|
|
81
|
+
"$fstrobeh", "$fstrobeo", "$ftell", "$fwrite", "$fwriteb", "$fwriteh", "$fwriteo",
|
|
82
|
+
"$rewind", "$sformat", "$sformatf", "$sscanf", "$swrite", "$swriteb", "$swriteh",
|
|
83
|
+
"$swriteo", "$ungetc", "$readmemb", "$readmemh", "$writememb", "$writememh",
|
|
84
|
+
"$test$plusargs", "$value$plusargs", "$dumpall", "$dumpfile", "$dumpflush",
|
|
85
|
+
"$dumplimit", "$dumpoff", "$dumpon", "$dumpports", "$dumpportsall",
|
|
86
|
+
"$dumpportsflush", "$dumpportslimit", "$dumpportsoff", "$dumpportson", "$dumpvars",
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
keywords_tup = (
|
|
90
|
+
"accept_on", "alias", "always", "always_comb", "always_ff", "always_latch", "and",
|
|
91
|
+
"assert", "assign", "assume", "automatic", "before", "begin", "bind", "bins",
|
|
92
|
+
"binsof", "break", "buf", "bufif0", "bufif1", "case", "casex", "casez", "cell",
|
|
93
|
+
"checker", "clocking", "cmos", "config", "constraint", "context", "continue",
|
|
94
|
+
"cover", "covergroup", "coverpoint", "cross", "deassign", "default", "defparam",
|
|
95
|
+
"design", "disable", "do", "edge", "else", "end", "endcase", "endchecker",
|
|
96
|
+
"endclocking", "endconfig", "endfunction", "endgenerate", "endgroup", "endinterface",
|
|
97
|
+
"endmodule", "endpackage", "endprimitive", "endprogram", "endproperty",
|
|
98
|
+
"endsequence", "endspecify", "endtable", "endtask", "enum", "eventually", "expect",
|
|
99
|
+
"export", "extern", "final", "first_match", "for", "force", "foreach", "forever",
|
|
100
|
+
"fork", "forkjoin", "function", "generate", "genvar", "global", "highz0", "highz1",
|
|
101
|
+
"if", "iff", "ifnone", "ignore_bins", "illegal_bins", "implies", "implements",
|
|
102
|
+
"import", "incdir", "include", "initial", "inout", "input", "instance",
|
|
103
|
+
"interconnect", "interface", "intersect", "join", "join_any", "join_none", "large",
|
|
104
|
+
"let", "liblist", "library", "local", "localparam", "macromodule", "matches",
|
|
105
|
+
"medium", "modport", "module", "nand", "negedge", "nettype", "new", "nexttime",
|
|
106
|
+
"nmos", "nor", "noshowcancelled", "not", "notif0", "notif1", "null", "or",
|
|
107
|
+
"output", "package", "packed", "parameter", "pmos", "posedge", "primitive",
|
|
108
|
+
"priority", "program", "property", "protected", "pull0", "pull1", "pulldown",
|
|
109
|
+
"pullup", "pulsestyle_ondetect", "pulsestyle_onevent", "pure", "rand", "randc",
|
|
110
|
+
"randcase", "randsequence", "rcmos", "ref", "reject_on", "release", "repeat",
|
|
111
|
+
"restrict", "return", "rnmos", "rpmos", "rtran", "rtranif0", "rtranif1",
|
|
112
|
+
"s_always", "s_eventually", "s_nexttime", "s_until", "s_until_with", "scalared",
|
|
113
|
+
"sequence", "showcancelled", "small", "soft", "solve", "specify", "specparam",
|
|
114
|
+
"static", "strong", "strong0", "strong1", "struct", "super", "sync_accept_on",
|
|
115
|
+
"sync_reject_on", "table", "tagged", "task", "this", "throughout", "timeprecision",
|
|
116
|
+
"timeunit", "tran", "tranif0", "tranif1", "typedef", "union", "unique", "unique0",
|
|
117
|
+
"until", "until_with", "untyped", "use", "vectored", "virtual", "wait",
|
|
118
|
+
"wait_order", "weak", "weak0", "weak1", "while", "wildcard", "with", "within",
|
|
119
|
+
"xnor", "xor",
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
variable_types_tup = (
|
|
123
|
+
# Variable types
|
|
124
|
+
"bit", "byte", "chandle", "const", "event", "int", "integer", "logic", "longint",
|
|
125
|
+
"real", "realtime", "reg", "shortint", "shortreal", "signed", "string", "time",
|
|
126
|
+
"type", "unsigned", "var", "void",
|
|
127
|
+
# Net types
|
|
128
|
+
"supply0", "supply1", "tri", "triand", "trior", "trireg", "tri0", "tri1", "uwire",
|
|
129
|
+
"wand", "wire", "wor",
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
port_types_tup = (
|
|
133
|
+
# Variable types
|
|
134
|
+
"bit", "byte", "chandle", "const", "event", "int", "integer", "logic", "longint",
|
|
135
|
+
"real", "realtime", "reg", "shortint", "shortreal", "signed", "string", "time",
|
|
136
|
+
"type", "unsigned", "var", "void",
|
|
137
|
+
# Net types
|
|
138
|
+
"supply0", "supply1", "tri", "triand", "trior", "trireg", "tri0", "tri1", "uwire",
|
|
139
|
+
"wand", "wire", "wor",
|
|
140
|
+
)
|
|
141
|
+
# fmt: on
|
|
142
|
+
|
|
143
|
+
keywords = (
|
|
144
|
+
words(
|
|
145
|
+
keywords_tup,
|
|
146
|
+
suffix=r"\b",
|
|
147
|
+
),
|
|
148
|
+
Keyword,
|
|
149
|
+
)
|
|
150
|
+
variable_types = (
|
|
151
|
+
words(
|
|
152
|
+
variable_types_tup,
|
|
153
|
+
suffix=r"\b",
|
|
154
|
+
),
|
|
155
|
+
Keyword.Type,
|
|
156
|
+
)
|
|
157
|
+
builtin = (
|
|
158
|
+
words(
|
|
159
|
+
builtin_tup,
|
|
160
|
+
suffix=r"\b",
|
|
161
|
+
),
|
|
162
|
+
Name.Builtin,
|
|
163
|
+
)
|
|
164
|
+
port_types = words(
|
|
165
|
+
port_types_tup,
|
|
166
|
+
suffix=r"\b",
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
keywords_types_tup = keywords_tup + variable_types_tup
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def filter_instance_keywords_callback(lexer, match, ctx): # noqa: ARG001
|
|
173
|
+
"""Callback used to filter false matches for the module instances."""
|
|
174
|
+
module_name = match.group(1)
|
|
175
|
+
instance_name = match.group(2)
|
|
176
|
+
connections = match.group(3)
|
|
177
|
+
|
|
178
|
+
if instance_name not in keywords_types_tup and module_name not in keywords_types_tup:
|
|
179
|
+
yield match.start(1), Module.Body.Instance.Module, module_name
|
|
180
|
+
yield match.start(2), Module.Body.Instance.Name, instance_name
|
|
181
|
+
ctx.stack.append("instance_connections")
|
|
182
|
+
ctx.pos = match.end(2)
|
|
183
|
+
else:
|
|
184
|
+
yield match.start(1), Error, module_name
|
|
185
|
+
yield match.start(2), Error, instance_name
|
|
186
|
+
yield match.start(3), Error, connections
|
|
187
|
+
ctx.pos = match.end()
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def comments_callback(lexer: ExtendedRegexLexer, match, ctx: LexerContext): # noqa: ARG001
|
|
191
|
+
current_state = ctx.stack[-1]
|
|
192
|
+
|
|
193
|
+
# The actual comment is located at group 2
|
|
194
|
+
match_string = match.group(2)
|
|
195
|
+
match_start = match.start(0)
|
|
196
|
+
|
|
197
|
+
if current_state == "port_declaration":
|
|
198
|
+
yield match_start, Module.Port.Comment, match_string
|
|
199
|
+
elif current_state in ("param_declaration", "param_value", "param_value_delimiter"):
|
|
200
|
+
yield match_start, Module.Param.Comment, match_string
|
|
201
|
+
elif current_state == "instance_connections":
|
|
202
|
+
yield match_start, Module.Body.Instance.Con.Comment, match_string
|
|
203
|
+
else:
|
|
204
|
+
yield match_start, Comment, match_string
|
|
205
|
+
ctx.pos = match.end()
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
class SystemVerilogLexer(ExtendedRegexLexer):
|
|
209
|
+
"""Extends verilog lexer to recognise all SystemVerilog keywords.
|
|
210
|
+
|
|
211
|
+
SystemVerilog IEEE 1800-2009 standard.
|
|
212
|
+
"""
|
|
213
|
+
|
|
214
|
+
name = "systemverilog"
|
|
215
|
+
aliases = ["systemverilog", "sv"]
|
|
216
|
+
filenames = ["*.sv", "*.svh"]
|
|
217
|
+
mimetypes = ["text/x-systemverilog"]
|
|
218
|
+
url = "https://en.wikipedia.org/wiki/SystemVerilog"
|
|
219
|
+
version_added = "1.5"
|
|
220
|
+
flags = re.DOTALL
|
|
221
|
+
|
|
222
|
+
#: optional Comment or Whitespace
|
|
223
|
+
_ws = r"(?:\s|//.*?\n|/[*].*?[*]/)+"
|
|
224
|
+
|
|
225
|
+
tokens = {
|
|
226
|
+
"root": [
|
|
227
|
+
(r"^(\s*)(`define)", bygroups(Whitespace, Comment.Preproc), "macro"),
|
|
228
|
+
(r"^(\s*)(package)(\s+)", bygroups(Whitespace, Keyword.Namespace, Whitespace)),
|
|
229
|
+
(r"^(\s*)(import)(\s+)", bygroups(Whitespace, Keyword.Namespace, Whitespace), "import"),
|
|
230
|
+
(r"\s+", Whitespace),
|
|
231
|
+
(r"(\\)(\n)", bygroups(String.Escape, Whitespace)), # line continuation
|
|
232
|
+
(r"/(\\\n)?/(\n|(.|\n)*?[^\\]\n)", Comment.Single),
|
|
233
|
+
(r"/(\\\n)?[*](.|\n)*?[*](\\\n)?/", Comment.Multiline),
|
|
234
|
+
(r"[{}#@]", Punctuation),
|
|
235
|
+
(r'L?"', String, "string"),
|
|
236
|
+
(r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
|
|
237
|
+
(r"(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?", Number.Float),
|
|
238
|
+
(r"(\d+\.\d*|\.\d+|\d+[fF])[fF]?", Number.Float),
|
|
239
|
+
(r"([1-9][_0-9]*)?\s*\'[sS]?[bB]\s*[xXzZ?01][_xXzZ?01]*", Number.Bin),
|
|
240
|
+
(r"([1-9][_0-9]*)?\s*\'[sS]?[oO]\s*[xXzZ?0-7][_xXzZ?0-7]*", Number.Oct),
|
|
241
|
+
(r"([1-9][_0-9]*)?\s*\'[sS]?[dD]\s*[xXzZ?0-9][_xXzZ?0-9]*", Number.Integer),
|
|
242
|
+
(r"([1-9][_0-9]*)?\s*\'[sS]?[hH]\s*[xXzZ?0-9a-fA-F][_xXzZ?0-9a-fA-F]*", Number.Hex),
|
|
243
|
+
(r"\'[01xXzZ]", Number),
|
|
244
|
+
(r"[0-9][_0-9]*", Number.Integer),
|
|
245
|
+
(r"[~!%^&*+=|?:<>/-]", Operator),
|
|
246
|
+
(words(("inside", "dist"), suffix=r"\b"), Operator.Word),
|
|
247
|
+
(r"[()\[\],.;\'$]", Punctuation),
|
|
248
|
+
include("ifdef"),
|
|
249
|
+
(r"`[a-zA-Z_]\w*", Name.Constant),
|
|
250
|
+
(r"\bmodule\b", Module.ModuleStart, ("module_body", "module_name")),
|
|
251
|
+
keywords,
|
|
252
|
+
builtin,
|
|
253
|
+
(r"(class)(\s+)([a-zA-Z_]\w*)", bygroups(Keyword.Declaration, Whitespace, Name.Class)),
|
|
254
|
+
(r"(extends)(\s+)([a-zA-Z_]\w*)", bygroups(Keyword.Declaration, Whitespace, Name.Class)),
|
|
255
|
+
(
|
|
256
|
+
r"(endclass\b)(?:(\s*)(:)(\s*)([a-zA-Z_]\w*))?",
|
|
257
|
+
bygroups(Keyword.Declaration, Whitespace, Punctuation, Whitespace, Name.Class),
|
|
258
|
+
),
|
|
259
|
+
# fmt: off
|
|
260
|
+
variable_types,
|
|
261
|
+
(
|
|
262
|
+
words(
|
|
263
|
+
(
|
|
264
|
+
"`__FILE__",
|
|
265
|
+
"`__LINE__",
|
|
266
|
+
"`begin_keywords",
|
|
267
|
+
"`celldefine",
|
|
268
|
+
"`default_nettype",
|
|
269
|
+
"`define",
|
|
270
|
+
"`else",
|
|
271
|
+
"`elsif",
|
|
272
|
+
"`end_keywords",
|
|
273
|
+
"`endcelldefine",
|
|
274
|
+
"`endif",
|
|
275
|
+
"`ifdef",
|
|
276
|
+
"`ifndef",
|
|
277
|
+
"`include",
|
|
278
|
+
"`line",
|
|
279
|
+
"`nounconnected_drive",
|
|
280
|
+
"`pragma",
|
|
281
|
+
"`resetall",
|
|
282
|
+
"`timescale",
|
|
283
|
+
"`unconnected_drive",
|
|
284
|
+
"`undef",
|
|
285
|
+
"`undefineall",
|
|
286
|
+
),
|
|
287
|
+
suffix=r"\b",
|
|
288
|
+
),
|
|
289
|
+
Comment.Preproc,
|
|
290
|
+
),
|
|
291
|
+
# fmt: on
|
|
292
|
+
(r"[a-zA-Z_]\w*:(?!:)", Name.Label),
|
|
293
|
+
(r"\$?[a-zA-Z_]\w*", Name),
|
|
294
|
+
(r"\\(\S+)", Name),
|
|
295
|
+
],
|
|
296
|
+
"string": [
|
|
297
|
+
(r'"', String, "#pop"),
|
|
298
|
+
(r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
|
|
299
|
+
(r'[^\\"\n]+', String), # all other characters
|
|
300
|
+
(r"(\\)(\n)", bygroups(String.Escape, Whitespace)), # line continuation
|
|
301
|
+
(r"\\", String), # stray backslash
|
|
302
|
+
],
|
|
303
|
+
"macro": [
|
|
304
|
+
(r"[^/\n]+", Comment.Preproc),
|
|
305
|
+
(r"/[*](.|\n)*?[*]/", Comment.Multiline),
|
|
306
|
+
(r"//.*?$", Comment.Single, "#pop"),
|
|
307
|
+
(r"/", Comment.Preproc),
|
|
308
|
+
(r"(?<=\\)\n", Comment.Preproc),
|
|
309
|
+
(r"\n", Whitespace, "#pop"),
|
|
310
|
+
],
|
|
311
|
+
"import": [(r"[\w:]+\*?", Name.Namespace, "#pop")],
|
|
312
|
+
"module_body": [
|
|
313
|
+
(r"`\w+\s*\(.*?\)", Module.Other),
|
|
314
|
+
(r"\bendmodule\b", Module.ModuleEnd, "#pop"),
|
|
315
|
+
include("comments"),
|
|
316
|
+
include("ifdef"),
|
|
317
|
+
(words(("input", "output", "inout"), prefix=r"\b", suffix=r"\b"), Port.PortDirection, "port_declaration"),
|
|
318
|
+
(r"\bparameter\b", Module.Param, "param_declaration"),
|
|
319
|
+
(r"\bbegin\b", Token.Begin, "begin"),
|
|
320
|
+
keywords,
|
|
321
|
+
builtin,
|
|
322
|
+
preproc,
|
|
323
|
+
(
|
|
324
|
+
r"(\w+)\s*(?:#\(.*?\))?\s+(\w+)\s*\((.*?)\)\s*;",
|
|
325
|
+
filter_instance_keywords_callback,
|
|
326
|
+
),
|
|
327
|
+
include("root"),
|
|
328
|
+
],
|
|
329
|
+
"begin": [
|
|
330
|
+
(r"\bend\b", Token.End, "#pop"),
|
|
331
|
+
],
|
|
332
|
+
"module_name": [
|
|
333
|
+
keywords, # The keyword module can be followed by the keywords static|automatic
|
|
334
|
+
include("comments"),
|
|
335
|
+
(r"\$?[a-zA-Z_]\w*", Module.ModuleName, ("#pop", "module_header")),
|
|
336
|
+
default("#pop"),
|
|
337
|
+
],
|
|
338
|
+
"module_header": [
|
|
339
|
+
include("comments"),
|
|
340
|
+
include("ifdef"),
|
|
341
|
+
(r"\bimport\b.*?;", Module.Other), # Package import declaration
|
|
342
|
+
(r"\bparameter\b", Module.Param, "param_declaration"), # Parameter declaration
|
|
343
|
+
(
|
|
344
|
+
words(("input", "output", "inout"), prefix=r"\b", suffix=r"\b"),
|
|
345
|
+
Port.PortDirection,
|
|
346
|
+
"port_declaration",
|
|
347
|
+
), # Port declaration
|
|
348
|
+
(r";", Module.ModuleHeaderEnd, "#pop"),
|
|
349
|
+
(r"\)\s*;", Module.ModuleHeaderEnd, "#pop"),
|
|
350
|
+
(r"\$?[a-zA-Z_]\w*", Name),
|
|
351
|
+
punctuation,
|
|
352
|
+
],
|
|
353
|
+
"port_declaration": [
|
|
354
|
+
include("comments"),
|
|
355
|
+
include("ifdef"),
|
|
356
|
+
(words(("signed", "unsigned"), suffix=r"\b", prefix=r"\b"), Port.Dtype),
|
|
357
|
+
(port_types, Port.Ptype),
|
|
358
|
+
# Filter ports used for param declarations
|
|
359
|
+
(r"((\[[^]]+\])+)", Port.PortWidth), # Match one or more brackets, indicating the port width
|
|
360
|
+
# port declaration ends with a ;, a ); or with the start of another port declaration
|
|
361
|
+
(words(("input", "output", "inout"), suffix=r"\b", prefix=r"\b"), Port.PortDirection),
|
|
362
|
+
(r"\$?[a-zA-Z_]\w*", Port.PortName),
|
|
363
|
+
(r"\)\s*;", Module.HeaderEnd, "#pop:2"),
|
|
364
|
+
(r",", Punctuation),
|
|
365
|
+
(r";", Punctuation, "#pop"),
|
|
366
|
+
default("#pop"),
|
|
367
|
+
],
|
|
368
|
+
"param_declaration": [
|
|
369
|
+
include("comments"),
|
|
370
|
+
include("ifdef"),
|
|
371
|
+
# Filter macros used for param declarations
|
|
372
|
+
(r"`\w+\s*\(.*?\)", Module.Other),
|
|
373
|
+
(port_types, Module.Param.ParamType),
|
|
374
|
+
# Match one or more brackets, indicating the param width
|
|
375
|
+
(r"((\[[^]]+\])+)", Module.Param.ParamWidth),
|
|
376
|
+
# param declaration ends with a ;, a ); or with the start of another port declaration
|
|
377
|
+
(r"\bparameter\b", Module.Param),
|
|
378
|
+
(r"\blocalparam\b", Keyword, "#pop"),
|
|
379
|
+
(r"=", Module.Param.Value.Start, "param_value"), # Filter parameter values
|
|
380
|
+
# (r'=\s*([\d\'hHbBdxXzZ?_][\w\'hHbBdxXzZ]*|"[^"]*")', Punctuation), # Filter parameter values
|
|
381
|
+
(r"\$?[a-zA-Z_]\w*", Module.Param.ParamName),
|
|
382
|
+
(r"\)\s*;", Module.HeaderEnd, "#pop:2"),
|
|
383
|
+
(r",", Punctuation),
|
|
384
|
+
(r";", Punctuation, "#pop"),
|
|
385
|
+
default("#pop"),
|
|
386
|
+
],
|
|
387
|
+
"param_value": [
|
|
388
|
+
include("comments_no_whitespace"),
|
|
389
|
+
(r"[{\[(]", Module.Param.Value, "param_value_delimiter"),
|
|
390
|
+
# detect strings "string"
|
|
391
|
+
(r'"(?:\\.|[^"\\])*"', Module.Param.Value),
|
|
392
|
+
(r"[,);]", Punctuation, "#pop"),
|
|
393
|
+
(r".", Module.Param.Value),
|
|
394
|
+
],
|
|
395
|
+
"param_value_delimiter": [
|
|
396
|
+
include("comments_no_whitespace"),
|
|
397
|
+
(r"[{\[(]", Module.Param.Value, "#push"),
|
|
398
|
+
(r"[}\])]", Module.Param.Value, "#pop"),
|
|
399
|
+
# detect strings "string"
|
|
400
|
+
(r'"(?:\\.|[^"\\])*"', Module.Param.Value),
|
|
401
|
+
# match any character inside delimiters as param value
|
|
402
|
+
(r".", Module.Param.Value),
|
|
403
|
+
],
|
|
404
|
+
"comments": [
|
|
405
|
+
(r"\s+", Whitespace),
|
|
406
|
+
(r"(\\)(\n)", bygroups(String.Escape, Whitespace)), # line continuation
|
|
407
|
+
(r"/(\\\n)?/(\n|(.|\n)*?[^\\]\n)", comments_callback),
|
|
408
|
+
(r"/(\\\n)?[*]((.|\n)*?)[*](\\\n)?/", comments_callback),
|
|
409
|
+
],
|
|
410
|
+
"comments_no_whitespace": [
|
|
411
|
+
(r"(\\)(\n)", bygroups(String.Escape, Whitespace)), # line continuation
|
|
412
|
+
(r"/(\\\n)?/(\n|(.|\n)*?[^\\]\n)", comments_callback),
|
|
413
|
+
(r"/(\\\n)?[*]((.|\n)*?)[*](\\\n)?/", comments_callback),
|
|
414
|
+
],
|
|
415
|
+
"ifdef": [
|
|
416
|
+
(r"(`ifdef)\s+([a-zA-Z_]\w*)", bygroups(Comment.Preproc, IFDEF.IFDEF)),
|
|
417
|
+
(r"(`ifndef)\s+([a-zA-Z_]\w*)", bygroups(Comment.Preproc, IFDEF.IFNDEF)),
|
|
418
|
+
(r"(`else)", IFDEF.ELSE),
|
|
419
|
+
(r"(`elsif)\s+([a-zA-Z_]\w*)", bygroups(Comment.Preproc, IFDEF.ELSIF)),
|
|
420
|
+
(r"(`endif)", IFDEF.ENDIF),
|
|
421
|
+
],
|
|
422
|
+
"instance_connections": [
|
|
423
|
+
include("comments"),
|
|
424
|
+
# take if-defs into account
|
|
425
|
+
include("ifdef"),
|
|
426
|
+
# Filter macros used for port connections
|
|
427
|
+
(r"`\w+\s*\(.*?\)", Module.Other),
|
|
428
|
+
# autoconnect .*,
|
|
429
|
+
(r"\.[*]\s*,", Module.Body.Instance.Con.Autoconnect),
|
|
430
|
+
# .port(connection),
|
|
431
|
+
(
|
|
432
|
+
r"(\.)([a-zA-Z_]\w*)\s*\(\s*(.*?)\s*\)\s*,?",
|
|
433
|
+
bygroups(
|
|
434
|
+
Module.Body.Instance.Con.Start, Module.Body.Instance.Con.Port, Module.Body.Instance.Con.Connection
|
|
435
|
+
),
|
|
436
|
+
),
|
|
437
|
+
# .port,
|
|
438
|
+
(
|
|
439
|
+
r"(\.)([a-zA-Z_]\w*)\s*,?",
|
|
440
|
+
bygroups(Module.Body.Instance.Con.Start, Module.Body.Instance.Con.PortConnection),
|
|
441
|
+
),
|
|
442
|
+
# connection by order: (port_a, port_b, port_c);
|
|
443
|
+
(r"([a-zA-Z_]\w*)\s*,?", Module.Body.Instance.Con.OrderedConnection),
|
|
444
|
+
# capture same name connection, example: .clk, .rst_b,
|
|
445
|
+
(r"\s*\(\s*", Punctuation),
|
|
446
|
+
(r"\)\s*;", Punctuation, "#pop"),
|
|
447
|
+
],
|
|
448
|
+
# "find_connection": [
|
|
449
|
+
# include("comments"),
|
|
450
|
+
# (r"\(\s*/\*.*?\*/\s*\)"),
|
|
451
|
+
# (r"\(\s*/\*.*?\*/\s*\)"),
|
|
452
|
+
# (r"\.[a-zA-Z_]\w*", Module.Instance.Port, "find_connection"),
|
|
453
|
+
# (r"\.[a-zA-Z_]\w*,", bygroups(Module.Instance.Port, Module.Instance.PortConnection)),
|
|
454
|
+
#
|
|
455
|
+
# ],
|
|
456
|
+
# "comments": [
|
|
457
|
+
# (r"\s+", Whitespace),
|
|
458
|
+
# (r"(\\)(\n)", bygroups(String.Escape, Whitespace)), # line continuation
|
|
459
|
+
# (r"/(\\\n)?/(\n|(.|\n)*?[^\\]\n)", Comment.Single),
|
|
460
|
+
# (r"/(\\\n)?[*](.|\n)*?[*](\\\n)?/", Comment.Multiline),
|
|
461
|
+
# (r"[{}#@]", Punctuation),
|
|
462
|
+
# (r'L?"', String, "string"),
|
|
463
|
+
# (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
|
|
464
|
+
# ],
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
def get_tokens_unprocessed(self, text=None, context=None):
|
|
468
|
+
# Override get_tokens_unprocessed to add debug logic
|
|
469
|
+
# In debug mode, print (Token, match, state_stack)
|
|
470
|
+
self.ctx = context or LexerContext(text, 0)
|
|
471
|
+
stack = self.ctx.stack.copy()
|
|
472
|
+
for item in ExtendedRegexLexer.get_tokens_unprocessed(self, text, self.ctx):
|
|
473
|
+
if stack == self.ctx.stack:
|
|
474
|
+
LOGGER.debug(f'({item[1]}, "{item[2]}")')
|
|
475
|
+
else:
|
|
476
|
+
LOGGER.debug(f'({item[1]}, "{item[2]}"),\n state stack: {self.ctx.stack}')
|
|
477
|
+
stack = self.ctx.stack.copy()
|
|
478
|
+
yield item
|