Python solidity解析器无法处理throw关键字

Python solidity解析器无法处理throw关键字,python,json,parsing,antlr,solidity,Python,Json,Parsing,Antlr,Solidity,我尝试使用python solidity parser()解析以下代码 它产生以下输出 {'children': [{'name': 'solidity', 'type': 'PragmaDirective', 'value': '^0.4.13'}, {'baseContracts': [], 'kind': 'contract', 'name':

我尝试使用python solidity parser()解析以下代码

它产生以下输出

{'children': [{'name': 'solidity',
               'type': 'PragmaDirective',
               'value': '^0.4.13'},
              {'baseContracts': [],
               'kind': 'contract',
               'name': 'someContract',
               'subNodes': [{'initialValue': None,
                             'type': 'StateVariableDeclaration',
                             'variables': [{'expression': None,
                                            'isDeclaredConst': False,
                                            'isIndexed': False,
                                            'isStateVar': True,
                                            'name': 'balances',
                                            'type': 'VariableDeclaration',
                                            'typeName': {'keyType': {'name': 'address',
                                                                     'type': 'ElementaryTypeName'},
                                                         'type': 'Mapping',
                                                         'valueType': {'name': 'uint',
                                                                       'type': 'ElementaryTypeName'}},
                                            'visibility': 'default'}]},
                            {'body': {'statements': [{'expression': {'left': {'base': {'name': 'balances',
                                                                                       'type': 'Identifier'},
                                                                              'index': {'expression': {'name': 'msg',
                                                                                                       'type': 'Identifier'},
                                                                                        'memberName': 'sender',
                                                                                        'type': 'MemberAccess'},
                                                                              'type': 'IndexAccess'},
                                                                     'operator': '+=',
                                                                     'right': {'expression': {'name': 'msg',
                                                                                              'type': 'Identifier'},
                                                                               'memberName': 'value',
                                                                               'type': 'MemberAccess'},
                                                                     'type': 'BinaryOperation'},
                                                      'type': 'ExpressionStatement'}],
                                      'type': 'Block'},
                             'isConstructor': False,
                             'modifiers': [],
                             'name': 'deposit',
                             'parameters': {'parameters': [],
                                            'type': 'ParameterList'},
                             'returnParameters': [],
                             'stateMutability': 'payable',
                             'type': 'FunctionDefinition',
                             'visibility': 'default'},
                            {'body': {'statements': [{'FalseBody': None,
                                                      'TrueBody': {'statements': [{'expression': {'left': {'base': {'name': 'balances',
                                                                                                                    'type': 'Identifier'},
                                                                                                           'index': {'expression': {'name': 'msg',
                                                                                                                                    'type': 'Identifier'},
                                                                                                                     'memberName': 'sender',
                                                                                                                     'type': 'MemberAccess'},
                                                                                                           'type': 'IndexAccess'},
                                                                                                  'operator': '-=',
                                                                                                  'right': {'name': 'amount',
                                                                                                            'type': 'Identifier'},
                                                                                                  'type': 'BinaryOperation'},
                                                                                   'type': 'ExpressionStatement'},
                                                                                  {'FalseBody': None,
                                                                                   'TrueBody': {'statements': [';'],
                                                                                                'type': 'Block'},
                                                                                   'condition': {'isPrefix': True,
                                                                                                 'operator': '!',
                                                                                                 'subExpression': {'arguments': [{'name': 'amount',
                                                                                                                                  'type': 'Identifier'}],
                                                                                                                   'expression': {'expression': {'expression': {'name': 'msg',
                                                                                                                                                                'type': 'Identifier'},
                                                                                                                                                 'memberName': 'sender',
                                                                                                                                                 'type': 'MemberAccess'},
                                                                                                                                  'memberName': 'send',
                                                                                                                                  'type': 'MemberAccess'},
                                                                                                                   'names': [],
                                                                                                                   'type': 'FunctionCall'},
                                                                                                 'type': 'UnaryOperation'},
                                                                                   'type': 'IfStatement'}],
                                                                   'type': 'Block'},
                                                      'condition': {'left': {'base': {'name': 'balances',
                                                                                      'type': 'Identifier'},
                                                                             'index': {'expression': {'name': 'msg',
                                                                                                      'type': 'Identifier'},
                                                                                       'memberName': 'sender',
                                                                                       'type': 'MemberAccess'},
                                                                             'type': 'IndexAccess'},
                                                                    'operator': '>=',
                                                                    'right': {'name': 'amount',
                                                                              'type': 'Identifier'},
                                                                    'type': 'BinaryOperation'},
                                                      'type': 'IfStatement'}],
                                      'type': 'Block'},
                             'isConstructor': False,
                             'modifiers': [],
                             'name': 'withdrawOkayish',
                             'parameters': {'parameters': [{'isIndexed': False,
                                                            'isStateVar': False,
                                                            'name': 'amount',
                                                            'storageLocation': None,
                                                            'type': 'Parameter',
                                                            'typeName': {'name': 'uint',
                                                                         'type': 'ElementaryTypeName'}}],
                                            'type': 'ParameterList'},
                             'returnParameters': [],
                             'stateMutability': None,
                             'type': 'FunctionDefinition',
                             'visibility': 'default'},
                            {'body': {'statements': [{'FalseBody': None,
                                                      'TrueBody': {'statements': [{'expression': {'left': {'base': {'name': 'balances',
                                                                                                                    'type': 'Identifier'},
                                                                                                           'index': {'expression': {'name': 'msg',
                                                                                                                                    'type': 'Identifier'},
                                                                                                                     'memberName': 'sender',
                                                                                                                     'type': 'MemberAccess'},
                                                                                                           'type': 'IndexAccess'},
                                                                                                  'operator': '-=',
                                                                                                  'right': {'name': 'amount',
                                                                                                            'type': 'Identifier'},
                                                                                                  'type': 'BinaryOperation'},
                                                                                   'type': 'ExpressionStatement'},
                                                                                  {'FalseBody': None,
                                                                                   'TrueBody': {'statements': [';'],
                                                                                                'type': 'Block'},
                                                                                   'condition': {'isPrefix': True,
                                                                                                 'operator': '!',
                                                                                                 'subExpression': {'arguments': [],
                                                                                                                   'expression': {'arguments': [{'name': 'amount',
                                                                                                                                                 'type': 'Identifier'}],
                                                                                                                                  'expression': {'expression': {'arguments': [{'number': '2500000',
                                                                                                                                                                               'subdenomination': None,
                                                                                                                                                                               'type': 'NumberLiteral'}],
                                                                                                                                                                'expression': {'expression': {'expression': {'expression': {'name': 'msg',
                                                                                                                                                                                                                            'type': 'Identifier'},
                                                                                                                                                                                                             'memberName': 'sender',
                                                                                                                                                                                                             'type': 'MemberAccess'},
                                                                                                                                                                                              'memberName': 'call',
                                                                                                                                                                                              'type': 'MemberAccess'},
                                                                                                                                                                               'memberName': 'gas',
                                                                                                                                                                               'type': 'MemberAccess'},
                                                                                                                                                                'names': [],
                                                                                                                                                                'type': 'FunctionCall'},
                                                                                                                                                 'memberName': 'value',
                                                                                                                                                 'type': 'MemberAccess'},
                                                                                                                                  'names': [],
                                                                                                                                  'type': 'FunctionCall'},
                                                                                                                   'names': [],
                                                                                                                   'type': 'FunctionCall'},
                                                                                                 'type': 'UnaryOperation'},
                                                                                   'type': 'IfStatement'}],
                                                                   'type': 'Block'},
                                                      'condition': {'left': {'base': {'name': 'balances',
                                                                                      'type': 'Identifier'},
                                                                             'index': {'expression': {'name': 'msg',
                                                                                                      'type': 'Identifier'},
                                                                                       'memberName': 'sender',
                                                                                       'type': 'MemberAccess'},
                                                                             'type': 'IndexAccess'},
                                                                    'operator': '>=',
                                                                    'right': {'name': 'amount',
                                                                              'type': 'Identifier'},
                                                                    'type': 'BinaryOperation'},
                                                      'type': 'IfStatement'}],
                                      'type': 'Block'},
                             'isConstructor': False,
                             'modifiers': [],
                             'name': 'withdrawBad2',
                             'parameters': {'parameters': [{'isIndexed': False,
                                                            'isStateVar': False,
                                                            'name': 'amount',
                                                            'storageLocation': None,
                                                            'type': 'Parameter',
                                                            'typeName': {'name': 'uint',
                                                                         'type': 'ElementaryTypeName'}}],
                                            'type': 'ParameterList'},
                             'returnParameters': [],
                             'stateMutability': None,
                             'type': 'FunctionDefinition',
                             'visibility': 'default'}],
               'type': 'ContractDefinition'}],
 'type': 'SourceUnit'}
我发现它不能处理throw关键字。如果大小写块只包含“;”,则两者都包含。 我找不到解决这个问题的办法。有没有办法解决这个问题

此处创建问题:

(仍然…不是Python程序员)

我花了相当多的时间来修改。(例如,在我的系统上,
scripts/antlr4.sh
(顺便说一句,文档说
script/antlr4
,所以…有一个很好的开始。)执行不正确)

也就是说,在
solidity\u antlr4
目录中有一个
parser.py
源文件

它缺少一个
visitThrowStatement
方法

我补充说:

def visitThrowStatement(self,ctx):
返回节点(ctx=ctx,
type='ThrowStatement')
在其他
访问中*
def
s

在将我的代码移动到站点包后(告诉ya…不是Python程序,而且肯定是在这里进行黑客攻击,所以我肯定不是实现此更改的正确方法)

运行
python3-m solidity\u parser parse./samples/so\u example.sol>testParse.txt
(其中
/samples/so\u example.sol
是您的示例输入)的输出是:

相关摘录示例:

 'TrueBody': {'statements': [{'type': 'ThrowStatement'}],
                                                                                                'type': 'Block'},
我甚至不知道这是否是正确的内容,但它确实证明了解决方案将是通过实现
visitThrowStatement
来重建


(如果没有,可能会让我怀疑其他访问者方法也可能丢失。)

这里不是python的人,但我确实花了一点时间引入语法并根据您的输入运行它。我可以确认该项目中使用的ANTLR语法确实找到了
throw
语句,并将其包含在它的解析树中。因此,问题将位于ANTLR语法之外的其他地方。非常感谢…问题已解决
{'children': [{'name': 'solidity',
               'type': 'PragmaDirective',
               'value': '^0.4.13'},
              {'baseContracts': [],
               'kind': 'contract',
               'name': 'someContract',
               'subNodes': [{'initialValue': None,
                             'type': 'StateVariableDeclaration',
                             'variables': [{'expression': None,
                                            'isDeclaredConst': False,
                                            'isIndexed': False,
                                            'isStateVar': True,
                                            'name': 'balances',
                                            'type': 'VariableDeclaration',
                                            'typeName': {'keyType': {'name': 'address',
                                                                     'type': 'ElementaryTypeName'},
                                                         'type': 'Mapping',
                                                         'valueType': {'name': 'uint',
                                                                       'type': 'ElementaryTypeName'}},
                                            'visibility': 'default'}]},
                            {'body': {'statements': [{'expression': {'left': {'base': {'name': 'balances',
                                                                                       'type': 'Identifier'},
                                                                              'index': {'expression': {'name': 'msg',
                                                                                                       'type': 'Identifier'},
                                                                                        'memberName': 'sender',
                                                                                        'type': 'MemberAccess'},
                                                                              'type': 'IndexAccess'},
                                                                     'operator': '+=',
                                                                     'right': {'expression': {'name': 'msg',
                                                                                              'type': 'Identifier'},
                                                                               'memberName': 'value',
                                                                               'type': 'MemberAccess'},
                                                                     'type': 'BinaryOperation'},
                                                      'type': 'ExpressionStatement'}],
                                      'type': 'Block'},
                             'isConstructor': False,
                             'modifiers': [],
                             'name': 'deposit',
                             'parameters': {'parameters': [],
                                            'type': 'ParameterList'},
                             'returnParameters': [],
                             'stateMutability': 'payable',
                             'type': 'FunctionDefinition',
                             'visibility': 'default'},
                            {'body': {'statements': [{'FalseBody': None,
                                                      'TrueBody': {'statements': [{'expression': {'left': {'base': {'name': 'balances',
                                                                                                                    'type': 'Identifier'},
                                                                                                           'index': {'expression': {'name': 'msg',
                                                                                                                                    'type': 'Identifier'},
                                                                                                                     'memberName': 'sender',
                                                                                                                     'type': 'MemberAccess'},
                                                                                                           'type': 'IndexAccess'},
                                                                                                  'operator': '-=',
                                                                                                  'right': {'name': 'amount',
                                                                                                            'type': 'Identifier'},
                                                                                                  'type': 'BinaryOperation'},
                                                                                   'type': 'ExpressionStatement'},
                                                                                  {'FalseBody': None,
                                                                                   'TrueBody': {'statements': [{'type': 'ThrowStatement'}],
                                                                                                'type': 'Block'},
                                                                                   'condition': {'isPrefix': True,
                                                                                                 'operator': '!',
                                                                                                 'subExpression': {'arguments': [{'name': 'amount',
                                                                                                                                  'type': 'Identifier'}],
                                                                                                                   'expression': {'expression': {'expression': {'name': 'msg',
                                                                                                                                                                'type': 'Identifier'},
                                                                                                                                                 'memberName': 'sender',
                                                                                                                                                 'type': 'MemberAccess'},
                                                                                                                                  'memberName': 'send',
                                                                                                                                  'type': 'MemberAccess'},
                                                                                                                   'names': [],
                                                                                                                   'type': 'FunctionCall'},
                                                                                                 'type': 'UnaryOperation'},
                                                                                   'type': 'IfStatement'}],
                                                                   'type': 'Block'},
                                                      'condition': {'left': {'base': {'name': 'balances',
                                                                                      'type': 'Identifier'},
                                                                             'index': {'expression': {'name': 'msg',
                                                                                                      'type': 'Identifier'},
                                                                                       'memberName': 'sender',
                                                                                       'type': 'MemberAccess'},
                                                                             'type': 'IndexAccess'},
                                                                    'operator': '>=',
                                                                    'right': {'name': 'amount',
                                                                              'type': 'Identifier'},
                                                                    'type': 'BinaryOperation'},
                                                      'type': 'IfStatement'}],
                                      'type': 'Block'},
                             'isConstructor': False,
                             'modifiers': [],
                             'name': 'withdrawOkayish',
                             'parameters': {'parameters': [{'isIndexed': False,
                                                            'isStateVar': False,
                                                            'name': 'amount',
                                                            'storageLocation': None,
                                                            'type': 'Parameter',
                                                            'typeName': {'name': 'uint',
                                                                         'type': 'ElementaryTypeName'}}],
                                            'type': 'ParameterList'},
                             'returnParameters': [],
                             'stateMutability': None,
                             'type': 'FunctionDefinition',
                             'visibility': 'default'},
                            {'body': {'statements': [{'FalseBody': None,
                                                      'TrueBody': {'statements': [{'expression': {'left': {'base': {'name': 'balances',
                                                                                                                    'type': 'Identifier'},
                                                                                                           'index': {'expression': {'name': 'msg',
                                                                                                                                    'type': 'Identifier'},
                                                                                                                     'memberName': 'sender',
                                                                                                                     'type': 'MemberAccess'},
                                                                                                           'type': 'IndexAccess'},
                                                                                                  'operator': '-=',
                                                                                                  'right': {'name': 'amount',
                                                                                                            'type': 'Identifier'},
                                                                                                  'type': 'BinaryOperation'},
                                                                                   'type': 'ExpressionStatement'},
                                                                                  {'FalseBody': None,
                                                                                   'TrueBody': {'statements': [{'type': 'ThrowStatement'}],
                                                                                                'type': 'Block'},
                                                                                   'condition': {'isPrefix': True,
                                                                                                 'operator': '!',
                                                                                                 'subExpression': {'arguments': [],
                                                                                                                   'expression': {'arguments': [{'name': 'amount',
                                                                                                                                                 'type': 'Identifier'}],
                                                                                                                                  'expression': {'expression': {'arguments': [{'number': '2500000',
                                                                                                                                                                               'subdenomination': None,
                                                                                                                                                                               'type': 'NumberLiteral'}],
                                                                                                                                                                'expression': {'expression': {'expression': {'expression': {'name': 'msg',
                                                                                                                                                                                                                            'type': 'Identifier'},
                                                                                                                                                                                                             'memberName': 'sender',
                                                                                                                                                                                                             'type': 'MemberAccess'},
                                                                                                                                                                                              'memberName': 'call',
                                                                                                                                                                                              'type': 'MemberAccess'},
                                                                                                                                                                               'memberName': 'gas',
                                                                                                                                                                               'type': 'MemberAccess'},
                                                                                                                                                                'names': [],
                                                                                                                                                                'type': 'FunctionCall'},
                                                                                                                                                 'memberName': 'value',
                                                                                                                                                 'type': 'MemberAccess'},
                                                                                                                                  'names': [],
                                                                                                                                  'type': 'FunctionCall'},
                                                                                                                   'names': [],
                                                                                                                   'type': 'FunctionCall'},
                                                                                                 'type': 'UnaryOperation'},
                                                                                   'type': 'IfStatement'}],
                                                                   'type': 'Block'},
                                                      'condition': {'left': {'base': {'name': 'balances',
                                                                                      'type': 'Identifier'},
                                                                             'index': {'expression': {'name': 'msg',
                                                                                                      'type': 'Identifier'},
                                                                                       'memberName': 'sender',
                                                                                       'type': 'MemberAccess'},
                                                                             'type': 'IndexAccess'},
                                                                    'operator': '>=',
                                                                    'right': {'name': 'amount',
                                                                              'type': 'Identifier'},
                                                                    'type': 'BinaryOperation'},
                                                      'type': 'IfStatement'}],
                                      'type': 'Block'},
                             'isConstructor': False,
                             'modifiers': [],
                             'name': 'withdrawBad2',
                             'parameters': {'parameters': [{'isIndexed': False,
                                                            'isStateVar': False,
                                                            'name': 'amount',
                                                            'storageLocation': None,
                                                            'type': 'Parameter',
                                                            'typeName': {'name': 'uint',
                                                                         'type': 'ElementaryTypeName'}}],
                                            'type': 'ParameterList'},
                             'returnParameters': [],
                             'stateMutability': None,
                             'type': 'FunctionDefinition',
                             'visibility': 'default'}],
               'type': 'ContractDefinition'}],
 'type': 'SourceUnit'}

 'TrueBody': {'statements': [{'type': 'ThrowStatement'}],
                                                                                                'type': 'Block'},