Skip to content

Add support for the a and L regex flags in grammar terminals (closes #1527) - #1697

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

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

Conversation

@VXNSH7

@VXNSH7 VXNSH7 commented Nov 8, 2026

Copy link
Copy Markdown

Summary

Closes #1527.

Lark currently only supports the flags imslux in grammar regexp terminals (e.g. /pattern/flags). This PR extends support to include the two remaining Python re flags:

  • a (ASCII): restricts \w, \d, \s, \b etc. to ASCII-only matches. Useful when you explicitly want to avoid matching Unicode word characters.
  • L (LOCALE): locale-dependent matching, only valid for bytes patterns in Python 3.

Changes

lark/load_grammar.py

  • Extended _RE_FLAGS from 'imslux' to 'imsluxaL'. The REGEXP terminal regex in TERMINALS automatically picks this up since it interpolates _RE_FLAGS at module load time.
  • Added a GrammarError when the L flag is used on a string literal, since Python 3's re module raises re.error: cannot use L flag with a str pattern when compiling such a pattern inline. This surfaces a clear, early error message instead of a cryptic LexError at lexer compile time.

tests/test_parser.py

  • test_regex_a_flag: Verifies that /\w+/a only matches ASCII word characters and rejects non-ASCII input like eleve with accents.
  • test_regex_L_flag_string_error: Verifies that using L on a string literal correctly raises GrammarError.

Notes

  • (?a:...) inline group flag works in Python 3.6+.
  • The L flag cannot be used as an inline flag with string patterns in Python 3. If users need L with use_bytes=True, they should pass re.LOCALE via g_regex_flags.
  • All 1008 existing tests continue to pass.

 lark-parser#1527)

- Extend _RE_FLAGS from 'imslux' to 'imsluxaL' so grammar terminals
  can now use the 'a' (ASCII) and 'L' (locale) Python regex flags.
- The REGEXP terminal regex in TERMINALS is auto-updated since it
  interpolates _RE_FLAGS at module load time.
- Add a GrammarError when the 'L' flag is used on a string literal,
  since Python's re module only supports 'L' for bytes patterns
  (use_bytes=True). This surfaces a clear error message early instead
  of a cryptic re.error at lexer compile time.
- Add two new tests: test_regex_a_flag verifies that /\w+/a only
  matches ASCII word characters; test_regex_L_flag_string_error
  verifies that using 'L' on a string literal raises GrammarError.
@VXNSH7
VXNSH7 force-pushed the fix/add-a-L-regex-flags branch from 59ab8b5 to 85e0be3 Compare November 8, 2026 08:49

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