Skip to content

Commit

Permalink
add wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed Apr 8, 2024
1 parent 0a481c3 commit 6ed0436
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions vyper/ast/pre_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@
# seems a bit early to be importing this but we want it to validate the
# evm-version pragma
from vyper.evm.opcodes import EVM_VERSIONS
from vyper.exceptions import StructureException, SyntaxException, VersionException
from vyper.exceptions import StructureException, SyntaxException, VersionException, VyperException
from vyper.typing import ModificationOffsets, ParserPosition


def validate_version_pragma(version_str: str, code: str, start: ParserPosition) -> None:
def _validate_version_pragma(version_str: str) -> None:
"""
Validates a version pragma directive against the current compiler version.
"""
from vyper import __version__

lineno, col_offset = start
if len(version_str) == 0:
raise VersionException("Version specification cannot be empty", code, lineno, col_offset)
raise VyperException("Version specification cannot be empty")

# X.Y.Z or vX.Y.Z => ==X.Y.Z, ==vX.Y.Z
if re.match("[v0-9]", version_str):
Expand All @@ -34,23 +33,24 @@ def validate_version_pragma(version_str: str, code: str, start: ParserPosition)
try:
spec = SpecifierSet(version_str)
except InvalidSpecifier:
raise VersionException(
f'Version specification "{version_str}" is not a valid PEP440 specifier',
code,
lineno,
col_offset,
raise VyperException(
f'Version specification "{version_str}" is not a valid PEP440 specifier'
)

if not spec.contains(__version__, prereleases=True):
raise VersionException(
raise VyperException(
f'Version specification "{version_str}" is not compatible '
f'with compiler version "{__version__}"',
code,
lineno,
col_offset,
f'with compiler version "{__version__}"'
)


def validate_version(version_str: str, code: str, start: ParserPosition) -> None:
try:
_validate_version_pragma(version_str)
except VyperException as e:
raise VersionException(e.message, code, *start)


class ForParserState(enum.Enum):
NOT_RUNNING = enum.auto()
START_SOON = enum.auto()
Expand Down Expand Up @@ -182,7 +182,7 @@ def pre_parse(code: str) -> tuple[Settings, ModificationOffsets, dict, str]:
if settings.compiler_version is not None:
raise StructureException("compiler version specified twice!", start)
compiler_version = contents.removeprefix("@version ").strip()
validate_version_pragma(compiler_version, line, start)
validate_version(compiler_version, line, start)
settings.compiler_version = compiler_version

if contents.startswith("pragma "):
Expand All @@ -191,7 +191,7 @@ def pre_parse(code: str) -> tuple[Settings, ModificationOffsets, dict, str]:
if settings.compiler_version is not None:
raise StructureException("pragma version specified twice!", start)
compiler_version = pragma.removeprefix("version ").strip()
validate_version_pragma(compiler_version, line, start)
validate_version(compiler_version, line, start)
settings.compiler_version = compiler_version

elif pragma.startswith("optimize "):
Expand Down

0 comments on commit 6ed0436

Please sign in to comment.