wire assignments
[sv2nmigen.git] / lexor.py
1 """
2 %{
3 /*
4 * Copyright (c) 1998-2017 Stephen Williams (steve@icarus.com)
5 *
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)
10 * any later version.
11 *
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.
16 *
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.
20 */
21 """
22
23 from parse_tokens import tokens
24 from ply import lex
25 lex_debug = 0
26
27
28 # DOCSTRING REMOVED
29
30 states = ( # ('module', 'exclusive'),
31 ('timescale', 'exclusive'),)
32
33 tokens += ['timescale', 'LITERAL', 'IDENTIFIER', 'DEC_NUMBER', 'BASED_NUMBER',
34 'UNBASED_NUMBER']
35
36
37 def t_ccomment(t):
38 r'/\*(.|\n)*?\*/'
39 t.lexer.lineno += t.value.count('\n')
40
41
42 t_ignore_cppcomment = r'//.*'
43
44 t_ignore = ' \t\n'
45
46 t_K_PSTAR = r"\(\*"
47 t_K_STARP = r"\*\)"
48 t_K_DOTSTAR = r"\.\*"
49 t_K_LS = r"(<<|<<<)"
50 t_K_RS = r">>"
51 t_K_RSS = r">>>"
52 t_K_POW = r"\*\*"
53 t_K_LE = r"<="
54 t_K_GE = r">="
55 t_K_EG = r"=>"
56 """
57 "+=>"|"-=>" {
58 /*
59 * Resolve the ambiguity between the += assignment
60 * operator and +=> polarity edge path operator
61 *
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
66 * the input stream.
67 */
68 yyless(1);
69 return yytext[0];
70 }
71 """
72 t_K_SG = r"\*>"
73 t_K_EQ = r"=="
74 t_K_NE = r"!="
75 t_K_CEQ = r"==="
76 t_K_CNE = r"!=="
77 t_K_WEQ = r"==\?"
78 t_K_WNE = r"!=\?"
79 t_K_LOR = r"\|\|"
80 t_K_LAND = r"\&\&"
81 t_K_TAND = r"\&\&\&"
82 t_K_NOR = r"\~\|"
83 t_K_NXOR = r"(\~\^|\^\~)"
84 t_K_NAND = r"\~\&"
85 t_K_TRIGGER = r"\->"
86 t_K_PO_POS = r"\+:"
87 t_K_PO_NEG = r"\-:"
88 t_K_CONTRIBUTE = r"<\+"
89 t_K_PLUS_EQ = r"\+="
90 t_K_MINUS_EQ = r"\-="
91 t_K_MUL_EQ = r"\*="
92 t_K_DIV_EQ = r"\/="
93 t_K_MOD_EQ = r"\%="
94 t_K_AND_EQ = r"\&="
95 t_K_OR_EQ = r"\|="
96 t_K_XOR_EQ = r"\^="
97 t_K_LS_EQ = r"(<<=|<<<=)"
98 t_K_RS_EQ = r">>="
99 t_K_RSS_EQ = r">>>="
100 t_K_INCR = r"\+\+"
101 t_K_DECR = r"\\--"
102 t_K_LP = r"\'\{"
103 t_K_SCOPE_RES = r"::"
104
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',
113 'K_SCOPE_RES'
114 ]
115
116 lexor_keyword_code = {
117 "above" : 'K_above',
118 "abs" : 'K_abs',
119 "absdelay" : 'K_absdelay',
120 "abstol" : 'K_abstol',
121 "accept_on" : 'K_accept_on',
122 "access" : 'K_access',
123 "acos" : 'K_acos',
124 "acosh" : 'K_acosh',
125 "ac_stim" : 'K_ac_stim',
126 "alias" : 'K_alias',
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',
134 "and" : 'K_and',
135 "asin" : 'K_asin',
136 "asinh" : 'K_asinh',
137 "assert" : 'K_assert',
138 "assign" : 'K_assign',
139 "assume" : 'K_assume',
140 "atan" : 'K_atan',
141 "atan2" : 'K_atan2',
142 "atanh" : 'K_atanh',
143 "automatic" : 'K_automatic',
144 "before" : 'K_before',
145 "begin" : 'K_begin',
146 "bind" : 'K_bind',
147 "bins" : 'K_bins',
148 "binsof" : 'K_binsof',
149 "bit" : 'K_bit',
150 "branch" : 'K_branch',
151 "break" : 'K_break',
152 "bool" : 'K_bool',
153 "buf" : 'K_buf',
154 "bufif0" : 'K_bufif0',
155 "bufif1" : 'K_bufif1',
156 "byte" : 'K_byte',
157 "case" : 'K_case',
158 "casex" : 'K_casex',
159 "casez" : 'K_casez',
160 "ceil" : 'K_ceil',
161 "cell" : 'K_cell',
162 "chandle" : 'K_chandle',
163 "checker" : 'K_checker',
164 "class" : 'K_class',
165 "clocking" : 'K_clocking',
166 "cmos" : 'K_cmos',
167 "config" : 'K_config',
168 "connect" : 'K_connect',
169 "connectmodule" : 'K_connectmodule',
170 "connectrules" : 'K_connectrules',
171 "const" : 'K_const',
172 "constraint" : 'K_constraint',
173 "context" : 'K_context',
174 "continue" : 'K_continue',
175 "continuous" : 'K_continuous',
176 "cos" : 'K_cos',
177 "cosh" : 'K_cosh',
178 "cover" : 'K_cover',
179 "covergroup" : 'K_covergroup',
180 "coverpoint" : 'K_coverpoint',
181 "cross" : 'K_cross',
182 "ddt" : 'K_ddt',
183 "ddt_nature" : 'K_ddt_nature',
184 "ddx" : 'K_ddx',
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',
192 "dist" : 'K_dist',
193 "do" : 'K_do',
194 "domain" : 'K_domain',
195 "driver_update" : 'K_driver_update',
196 "edge" : 'K_edge',
197 "else" : 'K_else',
198 "end" : 'K_end',
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',
221 "enum" : 'K_enum',
222 "event" : 'K_event',
223 "eventually" : 'K_eventually',
224 "exclude" : 'K_exclude',
225 "exp" : 'K_exp',
226 "expect" : 'K_expect',
227 "export" : 'K_export',
228 "extends" : 'K_extends',
229 "extern" : 'K_extern',
230 "final" : 'K_final',
231 "final_step" : 'K_final_step',
232 "first_match" : 'K_first_match',
233 "flicker_noise" : 'K_flicker_noise',
234 "floor" : 'K_floor',
235 "flow" : 'K_flow',
236 "for" : 'K_for',
237 "foreach" : 'K_foreach',
238 "force" : 'K_force',
239 "forever" : 'K_forever',
240 "fork" : 'K_fork',
241 "forkjoin" : 'K_forkjoin',
242 "from" : 'K_from',
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',
250 "hypot" : 'K_hypot',
251 "idt" : 'K_idt',
252 "idtmod" : 'K_idtmod',
253 "idt_nature" : 'K_idt_nature',
254 "if" : 'K_if',
255 "iff" : 'K_iff',
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',
264 "inf" : 'K_inf',
265 "initial" : 'K_initial',
266 "initial_step" : 'K_initial_step',
267 "inout" : 'K_inout',
268 "input" : 'K_input',
269 "inside" : 'K_inside',
270 "instance" : 'K_instance',
271 "int" : 'K_int',
272 "integer" : 'K_integer',
273 "interconnect" : 'K_interconnect',
274 "interface" : 'K_interface',
275 "intersect" : 'K_intersect',
276 "join" : 'K_join',
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',
283 "large" : 'K_large',
284 "last_crossing" : 'K_last_crossing',
285 "let" : 'K_let',
286 "liblist" : 'K_liblist',
287 "library" : 'K_library',
288 "limexp" : 'K_limexp',
289 "ln" : 'K_ln',
290 "local" : 'K_local',
291 "localparam" : 'K_localparam',
292 "log" : 'K_log',
293 # This is defined by SystemVerilog 1800-2005 and as an Icarus extension.'
294 "logic" : 'K_logic',
295 "longint" : 'K_longint',
296 "macromodule" : 'K_macromodule',
297 "matches" : 'K_matches',
298 "max" : 'K_max',
299 "medium" : 'K_medium',
300 "merged" : 'K_merged',
301 "min" : 'K_min',
302 "modport" : 'K_modport',
303 "module" : 'K_module',
304 "nand" : 'K_nand',
305 "nature" : 'K_nature',
306 "negedge" : 'K_negedge',
307 "net_resolution" : 'K_net_resolution',
308 "nettype" : 'K_nettype',
309 "new" : 'K_new',
310 "nexttime" : 'K_nexttime',
311 "nmos" : 'K_nmos',
312 "noise_table" : 'K_noise_table',
313 "nor" : 'K_nor',
314 "noshowcancelled" : 'K_noshowcancelled',
315 "not" : 'K_not',
316 "notif0" : 'K_notif0',
317 "notif1" : 'K_notif1',
318 "null" : 'K_null',
319 "or" : 'K_or',
320 "output" : 'K_output',
321 "package" : 'K_package',
322 "packed" : 'K_packed',
323 "parameter" : 'K_parameter',
324 "paramset" : 'K_paramset',
325 "pmos" : 'K_pmos',
326 "posedge" : 'K_posedge',
327 "potential" : 'K_potential',
328 "pow" : 'K_pow',
329 "primitive" : 'K_primitive',
330 "priority" : 'K_priority',
331 "program" : 'K_program',
332 "property" : 'K_property',
333 "protected" : 'K_protected',
334 "pull0" : 'K_pull0',
335 "pull1" : 'K_pull1',
336 "pulldown" : 'K_pulldown',
337 "pullup" : 'K_pullup',
338 "pulsestyle_onevent" : 'K_pulsestyle_onevent',
339 "pulsestyle_ondetect" : 'K_pulsestyle_ondetect',
340 "pure" : 'K_pure',
341 "rand" : 'K_rand',
342 "randc" : 'K_randc',
343 "randcase" : 'K_randcase',
344 "randsequence" : 'K_randsequence',
345 "rcmos" : 'K_rcmos',
346 "real" : 'K_real',
347 "realtime" : 'K_realtime',
348 "ref" : 'K_ref',
349 "reg" : 'K_reg',
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',
356 "rnmos" : 'K_rnmos',
357 "rpmos" : 'K_rpmos',
358 "rtran" : 'K_rtran',
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',
372 "sin" : 'K_sin',
373 "sinh" : 'K_sinh',
374 "slew" : 'K_slew',
375 "small" : 'K_small',
376 "soft" : 'K_soft',
377 "solve" : 'K_solve',
378 "specify" : 'K_specify',
379 "specparam" : 'K_specparam',
380 "split" : 'K_split',
381 "sqrt" : 'K_sqrt',
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',
389 "super" : 'K_super',
390 "supply0" : 'K_supply0',
391 "supply1" : 'K_supply1',
392 "sync_accept_on" : 'K_sync_accept_on',
393 "sync_reject_on" : 'K_sync_reject_on',
394 "table" : 'K_table',
395 "tagged" : 'K_tagged',
396 "tan" : 'K_tan',
397 "tanh" : 'K_tanh',
398 "task" : 'K_task',
399 "this" : 'K_this',
400 "throughout" : 'K_throughout',
401 "time" : 'K_time',
402 "timeprecision" : 'K_timeprecision',
403 "timer" : 'K_timer',
404 "timeunit" : 'K_timeunit',
405 "tran" : 'K_tran',
406 "tranif0" : 'K_tranif0',
407 "tranif1" : 'K_tranif1',
408 "transition" : 'K_transition',
409 "tri" : 'K_tri',
410 "tri0" : 'K_tri0',
411 "tri1" : 'K_tri1',
412 "triand" : 'K_triand',
413 "trior" : 'K_trior',
414 "trireg" : 'K_trireg',
415 "type" : 'K_type',
416 "typedef" : 'K_typedef',
417 "union" : 'K_union',
418 "unique" : 'K_unique',
419 "unique0" : 'K_unique',
420 "units" : 'K_units',
421 # Reserved for future use!',
422 "unsigned" : 'K_unsigned',
423 "until" : 'K_until',
424 "until_with" : 'K_until_with',
425 "untyped" : 'K_untyped',
426 "use" : 'K_use',
427 "uwire" : 'K_uwire',
428 "var" : 'K_var',
429 "vectored" : 'K_vectored',
430 "virtual" : 'K_virtual',
431 "void" : 'K_void',
432 "wait" : 'K_wait',
433 "wait_order" : 'K_wait_order',
434 "wand" : 'K_wand',
435 "weak" : 'K_weak',
436 "weak0" : 'K_weak0',
437 "weak1" : 'K_weak1',
438 "while" : 'K_while',
439 "white_noise" : 'K_white_noise',
440 "wildcard" : 'K_wildcard',
441 "wire" : 'K_wire',
442 "with" : 'K_with',
443 "within" : 'K_within',
444 # This is the name originally proposed for uwire and is deprecated!',
445 "wone" : 'K_wone',
446 "wor" : 'K_wor',
447 # This is defined by Verilog-AMS 2.3 and as an Icarus extension.',
448 "wreal" : 'K_wreal',
449 "xnor" : 'K_xnor',
450 "xor" : 'K_xor',
451 "zi_nd" : 'K_zi_nd',
452 "zi_np" : 'K_zi_np',
453 "zi_zd" : 'K_zi_zd',
454 "zi_zp" : 'K_zi_zp',
455 }
456
457 literals = ['[', '}', '{', ';', ':', '[', ']', ',', '(', ')',
458 '#', '=', '.', '@', '&', '!', '?', '<', '>', '%',
459 '|', '^', '~', '+', '*', '/', '-']
460
461 """
462 def t_module_end(t):
463 r'endmodule'
464 code = t.lexer.lexdata[t.modulestart:t.lexpos]
465 t.type = 'INITIAL'
466 t.value = code
467 t.lexer.lineno += t.value.count('\n')
468 return t
469
470 t_module_ignore = ' \t'
471 """
472
473
474 def t_LITERAL(t):
475 r'[a-zA-Z_$][a-zA-Z0-9$_]*'
476 word = t.value
477 keyword = lexor_keyword_code.get(t.value, 'IDENTIFIER')
478 if(lex_debug):
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'
485 t.value = word
486 return t
487 t.type = keyword
488 return t
489
490
491 def t_dec_number(t):
492 r'\'[sS]?[dD][ \t]*[0-9][0-9_]*'
493 t.type = 'BASED_NUMBER'
494 # t.value = word # make_unsized_dec(yytext);
495 return t
496
497
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);
502 return t
503
504
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);
509 return t
510
511
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);
516 return t
517
518
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);
523 return t
524
525
526 def t_unbased_make_unsized_binary(t):
527 r'\'[01xzXZ]'
528 t.type = 'UNBASED_NUMBER'
529 # t.value = word # make_unsized_binary(yytext);
530 return t
531
532
533 """
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. */
537 """
538
539
540 def t_make_unsized_dec(t):
541 r'[0-9][0-9_]*'
542 t.type = 'DEC_NUMBER'
543 # t.value = word # make_unsized_dec(yytext);
544 # based_size = yylval.number->as_ulong();
545 return t
546
547
548 """
549 /* Notice and handle the `timescale directive. */
550 """
551
552
553 def t_timescale(t):
554 # r'^{W}?`timescale'
555 r'`timescale'
556 t.lexer.timestart = t.lexpos+len(t.value)
557 t.lexer.push_state('timescale')
558
559
560 #t_timescale_ignore_toeol = r'.+\n'
561 t_timescale_ignore = ' \t'
562 #t_timescale_ignore_whitespace = r'\s+'
563 #t_code_ignore = ""
564
565
566 def t_timescale_end(t):
567 r'.+\n'
568 code = t.lexer.lexdata[t.lexer.timestart:t.lexpos]
569 t.type = 'timescale'
570 t.value = code
571 t.lexer.pop_state()
572 print("match", code)
573 return t
574
575
576 def t_timescale_error(t):
577 print("%d: Timescale error '%s'" % (t.lexer.lineno, t.value[0]))
578 print(t.value)
579 raise RuntimeError
580
581
582 """
583 def t_module_error(t):
584 print("%d: Module error '%s'" % (t.lexer.lineno, t.value[0]))
585 print(t.value)
586 raise RuntimeError
587 """
588
589
590 def t_error(t):
591 print("%d: Illegal character '%s'" % (t.lexer.lineno, t.value[0]))
592 print(t.value)
593 t.lexer.skip(1)
594
595
596 tokens = list(set(tokens))
597
598 lex.lex()
599
600 if __name__ == '__main__':
601 lex.runmain()