Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

re: character classes are parsed as regular expressions

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

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

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, python
Lĩnh vực
compilers

Hướng nghiên cứu

Start in transcrypt/modules/re/translate.py, reading shift() and countCaptureGroups() to understand how the tokenizer handles character classes. Compare the listed CPython and Transcrypt examples, including classes containing parentheses and a leading literal ]. Done means capture-group numbering matches CPython and the []x], and invalid [] cases behave as described.

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

Mô tả

re: character classes are parsed as regular expressions

transcrypt/modules/re/translate.py turns a Python regex into a token list. The tokenizer has no notion of character classes. Every character inside [...] becomes a token of its own. That causes two defects.

Parentheses inside a character class are counted as capture groups

countCaptureGroups() counts every token named (. A ( inside a character class is counted too. All groups behind such a class shift by one.

import re
r = re.compile(r"(?P<a>[()])(?P<b>x)")
CPython Transcrypt
r.groups 2 3
r.groupindex {'a': 1, 'b': 2} {'a': 1, 'b': 3}

r.match("(x").group("b") returns x under CPython and None under Transcrypt. The generated JavaScript is correct, only the group numbers are wrong.

A literal ] at the start of a character class is not translated

Python treats a ] right behind [ or [^ as a literal. JavaScript ends the class at it. The translator passes the class through unchanged.

re.compile(r"[]x]") matches ] and x under CPython. Transcrypt emits []x] and the RegExp constructor raises Invalid regular expression: /[]x]/u: Lone quantifier brackets. Without the u flag the pattern compiles but means an empty class followed by x], so it never matches.

The reverse case is silent. re.compile("[]") raises unterminated character set under CPython. Transcrypt accepts it and returns a pattern that never matches.

Fix

Both defects go away when shift() consumes a character class as a single token. The class content is then never parsed as a regular expression, and the literal ] can be escaped while the class is read.

Ngôn ngữ chính
Python
Star
2.9k
Fork
218
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

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.

Issue khác của TranscryptOrg/Transcrypt

Tất cả issue của TranscryptOrg/Transcrypt

Issue tương tự

Thêm issue về Python

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.