Skip to content

Commit bceceff

Browse files
authored
Merge pull request #81 from solidity-parser/user-defined-operators
User defined operators
2 parents df1802c + 9c620b4 commit bceceff

File tree

12 files changed

+2442
-2157
lines changed

12 files changed

+2442
-2157
lines changed

antlr

src/ASTBuilder.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -533,32 +533,48 @@ export class ASTBuilder
533533
typeName = this.visitTypeName(ctxTypeName)
534534
}
535535

536-
const isGlobal = ctx.GlobalKeyword() !== undefined;
536+
const isGlobal = ctx.GlobalKeyword() !== undefined
537+
538+
const usingForObjectCtx = ctx.usingForObject()
539+
540+
const userDefinedTypeNameCtx = usingForObjectCtx.userDefinedTypeName()
537541

538-
// the object of the `usingForDeclaration` can be a single identifier
539-
// (the library name) or a group of functions:
540-
// using Lib for uint;
541-
// using { f } for uint;
542542
let node: AST.UsingForDeclaration
543-
const usingForObject = ctx.usingForObject()
544-
const firstChild = this._toText(usingForObject.getChild(0))
545-
if (firstChild === '{') {
543+
if (userDefinedTypeNameCtx !== undefined) {
544+
// using Lib for ...
546545
node = {
547546
type: 'UsingForDeclaration',
548547
isGlobal,
549548
typeName,
550-
libraryName: null,
551-
functions: usingForObject
552-
.userDefinedTypeName()
553-
.map((x) => this._toText(x)),
549+
libraryName: this._toText(userDefinedTypeNameCtx),
550+
functions: [],
551+
operators: [],
554552
}
555553
} else {
554+
// using { } for ...
555+
const usingForObjectDirectives = usingForObjectCtx.usingForObjectDirective()
556+
const functions: string[] = []
557+
const operators: Array<string | null> = []
558+
559+
for (const usingForObjectDirective of usingForObjectDirectives) {
560+
functions.push(
561+
this._toText(usingForObjectDirective.userDefinedTypeName())
562+
)
563+
const operator = usingForObjectDirective.userDefinableOperators()
564+
if (operator !== undefined) {
565+
operators.push(this._toText(operator))
566+
} else {
567+
operators.push(null)
568+
}
569+
}
570+
556571
node = {
557572
type: 'UsingForDeclaration',
558573
isGlobal,
559574
typeName,
560-
libraryName: this._toText(usingForObject.userDefinedTypeName(0)),
561-
functions: [],
575+
libraryName: null,
576+
functions,
577+
operators,
562578
}
563579
}
564580

@@ -1726,7 +1742,7 @@ export class ASTBuilder
17261742
value: this._toText(ctx.BooleanLiteral()!) === 'true',
17271743
}
17281744

1729-
return this._addMeta(node, ctx);
1745+
return this._addMeta(node, ctx)
17301746
}
17311747

17321748
if (ctx.DecimalNumber()) {

src/antlr/Solidity.interp

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/antlr/Solidity.tokens

Lines changed: 48 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/antlr/SolidityLexer.interp

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/antlr/SolidityLexer.tokens

Lines changed: 48 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)