Repository navigation
Conversation
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
force-pushed
the
fix/add-a-L-regex-flags
branch
from
November 8, 2026 08:49
59ab8b5 to
85e0be3
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1527.
Lark currently only supports the flags
imsluxin grammar regexp terminals (e.g./pattern/flags). This PR extends support to include the two remaining Pythonreflags:a(ASCII): restricts\w,\d,\s,\betc. 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_RE_FLAGSfrom'imslux'to'imsluxaL'. TheREGEXPterminal regex inTERMINALSautomatically picks this up since it interpolates_RE_FLAGSat module load time.GrammarErrorwhen theLflag is used on a string literal, since Python 3'sremodule raisesre.error: cannot use L flag with a str patternwhen compiling such a pattern inline. This surfaces a clear, early error message instead of a crypticLexErrorat lexer compile time.tests/test_parser.pytest_regex_a_flag: Verifies that/\w+/aonly matches ASCII word characters and rejects non-ASCII input likeelevewith accents.test_regex_L_flag_string_error: Verifies that usingLon a string literal correctly raisesGrammarError.Notes
(?a:...)inline group flag works in Python 3.6+.Lflag cannot be used as an inline flag with string patterns in Python 3. If users needLwithuse_bytes=True, they should passre.LOCALEviag_regex_flags.