You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromtypingimportTuple, ListfrompathlibimportPathimportrefromCryptodome.HashimportBLAKE2bfrom .solidity_importimportSolidityImportExprfrom .solidity_versionimport (
SolidityVersionExpr,
SolidityVersionRange,
SolidityVersionRanges,
)
# TODO Raise error on `pragma` or `import` in contract definition# Instead of parsing `pragma solidity` and `import` statements inside a contract definition, we should raise an exception.# example:# ```# contract b {# pragma solidity ^0.8.0;# }# ```# assignees: michprevclassSoliditySourceParser:
PRAGMA_SOLIDITY_RE=re.compile(r"pragma\s+solidity\s+(?P<version>[^;]+)\s*;")
IMPORT_RE=re.compile(r"import\s*(?P<import>[^;]+)\s*;")
ONELINE_COMMENT_RE=re.compile(r"//.*$", re.MULTILINE)
MULTILINE_COMMENT_RE=re.compile(r"/\*.*?\*/", re.DOTALL)
@classmethoddef__string_closed(cls, line: str) ->bool:
opening_char=Noneforiinrange(len(line)):
ifopening_charisNone:
ifline[i] in {'"', "'"}:
opening_char=line[i]
else:
ifline[i] ==opening_char:
ifi>0andline[i-1] =="\\":
continueelse:
opening_char=Nonereturnopening_charisNone@classmethoddef__parse_version_pragma(cls, source_code: str) ->SolidityVersionRanges:
versions=Nonematches=cls.PRAGMA_SOLIDITY_RE.finditer(source_code)
formatchinmatches:
s=source_code[0 : match.start()].splitlines()
iflen(s) >0:
# ignore pragmas in a stringifnotcls.__string_closed(s[-1]):
continueversion_str=match.groupdict()["version"]
version_expr=SolidityVersionExpr(version_str)
ifversionsisNone:
versions=version_expr.version_rangeselse:
# in case of multiple version pragmas in a single file, intersection is performedversions &= version_expr.version_ranges# any version can be used when no pragma solidity presentifversionsisNone:
versions=SolidityVersionRanges(
[SolidityVersionRange("0.0.0", True, None, None)]
)
returnversions@classmethoddef__parse_import(cls, source_code: str) ->List[str]:
imports=set() # avoid listing the same import multiple timesmatches=cls.IMPORT_RE.finditer(source_code)
formatchinmatches:
s=source_code[0 : match.start()].splitlines()
iflen(s) >0:
# ignore imports in a stringifnotcls.__string_closed(s[-1]):
continueimport_str=match.groupdict()["import"]
import_expr=SolidityImportExpr(import_str)
imports.add(import_expr.filename)
returnlist(imports)
@classmethoddefparse(cls, path: Path) ->Tuple[SolidityVersionRanges, List[str], bytes]:
""" Return a tuple of two lists. The first list contains Solidity version ranges that can be used to compile the given file. The second list contains filenames / URLs that are imported from the given file. """raw_content=path.read_bytes()
content=raw_content.decode("utf-8")
h=BLAKE2b.new(data=raw_content, digest_bits=256)
# strip all commentscontent=cls.ONELINE_COMMENT_RE.sub("", content)
content=cls.MULTILINE_COMMENT_RE.sub("", content)
return (
cls.__parse_version_pragma(content),
cls.__parse_import(content),
h.digest(),
)
03bf24498f0d4422177b29c43b6550c76710ce25
The text was updated successfully, but these errors were encountered:
Raise error on
pragma
orimport
in contract definitionInstead of parsing
pragma solidity
andimport
statements inside a contract definition, we should raise an exception.example:
https://github.com/Ackee-Blockchain/woke/blob/0d27de25720142beb9619a89619b7a94c3556af1/woke/woke/c_regex_parsing/solidity_parser.py#L14
03bf24498f0d4422177b29c43b6550c76710ce25
The text was updated successfully, but these errors were encountered: