Skip to content

Keep escapes in verbose (x flag) regexp terminals - #1702

Open
imsurya26 wants to merge 1 commit into
lark-parser:masterfrom
imsurya26:fix/verbose-regexp-escapes
Open

imsurya26 wants to merge 1 commit into
lark-parser:masterfrom
imsurya26:fix/verbose-regexp-escapes

Conversation

@imsurya26

Copy link
Copy Markdown

In a regexp terminal with the x (verbose) flag, escapes such as \t, \n, \xHH and \uHHHH are evaluated by eval_escaping before the pattern reaches re. They become literal whitespace, which verbose mode then ignores:

Lark(r"""
start: NL
NL: /\t|a/x
""")
# LexError: Lexer does not allow zero-width terminals. (NL: (?x:	|a))

/\x20/x similarly compiles to (?x: ) and can never match a space, and /\x23a/x turns into (?x:#a), where the # now starts a comment.

_literal_to_pattern now asks eval_escaping to keep the backslash of these escapes for regexp terminals that use the x flag, so re interprets them itself (it supports all of them). String literals and regexps without x are unchanged.

Added test_token_flags_verbose_escapes next to the existing verbose tests; it fails without the change for every parser/lexer combination. tests/test_grammar.py, tests/test_lexer.py, tests/test_tools.py and the regexp/flag-related tests in tests/test_parser.py pass.

@imsurya26

Copy link
Copy Markdown
Author

The failing build (3.8, ubuntu-latest) check isn't caused by this change. The job stops at the actions/setup-python step with The version '3.8' with architecture 'x64' was not found for Ubuntu 26.04. ubuntu-latest now resolves to Ubuntu 26.04, which has no Python 3.8 build, so the tests never ran for that entry.

Since the cause is the python-version: ["3.8", ...] x ubuntu-latest entry in .github/workflows/tests.yml, I've left the workflow alone. It's a separate matter from this PR, for the maintainers to decide (for example dropping 3.8 or pinning that entry to an older runner).

On my side, test_token_flags_verbose_escapes and the other token_flags tests pass locally on Python 3.14 for every parser/lexer combination. The other matrix entries can be used to confirm the change on CI.

@erezsh

erezsh commented Nov 22, 2026

Copy link
Copy Markdown
Member

yes, it's an out of date version, that needs to be fixed separately.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants