Skip to content

Add support for a and L regex flags - #1698

Open
ununto wants to merge 1 commit into
lark-parser:masterfrom
ununto:add-regex-flags-a-L
Open

ununto wants to merge 1 commit into
lark-parser:masterfrom
ununto:add-regex-flags-a-L

Conversation

@ununto

@ununto ununto commented Nov 10, 2026

Copy link
Copy Markdown

Adds support for the ASCII (a) and locale (L) regular expression flags in Lark grammars.

Problem

Lark previously allowed only the flags imslux. Python's re module has supported local (?a:...) and (?L:...) flags since 3.7 (see #1527).

Implementation

  • Updated _RE_FLAGS in lark/load_grammar.py from imslux to aLimsux.
  • Updated the grammar reference in docs/grammar.md to document the new flags.

Flags continue to be applied via inline groups ((?f:...)) in Pattern._get_flags(), which is the correct mechanism for local flags.

Note on the L flag

Python only allows the L (LOCALE) flag on bytes patterns. Lark currently validates regexps as str first, so /.../L raises a clear error at grammar construction time for normal (str) grammars. Full bytes-mode support for L would need additional validation changes and is left for a follow-up if desired. Prefer Unicode matching (the default) or the a flag for ASCII-only matching.

Tests

Verified locally:

  • a flag restricts \w to ASCII (rejects non-ASCII word characters such as é).
  • Existing flag tests (test_token_flags, test_token_flags_verbose, test_join_regex_flags, etc.) continue to pass on LALR and Earley.
  • /.../L raises as expected on str grammars.

Related

Fixes #1527.

Update the allowed regex flags in the grammar parser and documentation to include the ASCII (a) and locale (L) flags supported by Python's re module since 3.7.

Fixes lark-parser#1527

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.

Add support for the a and L regex flags

1 participant