1
# -*- coding: utf-8 -*-
6
Lexers for hardware descriptor languages.
8
:copyright: Copyright 2010 by the Pygments team, see AUTHORS.
9
:license: BSD, see LICENSE for details.
14
from pygments.lexer import RegexLexer, include, bygroups
15
from pygments.token import \
16
Text, Comment, Operator, Keyword, Name, String, Number, Punctuation, \
19
__all__ = ['VerilogLexer']
22
class VerilogLexer(RegexLexer):
24
For verilog source code with preprocessor directives.
26
*New in Pygments 1.4.*
30
filenames = ['*.v', '*.sv']
31
mimetypes = ['text/x-verilog']
33
#: optional Comment or Whitespace
34
_ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
38
(r'^\s*`define', Comment.Preproc, 'macro'),
41
(r'\\\n', Text), # line continuation
42
(r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single),
43
(r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
44
(r'[{}#@]', Punctuation),
45
(r'L?"', String, 'string'),
46
(r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
47
(r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float),
48
(r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
49
(r'([0-9]+)|(\'h)[0-9a-fA-F]+', Number.Hex),
50
(r'([0-9]+)|(\'b)[0-1]+', Number.Hex), # should be binary
51
(r'([0-9]+)|(\'d)[0-9]+', Number.Integer),
52
(r'([0-9]+)|(\'o)[0-7]+', Number.Oct),
53
(r'\'[01xz]', Number),
54
(r'\d+[Ll]?', Number.Integer),
56
(r'[~!%^&*+=|?:<>/-]', Operator),
57
(r'[()\[\],.;\']', Punctuation),
58
(r'`[a-zA-Z_][a-zA-Z0-9_]*', Name.Constant),
60
(r'^\s*(package)(\s+)', bygroups(Keyword.Namespace, Text)),
61
(r'^\s*(import)(\s+)', bygroups(Keyword.Namespace, Text), 'import'),
63
(r'(always|always_comb|always_ff|always_latch|and|assign|automatic|'
64
r'begin|break|buf|bufif0|bufif1|case|casex|casez|cmos|const|'
65
r'continue|deassign|default|defparam|disable|do|edge|else|end|endcase|'
66
r'endfunction|endgenerate|endmodule|endpackage|endprimitive|endspecify|'
67
r'endtable|endtask|enum|event|final|for|force|forever|fork|function|'
68
r'generate|genvar|highz0|highz1|if|initial|inout|input|'
69
r'integer|join|large|localparam|macromodule|medium|module|'
70
r'nand|negedge|nmos|nor|not|notif0|notif1|or|output|packed|'
71
r'parameter|pmos|posedge|primitive|pull0|pull1|pulldown|pullup|rcmos|'
72
r'ref|release|repeat|return|rnmos|rpmos|rtran|rtranif0|'
73
r'rtranif1|scalared|signed|small|specify|specparam|strength|'
74
r'string|strong0|strong1|struct|table|task|'
75
r'tran|tranif0|tranif1|type|typedef|'
76
r'unsigned|var|vectored|void|wait|weak0|weak1|while|'
77
r'xnor|xor)\b', Keyword),
79
(r'(`accelerate|`autoexpand_vectornets|`celldefine|`default_nettype|'
80
r'`else|`elsif|`endcelldefine|`endif|`endprotect|`endprotected|'
81
r'`expand_vectornets|`ifdef|`ifndef|`include|`noaccelerate|`noexpand_vectornets|'
82
r'`noremove_gatenames|`noremove_netnames|`nounconnected_drive|'
83
r'`protect|`protected|`remove_gatenames|`remove_netnames|`resetall|'
84
r'`timescale|`unconnected_drive|`undef)\b', Comment.Preproc),
86
(r'(\$bits|\$bitstoreal|\$bitstoshortreal|\$countdrivers|\$display|\$fclose|'
87
r'\$fdisplay|\$finish|\$floor|\$fmonitor|\$fopen|\$fstrobe|\$fwrite|'
88
r'\$getpattern|\$history|\$incsave|\$input|\$itor|\$key|\$list|\$log|'
89
r'\$monitor|\$monitoroff|\$monitoron|\$nokey|\$nolog|\$printtimescale|'
90
r'\$random|\$readmemb|\$readmemh|\$realtime|\$realtobits|\$reset|\$reset_count|'
91
r'\$reset_value|\$restart|\$rtoi|\$save|\$scale|\$scope|\$shortrealtobits|'
92
r'\$showscopes|\$showvariables|\$showvars|\$sreadmemb|\$sreadmemh|'
93
r'\$stime|\$stop|\$strobe|\$time|\$timeformat|\$write)\b', Name.Builtin),
95
(r'(class)(\s+)', bygroups(Keyword, Text), 'classname'),
96
(r'(byte|shortint|int|longint|interger|time|'
98
r'supply0|supply1|tri|triand|trior|tri0|tri1|trireg|uwire|wire|wand|wor'
99
r'shortreal|real|realtime)\b', Keyword.Type),
100
('[a-zA-Z_][a-zA-Z0-9_]*:(?!:)', Name.Label),
101
('[a-zA-Z_][a-zA-Z0-9_]*', Name),
104
(r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop'),
107
(r'"', String, '#pop'),
108
(r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
109
(r'[^\\"\n]+', String), # all other characters
110
(r'\\\n', String), # line continuation
111
(r'\\', String), # stray backslash
114
(r'[^/\n]+', Comment.Preproc),
115
(r'/[*](.|\n)*?[*]/', Comment.Multiline),
116
(r'//.*?\n', Comment.Single, '#pop'),
117
(r'/', Comment.Preproc),
118
(r'(?<=\\)\n', Comment.Preproc),
119
(r'\n', Comment.Preproc, '#pop'),
122
(r'[a-zA-Z0-9_:]+\*?', Name.Namespace, '#pop')
126
def get_tokens_unprocessed(self, text):
127
for index, token, value in \
128
RegexLexer.get_tokens_unprocessed(self, text):
129
# Convention: mark all upper case names as constants
132
token = Name.Constant
133
yield index, token, value