Skip to content

Commit

Permalink
Fixed bug in parser where it was too permissive in allowing keywords …
Browse files Browse the repository at this point in the history
…to be used as identifiers. The Python interpreter is less permissive, so pyright's parser should match.
  • Loading branch information
msfterictraut committed Jul 27, 2021
1 parent e85aeb1 commit d9a66af
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/pyright-internal/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2275,7 +2275,7 @@ export class Parser {
}

while (true) {
const identifier = this._getTokenIfIdentifier([KeywordType.Import]);
const identifier = this._getTokenIfIdentifier();
if (!identifier) {
if (!allowJustDots || moduleNameNode.leadingDots === 0 || moduleNameNode.nameParts.length > 0) {
this._addError(Localizer.Diagnostic.expectedModuleName(), this._peekToken());
Expand Down Expand Up @@ -4461,7 +4461,7 @@ export class Parser {
return (nextToken as OperatorToken).operatorType;
}

private _getTokenIfIdentifier(disallowedKeywords: KeywordType[] = []): IdentifierToken | undefined {
private _getTokenIfIdentifier(): IdentifierToken | undefined {
const nextToken = this._peekToken();
if (nextToken.type === TokenType.Identifier) {
return this._getNextToken() as IdentifierToken;
Expand All @@ -4474,11 +4474,11 @@ export class Parser {
return IdentifierToken.create(nextToken.start, nextToken.length, '', nextToken.comments);
}

// If keywords are allowed in this context, convert the keyword
// to an identifier token.
// If this is a "soft keyword", it can be converted into an identifier.
if (nextToken.type === TokenType.Keyword) {
const keywordType = this._peekKeywordType();
if (!disallowedKeywords.find((type) => type === keywordType)) {
const softKeywords = [KeywordType.Debug, KeywordType.Match, KeywordType.Case];
if (softKeywords.find((type) => type === keywordType)) {
const keywordText = this._fileContents!.substr(nextToken.start, nextToken.length);
this._getNextToken();
return IdentifierToken.create(nextToken.start, nextToken.length, keywordText, nextToken.comments);
Expand Down

0 comments on commit d9a66af

Please sign in to comment.