1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
4 local l
= require('lexer')
5 local token
, word_match
= l
.token
, l
.word_match
6 local P
, R
, S
= lpeg
.P
, lpeg
.R
, lpeg
.S
8 local M
= {_NAME
= 'verilog'}
11 local ws
= token(l
.WHITESPACE
, l
.space^
1)
14 local line_comment
= '//' * l
.nonnewline^
0
15 local block_comment
= '/*' * (l
.any
- '*/')^
0 * P('*/')^
-1
16 local comment
= token(l
.COMMENT
, line_comment
+ block_comment
)
19 local string = token(l
.STRING
, l
.delimited_range('"'))
22 local bin_suffix
= S('bB') * S('01_xXzZ')^
1
23 local oct_suffix
= S('oO') * S('01234567_xXzZ')^
1
24 local dec_suffix
= S('dD') * S('0123456789_xXzZ')^
1
25 local hex_suffix
= S('hH') * S('0123456789abcdefABCDEF_xXzZ')^
1
26 local number = token(l
.NUMBER
, (l
.digit
+ '_')^
1 + "'" *
27 (bin_suffix
+ oct_suffix
+ dec_suffix
+
31 local keyword
= token(l
.KEYWORD
, word_match({
32 'always', 'assign', 'begin', 'case', 'casex', 'casez', 'default', 'deassign',
33 'disable', 'else', 'end', 'endcase', 'endfunction', 'endgenerate',
34 'endmodule', 'endprimitive', 'endspecify', 'endtable', 'endtask', 'for',
35 'force', 'forever', 'fork', 'function', 'generate', 'if', 'initial', 'join',
36 'macromodule', 'module', 'negedge', 'posedge', 'primitive', 'repeat',
37 'release', 'specify', 'table', 'task', 'wait', 'while',
38 -- Compiler directives.
39 '`include', '`define', '`undef', '`ifdef', '`ifndef', '`else', '`endif',
40 '`timescale', '`resetall', '`signed', '`unsigned', '`celldefine',
41 '`endcelldefine', '`default_nettype', '`unconnected_drive',
42 '`nounconnected_drive', '`protect', '`endprotect', '`protected',
43 '`endprotected', '`remove_gatename', '`noremove_gatename', '`remove_netname',
44 '`noremove_netname', '`expand_vectornets', '`noexpand_vectornets',
45 '`autoexpand_vectornets',
47 'strong0', 'strong1', 'pull0', 'pull1', 'weak0', 'weak1', 'highz0', 'highz1',
48 'small', 'medium', 'large'
52 local func
= token(l
.FUNCTION
, word_match({
53 '$stop', '$finish', '$time', '$stime', '$realtime', '$settrace',
54 '$cleartrace', '$showscopes', '$showvars', '$monitoron', '$monitoroff',
55 '$random', '$printtimescale', '$timeformat', '$display',
56 -- Built-in primitives.
57 'and', 'nand', 'or', 'nor', 'xor', 'xnor', 'buf', 'bufif0', 'bufif1', 'not',
58 'notif0', 'notif1', 'nmos', 'pmos', 'cmos', 'rnmos', 'rpmos', 'rcmos', 'tran',
59 'tranif0', 'tranif1', 'rtran', 'rtranif0', 'rtranif1', 'pullup', 'pulldown'
63 local type = token(l
.TYPE
, word_match({
64 'integer', 'reg', 'time', 'realtime', 'defparam', 'parameter', 'event',
65 'wire', 'wand', 'wor', 'tri', 'triand', 'trior', 'tri0', 'tri1', 'trireg',
66 'vectored', 'scalared', 'input', 'output', 'inout',
71 local identifier
= token(l
.IDENTIFIER
, l
.word
)
74 local operator
= token(l
.OPERATOR
, S('=~+-/*<>%&|^~,:;()[]{}'))
82 {'identifier', identifier
},
85 {'operator', operator
},
89 _patterns
= {'[a-z]+', '[%(%){}]', '/%*', '%*/', '//'},
91 case
= 1, casex
= 1, casez
= 1, endcase
= -1, ['function'] = 1,
92 endfunction
= -1, fork
= 1, join
= -1, table = 1, endtable
= -1, task
= 1,
93 endtask
= -1, generate
= 1, endgenerate
= -1, specify
= 1, endspecify
= -1,
94 primitive
= 1, endprimitive
= -1, ['module'] = 1, endmodule
= -1, begin
= 1,
97 [l
.OPERATOR
] = {['('] = 1, [')'] = -1, ['{'] = 1, ['}'] = -1},
98 [l
.COMMENT
] = {['/*'] = 1, ['*/'] = -1, ['//'] = l
.fold_line_comments('//')}