Skip to content

Commit

Permalink
feat: reduce and combine ast nodes, use xxxKind to distinguish
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonstudio committed Dec 26, 2023
1 parent 920f524 commit f602239
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 174 deletions.
13 changes: 2 additions & 11 deletions src/ast/declaration/contract-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type ContractDefinitionNodes =
export class ContractDefinition extends BaseNode {
type = 'ContractDefinition';
name: Identifier;
contractKind: ContractKind = 'contract';
abstract: boolean = false;
baseContracts: InheritanceSpecifier[] = [];
contractKind: ContractKind = 'contract';
nodes: ContractDefinitionNodes[] = [];
constructor(
ctx: ContractDefinitionContext | InterfaceDefinitionContext | LibraryDefinitionContext,
Expand All @@ -45,24 +45,15 @@ export class ContractDefinition extends BaseNode {
this.nodes = ctx.contractBodyElement().map((element) => element.accept(visitor));

if (ctx instanceof InterfaceDefinitionContext) {
this.type = 'InterfaceDefinition';
this.contractKind = 'interface';
} else if (ctx instanceof LibraryDefinitionContext) {
this.type = 'LibraryDefinition';
this.contractKind = 'library';
} else {
this.type = 'ContractDefinition';
this.contractKind = 'contract';
this.abstract = !!ctx.Abstract();
this.baseContracts = ctx.inheritanceSpecifierList()?.accept(visitor) ?? [];
}
}
}

export class InterfaceDefinition extends ContractDefinition {
type = 'InterfaceDefinition';
}

export class LibraryDefinition extends ContractDefinition {
type = 'InterfaceDefinition';
}
export { ContractDefinition as InterfaceDefinition, ContractDefinition as LibraryDefinition };
20 changes: 5 additions & 15 deletions src/ast/declaration/function-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class FunctionDefinition extends BaseNode {
) {
super(ctx, visitor);
if (ctx instanceof FunctionTypeNameContext) {
this.type = 'FunctionTypeName';
this.functionKind = 'function';
this.visibility = ctx.visibility(0)?.accept(visitor) ?? null;
this.stateMutability = ctx.stateMutability(0)?.accept(visitor) ?? null;
this.parameters = ctx._arguments?.accept(visitor) ?? null;
Expand All @@ -50,7 +50,6 @@ export class FunctionDefinition extends BaseNode {
this.body = ctx.block()?.accept(visitor) ?? null;

if (ctx instanceof ConstructorDefinitionContext) {
this.type = 'ConstructorDefinition';
this.functionKind = 'constructor';
this.visibility = new Visibility(ctx as any, visitor);
this.stateMutability = new StateMutability(ctx as any, visitor);
Expand All @@ -61,19 +60,16 @@ export class FunctionDefinition extends BaseNode {
this.override = overrideSpecifier ? overrideSpecifier.accept(visitor) : null;

if (ctx instanceof FallbackFunctionDefinitionContext) {
this.type = 'FallbackFunctionDefinition';
this.functionKind = 'fallback';
this.visibility = new Visibility(ctx as any, visitor);
this.stateMutability = ctx.stateMutability(0)?.accept(visitor) ?? this.stateMutability;
this.parameters = ctx.parameterList(0)?.accept(visitor) ?? null;
this.returnParameters = ctx.parameterList(1)?.accept(visitor) ?? null;
} else if (ctx instanceof ReceiveFunctionDefinitionContext) {
this.type = 'ReceiveFunctionDefinition';
this.functionKind = 'receive';
this.visibility = new Visibility(ctx as any, visitor);
this.stateMutability = new StateMutability(ctx as any, visitor);
} else {
this.type = 'FunctionDefinition';
this.functionKind = 'function';
this.name = ctx.identifier()?.accept(visitor) ?? null;
this.visibility = ctx.visibility(0)?.accept(visitor) ?? this.visibility;
Expand All @@ -86,14 +82,8 @@ export class FunctionDefinition extends BaseNode {
}
}

export class ConstructorDefinition extends FunctionDefinition {
type = 'ConstructorDefinition';
}

export class FallbackFunctionDefinition extends FunctionDefinition {
type = 'FallbackFunctionDefinition';
}

export class ReceiveFunctionDefinition extends FunctionDefinition {
type = 'ReceiveFunctionDefinition';
export {
FunctionDefinition as ConstructorDefinition,
FunctionDefinition as FallbackFunctionDefinition,
FunctionDefinition as ReceiveFunctionDefinition,
}
26 changes: 0 additions & 26 deletions src/ast/declaration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,57 +27,31 @@ import {
import { Visibility } from './visibility';

export type DeclarationNode =
| ContractBodyElement
| ContractDefinition
| InterfaceDefinition
| LibraryDefinition
| EnumDefinition
| ErrorDefinition
| EventDefinition
| ConstructorDefinition
| FunctionDefinition
| FallbackFunctionDefinition
| ReceiveFunctionDefinition
| ModifierDefinition
| StateMutability
| StructDefinition
| StructMember
| UserDefinedValueTypeDefinition
| VariableDeclarationList
| VariableDeclarationTuple
| VariableDeclaration
| ParameterDeclaration
| StateVariableDeclaration
| ConstantVariableDeclaration
| ErrorParameter
| EventParameter
| Visibility;

export type DeclarationNodeType =
| 'ContractBodyElement'
| 'ContractDefinition'
| 'InterfaceDefinition'
| 'LibraryDefinition'
| 'EnumDefinition'
| 'ErrorDefinition'
| 'EventDefinition'
| 'ConstructorDefinition'
| 'FunctionDefinition'
| 'FallbackFunctionDefinition'
| 'ReceiveFunctionDefinition'
| 'ModifierDefinition'
| 'StateMutability'
| 'StructDefinition'
| 'StructMember'
| 'UserDefinedValueTypeDefinition'
| 'VariableDeclarationList'
| 'VariableDeclarationTuple'
| 'VariableDeclaration'
| 'ParameterDeclaration'
| 'StateVariableDeclaration'
| 'ConstantVariableDeclaration'
| 'ErrorParameter'
| 'EventParameter'
| 'Visibility';

export {
Expand Down
34 changes: 7 additions & 27 deletions src/ast/declaration/variable-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,17 @@ export class VariableDeclaration extends BaseNode {
super(ctx, visitor);

if (ctx instanceof VariableDeclarationContext) {
this.type = 'VariableDeclaration';
this.name = ctx.identifier()?.accept(visitor) ?? null;
this.typeName = ctx.typeName().accept(visitor);
this.dataLocation = ctx.dataLocation()?.accept(visitor);
this.stateVariable = false;
this.constant = false;
this.indexed = false;
} else if (ctx instanceof ParameterDeclarationContext) {
this.type = 'ParameterDeclaration';
this.name = ctx.identifier()?.accept(visitor) ?? null;
this.typeName = ctx.typeName().accept(visitor);
this.dataLocation = ctx.dataLocation()?.accept(visitor);
} else if (ctx instanceof StateVariableDeclarationContext) {
this.type = 'StateVariableDeclaration';
this.name = ctx.identifier()?.accept(visitor) ?? null;
this.typeName = ctx.typeName().accept(visitor);
this.stateVariable = true;
Expand All @@ -69,42 +66,25 @@ export class VariableDeclaration extends BaseNode {
this.override = ctx.overrideSpecifier(0)?.accept(visitor) ?? null;
this.expression = ctx.expression()?.accept(visitor) ?? null;
} else if (ctx instanceof ConstantVariableDeclarationContext) {
this.type = 'ConstantVariableDeclaration';
this.name = ctx.identifier()?.accept(visitor) ?? null;
this.typeName = ctx.typeName().accept(visitor);
this.constant = true;
this.expression = ctx.expression().accept(visitor);
} else if (ctx instanceof ErrorParameterContext) {
this.type = 'ErrorParameter';
this.name = ctx.identifier()?.accept(visitor) ?? null;
this.typeName = ctx.typeName().accept(visitor);
} else if (ctx instanceof EventParameterContext) {
this.type = 'EventParameter';
this.name = ctx.identifier()?.accept(visitor) ?? null;
this.typeName = ctx.typeName().accept(visitor);
this.indexed = !!ctx.Indexed();
} else {
this.type = 'Unknown';
}
}
}

export class ParameterDeclaration extends VariableDeclaration {
type = 'ParameterDeclaration';
}

export class StateVariableDeclaration extends VariableDeclaration {
type = 'StateVariableDeclaration';
}

export class ConstantVariableDeclaration extends VariableDeclaration {
type = 'ConstantVariableDeclaration';
}

export class ErrorParameter extends VariableDeclaration {
type = 'ErrorParameter';
}

export class EventParameter extends VariableDeclaration {
type = 'EventParameter';
}
export {
VariableDeclaration as ParameterDeclaration,
VariableDeclaration as StateVariableDeclaration,
VariableDeclaration as ConstantVariableDeclaration,
VariableDeclaration as ErrorParameter,
VariableDeclaration as EventParameter,
};
46 changes: 3 additions & 43 deletions src/ast/expression/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,91 +35,51 @@ import { PrimaryExpression } from './primary-expression';
import { StringLiteral } from './string-literal';
import { TupleExpression } from './tuple-expression';
import { Tuple } from './tuple';
import { UnaryPrefixOperation, UnarySuffixOperation } from './unary-operation';
import { UnaryOperation, UnaryPrefixOperation, UnarySuffixOperation } from './unary-operation';
import { UnicodeStringLiteral } from './unicode-string-literal';
import { UserDefinableOperator } from './user-definable-operator';

export type ExpressionNode =
| AssignOp
| Assignment
| AndOperation
| AddSubOperation
| BitAndOperation
| BitOrOperation
| BitXorOperation
| EqualityComparison
| ExpOperation
| MulDivModOperation
| ShiftOperation
| OrderComparison
| OrOperation
| BooleanLiteral
| CallArgumentList
| Conditional
| Expression
| FunctionCallOptions
| FunctionCall
| HexStringLiteral
| Identifier
| IndexAccess
| IndexRangeAccess
| InlineArrayExpression
| InlineArray
| Literal
| MemberAccess
| NamedArgument
| NewExpr
| NumberLiteral
| LiteralWithSubDenomination
| PayableConversion
| PrimaryExpression
| StringLiteral
| TupleExpression
| Tuple
| UnaryPrefixOperation
| UnarySuffixOperation
| UnaryOperation
| UnicodeStringLiteral
| UserDefinableOperator;

export type ExpressionNodeType =
| 'AssignOp'
| 'Assignment'
| 'AndOperation'
| 'AddSubOperation'
| 'BitAndOperation'
| 'BitOrOperation'
| 'BitXorOperation'
| 'EqualityComparison'
| 'ExpOperation'
| 'MulDivModOperation'
| 'ShiftOperation'
| 'OrderComparison'
| 'OrOperation'
| 'BooleanLiteral'
| 'CallArgumentList'
| 'Conditional'
| 'Expression'
| 'FunctionCallOptions'
| 'FunctionCall'
| 'HexStringLiteral'
| 'Identifier'
| 'IndexAccess'
| 'IndexRangeAccess'
| 'InlineArrayExpression'
| 'InlineArray'
| 'Literal'
| 'MemberAccess'
| 'NamedArgument'
| 'NewExpr'
| 'NumberLiteral'
| 'LiteralWithSubDenomination'
| 'PayableConversion'
| 'PrimaryExpression'
| 'StringLiteral'
| 'TupleExpression'
| 'Tuple'
| 'UnaryPrefixOperation'
| 'UnarySuffixOperation'
| 'UnaryOperation'
| 'UnicodeStringLiteral'
| 'UserDefinableOperator';

Expand Down
2 changes: 1 addition & 1 deletion src/ast/expression/unary-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '../../grammar';
import { Expression } from './expression';

class UnaryOperation extends Expression {
export class UnaryOperation extends Expression {
type = 'UnaryOperation';
operator: string | null = null;
left: Expression | null = null;
Expand Down
4 changes: 2 additions & 2 deletions src/ast/meta/data-location.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BaseNodeString } from '../base';
import { DataLocationContext, SolidityParserVisitor } from '../../grammar';

type DataLocationKind = 'storage' | 'memory' | 'calldata';
type DataLocationName = 'storage' | 'memory' | 'calldata';

export class DataLocation extends BaseNodeString {
type = 'DataLocation';
name: DataLocationKind | null = null;
name: DataLocationName | null = null;
constructor(ctx: DataLocationContext, visitor: SolidityParserVisitor<any>) {
super(ctx, visitor);
if (ctx.Storage()) {
Expand Down
8 changes: 0 additions & 8 deletions src/ast/meta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,23 @@ export type MetaNode =
| IdentifierPath
| ImportAliases
| ImportDirective
| InheritanceSpecifierList
| InheritanceSpecifier
| ModifierInvocation
| OverrideSpecifier
| ParameterList
| Path
| PragmaDirective
| SourceUnit
| SymbolAliases
| UsingDirective;

export type MetaNodeType =
| 'DataLocation'
| 'IdentifierPath'
| 'ImportAliases'
| 'ImportDirective'
| 'InheritanceSpecifierList'
| 'InheritanceSpecifier'
| 'ModifierInvocation'
| 'OverrideSpecifier'
| 'ParameterList'
| 'Path'
| 'PragmaDirective'
| 'SourceUnit'
| 'SymbolAliases'
| 'UsingDirective';

export {
Expand Down
Loading

0 comments on commit f602239

Please sign in to comment.