eric6/ThirdParty/Pygments/pygments/lexers/hdl.py

changeset 6942
2602857055c5
parent 5713
6762afd9f963
child 7547
21b0534faebc
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.hdl
4 ~~~~~~~~~~~~~~~~~~~
5
6 Lexers for hardware descriptor languages.
7
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 import re
13
14 from pygments.lexer import RegexLexer, bygroups, include, using, this, words
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
16 Number, Punctuation, Error
17
18 __all__ = ['VerilogLexer', 'SystemVerilogLexer', 'VhdlLexer']
19
20
21 class VerilogLexer(RegexLexer):
22 """
23 For verilog source code with preprocessor directives.
24
25 .. versionadded:: 1.4
26 """
27 name = 'verilog'
28 aliases = ['verilog', 'v']
29 filenames = ['*.v']
30 mimetypes = ['text/x-verilog']
31
32 #: optional Comment or Whitespace
33 _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
34
35 tokens = {
36 'root': [
37 (r'^\s*`define', Comment.Preproc, 'macro'),
38 (r'\n', Text),
39 (r'\s+', Text),
40 (r'\\\n', Text), # line continuation
41 (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single),
42 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
43 (r'[{}#@]', Punctuation),
44 (r'L?"', String, 'string'),
45 (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
46 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float),
47 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
48 (r'([0-9]+)|(\'h)[0-9a-fA-F]+', Number.Hex),
49 (r'([0-9]+)|(\'b)[01]+', Number.Bin),
50 (r'([0-9]+)|(\'d)[0-9]+', Number.Integer),
51 (r'([0-9]+)|(\'o)[0-7]+', Number.Oct),
52 (r'\'[01xz]', Number),
53 (r'\d+[Ll]?', Number.Integer),
54 (r'\*/', Error),
55 (r'[~!%^&*+=|?:<>/-]', Operator),
56 (r'[()\[\],.;\']', Punctuation),
57 (r'`[a-zA-Z_]\w*', Name.Constant),
58
59 (r'^(\s*)(package)(\s+)', bygroups(Text, Keyword.Namespace, Text)),
60 (r'^(\s*)(import)(\s+)', bygroups(Text, Keyword.Namespace, Text),
61 'import'),
62
63 (words((
64 'always', 'always_comb', 'always_ff', 'always_latch', 'and',
65 'assign', 'automatic', 'begin', 'break', 'buf', 'bufif0', 'bufif1',
66 'case', 'casex', 'casez', 'cmos', 'const', 'continue', 'deassign',
67 'default', 'defparam', 'disable', 'do', 'edge', 'else', 'end', 'endcase',
68 'endfunction', 'endgenerate', 'endmodule', 'endpackage', 'endprimitive',
69 'endspecify', 'endtable', 'endtask', 'enum', 'event', 'final', 'for',
70 'force', 'forever', 'fork', 'function', 'generate', 'genvar', 'highz0',
71 'highz1', 'if', 'initial', 'inout', 'input', 'integer', 'join', 'large',
72 'localparam', 'macromodule', 'medium', 'module', 'nand', 'negedge',
73 'nmos', 'nor', 'not', 'notif0', 'notif1', 'or', 'output', 'packed',
74 'parameter', 'pmos', 'posedge', 'primitive', 'pull0', 'pull1',
75 'pulldown', 'pullup', 'rcmos', 'ref', 'release', 'repeat', 'return',
76 'rnmos', 'rpmos', 'rtran', 'rtranif0', 'rtranif1', 'scalared', 'signed',
77 'small', 'specify', 'specparam', 'strength', 'string', 'strong0',
78 'strong1', 'struct', 'table', 'task', 'tran', 'tranif0', 'tranif1',
79 'type', 'typedef', 'unsigned', 'var', 'vectored', 'void', 'wait',
80 'weak0', 'weak1', 'while', 'xnor', 'xor'), suffix=r'\b'),
81 Keyword),
82
83 (words((
84 'accelerate', 'autoexpand_vectornets', 'celldefine', 'default_nettype',
85 'else', 'elsif', 'endcelldefine', 'endif', 'endprotect', 'endprotected',
86 'expand_vectornets', 'ifdef', 'ifndef', 'include', 'noaccelerate',
87 'noexpand_vectornets', 'noremove_gatenames', 'noremove_netnames',
88 'nounconnected_drive', 'protect', 'protected', 'remove_gatenames',
89 'remove_netnames', 'resetall', 'timescale', 'unconnected_drive',
90 'undef'), prefix=r'`', suffix=r'\b'),
91 Comment.Preproc),
92
93 (words((
94 'bits', 'bitstoreal', 'bitstoshortreal', 'countdrivers', 'display', 'fclose',
95 'fdisplay', 'finish', 'floor', 'fmonitor', 'fopen', 'fstrobe', 'fwrite',
96 'getpattern', 'history', 'incsave', 'input', 'itor', 'key', 'list', 'log',
97 'monitor', 'monitoroff', 'monitoron', 'nokey', 'nolog', 'printtimescale',
98 'random', 'readmemb', 'readmemh', 'realtime', 'realtobits', 'reset',
99 'reset_count', 'reset_value', 'restart', 'rtoi', 'save', 'scale', 'scope',
100 'shortrealtobits', 'showscopes', 'showvariables', 'showvars', 'sreadmemb',
101 'sreadmemh', 'stime', 'stop', 'strobe', 'time', 'timeformat', 'write'),
102 prefix=r'\$', suffix=r'\b'),
103 Name.Builtin),
104
105 (words((
106 'byte', 'shortint', 'int', 'longint', 'integer', 'time',
107 'bit', 'logic', 'reg', 'supply0', 'supply1', 'tri', 'triand',
108 'trior', 'tri0', 'tri1', 'trireg', 'uwire', 'wire', 'wand', 'wo'
109 'shortreal', 'real', 'realtime'), suffix=r'\b'),
110 Keyword.Type),
111 (r'[a-zA-Z_]\w*:(?!:)', Name.Label),
112 (r'\$?[a-zA-Z_]\w*', Name),
113 ],
114 'string': [
115 (r'"', String, '#pop'),
116 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
117 (r'[^\\"\n]+', String), # all other characters
118 (r'\\\n', String), # line continuation
119 (r'\\', String), # stray backslash
120 ],
121 'macro': [
122 (r'[^/\n]+', Comment.Preproc),
123 (r'/[*](.|\n)*?[*]/', Comment.Multiline),
124 (r'//.*?\n', Comment.Single, '#pop'),
125 (r'/', Comment.Preproc),
126 (r'(?<=\\)\n', Comment.Preproc),
127 (r'\n', Comment.Preproc, '#pop'),
128 ],
129 'import': [
130 (r'[\w:]+\*?', Name.Namespace, '#pop')
131 ]
132 }
133
134 def get_tokens_unprocessed(self, text):
135 for index, token, value in \
136 RegexLexer.get_tokens_unprocessed(self, text):
137 # Convention: mark all upper case names as constants
138 if token is Name:
139 if value.isupper():
140 token = Name.Constant
141 yield index, token, value
142
143
144 class SystemVerilogLexer(RegexLexer):
145 """
146 Extends verilog lexer to recognise all SystemVerilog keywords from IEEE
147 1800-2009 standard.
148
149 .. versionadded:: 1.5
150 """
151 name = 'systemverilog'
152 aliases = ['systemverilog', 'sv']
153 filenames = ['*.sv', '*.svh']
154 mimetypes = ['text/x-systemverilog']
155
156 #: optional Comment or Whitespace
157 _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
158
159 tokens = {
160 'root': [
161 (r'^\s*`define', Comment.Preproc, 'macro'),
162 (r'^(\s*)(package)(\s+)', bygroups(Text, Keyword.Namespace, Text)),
163 (r'^(\s*)(import)(\s+)', bygroups(Text, Keyword.Namespace, Text), 'import'),
164
165 (r'\n', Text),
166 (r'\s+', Text),
167 (r'\\\n', Text), # line continuation
168 (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single),
169 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
170 (r'[{}#@]', Punctuation),
171 (r'L?"', String, 'string'),
172 (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
173 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float),
174 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
175 (r'([0-9]+)|(\'h)[0-9a-fA-F]+', Number.Hex),
176 (r'([0-9]+)|(\'b)[01]+', Number.Bin),
177 (r'([0-9]+)|(\'d)[0-9]+', Number.Integer),
178 (r'([0-9]+)|(\'o)[0-7]+', Number.Oct),
179 (r'\'[01xz]', Number),
180 (r'\d+[Ll]?', Number.Integer),
181 (r'\*/', Error),
182 (r'[~!%^&*+=|?:<>/-]', Operator),
183 (r'[()\[\],.;\']', Punctuation),
184 (r'`[a-zA-Z_]\w*', Name.Constant),
185
186 (words((
187 'accept_on', 'alias', 'always', 'always_comb', 'always_ff', 'always_latch',
188 'and', 'assert', 'assign', 'assume', 'automatic', 'before', 'begin', 'bind', 'bins',
189 'binsof', 'bit', 'break', 'buf', 'bufif0', 'bufif1', 'byte', 'case', 'casex', 'casez',
190 'cell', 'chandle', 'checker', 'class', 'clocking', 'cmos', 'config', 'const', 'constraint',
191 'context', 'continue', 'cover', 'covergroup', 'coverpoint', 'cross', 'deassign',
192 'default', 'defparam', 'design', 'disable', 'dist', 'do', 'edge', 'else', 'end', 'endcase',
193 'endchecker', 'endclass', 'endclocking', 'endconfig', 'endfunction', 'endgenerate',
194 'endgroup', 'endinterface', 'endmodule', 'endpackage', 'endprimitive',
195 'endprogram', 'endproperty', 'endsequence', 'endspecify', 'endtable',
196 'endtask', 'enum', 'event', 'eventually', 'expect', 'export', 'extends', 'extern',
197 'final', 'first_match', 'for', 'force', 'foreach', 'forever', 'fork', 'forkjoin',
198 'function', 'generate', 'genvar', 'global', 'highz0', 'highz1', 'if', 'iff', 'ifnone',
199 'ignore_bins', 'illegal_bins', 'implies', 'import', 'incdir', 'include',
200 'initial', 'inout', 'input', 'inside', 'instance', 'int', 'integer', 'interface',
201 'intersect', 'join', 'join_any', 'join_none', 'large', 'let', 'liblist', 'library',
202 'local', 'localparam', 'logic', 'longint', 'macromodule', 'matches', 'medium',
203 'modport', 'module', 'nand', 'negedge', 'new', 'nexttime', 'nmos', 'nor', 'noshowcancelled',
204 'not', 'notif0', 'notif1', 'null', 'or', 'output', 'package', 'packed', 'parameter',
205 'pmos', 'posedge', 'primitive', 'priority', 'program', 'property', 'protected',
206 'pull0', 'pull1', 'pulldown', 'pullup', 'pulsestyle_ondetect', 'pulsestyle_onevent',
207 'pure', 'rand', 'randc', 'randcase', 'randsequence', 'rcmos', 'real', 'realtime',
208 'ref', 'reg', 'reject_on', 'release', 'repeat', 'restrict', 'return', 'rnmos',
209 'rpmos', 'rtran', 'rtranif0', 'rtranif1', 's_always', 's_eventually', 's_nexttime',
210 's_until', 's_until_with', 'scalared', 'sequence', 'shortint', 'shortreal',
211 'showcancelled', 'signed', 'small', 'solve', 'specify', 'specparam', 'static',
212 'string', 'strong', 'strong0', 'strong1', 'struct', 'super', 'supply0', 'supply1',
213 'sync_accept_on', 'sync_reject_on', 'table', 'tagged', 'task', 'this', 'throughout',
214 'time', 'timeprecision', 'timeunit', 'tran', 'tranif0', 'tranif1', 'tri', 'tri0',
215 'tri1', 'triand', 'trior', 'trireg', 'type', 'typedef', 'union', 'unique', 'unique0',
216 'unsigned', 'until', 'until_with', 'untyped', 'use', 'uwire', 'var', 'vectored',
217 'virtual', 'void', 'wait', 'wait_order', 'wand', 'weak', 'weak0', 'weak1', 'while',
218 'wildcard', 'wire', 'with', 'within', 'wor', 'xnor', 'xor'), suffix=r'\b'),
219 Keyword),
220
221 (words((
222 '`__FILE__', '`__LINE__', '`begin_keywords', '`celldefine', '`default_nettype',
223 '`define', '`else', '`elsif', '`end_keywords', '`endcelldefine', '`endif',
224 '`ifdef', '`ifndef', '`include', '`line', '`nounconnected_drive', '`pragma',
225 '`resetall', '`timescale', '`unconnected_drive', '`undef', '`undefineall'),
226 suffix=r'\b'),
227 Comment.Preproc),
228
229 (words((
230 '$display', '$displayb', '$displayh', '$displayo', '$dumpall', '$dumpfile',
231 '$dumpflush', '$dumplimit', '$dumpoff', '$dumpon', '$dumpports',
232 '$dumpportsall', '$dumpportsflush', '$dumpportslimit', '$dumpportsoff',
233 '$dumpportson', '$dumpvars', '$fclose', '$fdisplay', '$fdisplayb',
234 '$fdisplayh', '$fdisplayo', '$feof', '$ferror', '$fflush', '$fgetc',
235 '$fgets', '$finish', '$fmonitor', '$fmonitorb', '$fmonitorh', '$fmonitoro',
236 '$fopen', '$fread', '$fscanf', '$fseek', '$fstrobe', '$fstrobeb', '$fstrobeh',
237 '$fstrobeo', '$ftell', '$fwrite', '$fwriteb', '$fwriteh', '$fwriteo',
238 '$monitor', '$monitorb', '$monitorh', '$monitoro', '$monitoroff',
239 '$monitoron', '$plusargs', '$random', '$readmemb', '$readmemh', '$rewind',
240 '$sformat', '$sformatf', '$sscanf', '$strobe', '$strobeb', '$strobeh', '$strobeo',
241 '$swrite', '$swriteb', '$swriteh', '$swriteo', '$test', '$ungetc',
242 '$value$plusargs', '$write', '$writeb', '$writeh', '$writememb',
243 '$writememh', '$writeo'), suffix=r'\b'),
244 Name.Builtin),
245
246 (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'),
247 (words((
248 'byte', 'shortint', 'int', 'longint', 'integer', 'time',
249 'bit', 'logic', 'reg', 'supply0', 'supply1', 'tri', 'triand',
250 'trior', 'tri0', 'tri1', 'trireg', 'uwire', 'wire', 'wand', 'wo'
251 'shortreal', 'real', 'realtime'), suffix=r'\b'),
252 Keyword.Type),
253 (r'[a-zA-Z_]\w*:(?!:)', Name.Label),
254 (r'\$?[a-zA-Z_]\w*', Name),
255 ],
256 'classname': [
257 (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
258 ],
259 'string': [
260 (r'"', String, '#pop'),
261 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
262 (r'[^\\"\n]+', String), # all other characters
263 (r'\\\n', String), # line continuation
264 (r'\\', String), # stray backslash
265 ],
266 'macro': [
267 (r'[^/\n]+', Comment.Preproc),
268 (r'/[*](.|\n)*?[*]/', Comment.Multiline),
269 (r'//.*?\n', Comment.Single, '#pop'),
270 (r'/', Comment.Preproc),
271 (r'(?<=\\)\n', Comment.Preproc),
272 (r'\n', Comment.Preproc, '#pop'),
273 ],
274 'import': [
275 (r'[\w:]+\*?', Name.Namespace, '#pop')
276 ]
277 }
278
279 def get_tokens_unprocessed(self, text):
280 for index, token, value in \
281 RegexLexer.get_tokens_unprocessed(self, text):
282 # Convention: mark all upper case names as constants
283 if token is Name:
284 if value.isupper():
285 token = Name.Constant
286 yield index, token, value
287
288
289 class VhdlLexer(RegexLexer):
290 """
291 For VHDL source code.
292
293 .. versionadded:: 1.5
294 """
295 name = 'vhdl'
296 aliases = ['vhdl']
297 filenames = ['*.vhdl', '*.vhd']
298 mimetypes = ['text/x-vhdl']
299 flags = re.MULTILINE | re.IGNORECASE
300
301 tokens = {
302 'root': [
303 (r'\n', Text),
304 (r'\s+', Text),
305 (r'\\\n', Text), # line continuation
306 (r'--.*?$', Comment.Single),
307 (r"'(U|X|0|1|Z|W|L|H|-)'", String.Char),
308 (r'[~!%^&*+=|?:<>/-]', Operator),
309 (r"'[a-z_]\w*", Name.Attribute),
310 (r'[()\[\],.;\']', Punctuation),
311 (r'"[^\n\\"]*"', String),
312
313 (r'(library)(\s+)([a-z_]\w*)',
314 bygroups(Keyword, Text, Name.Namespace)),
315 (r'(use)(\s+)(entity)', bygroups(Keyword, Text, Keyword)),
316 (r'(use)(\s+)([a-z_][\w.]*\.)(all)',
317 bygroups(Keyword, Text, Name.Namespace, Keyword)),
318 (r'(use)(\s+)([a-z_][\w.]*)',
319 bygroups(Keyword, Text, Name.Namespace)),
320 (r'(std|ieee)(\.[a-z_]\w*)',
321 bygroups(Name.Namespace, Name.Namespace)),
322 (words(('std', 'ieee', 'work'), suffix=r'\b'),
323 Name.Namespace),
324 (r'(entity|component)(\s+)([a-z_]\w*)',
325 bygroups(Keyword, Text, Name.Class)),
326 (r'(architecture|configuration)(\s+)([a-z_]\w*)(\s+)'
327 r'(of)(\s+)([a-z_]\w*)(\s+)(is)',
328 bygroups(Keyword, Text, Name.Class, Text, Keyword, Text,
329 Name.Class, Text, Keyword)),
330 (r'([a-z_]\w*)(:)(\s+)(process|for)',
331 bygroups(Name.Class, Operator, Text, Keyword)),
332 (r'(end)(\s+)', bygroups(using(this), Text), 'endblock'),
333
334 include('types'),
335 include('keywords'),
336 include('numbers'),
337
338 (r'[a-z_]\w*', Name),
339 ],
340 'endblock': [
341 include('keywords'),
342 (r'[a-z_]\w*', Name.Class),
343 (r'(\s+)', Text),
344 (r';', Punctuation, '#pop'),
345 ],
346 'types': [
347 (words((
348 'boolean', 'bit', 'character', 'severity_level', 'integer', 'time',
349 'delay_length', 'natural', 'positive', 'string', 'bit_vector',
350 'file_open_kind', 'file_open_status', 'std_ulogic', 'std_ulogic_vector',
351 'std_logic', 'std_logic_vector', 'signed', 'unsigned'), suffix=r'\b'),
352 Keyword.Type),
353 ],
354 'keywords': [
355 (words((
356 'abs', 'access', 'after', 'alias', 'all', 'and',
357 'architecture', 'array', 'assert', 'attribute', 'begin', 'block',
358 'body', 'buffer', 'bus', 'case', 'component', 'configuration',
359 'constant', 'disconnect', 'downto', 'else', 'elsif', 'end',
360 'entity', 'exit', 'file', 'for', 'function', 'generate',
361 'generic', 'group', 'guarded', 'if', 'impure', 'in',
362 'inertial', 'inout', 'is', 'label', 'library', 'linkage',
363 'literal', 'loop', 'map', 'mod', 'nand', 'new',
364 'next', 'nor', 'not', 'null', 'of', 'on',
365 'open', 'or', 'others', 'out', 'package', 'port',
366 'postponed', 'procedure', 'process', 'pure', 'range', 'record',
367 'register', 'reject', 'rem', 'return', 'rol', 'ror', 'select',
368 'severity', 'signal', 'shared', 'sla', 'sll', 'sra',
369 'srl', 'subtype', 'then', 'to', 'transport', 'type',
370 'units', 'until', 'use', 'variable', 'wait', 'when',
371 'while', 'with', 'xnor', 'xor'), suffix=r'\b'),
372 Keyword),
373 ],
374 'numbers': [
375 (r'\d{1,2}#[0-9a-f_]+#?', Number.Integer),
376 (r'\d+', Number.Integer),
377 (r'(\d+\.\d*|\.\d+|\d+)E[+-]?\d+', Number.Float),
378 (r'X"[0-9a-f_]+"', Number.Hex),
379 (r'O"[0-7_]+"', Number.Oct),
380 (r'B"[01_]+"', Number.Bin),
381 ],
382 }

eric ide

mercurial