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

Replace the blib2to3 tokenizer with pytokens #4536

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b37624f
Replace the blib2to3 tokenizer with pytokens
tusharsadhwani Dec 22, 2024
1174fbc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 22, 2024
e5d412b
formatting
tusharsadhwani Dec 22, 2024
920445f
wip
tusharsadhwani Dec 22, 2024
2fb18aa
add async/await keyword support
tusharsadhwani Dec 23, 2024
0fbdd2b
typo fix
tusharsadhwani Dec 23, 2024
bc785af
fix `\\\n` handling
tusharsadhwani Dec 23, 2024
945678c
typo fix
tusharsadhwani Dec 23, 2024
90dc1c7
fix form feed test
tusharsadhwani Dec 23, 2024
134f7b9
fix EOFError case
tusharsadhwani Dec 23, 2024
7002eff
fix async discrepancies
tusharsadhwani Dec 23, 2024
b3500c4
bump pytokens version
tusharsadhwani Dec 23, 2024
51e7ce1
remove empty fstring middle tokens from test
tusharsadhwani Dec 23, 2024
8cfe444
remove python2 test
tusharsadhwani Dec 23, 2024
ec0b568
fix backslash edge case
tusharsadhwani Dec 23, 2024
ceab505
fix another backslash edge case
tusharsadhwani Dec 23, 2024
cb9d48a
fix mypyc
tusharsadhwani Dec 23, 2024
1e105f8
remove use of |
tusharsadhwani Dec 23, 2024
4dace1a
Merge branch 'main' into pytokens
tusharsadhwani Dec 23, 2024
3420248
bump pytokens version
tusharsadhwani Dec 25, 2024
59ddf06
lints
tusharsadhwani Dec 25, 2024
e18332f
Merge branch 'main' into pytokens
tusharsadhwani Dec 25, 2024
b068867
Add changelog entry
tusharsadhwani Dec 25, 2024
74edc5b
bump pytokens version
tusharsadhwani Jan 8, 2025
f3715e9
Merge branch 'main' into pytokens
tusharsadhwani Jan 8, 2025
ad46848
bump pytokens once again
tusharsadhwani Jan 9, 2025
97a730d
bump upload-artifact
tusharsadhwani Jan 9, 2025
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
8 changes: 4 additions & 4 deletions .github/workflows/diff_shades.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ jobs:
${{ matrix.baseline-analysis }} ${{ matrix.target-analysis }}

- name: Upload diff report
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.mode }}-diff.html
path: diff.html

- name: Upload baseline analysis
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.baseline-analysis }}
path: ${{ matrix.baseline-analysis }}

- name: Upload target analysis
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.target-analysis }}
path: ${{ matrix.target-analysis }}
Expand All @@ -137,7 +137,7 @@ jobs:

- name: Upload summary file (PR only)
if: github.event_name == 'pull_request' && matrix.mode == 'preview-changes'
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: .pr-comment.json
path: .pr-comment.json
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ repos:
- click >= 8.1.0, != 8.1.4, != 8.1.5
- packaging >= 22.0
- platformdirs >= 2.1.0
- pytokens >= 0.1.5
- pytest
- hypothesis
- aiohttp >= 3.7.4
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

<!-- Changes to the parser or to version autodetection -->

- Rewrite tokenizer to improve performance and compliance (#4536)

### Performance

<!-- Changes that improve Black's performance. -->
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies = [
"packaging>=22.0",
"pathspec>=0.9.0",
"platformdirs>=2",
"pytokens>=0.1.9",
"tomli>=1.1.0; python_version < '3.11'",
"typing_extensions>=4.0.1; python_version < '3.11'",
]
Expand Down
20 changes: 5 additions & 15 deletions src/blib2to3/pgen2/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from typing import IO, Any, Optional, Union, cast

from blib2to3.pgen2.grammar import Grammar
from blib2to3.pgen2.tokenize import GoodTokenInfo
from blib2to3.pgen2.tokenize import TokenInfo
from blib2to3.pytree import NL

# Pgen imports
Expand Down Expand Up @@ -112,7 +112,7 @@ def __init__(self, grammar: Grammar, logger: Optional[Logger] = None) -> None:
logger = logging.getLogger(__name__)
self.logger = logger

def parse_tokens(self, tokens: Iterable[GoodTokenInfo], debug: bool = False) -> NL:
def parse_tokens(self, tokens: Iterable[TokenInfo], debug: bool = False) -> NL:
"""Parse a series of tokens and return the syntax tree."""
# XXX Move the prefix computation into a wrapper around tokenize.
proxy = TokenProxy(tokens)
Expand Down Expand Up @@ -180,27 +180,17 @@ def parse_tokens(self, tokens: Iterable[GoodTokenInfo], debug: bool = False) ->
assert p.rootnode is not None
return p.rootnode

def parse_stream_raw(self, stream: IO[str], debug: bool = False) -> NL:
"""Parse a stream and return the syntax tree."""
tokens = tokenize.generate_tokens(stream.readline, grammar=self.grammar)
return self.parse_tokens(tokens, debug)

def parse_stream(self, stream: IO[str], debug: bool = False) -> NL:
"""Parse a stream and return the syntax tree."""
return self.parse_stream_raw(stream, debug)

def parse_file(
self, filename: Path, encoding: Optional[str] = None, debug: bool = False
) -> NL:
"""Parse a file and return the syntax tree."""
with open(filename, encoding=encoding) as stream:
return self.parse_stream(stream, debug)
text = stream.read()
return self.parse_string(text, debug)

def parse_string(self, text: str, debug: bool = False) -> NL:
"""Parse a string and return the syntax tree."""
tokens = tokenize.generate_tokens(
io.StringIO(text).readline, grammar=self.grammar
)
tokens = tokenize.tokenize(text, grammar=self.grammar)
return self.parse_tokens(tokens, debug)

def _partially_consume_prefix(self, prefix: str, column: int) -> tuple[str, str]:
Expand Down
7 changes: 3 additions & 4 deletions src/blib2to3/pgen2/pgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import IO, Any, NoReturn, Optional, Union

from blib2to3.pgen2 import grammar, token, tokenize
from blib2to3.pgen2.tokenize import GoodTokenInfo
from blib2to3.pgen2.tokenize import TokenInfo

Path = Union[str, "os.PathLike[str]"]

Expand All @@ -18,7 +18,7 @@ class PgenGrammar(grammar.Grammar):
class ParserGenerator:
filename: Path
stream: IO[str]
generator: Iterator[GoodTokenInfo]
generator: Iterator[TokenInfo]
first: dict[str, Optional[dict[str, int]]]

def __init__(self, filename: Path, stream: Optional[IO[str]] = None) -> None:
Expand All @@ -27,8 +27,7 @@ def __init__(self, filename: Path, stream: Optional[IO[str]] = None) -> None:
stream = open(filename, encoding="utf-8")
close_stream = stream.close
self.filename = filename
self.stream = stream
self.generator = tokenize.generate_tokens(stream.readline)
self.generator = tokenize.tokenize(stream.read())
self.gettoken() # Initialize lookahead
self.dfas, self.startsymbol = self.parse()
if close_stream is not None:
Expand Down
Loading
Loading