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