diff --git a/README.md b/README.md index c565d54..c53e1be 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,10 @@ following options, in a style similar to the _esprima_ API: | loc | Boolean | false | When set to `true`, it will add location information to each node, with start and stop keys that contain the corresponding line and column numbers. Column numbers start from 0, lines start from 1. | | range | Boolean | false | When set to `true`, it will add range information to each node, which consists of a two-element array with start and stop character indexes in the input. | +In tolerant mode, a top-level declaration that cannot be converted after syntax +error recovery is omitted from the AST. Other declarations and the syntax errors +are still returned. + ### Example with location information ```javascript diff --git a/src/ASTBuilder.ts b/src/ASTBuilder.ts index c4a1abb..c2a3af0 100644 --- a/src/ASTBuilder.ts +++ b/src/ASTBuilder.ts @@ -17,7 +17,10 @@ export class ASTBuilder public result: AST.SourceUnit | null = null private _currentContract?: string - constructor(public options: ParseOptions) { + constructor( + public options: ParseOptions, + private readonly recoverErrors = false + ) { super() } @@ -34,7 +37,18 @@ export class ASTBuilder const node: AST.SourceUnit = { type: 'SourceUnit', - children: children.slice(0, -1).map((child) => this.visit(child)), + children: [], + } + for (const child of children.slice(0, -1)) { + try { + node.children.push(this.visit(child)) + } catch (error) { + // ANTLR recovery can leave required children missing. Preserve the + // other declarations when the caller requested syntax-error recovery. + if (!this.recoverErrors) { + throw error + } + } } const result = this._addMeta(node, ctx) this.result = result diff --git a/src/parser.ts b/src/parser.ts index 0d286e3..c105ff7 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -65,7 +65,14 @@ export function parse(input: string, options: ParseOptions = {}): ParseResult { const sourceUnit = parser.sourceUnit() - const astBuilder = new ASTBuilder(options) + if (listener.hasErrors() && options.tolerant !== true) { + throw new ParserError({ errors: listener.getErrors() }) + } + + const astBuilder = new ASTBuilder( + options, + listener.hasErrors() && options.tolerant === true + ) astBuilder.visit(sourceUnit) @@ -88,9 +95,6 @@ export function parse(input: string, options: ParseOptions = {}): ParseResult { } if (listener.hasErrors()) { - if (options.tolerant !== true) { - throw new ParserError({ errors: listener.getErrors() }) - } ast.errors = listener.getErrors() } diff --git a/test/index.ts b/test/index.ts index 4bb42d7..883f259 100644 --- a/test/index.ts +++ b/test/index.ts @@ -24,6 +24,61 @@ describe('#parse', function () { assert.isAbove(root.errors!.length, 0) }) + const incompleteSources = [ + 'contract A { uint x = ; }', + 'contract A { function f() public { if () {} } }', + 'uint constant X = ;', + 'contract A { function f() public { emit ; } }', + 'contract A { function fallback(address payable r) public {} }', + ] + + for (const source of incompleteSources) { + it(`reports syntax errors before building an incomplete AST: ${source}`, function () { + assert.throws(() => parser.parse(source), parser.ParserError) + }) + + it(`recovers from an incomplete AST in tolerant mode: ${source}`, function () { + const root = parser.parse(source, { tolerant: true }) + assert.equal(root.type, 'SourceUnit') + assert.isArray(root.children) + assert.isAbove(root.errors!.length, 0) + }) + } + + it('keeps valid declarations and metadata after tolerant recovery', function () { + const source = `pragma solidity ^0.8.0; +// invalid contract +contract A { function f() public { if () {} } } +contract B {}` + const root = parser.parse(source, { + tolerant: true, + loc: true, + range: true, + comments: true, + tokens: true, + }) + assert.deepEqual( + root.children.map((node) => node.type), + ['PragmaDirective', 'ContractDefinition'] + ) + assert.propertyVal(root.children[1], 'name', 'B') + assert.isAbove(root.errors!.length, 0) + assert.isAbove(root.tokens!.length, 0) + assert.lengthOf(root.comments!, 1) + assert.deepEqual(root.range, [0, source.length - 1]) + assert.deepEqual(root.loc!.start, { line: 1, column: 0 }) + }) + + it('does not suppress AST errors without syntax errors in tolerant mode', function () { + assert.throws( + () => + parser.parse('contract A layout at 1 layout at 2 {}', { + tolerant: true, + }), + 'Only one custom storage layout is allowed per contract' + ) + }) + it('supports loc', function () { const source = 'contract test { uint a; }' const root: any = parser.parse(source, { loc: true })