vyper.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """
  2. pygments.lexers.vyper
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Vyper Smart Contract language.
  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, words
  9. from pygments.token import (Comment, String, Name, Keyword, Number,
  10. Operator, Punctuation, Text, Whitespace)
  11. __all__ = ['VyperLexer']
  12. class VyperLexer(RegexLexer):
  13. """For the Vyper smart contract language.
  14. """
  15. name = 'Vyper'
  16. aliases = ['vyper']
  17. filenames = ['*.vy']
  18. url = "https://vyper.readthedocs.io"
  19. version_added = '2.17'
  20. tokens = {
  21. 'root': [
  22. # Whitespace
  23. (r'\s+', Whitespace),
  24. # Line continuations
  25. (r'(\\)(\n|\r\n|\r)', bygroups(Text, Whitespace)),
  26. # Comments - inline and multiline
  27. (r'#.*$', Comment.Single),
  28. (r'\"\"\"', Comment.Multiline, 'multiline-comment'),
  29. # Strings - single and double
  30. (r"'", String.Single, 'single-string'),
  31. (r'"', String.Double, 'double-string'),
  32. # Functions (working)
  33. (r'(def)(\s+)([a-zA-Z_][a-zA-Z0-9_]*)',
  34. bygroups(Keyword, Whitespace, Name.Function)),
  35. # Event and Struct
  36. (r'(event|struct|interface|log)(\s+)([a-zA-Z_][a-zA-Z0-9_]*)',
  37. bygroups(Keyword, Whitespace, Name.Class)),
  38. # Imports
  39. (r'(from)(\s+)(vyper\.\w+)(\s+)(import)(\s+)(\w+)',
  40. bygroups(Keyword, Whitespace, Name.Namespace, Whitespace,
  41. Keyword, Whitespace, Name.Class)),
  42. # Numeric Literals
  43. (r'\b0x[0-9a-fA-F]+\b', Number.Hex),
  44. (r'\b(\d{1,3}(?:_\d{3})*|\d+)\b', Number.Integer),
  45. (r'\b\d+\.\d*\b', Number.Float),
  46. # Keywords
  47. (words(('def', 'event', 'pass', 'return', 'for', 'while', 'if', 'elif',
  48. 'else', 'assert', 'raise', 'import', 'in', 'struct', 'implements',
  49. 'interface', 'from', 'indexed', 'log'),
  50. prefix=r'\b', suffix=r'\b'), Keyword),
  51. # Visibility and State Mutability
  52. (words(('public', 'private', 'view', 'pure', 'constant',
  53. 'immutable', 'nonpayable'), prefix=r'\b', suffix=r'\b'),
  54. Keyword.Declaration),
  55. # Built-in Functions
  56. (words(('bitwise_and', 'bitwise_not', 'bitwise_or', 'bitwise_xor', 'shift',
  57. 'create_minimal_proxy_to', 'create_copy_of', 'create_from_blueprint',
  58. 'ecadd', 'ecmul', 'ecrecover', 'keccak256', 'sha256', 'concat', 'convert',
  59. 'uint2str', 'extract32', 'slice', 'abs', 'ceil', 'floor', 'max', 'max_value',
  60. 'min', 'min_value', 'pow_mod256', 'sqrt', 'isqrt', 'uint256_addmod',
  61. 'uint256_mulmod', 'unsafe_add', 'unsafe_sub', 'unsafe_mul', 'unsafe_div',
  62. 'as_wei_value', 'blockhash', 'empty', 'len', 'method_id', '_abi_encode',
  63. '_abi_decode', 'print', 'range'), prefix=r'\b', suffix=r'\b'),
  64. Name.Builtin),
  65. # Built-in Variables and Attributes
  66. (words(('msg.sender', 'msg.value', 'block.timestamp', 'block.number', 'msg.gas'),
  67. prefix=r'\b', suffix=r'\b'),
  68. Name.Builtin.Pseudo),
  69. (words(('uint', 'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256',
  70. 'int', 'int8', 'int16', 'int32', 'int64', 'int128', 'int256', 'bool',
  71. 'decimal', 'bytes', 'bytes1', 'bytes2', 'bytes3', 'bytes4', 'bytes5',
  72. 'bytes6', 'bytes7', 'bytes8', 'bytes9', 'bytes10', 'bytes11',
  73. 'bytes12', 'bytes13', 'bytes14', 'bytes15', 'bytes16', 'bytes17',
  74. 'bytes18', 'bytes19', 'bytes20', 'bytes21', 'bytes22', 'bytes23',
  75. 'bytes24', 'bytes25', 'bytes26', 'bytes27', 'bytes28', 'bytes29',
  76. 'bytes30', 'bytes31', 'bytes32', 'string', 'String', 'address',
  77. 'enum', 'struct'), prefix=r'\b', suffix=r'\b'),
  78. Keyword.Type),
  79. # indexed keywords
  80. (r'\b(indexed)\b(\s*)(\()(\s*)(\w+)(\s*)(\))',
  81. bygroups(Keyword, Whitespace, Punctuation, Whitespace,
  82. Keyword.Type, Punctuation)),
  83. # Operators and Punctuation
  84. (r'(\+|\-|\*|\/|<=?|>=?|==|!=|=|\||&|%)', Operator),
  85. (r'[.,:;()\[\]{}]', Punctuation),
  86. # Other variable names and types
  87. (r'@[\w.]+', Name.Decorator),
  88. (r'__\w+__', Name.Magic), # Matches double underscores followed by word characters
  89. (r'EMPTY_BYTES32', Name.Constant),
  90. (r'\bERC20\b', Name.Class),
  91. (r'\bself\b', Name.Attribute),
  92. (r'Bytes\[\d+\]', Keyword.Type),
  93. # Generic names and variables
  94. (r'\b[a-zA-Z_]\w*\b:', Name.Variable),
  95. (r'\b[a-zA-Z_]\w*\b', Name),
  96. ],
  97. 'multiline-comment': [
  98. (r'\"\"\"', Comment.Multiline, '#pop'),
  99. (r'[^"]+', Comment.Multiline),
  100. (r'\"', Comment.Multiline)
  101. ],
  102. 'single-string': [
  103. (r"[^\\']+", String.Single),
  104. (r"'", String.Single, '#pop'),
  105. (r'\\.', String.Escape),
  106. ],
  107. 'double-string': [
  108. (r'[^\\"]+', String.Double),
  109. (r'"', String.Double, '#pop'),
  110. (r'\\.', String.Escape),
  111. ]
  112. }