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

Only skip supported escaped characters in f-strings #700

Merged
merged 2 commits into from
Jun 16, 2022
Merged

Only skip supported escaped characters in f-strings #700

merged 2 commits into from
Jun 16, 2022

Conversation

zsol
Copy link
Member

@zsol zsol commented Jun 14, 2022

Summary

When tokenizing f-strings, we should only skip characters that are allowed to be escaped (spec).

Fixes #699

Test Plan

Added test cases.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 14, 2022
@zsol
Copy link
Member Author

zsol commented Jun 14, 2022

@bgw if you have some time I'd appreciate your eyes on this :) There's one more fix like this coming (to address #668

Copy link
Contributor

@bgw bgw left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for fixing!

I think we could greatly reduce the number of bugs here if we stopped splitting f-strings, and instead recursively ran the parser, like CPython does. But that would involve a bigger architectural change.

@@ -940,7 +940,25 @@ impl<'t> TokState<'t> {
// skip escaped char (e.g. \', \", or newline/line continuation)
self.text_pos.next();
}
} else {
} else if let Some(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, this syntax was finally added in Rust 1.53.0: https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#or-patterns

We could probably clean up the rest of the tokenizer to use this more compact match format.

@zsol
Copy link
Member Author

zsol commented Jun 16, 2022

Agreed on both counts. Thanks for taking a look

@zsol zsol merged commit 153c6d1 into main Jun 16, 2022
@zsol zsol deleted the issue-699 branch June 16, 2022 08:47
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Sep 16, 2022
0.4.7 - 2022-07-12

Fixed
* Fix get_qualified_names_for matching on prefixes of the given name by @lpetre in Instagram/LibCST#719

Added
* Implement lazy loading mechanism for expensive metadata providers by @Chenguang-Zhu in Instagram/LibCST#720


0.4.6 - 2022-07-04

New Contributors
- @superbobry made their first contribution in Instagram/LibCST#702

Fixed
- convert_type_comments now preserves comments following type comments by @superbobry in Instagram/LibCST#702
- QualifiedNameProvider optimizations
  - Cache the scope name prefix to prevent scope traversal in a tight loop by @lpetre in Instagram/LibCST#708
  - Faster qualified name formatting by @lpetre in Instagram/LibCST#710
  - Prevent unnecessary work in Scope.get_qualified_names_for_ by @lpetre in Instagram/LibCST#709
- Fix parsing of parenthesized empty tuples by @zsol in Instagram/LibCST#712
- Support whitespace after ParamSlash by @zsol in Instagram/LibCST#713
- [parser] bail on deeply nested expressions by @zsol in Instagram/LibCST#718


0.4.5 - 2022-06-17

New Contributors

-   @zzl0 made their first contribution in Instagram/LibCST#704

Fixed

-   Only skip supported escaped characters in f-strings by @zsol in Instagram/LibCST#700
-   Escaping quote characters in raw string literals causes a tokenizer error by @zsol in Instagram/LibCST#668
-   Corrected a code example in the documentation by @zzl0 in Instagram/LibCST#703
-   Handle multiline strings that start with quotes by @zzl0 in Instagram/LibCST#704
-   Fixed a performance regression in libcst.metadata.ScopeProvider by @lpetre in Instagram/LibCST#698


0.4.4 - 2022-06-13

New Contributors

-   @adamchainz made their first contribution in Instagram/LibCST#688

Added

-   Add package links to PyPI by @adamchainz in Instagram/LibCST#688
-   native: add overall benchmark by @zsol in Instagram/LibCST#692
-   Add support for PEP-646 by @zsol in Instagram/LibCST#696

Updated

-   parser: use references instead of smart pointers for Tokens by @zsol in Instagram/LibCST#691
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Escaping opening braces in f-strings causes a tokenizer error
3 participants