Skip to content

Commit c6bb3c4

Browse files
committed
Add identifiers to name/value pairs
1 parent d596ae1 commit c6bb3c4

File tree

3 files changed

+171
-6
lines changed

3 files changed

+171
-6
lines changed

src/ASTBuilder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ export class ASTBuilder
746746
): AST.FunctionCall & WithMeta {
747747
let args: AST.Expression[] = []
748748
const names = []
749+
const identifiers = []
749750

750751
const ctxArgs = ctx.functionCallArguments()
751752
const ctxArgsExpressionList = ctxArgs.expressionList()
@@ -758,6 +759,7 @@ export class ASTBuilder
758759
for (const nameValue of ctxArgsNameValueList.nameValue()) {
759760
args.push(this.visitExpression(nameValue.expression()))
760761
names.push(this._toText(nameValue.identifier()))
762+
identifiers.push(this.visitIdentifier(nameValue.identifier()))
761763
}
762764
}
763765

@@ -766,6 +768,7 @@ export class ASTBuilder
766768
expression: this.visitExpression(ctx.expression()),
767769
arguments: args,
768770
names,
771+
identifiers,
769772
}
770773

771774
return this._addMeta(node, ctx)
@@ -1096,6 +1099,7 @@ export class ASTBuilder
10961099
) {
10971100
let args: AST.Expression[] = []
10981101
const names = []
1102+
const identifiers = []
10991103

11001104
const ctxArgs = ctx.functionCallArguments()!
11011105
if (ctxArgs.expressionList()) {
@@ -1107,6 +1111,7 @@ export class ASTBuilder
11071111
for (const nameValue of ctxArgs.nameValueList()!.nameValue()) {
11081112
args.push(this.visitExpression(nameValue.expression()))
11091113
names.push(this._toText(nameValue.identifier()))
1114+
identifiers.push(this.visitIdentifier(nameValue.identifier()))
11101115
}
11111116
}
11121117

@@ -1115,6 +1120,7 @@ export class ASTBuilder
11151120
expression: this.visitExpression(ctx.expression(0)),
11161121
arguments: args,
11171122
names,
1123+
identifiers,
11181124
}
11191125

