From f531a481075d02c43de0c44e12e4eb455b6447b6 Mon Sep 17 00:00:00 2001 From: isgav <51133755+isaranga@users.noreply.github.com> Date: Mon, 30 Nov 2026 09:08:09 +0200 Subject: [PATCH] Say that rules P2/P3 are skipped when the paragraph level is given When --direction ltr/rtl (paragraph_embedding_level) is passed, verbose output now prints the Phase P header and a line saying P2/P3 were skipped, instead of leaving Phase P out. --- CLAUDE.md | 2 +- bidi.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CLAUDE.md b/CLAUDE.md index 4137018..c2e6ef2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -83,7 +83,7 @@ Bracket pair detection for N0 is handled by `_find_bracket_pairs`, using `OPEN_B ### Verbose mode -Every phase function (and `_apply_i1`/`_apply_i2`) accepts `verbose=True`; the per-rule W, N and L helpers do not, and their phase function prints on their behalf. When enabled, `print_table` (in `verbose_table.py`, together with the helpers below) prints a before/after table for each rule application, with rows for character index, code point, the character itself, the type (or level, or position) before and after the rule, and embedding level. `print_table` decides "(no changes)" by comparing only the Before/After rows, so Phase X prints two tables: "X1-X8 (levels)" (Before is the paragraph level from X1) and "X1-X9 (types)" (overrides, FSI resolution, X9's BN). Without the levels table, an isolate or embedding with no override would show as "(no changes)". After phase X, `apply_bidi_algorithm` calls `print_isolating_run_sequences` (also in `verbose_table.py`), which prints the X10 sequences one line each (level, character indices as ranges, `sos`/`eos`), then the indices of the characters removed by X9. Columns are always in logical order, including the L2 and L4 tables, where Before/After give each character's position before and after reordering. From I1 on (the I1, I2, L1, L2 and L4 tables), a character removed by X9 shows `x` as its level, in the Level row and in the I1/I2/L1 Before/After rows, and `x` as its L2 position. `print_table` does this when passed `after_x9=True`, through `format_level_for_table`. The P and X tables keep the numbers the X rules assigned, and the W and N tables never contain removed characters. +Every phase function (and `_apply_i1`/`_apply_i2`) accepts `verbose=True`; the per-rule W, N and L helpers do not, and their phase function prints on their behalf. When enabled, `print_table` (in `verbose_table.py`, together with the helpers below) prints a before/after table for each rule application, with rows for character index, code point, the character itself, the type (or level, or position) before and after the rule, and embedding level. `print_table` decides "(no changes)" by comparing only the Before/After rows, so Phase X prints two tables: "X1-X8 (levels)" (Before is the paragraph level from X1) and "X1-X9 (types)" (overrides, FSI resolution, X9's BN). Without the levels table, an isolate or embedding with no override would show as "(no changes)". When `paragraph_embedding_level` is given (the demo's `--direction ltr|rtl`), `determine_paragraph_embedding_level` is not called; `apply_bidi_algorithm` prints the Phase P header itself, followed by a line saying that P2/P3 were skipped. After phase X, `apply_bidi_algorithm` calls `print_isolating_run_sequences` (also in `verbose_table.py`), which prints the X10 sequences one line each (level, character indices as ranges, `sos`/`eos`), then the indices of the characters removed by X9. Columns are always in logical order, including the L2 and L4 tables, where Before/After give each character's position before and after reordering. From I1 on (the I1, I2, L1, L2 and L4 tables), a character removed by X9 shows `x` as its level, in the Level row and in the I1/I2/L1 Before/After rows, and `x` as its L2 position. `print_table` does this when passed `after_x9=True`, through `format_level_for_table`. The P and X tables keep the numbers the X rules assigned, and the W and N tables never contain removed characters. Characters in table cells go through `format_char_for_table`: controls and whitespace are left blank, combining marks are shown on a dotted circle, and each R/AL character is followed by an invisible LEFT-TO-RIGHT MARK so that bidi-aware viewers (Markdown previews, GitHub) don't reverse runs of the row. Cells are sized and centered by `display_width`/`center_in_column`, not `len()`, because those marks take no column on screen. The tracing example in README.md is copied from real output (LRMs included); update it whenever the table format changes. diff --git a/bidi.py b/bidi.py index 6c794a0..164a7d2 100644 --- a/bidi.py +++ b/bidi.py @@ -1245,6 +1245,9 @@ def apply_bidi_algorithm( # Phase P: Determine paragraph embedding level if paragraph_embedding_level is not None: paragraph_level = paragraph_embedding_level + if verbose: + print(f"=== Phase P: Paragraph Embedding Level = {paragraph_level} ===") + print("--- Rule P2/P3 --- (skipped: paragraph embedding level given by the caller)") else: paragraph_level = determine_paragraph_embedding_level(chars, verbose=verbose)