Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit eee4803

Browse files
committed
Various fixes/missing fields
- Added `name` field to `TypeDefinition` nodes - Added `isVirtual` and `isOverride` fields to `ModifierDefinition` nodes - Added support for parsing for `FunctionCallOptions` (of the form `fn{key=value}(args)`) - Added support for parsing `IndexRangeAccess`es (of the form `array[from:to]`) - Fixed parser discarding all variable declarations in a variable declaration list if one is `None` - Fixed parser failing if an assembly member's identifier is a list - Fixed parser failing an assembly assignment to a member - Added proper nodes for the `ReturnStatement`, `BreakStatement`, and `ContinueStatement`
1 parent 5b0977c commit eee4803

File tree

1 file changed

+84
-15
lines changed

1 file changed

+84
-15
lines changed

‎solidity_parser/parser.py

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def visitTypeDefinition(self, ctx):
128128
return Node(ctx=ctx,
129129
type="TypeDefinition",
130130
typeKeyword=ctx.TypeKeyword().getText(),
131+
name=ctx.identifier().getText(),
131132
elementaryTypeName=self.visit(ctx.elementaryTypeName()))
132133

133134

@@ -467,7 +468,9 @@ def visitModifierDefinition(self, ctx):
467468
type='ModifierDefinition',
468469
name=ctx.identifier().getText(),
469470
parameters=parameters,
470-
body=self.visit(ctx.block()))
471+
body=self.visit(ctx.block()),
472+
isVirtual=ctx.VirtualKeyword() is not None,
473+
isOverride=ctx.overrideSpecifier() is not None)
471474

472475
def visitStatement(self, ctx):
473476
return self.visit(ctx.getChild(0))
@@ -485,6 +488,13 @@ def visitRevertStatement(self, ctx):
485488
type='RevertStatement',
486489
functionCall=self.visit(ctx.functionCall()))
487490

491+
def _index_of_child(self, ctx, child):
492+
for i in range(0, len(ctx.children)):
493+
if ctx.getChild(i).getText() == child:
494+
return i
495+
496+
return None
497+
488498
def visitExpression(self, ctx):
489499

490500
children_length = len(ctx.children)
@@ -599,12 +609,28 @@ def visitExpression(self, ctx):
599609
arguments=args,
600610
names=names)
601611

602-
if ctx.getChild(1).getText() == '[' and ctx.getChild(3).getText() == ']':
612+
if (ctx.getChild(1).getText() == '[' and
613+
ctx.getChild(2).getText() != ':' and
614+
ctx.getChild(3).getText() == ']'):
603615
return Node(ctx=ctx,
604616
type='IndexAccess',
605617
base=self.visit(ctx.getChild(0)),
606618
index=self.visit(ctx.getChild(2)))
607619

620+
if ctx.getChild(1).getText() == '{' and ctx.getChild(3).getText() == '}':
621+
args = []
622+
names = []
623+
624+
for nameValue in ctx.nameValueList().nameValue():
625+
args.append(self.visit(nameValue.expression()))
626+
names.append(nameValue.identifier().getText())
627+
628+
return Node(ctx=ctx,
629+
type='FunctionCallOptions',
630+
expression=self.visit(ctx.getChild(0)),
631+
arguments=args,
632+
names=names)
633+
608634
elif children_length == 5:
609635
# ternary
610636
if ctx.getChild(1).getText() == '?' and ctx.getChild(3).getText() == ':':
@@ -614,6 +640,30 @@ def visitExpression(self, ctx):
614640
TrueExpression=self.visit(ctx.getChild(2)),
615641
FalseExpression=self.visit(ctx.getChild(4)))
616642

643+
if 4 <= children_length <= 6 and ctx.getChild(1).getText() == '[':
644+
left_bracket_index = self._index_of_child(ctx, '[')
645+
colon_index = self._index_of_child(ctx, ':')
646+
right_bracket_index = self._index_of_child(ctx, ']')
647+
648+
if (left_bracket_index == 1 and
649+
left_bracket_index < colon_index <= left_bracket_index + 2 and
650+
colon_index < right_bracket_index <= colon_index + 2 and
651+
right_bracket_index == children_length - 1):
652+
indexLower = None
653+
indexUpper = None
654+
655+
if colon_index == left_bracket_index + 2:
656+
indexLower = self.visit(ctx.getChild(left_bracket_index + 1))
657+
658+
if right_bracket_index == colon_index + 2:
659+
indexUpper = self.visit(ctx.getChild(colon_index + 1))
660+
661+
return Node(ctx=ctx,
662+
type='IndexRangeAccess',
663+
base=self.visit(ctx.getChild(0)),
664+
indexLower=indexLower,
665+
indexUpper=indexUpper)
666+
617667
return self.visit(list(ctx.getChildren()))
618668

619669

@@ -741,16 +791,16 @@ def visitIdentifierList(self, ctx: SolidityParser.IdentifierListContext):
741791
def visitVariableDeclarationList(self, ctx: SolidityParser.VariableDeclarationListContext):
742792
result = []
743793
for decl in self._mapCommasToNulls(ctx.children):
744-
if decl == None:
745-
return None
746-
747-
result.append(self._createNode(ctx=ctx,
748-
type='VariableDeclaration',
749-
name=decl.identifier().getText(),
750-
typeName=self.visit(decl.typeName()),
751-
isStateVar=False,
752-
isIndexed=False,
753-
decl=decl))
794+
if decl is None:
795+
result.append(None)
796+
else:
797+
result.append(self._createNode(ctx=ctx,
798+
type='VariableDeclaration',
799+
name=decl.identifier().getText(),
800+
typeName=self.visit(decl.typeName()),
801+
isStateVar=False,
802+
isIndexed=False,
803+
decl=decl))
754804

755805
return result
756806

@@ -845,9 +895,16 @@ def visitAssemblyExpression(self, ctx):
845895
return self.visit(ctx.getChild(0))
846896

847897
def visitAssemblyMember(self, ctx):
898+
identifier = ctx.identifier()
899+
900+
if isinstance(identifier, list):
901+
name = [n.getText() for n in identifier]
902+
else:
903+
name = identifier.getText()
904+
848905
return Node(ctx=ctx,
849906
type='AssemblyMember',
850-
name=ctx.identifier().getText())
907+
name=name)
851908

852909
def visitAssemblyCall(self, ctx):
853910
functionName = ctx.getChild(0).getText()
@@ -935,8 +992,10 @@ def visitAssemblyAssignment(self, ctx):
935992

936993
if names.identifier():
937994
names = [self.visit(names.identifier())]
938-
else:
995+
elif names.assemblyIdentifierList():
939996
names = self.visit(names.assemblyIdentifierList().identifier())
997+
else:
998+
names = self.visit(names.assemblyMember())
940999

9411000
return Node(ctx=ctx,
9421001
type='AssemblyAssignment',
@@ -1016,7 +1075,17 @@ def visitUserDefinedTypename(self, ctx):
10161075
name=ctx.getText())
10171076

10181077
def visitReturnStatement(self, ctx):
1019-
return self.visit(ctx.expression())
1078+
return Node(ctx=ctx,
1079+
type="ReturnStatement",
1080+
expression=self.visit(ctx.expression()))
1081+
1082+
def visitBreakStatement(self, ctx):
1083+
return Node(ctx=ctx,
1084+
type="BreakStatement")
1085+
1086+
def visitContinueStatement(self, ctx):
1087+
return Node(ctx=ctx,
1088+
type="ContinueStatement")
10201089

10211090
def visitTerminal(self, ctx):
10221091
return ctx.getText()

0 commit comments

Comments
 (0)