hdl.py 18 KB

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