11201126
return this._addMeta(node, ctx)
@@ -1229,16 +1235,19 @@ export class ASTBuilder
12291235
ctx: SP.NameValueListContext
12301236
): AST.NameValueList & WithMeta {
12311237
const names: string[] = []
1238+
const identifiers: AST.Identifier[] = []
12321239
const args: AST.Expression[] = []
12331240

12341241
for (const nameValue of ctx.nameValue()) {
12351242
names.push(this._toText(nameValue.identifier()))
1243+
identifiers.push(this.visitIdentifier(nameValue.identifier()))
12361244
args.push(this.visitExpression(nameValue.expression()))
12371245
}
12381246

12391247
const node: AST.NameValueList = {
12401248
type: 'NameValueList',
12411249
names,
1250+
identifiers,
12421251
arguments: args,
12431252
}
12441253

src/ast-types.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ export interface FunctionCall extends BaseASTNode {
337337
expression: Expression
338338
arguments: Expression[]
339339
names: string[]
340+
identifiers: Identifier[]
340341
}
341342
export interface AssemblyBlock extends BaseASTNode {
342343
type: 'AssemblyBlock'
@@ -498,7 +499,7 @@ export const binaryOpValues = [
498499
'/=',
499500
'%=',
500501
'|',
501-
'|='
502+
'|=',
502503
] as const
503504
export type BinOp = typeof binaryOpValues[number]
504505

@@ -559,6 +560,7 @@ export interface DecimalNumber extends BaseASTNode {
559560
export interface NameValueList extends BaseASTNode {
560561
type: 'NameValueList'
561562
names: string[]
563+
identifiers: Identifier[]
562564
arguments: Expression[]
563565
}
564566
export type ASTNode =
@@ -678,14 +680,17 @@ export type Statement =
678680
| TryStatement
679681
| RevertStatement
680682

681-
type ASTMap<U> = { [K in ASTNodeTypeString]: U extends { type: K } ? U : never };
682-
type ASTTypeMap = ASTMap<ASTNode>;
683+
type ASTMap<U> = { [K in ASTNodeTypeString]: U extends { type: K } ? U : never }
684+
type ASTTypeMap = ASTMap<ASTNode>
683685
type ASTVisitorEnter = {
684686
[K in keyof ASTTypeMap]?: (ast: ASTTypeMap[K], parent?: ASTNode) => any
685-
};
687+
}
686688
type ASTVisitorExit = {
687-
[K in keyof ASTTypeMap as `${K}:exit`]?: (ast: ASTTypeMap[K], parent?: ASTNode) => any
688-
};
689+
[K in keyof ASTTypeMap as `${K}:exit`]?: (
690+
ast: ASTTypeMap[K],
691+
parent?: ASTNode
692+
) => any
693+
}
689694

690695
export type ASTVisitor = ASTVisitorEnter & ASTVisitorExit
691696

test/ast.ts

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,35 @@ describe('AST', () => {
10761076
},
10771077
],
10781078
names: [],
1079+
identifiers: [],
1080+
},
1081+
})
1082+
})
1083+
1084+
it('EmitStatement with name/value pair', function () {
1085+
const ast: any = parseStatement('emit EventCalled({x : 1});')
1086+
assert.deepEqual(ast, {
1087+
type: 'EmitStatement',
1088+
eventCall: {
1089+
type: 'FunctionCall',
1090+
expression: {
1091+
type: 'Identifier',
1092+
name: 'EventCalled',
1093+
},
1094+
arguments: [
1095+
{
1096+
type: 'NumberLiteral',
1097+
number: '1',
1098+
subdenomination: null,
1099+
},
1100+
],
1101+
names: ['x'],
1102+
identifiers: [
1103+
{
1104+
type: 'Identifier',
1105+
name: 'x',
1106+
},
1107+
],
10791108
},
10801109
})
10811110
})
@@ -1221,6 +1250,7 @@ describe('AST', () => {
12211250
},
12221251
],
12231252
names: [],
1253+
identifiers: [],
12241254
},
12251255
returnParameters: [
12261256
{
@@ -1303,6 +1333,7 @@ describe('AST', () => {
13031333
},
13041334
],
13051335
names: [],
1336+
identifiers: [],
13061337
},
13071338
returnParameters: [
13081339
{
@@ -1413,6 +1444,7 @@ describe('AST', () => {
14131444
},
14141445
],
14151446
names: [],
1447+
identifiers: [],
14161448
},
14171449
returnParameters: [
14181450
{
@@ -1523,6 +1555,7 @@ describe('AST', () => {
15231555
},
15241556
],
15251557
names: [],
1558+
identifiers: [],
15261559
},
15271560
returnParameters: [
15281561
{
@@ -1949,6 +1982,63 @@ describe('AST', () => {
19491982
},
19501983
],
19511984
names: [],
1985+
identifiers: [],
1986+
})
1987+
expr = parseExpression('type(MyContract)')
1988+
assert.deepEqual(expr, {
1989+
type: 'FunctionCall',
1990+
expression: {
1991+
type: 'Identifier',
1992+
name: 'type',
1993+
},
1994+
arguments: [
1995+
{
1996+
type: 'Identifier',
1997+
name: 'MyContract',
1998+
},
1999+
],
2000+
names: [],
2001+
identifiers: [],
2002+
})
2003+
})
2004+
2005+
it('FunctionCall with name/value pairs', function () {
2006+
let expr = parseExpression('f{value: 10}(1, 2)')
2007+
assert.deepEqual(expr, {
2008+
type: 'FunctionCall',
2009+
expression: {
2010+
type: 'NameValueExpression',
2011+
expression: {
2012+
type: 'Identifier',
2013+
name: 'f',
2014+
},
2015+
arguments: {
2016+
type: 'NameValueList',
2017+
names: ['value'],
2018+
identifiers: [{ type: 'Identifier', name: 'value' }],
2019+
arguments: [
2020+
{
2021+
type: 'NumberLiteral',
2022+
number: '10',
2023+
subdenomination: null,
2024+
},
2025+
],
2026+
},
2027+
},
2028+
arguments: [
2029+
{
2030+
type: 'NumberLiteral',
2031+
number: '1',
2032+
subdenomination: null,
2033+
},
2034+
{
2035+
type: 'NumberLiteral',
2036+
number: '2',
2037+
subdenomination: null,
2038+
},
2039+
],
2040+
names: [],
2041+
identifiers: [],
19522042
})
19532043
expr = parseExpression('type(MyContract)')
19542044
assert.deepEqual(expr, {
@@ -1964,6 +2054,57 @@ describe('AST', () => {
19642054
},
19652055
],
19662056
names: [],
2057+
identifiers: [],
2058+
})
2059+
})
2060+
2061+
it('FunctionCall with name/value arguments', function () {
2062+
let expr = parseExpression('f({x: 1, y: 2})')
2063+
assert.deepEqual(expr, {
2064+
type: 'FunctionCall',
2065+
expression: {
2066+
type: 'Identifier',
2067+
name: 'f',
2068+
},
2069+
arguments: [
2070+
{
2071+
type: 'NumberLiteral',
2072+
number: '1',
2073+
subdenomination: null,
2074+
},
2075+
{
2076+
type: 'NumberLiteral',
2077+
number: '2',
2078+
subdenomination: null,
2079+
},
2080+
],
2081+
names: ['x', 'y'],
2082+
identifiers: [
2083+
{
2084+
type: 'Identifier',
2085+
name: 'x',
2086+
},
2087+
{
2088+
type: 'Identifier',
2089+
name: 'y',
2090+
},
2091+
],
2092+
})
2093+
expr = parseExpression('type(MyContract)')
2094+
assert.deepEqual(expr, {
2095+
type: 'FunctionCall',
2096+
expression: {
2097+
type: 'Identifier',
2098+
name: 'type',
2099+
},
2100+
arguments: [
2101+
{
2102+
type: 'Identifier',
2103+
name: 'MyContract',
2104+
},
2105+
],
2106+
names: [],
2107+
identifiers: [],
19672108
})
19682109
})
19692110

@@ -2825,6 +2966,7 @@ describe('AST', () => {
28252966
arguments: {
28262967
type: 'NameValueList',
28272968
names: ['value'],
2969+
identifiers: [{ type: 'Identifier', name: 'value' }],
28282970
arguments: [
28292971
{
28302972
number: '1',
@@ -2845,6 +2987,7 @@ describe('AST', () => {
28452987
},
28462988
arguments: [],
28472989
names: [],
2990+
identifiers: [],
28482991
})
28492992

28502993
expr = parseExpression('recipient.call{value: 1, gas: 21000}()')
@@ -2854,6 +2997,10 @@ describe('AST', () => {
28542997
arguments: {
28552998
type: 'NameValueList',
28562999
names: ['value', 'gas'],
3000+
identifiers: [
3001+
{ type: 'Identifier', name: 'value' },
3002+
{ type: 'Identifier', name: 'gas' },
3003+
],
28573004
arguments: [
28583005
{
28593006
number: '1',
@@ -2879,6 +3026,7 @@ describe('AST', () => {
28793026
},
28803027
arguments: [],
28813028
names: [],
3029+
identifiers: [],
28823030
})
28833031
})
28843032

@@ -2897,6 +3045,7 @@ describe('AST', () => {
28973045
},
28983046
],
28993047
names: [],
3048+
identifiers: [],
29003049
})
29013050
})
29023051

@@ -3252,6 +3401,7 @@ describe('AST', () => {
32523401
type: 'Identifier',
32533402
},
32543403
names: [],
3404+
identifiers: [],
32553405
type: 'FunctionCall',
32563406
},
32573407
})
@@ -3272,6 +3422,7 @@ describe('AST', () => {
32723422
type: 'Identifier',
32733423
},
32743424
names: [],
3425+
identifiers: [],
32753426
type: 'FunctionCall',
32763427
},
32773428
})

0 commit comments

Comments
 (0)