From 3f00c6150b1292084867c9b95658cc6ad95aa936 Mon Sep 17 00:00:00 2001 From: ayazhafiz Date: Sun, 4 Oct 2020 23:31:22 -0500 Subject: [PATCH] test(compiler): Demonstrate recoverable parsing of unterminated pipes (#39113) There is no actionable change in this commit other than to pretty-print EOF tokens. Actual parsing of unterminated pipes is already supported, this just adds a test for it. Part of #38596 PR Close #39113 --- packages/compiler/src/expression_parser/parser.ts | 8 ++++++-- packages/compiler/test/expression_parser/parser_spec.ts | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/compiler/src/expression_parser/parser.ts b/packages/compiler/src/expression_parser/parser.ts index 751e8533ab..577b3ea249 100644 --- a/packages/compiler/src/expression_parser/parser.ts +++ b/packages/compiler/src/expression_parser/parser.ts @@ -491,10 +491,14 @@ export class _ParseAST { this.error(`Missing expected operator ${operator}`); } + prettyPrintToken(tok: Token): string { + return tok === EOF ? 'end of input' : `token ${tok}`; + } + expectIdentifierOrKeyword(): string { const n = this.next; if (!n.isIdentifier() && !n.isKeyword()) { - this.error(`Unexpected token ${n}, expected identifier or keyword`); + this.error(`Unexpected ${this.prettyPrintToken(n)}, expected identifier or keyword`); return ''; } this.advance(); @@ -504,7 +508,7 @@ export class _ParseAST { expectIdentifierOrKeywordOrString(): string { const n = this.next; if (!n.isIdentifier() && !n.isKeyword() && !n.isString()) { - this.error(`Unexpected token ${n}, expected identifier, keyword, or string`); + this.error(`Unexpected ${this.prettyPrintToken(n)}, expected identifier, keyword, or string`); return ''; } this.advance(); diff --git a/packages/compiler/test/expression_parser/parser_spec.ts b/packages/compiler/test/expression_parser/parser_spec.ts index f6786bc267..c091a25a1a 100644 --- a/packages/compiler/test/expression_parser/parser_spec.ts +++ b/packages/compiler/test/expression_parser/parser_spec.ts @@ -393,6 +393,11 @@ describe('parser', () => { checkBinding('a | b:(c | d)', '(a | b:(c | d))'); }); + it('should parse incomplete pipes', () => { + checkBinding('a | b | ', '((a | b) | )'); + expectBindingError('a | b | ', 'Unexpected end of input, expected identifier or keyword'); + }); + it('should only allow identifier or keyword as formatter names', () => { expectBindingError('"Foo"|(', 'identifier or keyword'); expectBindingError('"Foo"|1234', 'identifier or keyword');