Skip to content

Commit

Permalink
Use token type not text when detecting keywords after "."
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jan 26, 2024
1 parent f96511f commit afd2f5e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/lexer/disambiguateTokens.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { isReserved, Token, TokenType } from './token.js';

/**
* Ensures that no keyword token (RESERVED_*) is preceded by dot (.).
* Ensures that no keyword token (RESERVED_*) is preceded by dot (.)
* or any other property-access operator.
*
* Ensures that all RESERVED_FUNCTION_NAME tokens are followed by "(".
* If they're not, converts the token to RESERVED_KEYWORD.
Expand All @@ -17,17 +18,17 @@ import { isReserved, Token, TokenType } from './token.js';
*/
export function disambiguateTokens(tokens: Token[]): Token[] {
return tokens
.map(dotKeywordToIdent)
.map(propertyNameKeywordToIdent)
.map(funcNameToKeyword)
.map(dataTypeToParameterizedDataType)
.map(identToArrayIdent)
.map(dataTypeToArrayKeyword);
}

const dotKeywordToIdent = (token: Token, i: number, tokens: Token[]): Token => {
const propertyNameKeywordToIdent = (token: Token, i: number, tokens: Token[]): Token => {
if (isReserved(token.type)) {
const prevToken = prevNonCommentToken(tokens, i);
if (prevToken && prevToken.text === '.') {
if (prevToken && prevToken.type === TokenType.PROPERTY_ACCESS_OPERATOR) {
return { ...token, type: TokenType.IDENTIFIER, text: token.raw };
}
}
Expand Down

0 comments on commit afd2f5e

Please sign in to comment.