Skip to content

Update lexer snapshots with token flags#11614

Merged
dhruvmanila merged 1 commit intodhruv/parser-phase-2from
dhruv/lexer-tests
May 30, 2024
Merged

Update lexer snapshots with token flags#11614
dhruvmanila merged 1 commit intodhruv/parser-phase-2from
dhruv/lexer-tests

Conversation

@dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented May 30, 2024

Summary

This PR updates the lexer test snapshots to include the TokenFlags.

Test Plan

Verify the snapshots.

@dhruvmanila dhruvmanila added the testing Related to testing Ruff itself label May 30, 2024
@codspeed-hq
Copy link

codspeed-hq bot commented May 30, 2024

CodSpeed Performance Report

Merging #11614 will not alter performance

Comparing dhruv/lexer-tests (66953c5) with dhruv/parser-phase-2 (d55bfcc)

Summary

✅ 30 untouched benchmarks

FStringStart,
11..13,
TokenFlags(
F_STRING,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the usage of the FStringFlag. Can't we tell that it is an FString by looking at the TokenKind?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point. Currently, it's only used inside the lexer to call either lex_fstring_start or lex_string method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's required because we represent regular string with no string kind flag set. And, prefix method would also not be able to differentiate because the flags themselves don't have access to the token kind.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm okay. I would need to check out the code to better understand it. We could move the prefix method to the Token? And could we only keep it for FStringFlags? But it's probably not that important.

Base automatically changed from dhruv/compile to dhruv/parser-phase-2 May 30, 2024 11:14
Comment on lines 587 to 597

// Keep the current flags in sync throughout the f-string context.
self.current_flags = fstring.flags();

// Check if we're at the end of the f-string.
if fstring.is_triple_quoted() {
let quote_char = fstring.quote_char();
if self.cursor.eat_char3(quote_char, quote_char, quote_char) {
self.current_flags = fstring.flags();
return Some(TokenKind::FStringEnd);
}
} else if self.cursor.eat_char(fstring.quote_char()) {
self.current_flags = fstring.flags();
return Some(TokenKind::FStringEnd);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the token flags only if we know that it's FStringEnd.

Comment on lines 718 to 720

self.current_flags = fstring.flags();
Some(TokenKind::FStringMiddle)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the token flags only if we know that it's FStringMiddle.

@dhruvmanila
Copy link
Member Author

(I'll look at the remaining test cases in a separate PR.)

@dhruvmanila dhruvmanila marked this pull request as ready for review May 30, 2024 11:35
Comment on lines +1900 to +1905
let mut tuple = if matches!(self.value, TokenValue::None) {
tuple.field(&self.kind)
} else {
tuple.field(&self.value)
};
tuple = tuple.field(&self.range);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: At least debug_map etc. mutate the state in place.

Suggested change
let mut tuple = if matches!(self.value, TokenValue::None) {
tuple.field(&self.kind)
} else {
tuple.field(&self.value)
};
tuple = tuple.field(&self.range);
if matches!(self.value, TokenValue::None) {
tuple.field(&self.kind);
} else {
tuple.field(&self.value);
};
tuple.field(&self.range);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you suggesting to use debug_map? I want to keep the tuple here to have minimal diff which makes it easier to verify the changes. We can explore other formats as a follow-up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no, I'm suggesting to not use let mut tuple = tuple.field because I think it might not be necessary (it's not necessary for debug_map)

tuple.field(&self.value)
};
tuple = tuple.field(&self.range);
if self.flags.is_empty() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe

if !self.flags.is_empty() {
	tuple.field(&self.flags);
}

tuple.finish()

FStringStart,
11..13,
TokenFlags(
F_STRING,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm okay. I would need to check out the code to better understand it. We could move the prefix method to the Token? And could we only keep it for FStringFlags? But it's probably not that important.

@dhruvmanila dhruvmanila merged commit 400d074 into dhruv/parser-phase-2 May 30, 2024
@dhruvmanila dhruvmanila deleted the dhruv/lexer-tests branch May 30, 2024 15:11
dhruvmanila added a commit that referenced this pull request May 31, 2024
## Summary

This PR updates the lexer test snapshots to include the `TokenFlags`.

## Test Plan

Verify the snapshots.
dhruvmanila added a commit that referenced this pull request Jun 3, 2024
## Summary

This PR updates the lexer test snapshots to include the `TokenFlags`.

## Test Plan

Verify the snapshots.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Related to testing Ruff itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants