solidity.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """
  2. pygments.lexers.solidity
  3. ~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for Solidity.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, include, words
  9. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation, Whitespace
  11. __all__ = ['SolidityLexer']
  12. class SolidityLexer(RegexLexer):
  13. """
  14. For Solidity source code.
  15. """
  16. name = 'Solidity'
  17. aliases = ['solidity']
  18. filenames = ['*.sol']
  19. mimetypes = []
  20. url = 'https://soliditylang.org'
  21. version_added = '2.5'
  22. datatype = (
  23. r'\b(address|bool|(?:(?:bytes|hash|int|string|uint)(?:8|16|24|32|40|48|56|64'
  24. r'|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208'
  25. r'|216|224|232|240|248|256)?))\b'
  26. )
  27. tokens = {
  28. 'root': [
  29. include('whitespace'),
  30. include('comments'),
  31. (r'\bpragma\s+solidity\b', Keyword, 'pragma'),
  32. (r'\b(contract)(\s+)([a-zA-Z_]\w*)',
  33. bygroups(Keyword, Whitespace, Name.Entity)),
  34. (datatype + r'(\s+)((?:external|public|internal|private)\s+)?' +
  35. r'([a-zA-Z_]\w*)',
  36. bygroups(Keyword.Type, Whitespace, Keyword, Name.Variable)),
  37. (r'\b(enum|event|function|struct)(\s+)([a-zA-Z_]\w*)',
  38. bygroups(Keyword.Type, Whitespace, Name.Variable)),
  39. (r'\b(msg|block|tx)\.([A-Za-z_][a-zA-Z0-9_]*)\b', Keyword),
  40. (words((
  41. 'block', 'break', 'constant', 'constructor', 'continue',
  42. 'contract', 'do', 'else', 'external', 'false', 'for',
  43. 'function', 'if', 'import', 'inherited', 'internal', 'is',
  44. 'library', 'mapping', 'memory', 'modifier', 'msg', 'new',
  45. 'payable', 'private', 'public', 'require', 'return',
  46. 'returns', 'struct', 'suicide', 'throw', 'this', 'true',
  47. 'tx', 'var', 'while'), prefix=r'\b', suffix=r'\b'),
  48. Keyword.Type),
  49. (words(('keccak256',), prefix=r'\b', suffix=r'\b'), Name.Builtin),
  50. (datatype, Keyword.Type),
  51. include('constants'),
  52. (r'[a-zA-Z_]\w*', Text),
  53. (r'[~!%^&*+=|?:<>/-]', Operator),
  54. (r'[.;{}(),\[\]]', Punctuation)
  55. ],
  56. 'comments': [
  57. (r'//(\n|[\w\W]*?[^\\]\n)', Comment.Single),
  58. (r'/(\\\n)?[*][\w\W]*?[*](\\\n)?/', Comment.Multiline),
  59. (r'/(\\\n)?[*][\w\W]*', Comment.Multiline)
  60. ],
  61. 'constants': [
  62. (r'("(\\"|.)*?")', String.Double),
  63. (r"('(\\'|.)*?')", String.Single),
  64. (r'\b0[xX][0-9a-fA-F]+\b', Number.Hex),
  65. (r'\b\d+\b', Number.Decimal),
  66. ],
  67. 'pragma': [
  68. include('whitespace'),
  69. include('comments'),
  70. (r'(\^|>=|<)(\s*)(\d+\.\d+\.\d+)',
  71. bygroups(Operator, Whitespace, Keyword)),
  72. (r';', Punctuation, '#pop')
  73. ],
  74. 'whitespace': [
  75. (r'\s+', Whitespace),
  76. (r'\n', Whitespace)
  77. ]
  78. }