4 * Copyright (c) 1998-2017 Stephen Williams (steve@icarus.com)
6 * This source code is free software; you can redistribute it
7 * and/or modify it in source code form under the terms of the GNU
8 * General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 from parse_tokens
import tokens
30 states
= ( # ('module', 'exclusive'),
31 ('timescale', 'exclusive'),)
33 tokens
+= ['timescale', 'LITERAL', 'IDENTIFIER', 'DEC_NUMBER', 'BASED_NUMBER',
39 t
.lexer
.lineno
+= t
.value
.count('\n')
42 t_ignore_cppcomment
= r
'//.*'
59 * Resolve the ambiguity between the += assignment
60 * operator and +=> polarity edge path operator
62 * +=> should be treated as two separate tokens '+' and
63 * '=>' (K_EG), therefore we only consume the first
64 * character of the matched pattern i.e. either + or -
65 * and push back the rest of the matches text (=>) in
83 t_K_NXOR
= r
"(\~\^|\^\~)"
88 t_K_CONTRIBUTE
= r
"<\+"
97 t_K_LS_EQ
= r
"(<<=|<<<=)"
103 t_K_SCOPE_RES
= r
"::"
105 tokens
+= ['K_PSTAR', 'K_STARP', 'K_DOTSTAR', 'K_LS',
106 'K_RS', 'K_RSS', 'K_POW', 'K_LE', 'K_GE', 'K_EG', 'K_SG',
107 'K_EQ', 'K_NE', 'K_CEQ', 'K_CNE', 'K_WEQ', 'K_WNE',
108 'K_LOR', 'K_LAND', 'K_TAND', 'K_NOR', 'K_NXOR',
109 'K_NAND', 'K_TRIGGER', 'K_PO_POS', 'K_PO_NEG', 'K_CONTRIBUTE',
110 'K_PLUS_EQ', 'K_MINUS_EQ', 'K_MUL_EQ', 'K_DIV_EQ', 'K_MOD_EQ',
111 'K_AND_EQ', 'K_OR_EQ', 'K_XOR_EQ', 'K_LS_EQ', 'K_RS_EQ',
112 'K_RSS_EQ', 'K_INCR', 'K_DECR', 'K_LP',
116 lexor_keyword_code
= {
119 "absdelay" : 'K_absdelay',
120 "abstol" : 'K_abstol',
121 "accept_on" : 'K_accept_on',
122 "access" : 'K_access',
125 "ac_stim" : 'K_ac_stim',
127 "aliasparam" : 'K_aliasparam',
128 "always" : 'K_always',
129 "always_comb" : 'K_always_comb',
130 "always_ff" : 'K_always_ff',
131 "always_latch" : 'K_always_latch',
132 "analog" : 'K_analog',
133 "analysis" : 'K_analysis',
137 "assert" : 'K_assert',
138 "assign" : 'K_assign',
139 "assume" : 'K_assume',
143 "automatic" : 'K_automatic',
144 "before" : 'K_before',
148 "binsof" : 'K_binsof',
150 "branch" : 'K_branch',
154 "bufif0" : 'K_bufif0',
155 "bufif1" : 'K_bufif1',
162 "chandle" : 'K_chandle',
163 "checker" : 'K_checker',
165 "clocking" : 'K_clocking',
167 "config" : 'K_config',
168 "connect" : 'K_connect',
169 "connectmodule" : 'K_connectmodule',
170 "connectrules" : 'K_connectrules',
172 "constraint" : 'K_constraint',
173 "context" : 'K_context',
174 "continue" : 'K_continue',
175 "continuous" : 'K_continuous',
179 "covergroup" : 'K_covergroup',
180 "coverpoint" : 'K_coverpoint',
183 "ddt_nature" : 'K_ddt_nature',
185 "deassign" : 'K_deassign',
186 "default" : 'K_default',
187 "defparam" : 'K_defparam',
188 "design" : 'K_design',
189 "disable" : 'K_disable',
190 "discipline" : 'K_discipline',
191 "discrete" : 'K_discrete',
194 "domain" : 'K_domain',
195 "driver_update" : 'K_driver_update',
199 "endcase" : 'K_endcase',
200 "endchecker" : 'K_endchecker',
201 "endconfig" : 'K_endconfig',
202 "endclass" : 'K_endclass',
203 "endclocking" : 'K_endclocking',
204 "endconnectrules" : 'K_endconnectrules',
205 "enddiscipline" : 'K_enddiscipline',
206 "endfunction" : 'K_endfunction',
207 "endgenerate" : 'K_endgenerate',
208 "endgroup" : 'K_endgroup',
209 "endinterface" : 'K_endinterface',
210 "endmodule" : 'K_endmodule',
211 "endnature" : 'K_endnature',
212 "endpackage" : 'K_endpackage',
213 "endparamset" : 'K_endparamset',
214 "endprimitive" : 'K_endprimitive',
215 "endprogram" : 'K_endprogram',
216 "endproperty" : 'K_endproperty',
217 "endspecify" : 'K_endspecify',
218 "endsequence" : 'K_endsequence',
219 "endtable" : 'K_endtable',
220 "endtask" : 'K_endtask',
223 "eventually" : 'K_eventually',
224 "exclude" : 'K_exclude',
226 "expect" : 'K_expect',
227 "export" : 'K_export',
228 "extends" : 'K_extends',
229 "extern" : 'K_extern',
231 "final_step" : 'K_final_step',
232 "first_match" : 'K_first_match',
233 "flicker_noise" : 'K_flicker_noise',
237 "foreach" : 'K_foreach',
239 "forever" : 'K_forever',
241 "forkjoin" : 'K_forkjoin',
243 "function" : 'K_function',
244 "generate" : 'K_generate',
245 "genvar" : 'K_genvar',
246 "global" : 'K_global',
247 "ground" : 'K_ground',
248 "highz0" : 'K_highz0',
249 "highz1" : 'K_highz1',
252 "idtmod" : 'K_idtmod',
253 "idt_nature" : 'K_idt_nature',
256 "ifnone" : 'K_ifnone',
257 "ignore_bins" : 'K_ignore_bins',
258 "illegal_bins" : 'K_illegal_bins',
259 "implies" : 'K_implies',
260 "implements" : 'K_implements',
261 "import" : 'K_import',
262 "incdir" : 'K_incdir',
263 "include" : 'K_include',
265 "initial" : 'K_initial',
266 "initial_step" : 'K_initial_step',
269 "inside" : 'K_inside',
270 "instance" : 'K_instance',
272 "integer" : 'K_integer',
273 "interconnect" : 'K_interconnect',
274 "interface" : 'K_interface',
275 "intersect" : 'K_intersect',
277 "join_any" : 'K_join_any',
278 "join_none" : 'K_join_none',
279 "laplace_nd" : 'K_laplace_nd',
280 "laplace_np" : 'K_laplace_np',
281 "laplace_zd" : 'K_laplace_zd',
282 "laplace_zp" : 'K_laplace_zp',
284 "last_crossing" : 'K_last_crossing',
286 "liblist" : 'K_liblist',
287 "library" : 'K_library',
288 "limexp" : 'K_limexp',
291 "localparam" : 'K_localparam',
293 # This is defined by SystemVerilog 1800-2005 and as an Icarus extension.'
295 "longint" : 'K_longint',
296 "macromodule" : 'K_macromodule',
297 "matches" : 'K_matches',
299 "medium" : 'K_medium',
300 "merged" : 'K_merged',
302 "modport" : 'K_modport',
303 "module" : 'K_module',
305 "nature" : 'K_nature',
306 "negedge" : 'K_negedge',
307 "net_resolution" : 'K_net_resolution',
308 "nettype" : 'K_nettype',
310 "nexttime" : 'K_nexttime',
312 "noise_table" : 'K_noise_table',
314 "noshowcancelled" : 'K_noshowcancelled',
316 "notif0" : 'K_notif0',
317 "notif1" : 'K_notif1',
320 "output" : 'K_output',
321 "package" : 'K_package',
322 "packed" : 'K_packed',
323 "parameter" : 'K_parameter',
324 "paramset" : 'K_paramset',
326 "posedge" : 'K_posedge',
327 "potential" : 'K_potential',
329 "primitive" : 'K_primitive',
330 "priority" : 'K_priority',
331 "program" : 'K_program',
332 "property" : 'K_property',
333 "protected" : 'K_protected',
336 "pulldown" : 'K_pulldown',
337 "pullup" : 'K_pullup',
338 "pulsestyle_onevent" : 'K_pulsestyle_onevent',
339 "pulsestyle_ondetect" : 'K_pulsestyle_ondetect',
343 "randcase" : 'K_randcase',
344 "randsequence" : 'K_randsequence',
347 "realtime" : 'K_realtime',
350 "reject_on" : 'K_reject_on',
351 "release" : 'K_release',
352 "repeat" : 'K_repeat',
353 "resolveto" : 'K_resolveto',
354 "restrict" : 'K_restrict',
355 "return" : 'K_return',
359 "rtranif0" : 'K_rtranif0',
360 "rtranif1" : 'K_rtranif1',
361 "s_always" : 'K_s_always',
362 "s_eventually" : 'K_s_eventually',
363 "s_nexttime" : 'K_s_nexttime',
364 "s_until" : 'K_s_until',
365 "s_until_with" : 'K_s_until_with',
366 "scalared" : 'K_scalared',
367 "sequence" : 'K_sequence',
368 "shortint" : 'K_shortint',
369 "shortreal" : 'K_shortreal',
370 "showcancelled" : 'K_showcancelled',
371 "signed" : 'K_signed',
378 "specify" : 'K_specify',
379 "specparam" : 'K_specparam',
382 "static" : 'K_static',
383 # This is defined by both SystemVerilog 1800-2005 and Verilog-AMS 2.3',
384 "string" : 'K_string',
385 "strong" : 'K_strong',
386 "strong0" : 'K_strong0',
387 "strong1" : 'K_strong1',
388 "struct" : 'K_struct',
390 "supply0" : 'K_supply0',
391 "supply1" : 'K_supply1',
392 "sync_accept_on" : 'K_sync_accept_on',
393 "sync_reject_on" : 'K_sync_reject_on',
395 "tagged" : 'K_tagged',
400 "throughout" : 'K_throughout',
402 "timeprecision" : 'K_timeprecision',
404 "timeunit" : 'K_timeunit',
406 "tranif0" : 'K_tranif0',
407 "tranif1" : 'K_tranif1',
408 "transition" : 'K_transition',
412 "triand" : 'K_triand',
414 "trireg" : 'K_trireg',
416 "typedef" : 'K_typedef',
418 "unique" : 'K_unique',
419 "unique0" : 'K_unique',
421 # Reserved for future use!',
422 "unsigned" : 'K_unsigned',
424 "until_with" : 'K_until_with',
425 "untyped" : 'K_untyped',
429 "vectored" : 'K_vectored',
430 "virtual" : 'K_virtual',
433 "wait_order" : 'K_wait_order',
439 "white_noise" : 'K_white_noise',
440 "wildcard" : 'K_wildcard',
443 "within" : 'K_within',
444 # This is the name originally proposed for uwire and is deprecated!',
447 # This is defined by Verilog-AMS 2.3 and as an Icarus extension.',
457 literals
= ['[', '}', '{', ';', ':', '[', ']', ',', '(', ')',
458 '#', '=', '.', '@', '&', '!', '?', '<', '>', '%',
459 '|', '^', '~', '+', '*', '/', '-']
464 code = t.lexer.lexdata[t.modulestart:t.lexpos]
467 t.lexer.lineno += t.value.count('\n')
470 t_module_ignore = ' \t'
475 r
'[a-zA-Z_$][a-zA-Z0-9$_]*'
477 keyword
= lexor_keyword_code
.get(t
.value
, 'IDENTIFIER')
479 print("literal", word
, keyword
)
480 # if keyword in ['K_module', 'K_macromodule']:
481 # t.lexer.modulestart = t.lexpos+len(t.value)
482 # t.lexer.begin('module')
483 if keyword
== 'IDENTIFIER':
484 t
.type = 'IDENTIFIER'
492 r
'\'[sS
]?
[dD
][ \t]*[0-9][0-9_]*'
493 t.type = 'BASED_NUMBER
'
494 # t.value = word # make_unsized_dec(yytext);
498 def t_undef_highz_dec(t):
499 r'\'[sS
]?
[dD
][ \t]*[xzXZ?
]_
*'
500 t.type = 'BASED_NUMBER
'
501 # t.value = word # make_undef_highz_dec(yytext);
505 def t_based_make_unsized_binary(t):
506 r'\'[sS
]?
[bB
][ \t]*[0-1xzXZ?
][0-1xzXZ?_
]*'
507 t.type = 'BASED_NUMBER
'
508 # t.value = word # make_unsized_binary(yytext);
512 def t_make_unsized_octal(t):
513 r'\'[sS
]?
[oO
][ \t]*[0-7xzXZ?
][0-7xzXZ?_
]*'
514 t.type = 'BASED_NUMBER
'
515 # t.value = word # make_unsized_octal(yytext);
519 def t_make_unsized_hex(t):
520 r'\'[sS
]?
[hH
][ \t]*[0-9a
-fA
-FxzXZ?
][0-9a
-fA
-FxzXZ?_
]*'
521 t.type = 'BASED_NUMBER
'
522 # t.value = word # make_unsized_hex(yytext);
526 def t_unbased_make_unsized_binary(t):
528 t.type = 'UNBASED_NUMBER
'
529 # t.value = word # make_unsized_binary(yytext);
534 /* Decimal numbers are the usual. But watch out for the UDPTABLE
535 mode, where there are no decimal numbers. Reject the match if we
536 are in the UDPTABLE state. */
540 def t_make_unsized_dec(t):
542 t.type = 'DEC_NUMBER
'
543 # t.value = word # make_unsized_dec(yytext);
544 # based_size = yylval.number->as_ulong();
549 /* Notice and handle the `timescale directive. */
556 t.lexer.timestart = t.lexpos+len(t.value)
557 t.lexer.push_state('timescale
')
560 #t_timescale_ignore_toeol = r'.+\n'
561 t_timescale_ignore = ' \t'
562 #t_timescale_ignore_whitespace = r'\s
+'
566 def t_timescale_end(t):
568 code = t.lexer.lexdata[t.lexer.timestart:t.lexpos]
576 def t_timescale_error(t):
577 print("%d: Timescale error '%s'" % (t.lexer.lineno, t.value[0]))
583 def t_module_error(t):
584 print("%d: Module error '%s'" % (t.lexer.lineno, t.value[0]))
591 print("%d: Illegal character '%s'" % (t.lexer.lineno, t.value[0]))
596 tokens = list(set(tokens))
600 if __name__ == '__main__
':