Skip to content

Commit

Permalink
fix: some identifier issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonstudio committed Jan 9, 2024
1 parent 209fded commit c241c03
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/ast/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { SyntaxNodeType } from './index';
import { ParseTree, ParserRuleContext, SolidityParserVisitor } from '../antlr4';
import type { AstPath, ParserOptions, Doc } from 'prettier';

export type SyntaxPrint<T extends BaseNode> = (params: {
path: AstPath<T>;
options: ParserOptions<T>;
print: (path: AstPath<T>) => Doc;
args?: unknown;
}) => Doc;

export class Position {
static create(line: number, column: number): Position {
Expand Down Expand Up @@ -34,7 +42,7 @@ export const isSyntaxNode = <T extends any>(node: T): boolean => {
};

export const keysInNode = <T extends BaseNode>(node: T): string[] => {
const forbiddenKeys = ['context', 'serialize'];
const forbiddenKeys = ['context', 'serialize', 'print'];
const keys: string[] = [];

for (const key in node) {
Expand Down Expand Up @@ -66,6 +74,9 @@ export abstract class BaseNode {

/** @ignore */
// context: ParserRuleContext;

/** @ignore */
// print?: SyntaxPrint<typeof this>;
}

export abstract class BaseNodeList<T extends any = BaseNode> extends Array<T> {
Expand Down
5 changes: 3 additions & 2 deletions src/ast/declaration/error-definition.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { BaseNode } from '../base';
import { ErrorDefinitionContext, SolidityParserVisitor } from '../../antlr4';
import { ErrorParameter } from './variable-declaration';
import { Identifier } from '../expression';

export class ErrorDefinition extends BaseNode {
type = 'ErrorDefinition' as const;
name: string;
name: Identifier;
parameters: ErrorParameter[];
constructor(ctx: ErrorDefinitionContext, visitor: SolidityParserVisitor<any>) {
super(ctx, visitor);
this.name = ctx.identifier().getText();
this.name = ctx.identifier().accept(visitor);
this.parameters = ctx.errorParameter().map((param) => param.accept(visitor));
}
}
5 changes: 3 additions & 2 deletions src/ast/declaration/event-definition.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { BaseNode } from '../base';
import { EventDefinitionContext, SolidityParserVisitor } from '../../antlr4';
import { EventParameter } from './variable-declaration';
import { Identifier } from '../expression';

export class EventDefinition extends BaseNode {
type = 'EventDefinition' as const;
name: string;
name: Identifier;
anonymous: boolean;
parameters: EventParameter[] = [];
constructor(ctx: EventDefinitionContext, visitor: SolidityParserVisitor<any>) {
super(ctx, visitor);
this.name = ctx.identifier().getText();
this.name = ctx.identifier().accept(visitor);
this.anonymous = !!ctx.Anonymous();
this.parameters = ctx.eventParameter().map((param) => param.accept(visitor));
}
Expand Down
5 changes: 3 additions & 2 deletions src/ast/meta/import-directive.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { BaseNode } from '../base';
import { ImportDirectiveContext, PathContext, SolidityParserVisitor } from '../../antlr4';
import { ImportDirectiveContext, SolidityParserVisitor } from '../../antlr4';
import { SymbolAliases } from './symbol-aliases';
import { Identifier } from '../expression';
import { Path } from './path';

export class ImportDirective extends BaseNode {
type = 'ImportDirective' as const;
path: PathContext;
path: Path;
unitAlias: Identifier | null = null;
symbolAliases: SymbolAliases[] = [];
constructor(ctx: ImportDirectiveContext, visitor: SolidityParserVisitor<any>) {
Expand Down
11 changes: 7 additions & 4 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SolidityASTBuilder } from '../ast/builder';
import { SyntaxNode } from '../ast';
import { SourceUnit, SyntaxNode } from '../ast';
import { SolidityParser, ParseTree, SolidityLexer, CommonTokenStream, CharStream } from '../antlr4';
import { ParseError, SolidityErrorListener } from './error-listener';

Expand All @@ -13,8 +13,11 @@ export const defaultParseOption: ParseOptions = {
selector: (p) => p.sourceUnit(),
};

export const parse = (source: string, _options?: ParseOptions): SyntaxNode => {
let syntaxTree: SyntaxNode;
export const parse = <T extends SyntaxNode = SourceUnit>(
source: string,
_options?: ParseOptions,
): T => {
let syntaxTree: T;
const options: ParseOptions = Object.assign({}, defaultParseOption, _options);
const listener = new SolidityErrorListener();

Expand All @@ -28,7 +31,7 @@ export const parse = (source: string, _options?: ParseOptions): SyntaxNode => {

const visitor = new SolidityASTBuilder();
const parseTree = options.selector!(parser);
syntaxTree = parseTree.accept(visitor)! as SyntaxNode;
syntaxTree = parseTree.accept(visitor)! as T;
} catch (error) {
if (error instanceof ParseError) {
} else {
Expand Down

0 comments on commit c241c03

Please sign in to comment.