python / python/cpython

Base the tokenizer API on source offsets

Đang mở
#153,569 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

interpreter-core type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

The tokenizer currently stores many source positions as pointers into buffers that can move. Reallocating the input means rebasing all these pointers, and missing one is very easy, especially with incremental input and f-strings.

I think tokenizer positions should be offsets into the decoded source instead:

typedef Py_ssize_t TokenizerOffset;

typedef struct {
    TokenizerOffset start;
    TokenizerOffset end;
} TokenizerSpan;

The main ideas would be:

  • One source object owns the decoded text.
  • The cursor only stores its current offset and line boundaries.
  • Tokens, errors and f-string state use offset spans instead of pointers.
  • pegen and _tokenize ask the source for a view or copy of a span.
  • Sequential tokenization keeps the current line in the cursor, while uncommon line lookups can use a small sparse index.
  • Normal parsing can keep one contiguous buffer, while tokenize(readline) can eventually use reclaimable chunks.

For incremental tokenization, new decoded input would be appended only when the cursor needs more data. Existing offsets would remain valid even if the underlying storage moves. Once tokenize has returned copied token and line strings, old input could be discarded when no active token, cursor, f-string frame or error still refers to it.

This is very nice because it separates source storage from tokenizer state, removes pointer rebasing, and means consumers no longer need to access tokenizer internals.

Linked PRs
  • gh-153585
  • gh-153587
  • gh-156472
  • gh-156482
  • gh-156484
  • gh-156654
  • gh-157055

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách đọc phần triển khai tokenizer và các interface được pegen và _tokenize sử dụng, sau đó xem xét các PR được liên kết gh-153585, gh-153587, gh-156472, gh-156482, gh-156484, gh-156654 và gh-157055. Công việc được xem là hoàn tất khi trạng thái tokenizer, các token, lỗi và trạng thái f-string sử dụng các khoảng offset nguồn mà không cần tái cơ sở con trỏ, đồng thời vẫn duy trì việc tokenization thông thường và gia tăng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c, python
Lĩnh vực
compilers
Loại issue
Tái cấu trúc
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.