From 03f3f682760312cc3a07c4da348247ed0a6343b7 Mon Sep 17 00:00:00 2001 From: matt rice Date: Thu, 3 Dec 2026 08:57:17 -0800 Subject: [PATCH 1/7] Add a flag for ignoring verbatim code This can improve grammar source compatibility posix lex. Other lex implementations still generally emit verbatim code in an unreachable area of the generated source. Meaning it is parsed, but not evaluated. --- cfgrammar/src/lib/header.rs | 3 +-- doc/src/lexextensions.md | 4 +++- lrlex/src/lib/codegen.rs | 3 +++ lrlex/src/lib/lexer.rs | 5 +++++ lrlex/src/lib/mod.rs | 2 +- lrlex/src/lib/parser.rs | 15 ++++++++++----- lrpar/cttests/src/ctfails/verbatim_code.test | 16 ++++++++++++++++ lrpar/cttests/src/ignore_verbatim_code.test | 17 +++++++++++++++++ 8 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 lrpar/cttests/src/ctfails/verbatim_code.test create mode 100644 lrpar/cttests/src/ignore_verbatim_code.test diff --git a/cfgrammar/src/lib/header.rs b/cfgrammar/src/lib/header.rs index 634dc9a38..727e8261f 100644 --- a/cfgrammar/src/lib/header.rs +++ b/cfgrammar/src/lib/header.rs @@ -214,8 +214,7 @@ pub static KEY_CRATE_MAP: LazyLock> = LazyLo let mut map = HashMap::new(); let cfgrammar = ["yacckind"]; let lrpar = ["recoverer", "test_files", "serialisation_format"]; - - let lrlex = ["lexerkind", "allow_wholeline_comments", "posix_escapes"]; + let lrlex = ["lexerkind", "allow_wholeline_comments", "posix_escapes", "ignore_verbatim_code",]; let regex = [ "case_insensitive", "dot_matches_new_line", diff --git a/doc/src/lexextensions.md b/doc/src/lexextensions.md index 5e7f08c0d..9f339dd05 100644 --- a/doc/src/lexextensions.md +++ b/doc/src/lexextensions.md @@ -31,6 +31,7 @@ other flags should specify their value immediately after the flag name. | `lrlex.lexerkind` | [LexerKind](lexcompatibility.md#lexerkinds) | ✗ | | `lrlex.posix_escapes`[^†] | bool | ✗ | | `lrlex.allow_wholeline_comments`[^‡] | bool | ✗ | +| `lrlex.ignore_verbatim_code` | bool | ✗ | | `regex.case_insensitive` | bool | ✗ | | `regex.dot_matches_new_line` | bool | ✗ | | `regex.multi_line` | bool | ✗ | @@ -58,7 +59,8 @@ The following flags can change the behavior to match posix lex more closely. ``` %grmtools { !dot_matches_new_line, - posix_escapes + posix_escapes, + ignore_verbatim_code, } %% ... diff --git a/lrlex/src/lib/codegen.rs b/lrlex/src/lib/codegen.rs index c5a147acb..a2af88397 100644 --- a/lrlex/src/lib/codegen.rs +++ b/lrlex/src/lib/codegen.rs @@ -382,6 +382,7 @@ where case_insensitive, unicode, swap_greed, + ignore_verbatim_code, ignore_whitespace, size_limit, dfa_size_limit, @@ -396,6 +397,7 @@ where let unicode = QuoteOption(unicode.as_ref()); let swap_greed = QuoteOption(swap_greed.as_ref()); let ignore_whitespace = QuoteOption(ignore_whitespace.as_ref()); + let ignore_verbatim_code = QuoteOption(ignore_verbatim_code.as_ref()); let size_limit = QuoteOption(size_limit.as_ref()); let dfa_size_limit = QuoteOption(dfa_size_limit.as_ref()); let nest_limit = QuoteOption(nest_limit.as_ref()); @@ -411,6 +413,7 @@ where lex_flags.case_insensitive = #case_insensitive.or(::lrlex::DEFAULT_LEX_FLAGS.case_insensitive); lex_flags.unicode = #unicode.or(::lrlex::DEFAULT_LEX_FLAGS.unicode); lex_flags.swap_greed = #swap_greed.or(::lrlex::DEFAULT_LEX_FLAGS.swap_greed); + lex_flags.ignore_verbatim_code = #ignore_verbatim_code.or(::lrlex::DEFAULT_LEX_FLAGS.ignore_verbatim_code); lex_flags.ignore_whitespace = #ignore_whitespace.or(::lrlex::DEFAULT_LEX_FLAGS.ignore_whitespace); lex_flags.size_limit = #size_limit.or(::lrlex::DEFAULT_LEX_FLAGS.size_limit); lex_flags.dfa_size_limit = #dfa_size_limit.or(::lrlex::DEFAULT_LEX_FLAGS.dfa_size_limit); diff --git a/lrlex/src/lib/lexer.rs b/lrlex/src/lib/lexer.rs index d2f9eb94c..bbb87bf4f 100644 --- a/lrlex/src/lib/lexer.rs +++ b/lrlex/src/lib/lexer.rs @@ -32,6 +32,7 @@ pub struct LexFlags { pub octal: Option, pub posix_escapes: Option, pub allow_wholeline_comments: Option, + pub ignore_verbatim_code: Option, // All the following values when `None` default to the `regex` crate's default value. pub case_insensitive: Option, @@ -55,6 +56,7 @@ impl TryFrom<&mut Header> for LexFlags { allow_wholeline_comments, case_insensitive, swap_greed, + ignore_verbatim_code, ignore_whitespace, unicode, size_limit, @@ -83,6 +85,7 @@ impl TryFrom<&mut Header> for LexFlags { cvt_flag!(lrlex, allow_wholeline_comments); cvt_flag!(regex, case_insensitive); cvt_flag!(regex, swap_greed); + cvt_flag!(regex, ignore_verbatim_code); cvt_flag!(regex, ignore_whitespace); cvt_flag!(regex, unicode); macro_rules! cvt_num { @@ -116,6 +119,7 @@ pub const DEFAULT_LEX_FLAGS: LexFlags = LexFlags { octal: Some(true), posix_escapes: Some(false), case_insensitive: None, + ignore_verbatim_code: Some(false), ignore_whitespace: None, swap_greed: None, unicode: None, @@ -133,6 +137,7 @@ pub const UNSPECIFIED_LEX_FLAGS: LexFlags = LexFlags { octal: None, posix_escapes: None, case_insensitive: None, + ignore_verbatim_code: None, ignore_whitespace: None, swap_greed: None, unicode: None, diff --git a/lrlex/src/lib/mod.rs b/lrlex/src/lib/mod.rs index 831d51c8a..9b1dceeaf 100644 --- a/lrlex/src/lib/mod.rs +++ b/lrlex/src/lib/mod.rs @@ -119,7 +119,7 @@ impl Spanned for LexBuildError { impl fmt::Display for LexBuildError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let s = match &self.kind { - LexErrorKind::VerbatimNotSupported => "Verbatim code not supported", + LexErrorKind::VerbatimNotSupported => "Verbatim code not supported, to ignore verbatim code enable the `ignore_verbatim_code` flag", LexErrorKind::PrematureEnd => "File ends prematurely", LexErrorKind::RoutinesNotSupported => "Routines not currently supported", LexErrorKind::UnknownDeclaration => "Unknown declaration", diff --git a/lrlex/src/lib/parser.rs b/lrlex/src/lib/parser.rs index 8192f33cc..90a123aa7 100644 --- a/lrlex/src/lib/parser.rs +++ b/lrlex/src/lib/parser.rs @@ -160,6 +160,7 @@ where octal, posix_escapes, case_insensitive, + ignore_verbatim_code, ignore_whitespace, swap_greed, unicode, @@ -174,6 +175,7 @@ where *allow_wholeline_comments = allow_wholeline_comments.or(DEFAULT_LEX_FLAGS.allow_wholeline_comments); *case_insensitive = case_insensitive.or(DEFAULT_LEX_FLAGS.case_insensitive); + *ignore_verbatim_code = ignore_verbatim_code.or(DEFAULT_LEX_FLAGS.ignore_verbatim_code); *ignore_whitespace = ignore_whitespace.or(DEFAULT_LEX_FLAGS.ignore_whitespace); *swap_greed = swap_greed.or(DEFAULT_LEX_FLAGS.swap_greed); *unicode = unicode.or(DEFAULT_LEX_FLAGS.unicode); @@ -418,11 +420,14 @@ where // Previously we allowed these, and trimmed leading spaces, parsing any rules after them. Currently we will emit an error. let j = self.parse_ws(i)?; if j != i { - let err = LexBuildError { - kind: LexErrorKind::VerbatimNotSupported, - spans: vec![Span::new(i, i + line_len)], - }; - errs.push(err); + if let Some(false) = self.lex_flags.ignore_verbatim_code { + let err = LexBuildError { + kind: LexErrorKind::VerbatimNotSupported, + spans: vec![Span::new(i, i + line_len)], + }; + errs.push(err); + } + i += line_len; continue; } diff --git a/lrpar/cttests/src/ctfails/verbatim_code.test b/lrpar/cttests/src/ctfails/verbatim_code.test new file mode 100644 index 000000000..3edeab9a0 --- /dev/null +++ b/lrpar/cttests/src/ctfails/verbatim_code.test @@ -0,0 +1,16 @@ +name: error on verbatim code in lrlex +grammar: | + %grmtools{ + yacckind: Original(NoAction), + } + %start Start + %% + Start: 'ANY' | 'a' | 'NL'; + +lexer: | + %grmtools{!dot_matches_new_line, octal, size_limit: 1048576} + %% + // verbatim code disguised as comment. + \141 'a' + . 'ANY' + [\n] 'NL' \ No newline at end of file diff --git a/lrpar/cttests/src/ignore_verbatim_code.test b/lrpar/cttests/src/ignore_verbatim_code.test new file mode 100644 index 000000000..bf03a27d9 --- /dev/null +++ b/lrpar/cttests/src/ignore_verbatim_code.test @@ -0,0 +1,17 @@ +name: ignore verbatim code in lrlex +grammar: | + %grmtools{ + yacckind: Original(NoAction), + ignore_verbatim_code, + } + %start Start + %% + // comment starting with whitespace + Start: 'ANY' | 'a' | 'NL'; + +lexer: | + %grmtools{!dot_matches_new_line, octal, size_limit: 1048576} + %% + \141 'a' + . 'ANY' + [\n] 'NL' \ No newline at end of file From 14feade623f09ed3b9b31e5d164fd26021647c1e Mon Sep 17 00:00:00 2001 From: matt rice Date: Thu, 3 Dec 2026 09:44:09 -0800 Subject: [PATCH 2/7] rustfmt --- cfgrammar/src/lib/header.rs | 7 ++++++- lrlex/src/lib/mod.rs | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cfgrammar/src/lib/header.rs b/cfgrammar/src/lib/header.rs index 727e8261f..7c4dccbfe 100644 --- a/cfgrammar/src/lib/header.rs +++ b/cfgrammar/src/lib/header.rs @@ -214,7 +214,12 @@ pub static KEY_CRATE_MAP: LazyLock> = LazyLo let mut map = HashMap::new(); let cfgrammar = ["yacckind"]; let lrpar = ["recoverer", "test_files", "serialisation_format"]; - let lrlex = ["lexerkind", "allow_wholeline_comments", "posix_escapes", "ignore_verbatim_code",]; + let lrlex = [ + "lexerkind", + "allow_wholeline_comments", + "posix_escapes", + "ignore_verbatim_code", + ]; let regex = [ "case_insensitive", "dot_matches_new_line", diff --git a/lrlex/src/lib/mod.rs b/lrlex/src/lib/mod.rs index 9b1dceeaf..deaa8ccfd 100644 --- a/lrlex/src/lib/mod.rs +++ b/lrlex/src/lib/mod.rs @@ -119,7 +119,9 @@ impl Spanned for LexBuildError { impl fmt::Display for LexBuildError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let s = match &self.kind { - LexErrorKind::VerbatimNotSupported => "Verbatim code not supported, to ignore verbatim code enable the `ignore_verbatim_code` flag", + LexErrorKind::VerbatimNotSupported => { + "Verbatim code not supported, to ignore verbatim code enable the `ignore_verbatim_code` flag" + } LexErrorKind::PrematureEnd => "File ends prematurely", LexErrorKind::RoutinesNotSupported => "Routines not currently supported", LexErrorKind::UnknownDeclaration => "Unknown declaration", From 74552ee6d13e1d083909bf629c3c1762bfa1657d Mon Sep 17 00:00:00 2001 From: matt rice Date: Thu, 3 Dec 2026 11:49:09 -0800 Subject: [PATCH 3/7] Fix codegen parsing of new field --- lrlex/src/lib/codegen.rs | 1 + lrpar/cttests/src/ctfails/wrong_crate_flags.test | 16 ++++++++++++++++ lrpar/cttests/src/ignore_verbatim_code.test | 14 ++++++++------ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 lrpar/cttests/src/ctfails/wrong_crate_flags.test diff --git a/lrlex/src/lib/codegen.rs b/lrlex/src/lib/codegen.rs index a2af88397..61038a648 100644 --- a/lrlex/src/lib/codegen.rs +++ b/lrlex/src/lib/codegen.rs @@ -228,6 +228,7 @@ where errs ); cvt_bool!("lrlex.posix_escapes", posix_escapes, errs); + cvt_bool!("lrlex.ignore_verbatim_code", ignore_verbatim_code, errs); cvt_bool!("regex.dot_matches_new_line", dot_matches_new_line, errs); cvt_bool!("regex.multi_line", multi_line, errs); cvt_bool!("regex.swap_greed", swap_greed, errs); diff --git a/lrpar/cttests/src/ctfails/wrong_crate_flags.test b/lrpar/cttests/src/ctfails/wrong_crate_flags.test new file mode 100644 index 000000000..bb29fe4be --- /dev/null +++ b/lrpar/cttests/src/ctfails/wrong_crate_flags.test @@ -0,0 +1,16 @@ +name: lrlex flag in lrpar +grammar: | + %grmtools{ + yacckind: Original(NoAction), + ignore_verbatim_code, + } + %start Start + %% + Start: 'ANY' | 'a' | 'NL'; + +lexer: | + %grmtools{!dot_matches_new_line, octal, size_limit: 1048576} + %% + \141 'a' + . 'ANY' + [\n] 'NL' \ No newline at end of file diff --git a/lrpar/cttests/src/ignore_verbatim_code.test b/lrpar/cttests/src/ignore_verbatim_code.test index bf03a27d9..f08a59a0e 100644 --- a/lrpar/cttests/src/ignore_verbatim_code.test +++ b/lrpar/cttests/src/ignore_verbatim_code.test @@ -1,17 +1,19 @@ name: ignore verbatim code in lrlex grammar: | - %grmtools{ - yacckind: Original(NoAction), - ignore_verbatim_code, - } + %grmtools{yacckind: Original(NoAction)} %start Start %% - // comment starting with whitespace Start: 'ANY' | 'a' | 'NL'; lexer: | - %grmtools{!dot_matches_new_line, octal, size_limit: 1048576} + %grmtools{ + !dot_matches_new_line, + octal, + ignore_verbatim_code, + size_limit: 1048576 + } %% + // verbatim code \141 'a' . 'ANY' [\n] 'NL' \ No newline at end of file From 3b99f9392bf0fb5939328ea7c0ad694d263e545c Mon Sep 17 00:00:00 2001 From: matt rice Date: Thu, 3 Dec 2026 12:13:28 -0800 Subject: [PATCH 4/7] move wrong_crate_flags test to other PR --- lrpar/cttests/src/ctfails/wrong_crate_flags.test | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 lrpar/cttests/src/ctfails/wrong_crate_flags.test diff --git a/lrpar/cttests/src/ctfails/wrong_crate_flags.test b/lrpar/cttests/src/ctfails/wrong_crate_flags.test deleted file mode 100644 index bb29fe4be..000000000 --- a/lrpar/cttests/src/ctfails/wrong_crate_flags.test +++ /dev/null @@ -1,16 +0,0 @@ -name: lrlex flag in lrpar -grammar: | - %grmtools{ - yacckind: Original(NoAction), - ignore_verbatim_code, - } - %start Start - %% - Start: 'ANY' | 'a' | 'NL'; - -lexer: | - %grmtools{!dot_matches_new_line, octal, size_limit: 1048576} - %% - \141 'a' - . 'ANY' - [\n] 'NL' \ No newline at end of file From b222f6fb58d717d86b6935a6f16707a9548fe0ab Mon Sep 17 00:00:00 2001 From: matt rice Date: Fri, 4 Dec 2026 04:53:15 -0800 Subject: [PATCH 5/7] Simplify test case --- lrpar/cttests/src/ctfails/verbatim_code.test | 10 ++++------ lrpar/cttests/src/ignore_verbatim_code.test | 11 +++-------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/lrpar/cttests/src/ctfails/verbatim_code.test b/lrpar/cttests/src/ctfails/verbatim_code.test index 3edeab9a0..2811f9f71 100644 --- a/lrpar/cttests/src/ctfails/verbatim_code.test +++ b/lrpar/cttests/src/ctfails/verbatim_code.test @@ -5,12 +5,10 @@ grammar: | } %start Start %% - Start: 'ANY' | 'a' | 'NL'; + Start: 'A'; lexer: | - %grmtools{!dot_matches_new_line, octal, size_limit: 1048576} + %grmtools{} %% - // verbatim code disguised as comment. - \141 'a' - . 'ANY' - [\n] 'NL' \ No newline at end of file + treat any line that starts with initial leading whitespace like this, as an error. + a 'A' \ No newline at end of file diff --git a/lrpar/cttests/src/ignore_verbatim_code.test b/lrpar/cttests/src/ignore_verbatim_code.test index f08a59a0e..ddb7a4590 100644 --- a/lrpar/cttests/src/ignore_verbatim_code.test +++ b/lrpar/cttests/src/ignore_verbatim_code.test @@ -3,17 +3,12 @@ grammar: | %grmtools{yacckind: Original(NoAction)} %start Start %% - Start: 'ANY' | 'a' | 'NL'; + Start: 'A'; lexer: | %grmtools{ - !dot_matches_new_line, - octal, ignore_verbatim_code, - size_limit: 1048576 } %% - // verbatim code - \141 'a' - . 'ANY' - [\n] 'NL' \ No newline at end of file + ignore completely the contents of any line that starts with initial whitespace as verbatim code + a 'A' From b292d4343cdc1b848f3fdba23bbffff18623ca0a Mon Sep 17 00:00:00 2001 From: matt rice Date: Fri, 4 Dec 2026 04:54:32 -0800 Subject: [PATCH 6/7] Remove empty %grmtools header --- lrpar/cttests/src/ctfails/verbatim_code.test | 1 - 1 file changed, 1 deletion(-) diff --git a/lrpar/cttests/src/ctfails/verbatim_code.test b/lrpar/cttests/src/ctfails/verbatim_code.test index 2811f9f71..02d686f38 100644 --- a/lrpar/cttests/src/ctfails/verbatim_code.test +++ b/lrpar/cttests/src/ctfails/verbatim_code.test @@ -8,7 +8,6 @@ grammar: | Start: 'A'; lexer: | - %grmtools{} %% treat any line that starts with initial leading whitespace like this, as an error. a 'A' \ No newline at end of file From f842132be6ef0e1a7a2efcf09efbdc637ce90b31 Mon Sep 17 00:00:00 2001 From: matt rice Date: Fri, 4 Dec 2026 05:08:36 -0800 Subject: [PATCH 7/7] Correct the key prefix from regex to lrlex --- lrlex/src/lib/lexer.rs | 6 +++--- lrpar/cttests/src/ctfails/verbatim_code.test | 3 ++- lrpar/cttests/src/ignore_verbatim_code.test | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lrlex/src/lib/lexer.rs b/lrlex/src/lib/lexer.rs index bbb87bf4f..576430ae6 100644 --- a/lrlex/src/lib/lexer.rs +++ b/lrlex/src/lib/lexer.rs @@ -78,14 +78,14 @@ impl TryFrom<&mut Header> for LexFlags { } }; } + cvt_flag!(lrlex, allow_wholeline_comments); + cvt_flag!(lrlex, ignore_verbatim_code); + cvt_flag!(lrlex, posix_escapes); cvt_flag!(regex, dot_matches_new_line); cvt_flag!(regex, multi_line); cvt_flag!(regex, octal); - cvt_flag!(lrlex, posix_escapes); - cvt_flag!(lrlex, allow_wholeline_comments); cvt_flag!(regex, case_insensitive); cvt_flag!(regex, swap_greed); - cvt_flag!(regex, ignore_verbatim_code); cvt_flag!(regex, ignore_whitespace); cvt_flag!(regex, unicode); macro_rules! cvt_num { diff --git a/lrpar/cttests/src/ctfails/verbatim_code.test b/lrpar/cttests/src/ctfails/verbatim_code.test index 02d686f38..148c75296 100644 --- a/lrpar/cttests/src/ctfails/verbatim_code.test +++ b/lrpar/cttests/src/ctfails/verbatim_code.test @@ -10,4 +10,5 @@ grammar: | lexer: | %% treat any line that starts with initial leading whitespace like this, as an error. - a 'A' \ No newline at end of file + a 'A' + [\t\n ]+ ; \ No newline at end of file diff --git a/lrpar/cttests/src/ignore_verbatim_code.test b/lrpar/cttests/src/ignore_verbatim_code.test index ddb7a4590..1bc91158f 100644 --- a/lrpar/cttests/src/ignore_verbatim_code.test +++ b/lrpar/cttests/src/ignore_verbatim_code.test @@ -12,3 +12,4 @@ lexer: | %% ignore completely the contents of any line that starts with initial whitespace as verbatim code a 'A' + [\t\n ]+ ;