Skip to content

Commit

Permalink
Fix infinite loop when parsing empty @ symbol for annotation (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron authored Jan 26, 2021
1 parent e194fec commit 12aad7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/parser/Parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,19 @@ describe('parser', () => {
sub main()
end sub
`, ParseMode.BrighterScript);
expect(diagnostics[0]?.code).to.equal(1081); //unexpected token '@'
expect(diagnostics[0]?.message).to.equal(DiagnosticMessages.foundUnexpectedToken('@').message);
});

it('properly handles empty annotation above class method', () => {
//this code used to cause an infinite loop, so the fact that the test passes/fails on its own is a success!
let { diagnostics } = parse(`
class Person
@
sub new()
end sub
end class
`, ParseMode.BrighterScript);
expect(diagnostics[0]?.message).to.equal(DiagnosticMessages.expectedIdentifier().message);
});

it('parses with error if annotation is not followed by a statement', () => {
Expand Down
12 changes: 7 additions & 5 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export class Parser {
let decl: Statement;
let accessModifier: Token;

if (this.check(TokenKind.At) && this.checkNext(TokenKind.Identifier)) {
if (this.check(TokenKind.At)) {
this.annotationExpression();
}

Expand Down Expand Up @@ -1176,10 +1176,12 @@ export class Parser {
}

private annotationExpression() {
let annotation = new AnnotationExpression(
this.advance(),
this.advance()
);
const atToken = this.advance();
const identifier = this.tryConsume(DiagnosticMessages.expectedIdentifier(), TokenKind.Identifier, ...AllowedProperties);
if (identifier) {
identifier.kind = TokenKind.Identifier;
}
let annotation = new AnnotationExpression(atToken, identifier);
this.pendingAnnotations.push(annotation);

//optional arguments
Expand Down

0 comments on commit 12aad7a

Please sign in to comment.