diff --git a/cfgrammar/src/lib/header.rs b/cfgrammar/src/lib/header.rs index 634dc9a38..7c4dccbfe 100644 --- a/cfgrammar/src/lib/header.rs +++ b/cfgrammar/src/lib/header.rs @@ -214,8 +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"]; + 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..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); @@ -382,6 +383,7 @@ where case_insensitive, unicode, swap_greed, + ignore_verbatim_code, ignore_whitespace, size_limit, dfa_size_limit, @@ -396,6 +398,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 +414,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..576430ae6 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, @@ -76,11 +78,12 @@ 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_whitespace); @@ -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..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", + 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..148c75296 --- /dev/null +++ b/lrpar/cttests/src/ctfails/verbatim_code.test @@ -0,0 +1,14 @@ +name: error on verbatim code in lrlex +grammar: | + %grmtools{ + yacckind: Original(NoAction), + } + %start Start + %% + Start: 'A'; + +lexer: | + %% + treat any line that starts with initial leading whitespace like this, as an error. + 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 new file mode 100644 index 000000000..1bc91158f --- /dev/null +++ b/lrpar/cttests/src/ignore_verbatim_code.test @@ -0,0 +1,15 @@ +name: ignore verbatim code in lrlex +grammar: | + %grmtools{yacckind: Original(NoAction)} + %start Start + %% + Start: 'A'; + +lexer: | + %grmtools{ + ignore_verbatim_code, + } + %% + ignore completely the contents of any line that starts with initial whitespace as verbatim code + a 'A' + [\t\n ]+ ;