Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Vyper lexer (#2531 redux) #2579

Merged
merged 20 commits into from Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pygments/lexers/_mapping.py
Expand Up @@ -545,6 +545,7 @@
'VimLexer': ('pygments.lexers.textedit', 'VimL', ('vim',), ('*.vim', '.vimrc', '.exrc', '.gvimrc', '_vimrc', '_exrc', '_gvimrc', 'vimrc', 'gvimrc'), ('text/x-vim',)),
'VisualPrologGrammarLexer': ('pygments.lexers.vip', 'Visual Prolog Grammar', ('visualprologgrammar',), ('*.vipgrm',), ()),
'VisualPrologLexer': ('pygments.lexers.vip', 'Visual Prolog', ('visualprolog',), ('*.pro', '*.cl', '*.i', '*.pack', '*.ph'), ()),
'VyperLexer': ('pygments.lexers.vyper', 'Vyper', ('vyper',), ('*.vy',), ()),
'WDiffLexer': ('pygments.lexers.diff', 'WDiff', ('wdiff',), ('*.wdiff',), ()),
'WatLexer': ('pygments.lexers.webassembly', 'WebAssembly', ('wast', 'wat'), ('*.wat', '*.wast'), ()),
'WebIDLLexer': ('pygments.lexers.webidl', 'Web IDL', ('webidl',), ('*.webidl',), ()),
Expand Down
138 changes: 138 additions & 0 deletions pygments/lexers/vyper.py
@@ -0,0 +1,138 @@
"""
pygments.lexers.vyper
~~~~~~~~~~~~~~~~~~~~~

Lexer for the Vyper Smart Contract language.

:copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""

from pygments.lexer import RegexLexer, bygroups, words
from pygments.token import (Comment, String, Name, Keyword, Number,
Operator, Punctuation, Text, Whitespace)

__all__ = ['VyperLexer']


class VyperLexer(RegexLexer):
"""For the Vyper smart contract language."""
name = 'Vyper'
aliases = ['vyper']
filenames = ['*.vy']
url = "https://vyper.readthedocs.io"

tokens = {
'root': [
# Whitespace
(r'\s+', Whitespace),

# Line continuations
(r'(\\)(\n|\r\n|\r)', bygroups(Text, Whitespace)),

# Comments - inline and multiline
(r'#.*$', Comment.Single),
(r'\"\"\"', Comment.Multiline, 'multiline-comment'),

# Strings - single and double
(r"'", String.Single, 'single-string'),
(r'"', String.Double, 'double-string'),

# Functions (working)
(r'(def)(\s+)([a-zA-Z_][a-zA-Z0-9_]*)',
bygroups(Keyword, Whitespace, Name.Function)),

# Event and Struct
(r'(event|struct|interface|log)(\s+)([a-zA-Z_][a-zA-Z0-9_]*)',
bygroups(Keyword, Whitespace, Name.Class)),

# Imports
(r'(from)(\s+)(vyper\.\w+)(\s+)(import)(\s+)(\w+)',
bygroups(Keyword, Whitespace, Name.Namespace, Whitespace,
Keyword, Whitespace, Name.Class)),

# Numeric Literals
(r'\b0x[0-9a-fA-F]+\b', Number.Hex),
(r'\b(\d{1,3}(?:_\d{3})*|\d+)\b', Number.Integer),
(r'\b\d+\.\d*\b', Number.Float),

# Keywords
(words(('def', 'event', 'pass', 'return', 'for', 'while', 'if', 'elif',
'else', 'assert', 'raise', 'import', 'in', 'struct', 'implements',
'interface', 'from', 'indexed', 'log'),
prefix=r'\b', suffix=r'\b'), Keyword),

# Visibility and State Mutability
(words(('public', 'private', 'view', 'pure', 'constant',
'immutable', 'nonpayable'), prefix=r'\b', suffix=r'\b'),
Keyword.Declaration),

# Built-in Functions
(words(('bitwise_and', 'bitwise_not', 'bitwise_or', 'bitwise_xor', 'shift',
'create_minimal_proxy_to', 'create_copy_of', 'create_from_blueprint',
'ecadd', 'ecmul', 'ecrecover', 'keccak256', 'sha256', 'concat', 'convert',
'uint2str', 'extract32', 'slice', 'abs', 'ceil', 'floor', 'max', 'max_value',
'min', 'min_value', 'pow_mod256', 'sqrt', 'isqrt', 'uint256_addmod',
'uint256_mulmod', 'unsafe_add', 'unsafe_sub', 'unsafe_mul', 'unsafe_div',
'as_wei_value', 'blockhash', 'empty', 'len', 'method_id', '_abi_encode',
'_abi_decode', 'print', 'range'), prefix=r'\b', suffix=r'\b'),
Name.Builtin),

# Built-in Variables and Attributes
(words(('msg.sender', 'msg.value', 'block.timestamp', 'block.number', 'msg.gas'),
prefix=r'\b', suffix=r'\b'),
Name.Builtin.Pseudo),

(words(('uint', 'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256',
'int', 'int8', 'int16', 'int32', 'int64', 'int128', 'int256', 'bool',
'decimal', 'bytes', 'bytes1', 'bytes2', 'bytes3', 'bytes4', 'bytes5',
'bytes6', 'bytes7', 'bytes8', 'bytes9', 'bytes10', 'bytes11',
'bytes12', 'bytes13', 'bytes14', 'bytes15', 'bytes16', 'bytes17',
'bytes18', 'bytes19', 'bytes20', 'bytes21', 'bytes22', 'bytes23',
'bytes24', 'bytes25', 'bytes26', 'bytes27', 'bytes28', 'bytes29',
'bytes30', 'bytes31', 'bytes32', 'string', 'String', 'address',
'enum', 'struct'), prefix=r'\b', suffix=r'\b'),
Keyword.Type),

# indexed keywords
(r'\b(indexed)\b(\s*)(\()(\s*)(\w+)(\s*)(\))',
bygroups(Keyword, Whitespace, Punctuation, Whitespace,
Keyword.Type, Punctuation)),

# Operators and Punctuation
(r'(\+|\-|\*|\/|<=?|>=?|==|!=|=|\||&|%)', Operator),
(r'[.,:;()\[\]{}]', Punctuation),

# Other variable names and types
(r'@[\w.]+', Name.Decorator),
(r'__\w+__', Name.Magic), # Matches double underscores followed by word characters
(r'EMPTY_BYTES32', Name.Constant),
(r'\bERC20\b', Name.Class),
(r'\bself\b', Name.Attribute),

(r'Bytes\[\d+\]', Keyword.Type),

# Generic names and variables
(r'\b[a-zA-Z_]\w*\b:', Name.Variable),
(r'\b[a-zA-Z_]\w*\b', Name),

],

'multiline-comment': [
(r'\"\"\"', Comment.Multiline, '#pop'),
(r'[^"]+', Comment.Multiline),
(r'\"', Comment.Multiline)
],

'single-string': [
(r"[^\\']+", String.Single),
(r"'", String.Single, '#pop'),
(r'\\.', String.Escape),
],

'double-string': [
(r'[^\\"]+', String.Double),
(r'"', String.Double, '#pop'),
(r'\\.', String.Escape),
]
}