Skip to content

Commit d596ae1

Browse files
committed
Add identifiers to VariableDeclaration and ImportDeclaration nodes
1 parent b06b20b commit d596ae1

File tree

3 files changed

+251
-33
lines changed

3 files changed

+251
-33
lines changed

src/ASTBuilder.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export class ASTBuilder
129129
type: 'VariableDeclaration',
130130
typeName: type,
131131
name,
132+
identifier: this.visitIdentifier(iden),
132133
expression,
133134
visibility,
134135
isStateVar: true,
@@ -157,10 +158,13 @@ export class ASTBuilder
157158
storageLocation = this._toText(ctxStorageLocation)
158159
}
159160

161+
const identifierCtx = ctx.identifier()
162+
160163
const node: AST.VariableDeclaration = {
161164
type: 'VariableDeclaration',
162165
typeName: this.visitTypeName(ctx.typeName()),
163-
name: this._toText(ctx.identifier()),
166+
name: this._toText(identifierCtx),
167+
identifier: this.visitIdentifier(identifierCtx),
164168
storageLocation,
165169
isStateVar: false,
166170
isIndexed: false,
@@ -224,6 +228,10 @@ export class ASTBuilder
224228
type: 'VariableDeclaration',
225229
typeName: type,
226230
name,
231+
identifier:
232+
paramCtxIdentifier !== undefined
233+
? this.visitIdentifier(paramCtxIdentifier)
234+
: null,
227235
isStateVar: false,
228236
isIndexed: paramCtx.IndexedKeyword() !== undefined,
229237
storageLocation: null,
@@ -268,6 +276,10 @@ export class ASTBuilder
268276
type: 'VariableDeclaration',
269277
typeName: this.visitTypeName(ctx.typeName()),
270278
name,
279+
identifier:
280+
ctxIdentifier !== undefined
281+
? this.visitIdentifier(ctxIdentifier)
282+
: null,
271283
storageLocation,
272284
isStateVar: false,
273285
isIndexed: false,
@@ -658,6 +670,7 @@ export class ASTBuilder
658670
type: 'VariableDeclaration',
659671
typeName: this.visitTypeName(ctx.typeName()),
660672
name: null,
673+
identifier: null,
661674
storageLocation,
662675
isStateVar: false,
663676
isIndexed: false,
@@ -1429,6 +1442,7 @@ export class ASTBuilder
14291442
const node: AST.VariableDeclaration = {
14301443
type: 'VariableDeclaration',
14311444
name: this._toText(iden),
1445+
identifier: this.visitIdentifier(iden),
14321446
isStateVar: false,
14331447
isIndexed: false,
14341448
typeName: null,
@@ -1461,9 +1475,12 @@ export class ASTBuilder
14611475
storageLocation = this._toText(decl.storageLocation()!)
14621476
}
14631477

1478+
const identifierCtx = decl.identifier()
1479+
14641480
const result: AST.VariableDeclaration = {
14651481
type: 'VariableDeclaration',
1466-
name: this._toText(decl.identifier()),
1482+
name: this._toText(identifierCtx),
1483+
identifier: this.visitIdentifier(identifierCtx),
14671484
typeName: this.visitTypeName(decl.typeName()),
14681485
storageLocation,
14691486
isStateVar: false,
@@ -1478,7 +1495,9 @@ export class ASTBuilder
14781495
public visitImportDirective(ctx: SP.ImportDirectiveContext) {
14791496
const pathString = this._toText(ctx.StringLiteralFragment())!
14801497
let unitAlias = null
1498+
let unitAliasIdentifier = null
14811499
let symbolAliases = null
1500+
let symbolAliasesIdentifiers = null
14821501

14831502
if (ctx.importDeclaration().length > 0) {
14841503
symbolAliases = ctx.importDeclaration().map((decl) => {
@@ -1489,17 +1508,46 @@ export class ASTBuilder
14891508
}
14901509
return [symbol, alias] as [string, string | null]
14911510
})
1492-
} else if (ctx.children!.length === 7) {
1493-
unitAlias = this._toText(ctx.getChild(3))
1494-
} else if (ctx.children!.length === 5) {
1495-
unitAlias = this._toText(ctx.getChild(3))
1511+
symbolAliasesIdentifiers = ctx.importDeclaration().map((decl) => {
1512+
const symbolIdentifier = this.visitIdentifier(decl.identifier(0))
1513+
let aliasIdentifier = null
1514+
if (decl.identifier().length > 1) {
1515+
aliasIdentifier = this.visitIdentifier(decl.identifier(1))
1516+
}
1517+
return [symbolIdentifier, aliasIdentifier] as [
1518+
AST.Identifier,
1519+
AST.Identifier | null
1520+
]
1521+
})
1522+
} else {
1523+
const identifierCtxList = ctx.identifier()
1524+
if (identifierCtxList.length === 0) {
1525+
// nothing to do
1526+
} else if (identifierCtxList.length === 1) {
1527+
const aliasIdentifierCtx = ctx.identifier(0)
1528+
unitAlias = this._toText(aliasIdentifierCtx)
1529+
unitAliasIdentifier = this.visitIdentifier(aliasIdentifierCtx)
1530+
} else if (identifierCtxList.length === 2) {
1531+
const aliasIdentifierCtx = ctx.identifier(1)
1532+
unitAlias = this._toText(aliasIdentifierCtx)
1533+
unitAliasIdentifier = this.visitIdentifier(aliasIdentifierCtx)
1534+
} else {
1535+
throw new Error(
1536+
'Assertion error: an import should have one or two identifiers'
1537+
)
1538+
}
14961539
}
1540+
// else if (ctx.children!.length === 5) {
1541+
// unitAlias = this._toText(ctx.getChild(3))
1542+
// }
14971543

14981544
const node: AST.ImportDirective = {
14991545
type: 'ImportDirective',
15001546
path: pathString.substring(1, pathString.length - 1),
15011547
unitAlias,
1548+
unitAliasIdentifier,
15021549
symbolAliases,
1550+
symbolAliasesIdentifiers,
15031551
}
15041552

15051553
return this._addMeta(node, ctx)

src/ast-types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ export interface ImportDirective extends BaseASTNode {
132132
type: 'ImportDirective'
133133
path: string
134134
unitAlias: string | null
135+
unitAliasIdentifier: Identifier | null
135136
symbolAliases: Array<[string, string | null]> | null
137+
symbolAliasesIdentifiers: Array<[Identifier, Identifier | null]> | null
136138
}
137139
export interface StateVariableDeclaration extends BaseASTNode {
138140
type: 'StateVariableDeclaration'
@@ -215,6 +217,7 @@ export interface VariableDeclaration extends BaseASTNode {
215217
isStateVar: boolean
216218
typeName: TypeName | null
217219
name: string | null
220+
identifier: Identifier | null
218221
isDeclaredConst?: boolean
219222
storageLocation: string | null
220223
expression: Expression | null

0 commit comments

Comments
 (0)