python / python/cpython

Base the tokenizer API on source offsets

Aperta
#153,569 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

interpreter-core type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia leggendo l’implementazione del tokenizer e le interfacce utilizzate da pegen e _tokenize, quindi esamina le PR collegati gh-153585, gh-153587, gh-156472, gh-156482, gh-156484, gh-156654 e gh-157055. Il lavoro sarà completo quando lo stato del tokenizer, i token, gli errori e lo stato di f-string utilizzeranno intervalli di offset della sorgente senza riassegnazione dei puntatori, preservando la tokenizzazione normale e incrementale.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
c, python
Ambito
compilers
Tipo di issue
Refactoring
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.