Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotation fixes #291

Merged
merged 2 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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