solidity.py 3.1 KB

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