dbf7c880dc2fe93c23a0ce3a4aaa967e83eeb484
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.
29 states
= (#('module', 'exclusive'),
30 ('timescale', 'exclusive'),)
32 from parse_tokens
import tokens
33 tokens
+= ['timescale', 'LITERAL', 'IDENTIFIER', 'DEC_NUMBER', 'BASED_NUMBER',
38 t
.lexer
.lineno
+= t
.value
.count('\n')
40 t_ignore_cppcomment
= r
'//.*'
57 * Resolve the ambiguity between the += assignment
58 * operator and +=> polarity edge path operator
60 * +=> should be treated as two separate tokens '+' and
61 * '=>' (K_EG), therefore we only consume the first
62 * character of the matched pattern i.e. either + or -
63 * and push back the rest of the matches text (=>) in
81 t_K_NXOR
= r
"(\~\^|\^\~)"
86 t_K_CONTRIBUTE
= r
"<\+"
95 t_K_LS_EQ
= r
"(<<=|<<<=)"
101 t_K_SCOPE_RES
= r
"::"
103 tokens
+= [ 'K_PSTAR', 'K_STARP', 'K_DOTSTAR', 'K_LS',
104 'K_RS', 'K_RSS', 'K_POW', 'K_LE', 'K_GE', 'K_EG', 'K_SG',
105 'K_EQ', 'K_NE', 'K_CEQ', 'K_CNE', 'K_WEQ', 'K_WNE',
106 'K_LOR', 'K_LAND', 'K_TAND', 'K_NOR', 'K_NXOR',
107 'K_NAND', 'K_TRIGGER', 'K_PO_POS', 'K_PO_NEG', 'K_CONTRIBUTE',
108 'K_PLUS_EQ', 'K_MINUS_EQ', 'K_MUL_EQ', 'K_DIV_EQ', 'K_MOD_EQ',
109 'K_AND_EQ', 'K_OR_EQ', 'K_XOR_EQ', 'K_LS_EQ', 'K_RS_EQ',
110 'K_RSS_EQ', 'K_INCR', 'K_DECR', 'K_LP',
114 lexor_keyword_code
= {
117 "absdelay" : 'K_absdelay',
118 "abstol" : 'K_abstol',
119 "accept_on" : 'K_accept_on',
120 "access" : 'K_access',
123 "ac_stim" : 'K_ac_stim',
125 "aliasparam" : 'K_aliasparam',
126 "always" : 'K_always',
127 "always_comb" : 'K_always_comb',
128 "always_ff" : 'K_always_ff',
129 "always_latch" : 'K_always_latch',
130 "analog" : 'K_analog',
131 "analysis" : 'K_analysis',
135 "assert" : 'K_assert',
136 "assign" : 'K_assign',
137 "assume" : 'K_assume',
141 "automatic" : 'K_automatic',
142 "before" : 'K_before',
146 "binsof" : 'K_binsof',
148 "branch" : 'K_branch',
152 "bufif0" : 'K_bufif0',
153 "bufif1" : 'K_bufif1',
160 "chandle" : 'K_chandle',
161 "checker" : 'K_checker',
163 "clocking" : 'K_clocking',
165 "config" : 'K_config',
166 "connect" : 'K_connect',
167 "connectmodule" : 'K_connectmodule',
168 "connectrules" : 'K_connectrules',
170 "constraint" : 'K_constraint',
171 "context" : 'K_context',
172 "continue" : 'K_continue',
173 "continuous" : 'K_continuous',
177 "covergroup" : 'K_covergroup',
178 "coverpoint" : 'K_coverpoint',
181 "ddt_nature" : 'K_ddt_nature',
183 "deassign" : 'K_deassign',
184 "default" : 'K_default',
185 "defparam" : 'K_defparam',
186 "design" : 'K_design',
187 "disable" : 'K_disable',
188 "discipline" : 'K_discipline',
189 "discrete" : 'K_discrete',
192 "domain" : 'K_domain',
193 "driver_update" : 'K_driver_update',
197 "endcase" : 'K_endcase',
198 "endchecker" : 'K_endchecker',
199 "endconfig" : 'K_endconfig',
200 "endclass" : 'K_endclass',
201 "endclocking" : 'K_endclocking',
202 "endconnectrules" : 'K_endconnectrules',
203 "enddiscipline" : 'K_enddiscipline',
204 "endfunction" : 'K_endfunction',
205 "endgenerate" : 'K_endgenerate',
206 "endgroup" : 'K_endgroup',
207 "endinterface" : 'K_endinterface',
208 "endmodule" : 'K_endmodule',
209 "endnature" : 'K_endnature',
210 "endpackage" : 'K_endpackage',
211 "endparamset" : 'K_endparamset',
212 "endprimitive" : 'K_endprimitive',
213 "endprogram" : 'K_endprogram',
214 "endproperty" : 'K_endproperty',
215 "endspecify" : 'K_endspecify',
216 "endsequence" : 'K_endsequence',
217 "endtable" : 'K_endtable',
218 "endtask" : 'K_endtask',
221 "eventually" : 'K_eventually',
222 "exclude" : 'K_exclude',
224 "expect" : 'K_expect',
225 "export" : 'K_export',
226 "extends" : 'K_extends',
227 "extern" : 'K_extern',
229 "final_step" : 'K_final_step',
230 "first_match" : 'K_first_match',
231 "flicker_noise" : 'K_flicker_noise',
235 "foreach" : 'K_foreach',
237 "forever" : 'K_forever',
239 "forkjoin" : 'K_forkjoin',
241 "function" : 'K_function',
242 "generate" : 'K_generate',
243 "genvar" : 'K_genvar',
244 "global" : 'K_global',
245 "ground" : 'K_ground',
246 "highz0" : 'K_highz0',
247 "highz1" : 'K_highz1',
250 "idtmod" : 'K_idtmod',
251 "idt_nature" : 'K_idt_nature',
254 "ifnone" : 'K_ifnone',
255 "ignore_bins" : 'K_ignore_bins',
256 "illegal_bins" : 'K_illegal_bins',
257 "implies" : 'K_implies',
258 "implements" : 'K_implements',
259 "import" : 'K_import',
260 "incdir" : 'K_incdir',
261 "include" : 'K_include',
263 "initial" : 'K_initial',
264 "initial_step" : 'K_initial_step',
267 "inside" : 'K_inside',
268 "instance" : 'K_instance',
270 "integer" : 'K_integer',
271 "interconnect" : 'K_interconnect',
272 "interface" : 'K_interface',
273 "intersect" : 'K_intersect',
275 "join_any" : 'K_join_any',
276 "join_none" : 'K_join_none',
277 "laplace_nd" : 'K_laplace_nd',
278 "laplace_np" : 'K_laplace_np',
279 "laplace_zd" : 'K_laplace_zd',
280 "laplace_zp" : 'K_laplace_zp',
282 "last_crossing" : 'K_last_crossing',
284 "liblist" : 'K_liblist',
285 "library" : 'K_library',
286 "limexp" : 'K_limexp',
289 "localparam" : 'K_localparam',
291 # This is defined by SystemVerilog 1800-2005 and as an Icarus extension.'
293 "longint" : 'K_longint',
294 "macromodule" : 'K_macromodule',
295 "matches" : 'K_matches',
297 "medium" : 'K_medium',
298 "merged" : 'K_merged',
300 "modport" : 'K_modport',
301 "module" : 'K_module',
303 "nature" : 'K_nature',
304 "negedge" : 'K_negedge',
305 "net_resolution" : 'K_net_resolution',
306 "nettype" : 'K_nettype',
308 "nexttime" : 'K_nexttime',
310 "noise_table" : 'K_noise_table',
312 "noshowcancelled" : 'K_noshowcancelled',
314 "notif0" : 'K_notif0',
315 "notif1" : 'K_notif1',
318 "output" : 'K_output',
319 "package" : 'K_package',
320 "packed" : 'K_packed',
321 "parameter" : 'K_parameter',
322 "paramset" : 'K_paramset',
324 "posedge" : 'K_posedge',
325 "potential" : 'K_potential',
327 "primitive" : 'K_primitive',
328 "priority" : 'K_priority',
329 "program" : 'K_program',
330 "property" : 'K_property',
331 "protected" : 'K_protected',
334 "pulldown" : 'K_pulldown',
335 "pullup" : 'K_pullup',
336 "pulsestyle_onevent" : 'K_pulsestyle_onevent',
337 "pulsestyle_ondetect" : 'K_pulsestyle_ondetect',
341 "randcase" : 'K_randcase',
342 "randsequence" : 'K_randsequence',
345 "realtime" : 'K_realtime',
348 "reject_on" : 'K_reject_on',
349 "release" : 'K_release',
350 "repeat" : 'K_repeat',
351 "resolveto" : 'K_resolveto',
352 "restrict" : 'K_restrict',
353 "return" : 'K_return',
357 "rtranif0" : 'K_rtranif0',
358 "rtranif1" : 'K_rtranif1',
359 "s_always" : 'K_s_always',
360 "s_eventually" : 'K_s_eventually',
361 "s_nexttime" : 'K_s_nexttime',
362 "s_until" : 'K_s_until',
363 "s_until_with" : 'K_s_until_with',
364 "scalared" : 'K_scalared',
365 "sequence" : 'K_sequence',
366 "shortint" : 'K_shortint',
367 "shortreal" : 'K_shortreal',
368 "showcancelled" : 'K_showcancelled',
369 "signed" : 'K_signed',
376 "specify" : 'K_specify',
377 "specparam" : 'K_specparam',
380 "static" : 'K_static',
381 # This is defined by both SystemVerilog 1800-2005 and Verilog-AMS 2.3',
382 "string" : 'K_string',
383 "strong" : 'K_strong',
384 "strong0" : 'K_strong0',
385 "strong1" : 'K_strong1',
386 "struct" : 'K_struct',
388 "supply0" : 'K_supply0',
389 "supply1" : 'K_supply1',
390 "sync_accept_on" : 'K_sync_accept_on',
391 "sync_reject_on" : 'K_sync_reject_on',
393 "tagged" : 'K_tagged',
398 "throughout" : 'K_throughout',
400 "timeprecision" : 'K_timeprecision',
402 "timeunit" : 'K_timeunit',
404 "tranif0" : 'K_tranif0',
405 "tranif1" : 'K_tranif1',
406 "transition" : 'K_transition',
410 "triand" : 'K_triand',
412 "trireg" : 'K_trireg',
414 "typedef" : 'K_typedef',
416 "unique" : 'K_unique',
417 "unique0" : 'K_unique',
419 # Reserved for future use!',
420 "unsigned" : 'K_unsigned',
422 "until_with" : 'K_until_with',
423 "untyped" : 'K_untyped',
427 "vectored" : 'K_vectored',
428 "virtual" : 'K_virtual',
431 "wait_order" : 'K_wait_order',
437 "white_noise" : 'K_white_noise',
438 "wildcard" : 'K_wildcard',
441 "within" : 'K_within',
442 # This is the name originally proposed for uwire and is deprecated!',
445 # This is defined by Verilog-AMS 2.3 and as an Icarus extension.',
455 literals
= [ '[', '}', '{', ';', ':', '[', ']', ',', '(', ')',
456 '#', '=', '.', '@', '&', '!', '?', '<', '>', '%',
457 '|', '^', '~', '+', '*', '/', '-']
462 code = t.lexer.lexdata[t.modulestart:t.lexpos]
465 t.lexer.lineno += t.value.count('\n')
468 t_module_ignore = ' \t'
472 r
'[a-zA-Z_$][a-zA-Z0-9$_]*'
474 keyword
= lexor_keyword_code
.get(t
.value
, 'IDENTIFIER')
475 if(lex_debug
): print ("literal", word
,keyword
)
476 #if keyword in ['K_module', 'K_macromodule']:
477 # t.lexer.modulestart = t.lexpos+len(t.value)
478 # t.lexer.begin('module')
479 if keyword
== 'IDENTIFIER':
480 t
.type = 'IDENTIFIER'
487 r
'\'[sS
]?
[dD
][ \t]*[0-9][0-9_]*'
488 t.type = 'BASED_NUMBER
'
489 #t.value = word # make_unsized_dec(yytext);
492 def t_undef_highz_dec(t):
493 r'\'[sS
]?
[dD
][ \t]*[xzXZ?
]_
*'
494 t.type = 'BASED_NUMBER
'
495 #t.value = word # make_undef_highz_dec(yytext);
498 def t_based_make_unsized_binary(t):
499 r'\'[sS
]?
[bB
][ \t]*[0-1xzXZ?
][0-1xzXZ?_
]*'
500 t.type = 'BASED_NUMBER
'
501 #t.value = word # make_unsized_binary(yytext);
504 def t_make_unsized_octal(t):
505 r'\'[sS
]?
[oO
][ \t]*[0-7xzXZ?
][0-7xzXZ?_
]*'
506 t.type = 'BASED_NUMBER
'
507 #t.value = word # make_unsized_octal(yytext);
510 def t_make_unsized_hex(t):
511 r'\'[sS
]?
[hH
][ \t]*[0-9a
-fA
-FxzXZ?
][0-9a
-fA
-FxzXZ?_
]*'
512 t.type = 'BASED_NUMBER
'
513 #t.value = word # make_unsized_hex(yytext);
516 def t_unbased_make_unsized_binary(t):
518 t.type = 'UNBASED_NUMBER
'
519 #t.value = word # make_unsized_binary(yytext);
523 /* Decimal numbers are the usual. But watch out for the UDPTABLE
524 mode, where there are no decimal numbers. Reject the match if we
525 are in the UDPTABLE state. */
527 def t_make_unsized_dec(t):
529 t.type = 'DEC_NUMBER
'
530 #t.value = word # make_unsized_dec(yytext);
531 #based_size = yylval.number->as_ulong();
535 /* Notice and handle the `timescale directive. */
541 t.lexer.timestart = t.lexpos+len(t.value)
542 t.lexer.push_state('timescale
')
544 #t_timescale_ignore_toeol = r'.+\n'
545 t_timescale_ignore = ' \t'
546 #t_timescale_ignore_whitespace = r'\s
+'
549 def t_timescale_end(t):
551 code = t.lexer.lexdata[t.lexer.timestart:t.lexpos]
555 print ("match", code)
558 def t_timescale_error(t):
559 print("%d: Timescale error '%s'" % (t.lexer.lineno, t.value[0]))
564 def t_module_error(t):
565 print("%d: Module error '%s'" % (t.lexer.lineno, t.value[0]))
571 print("%d: Illegal character '%s'" % (t.lexer.lineno, t.value[0]))
575 tokens = list(set(tokens))
579 if __name__ == '__main__
':