Skip to content

Commit

Permalink
Merge pull request #8 from OpenZeppelin/fix-incorrect-address-payable…
Browse files Browse the repository at this point in the history
…-in-function-returns-part-parsing

Incorrect `address payable` in `function definition` `returns` part parsing
  • Loading branch information
0xGeorgii authored Feb 2, 2024
2 parents 1003de0 + 94a36bd commit 78df0ec
Show file tree
Hide file tree
Showing 8 changed files with 824 additions and 771 deletions.
4 changes: 2 additions & 2 deletions Solidity.g4
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ variableDeclaration
: typeName storageLocation? identifier ;

typeName
: elementaryTypeName
: elementaryTypeName stateMutability?
| userDefinedTypeName
| mapping
| typeName '[' expression? ']'
| functionTypeName
| 'address' 'payable' ;
| 'address';

userDefinedTypeName
: identifier ( '.' identifier )* ;
Expand Down
12 changes: 4 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ build-backend = "setuptools.build_meta"

[project]
name = "solidity-grammar-parser"
version = "0.0.2"
authors = [
{ name="Georgii Plotnikov", email="[email protected]" },
]
version = "0.0.3"
authors = [{ name = "Georgii Plotnikov", email = "[email protected]" }]
description = "Solidity ANTLR4 grammar Python parser"
readme = "README.md"
requires-python = ">=3.10"
Expand All @@ -21,7 +19,7 @@ dependencies = [
"coverage == 7.3.1",
"simplejson == 3.19.1",
"typing == 3.7.4.3",
"typing_extensions == 4.8.0"
"typing_extensions == 4.8.0",
]

[project.urls]
Expand All @@ -30,9 +28,7 @@ dependencies = [

[tool.setuptools.packages.find]
where = ["."]
include = [
"sgp*",
]
include = ["sgp*"]

[tool.black]
line-length = 88
Expand Down
2 changes: 1 addition & 1 deletion sgp/parser/Solidity.interp

Large diffs are not rendered by default.

1,513 changes: 761 additions & 752 deletions sgp/parser/SolidityParser.py

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions test/test_misc/test_function_returns_address_payable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import unittest

from sgp.sgp_parser import parse


class TestFunctionReturnsAddressPayable(unittest.TestCase):
def test_function_returns_address_payable(self) -> None:
input = """function test() public returns(address payable) {}"""
ast = parse(input)
self.assertIsNotNone(ast)
self.assertEqual(1, len(ast.children))
self.assertEqual("FunctionDefinition", ast.children[0].type)
self.assertEqual(1, len(ast.children[0].return_parameters))
self.assertEqual(
"VariableDeclaration", ast.children[0].return_parameters[0].type
)
self.assertIsNone(ast.children[0].return_parameters[0].name)
self.assertIsNotNone(ast.children[0].return_parameters[0].type_name)
self.assertEqual(
"ElementaryTypeName", ast.children[0].return_parameters[0].type_name.type
)
self.assertEqual("address", ast.children[0].return_parameters[0].type_name.name)
self.assertEqual(
"payable", ast.children[0].return_parameters[0].type_name.state_mutability
)

def test_function_returns_address(self) -> None:
input = """function test() public returns(address) {}"""
ast = parse(input)
self.assertIsNotNone(ast)
self.assertEqual(1, len(ast.children))
self.assertEqual("FunctionDefinition", ast.children[0].type)
self.assertEqual(1, len(ast.children[0].return_parameters))
self.assertEqual(
"VariableDeclaration", ast.children[0].return_parameters[0].type
)
self.assertIsNone(ast.children[0].return_parameters[0].name)
self.assertIsNotNone(ast.children[0].return_parameters[0].type_name)
self.assertEqual(
"ElementaryTypeName", ast.children[0].return_parameters[0].type_name.type
)
self.assertEqual("address", ast.children[0].return_parameters[0].type_name.name)
self.assertIsNone(
ast.children[0].return_parameters[0].type_name.state_mutability
)
2 changes: 1 addition & 1 deletion test/test_misc/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class TestMisc(unittest.TestCase):
def test_misc(self) -> None:
input = """contract add_your_code_here { }"""
input = """function add_your_solidity_code_here {}"""

ast = parse(input)
self.assertIsNotNone(ast)
2 changes: 1 addition & 1 deletion test/test_parsing/result.json

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions test/test_parsing/test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ contract FallbackWithArgs {
* Issues
*/

// issue #12
// issue https://github.com/solidity-parser/parser/issues/12
contract C {
function f() public {
assembly {
Expand All @@ -1005,7 +1005,7 @@ contract C {
}
}

// issue #54
// issue https://github.com/solidity-parser/parser/issues/54
contract Foo {
function f() public {
(uint[][] memory x, uint y) = abi.decode(data, (uint[][], uint));
Expand All @@ -1015,15 +1015,15 @@ contract Foo {
}
}

// issue #55
// issue https://github.com/solidity-parser/parser/issues/55
pragma solidity *;

// issue #59
// issue https://github.com/solidity-parser/parser/issues/59
contract C {
using L.Lib for uint;
}

// issue #60
// issue https://github.com/solidity-parser/parser/issues/60
contract AssemblyAssingment {
function foo() public pure {
assembly {
Expand All @@ -1039,7 +1039,7 @@ contract AssemblyAssingment {
}
}

// issue #61
// issue https://github.com/solidity-parser/parser/issues/61
type Fixed18 is int256;
using Fixed18Lib for Fixed18 global;
using {plusOne, minusOne} for RestrictedNumber global;
Expand Down Expand Up @@ -1071,3 +1071,6 @@ contract NamedMappingParams {
// solc 0.8.19, user defined operators
using { add as + } for Fixed18 global;
using { add as +, sub as - } for Fixed18 global;

// https://github.com/OpenZeppelin/sgp/issues/7
function test() public returns(address payable) {}

0 comments on commit 78df0ec

Please sign in to comment.