solidity.py 3.2 KB

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