vis: move text object definitions to separate file
[vis.git] / lexers / verilog.lua
blob9160e0f9d094b19ce1403bb9fed6720d322c79d3
1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- Verilog LPeg lexer.
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'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
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)
18 -- Strings.
19 local string = token(l.STRING, l.delimited_range('"'))
21 -- Numbers.
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 +
28 hex_suffix))
30 -- Keywords.
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',
46 -- Signal strengths.
47 'strong0', 'strong1', 'pull0', 'pull1', 'weak0', 'weak1', 'highz0', 'highz1',
48 'small', 'medium', 'large'
49 }, '`01'))
51 -- Function.
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'
60 }, '$01'))
62 -- Types.
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',
67 'supply0', 'supply1'
68 }, '01'))
70 -- Identifiers.
71 local identifier = token(l.IDENTIFIER, l.word)
73 -- Operators.
74 local operator = token(l.OPERATOR, S('=~+-/*<>%&|^~,:;()[]{}'))
76 M._rules = {
77 {'whitespace', ws},
78 {'number', number},
79 {'keyword', keyword},
80 {'function', func},
81 {'type', type},
82 {'identifier', identifier},
83 {'string', string},
84 {'comment', comment},
85 {'operator', operator},
88 M._foldsymbols = {
89 _patterns = {'[a-z]+', '[%(%){}]', '/%*', '%*/', '//'},
90 [l.KEYWORD] = {
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,
95 ['end'] = -1
97 [l.OPERATOR] = {['('] = 1, [')'] = -1, ['{'] = 1, ['}'] = -1},
98 [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')}
101 return M