solidity.py 3.1 KB

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