Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cfgrammar/src/lib/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,12 @@ pub static KEY_CRATE_MAP: LazyLock<HashMap<&'static str, &'static str>> = 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",
Expand Down
4 changes: 3 additions & 1 deletion doc/src/lexextensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ other flags should specify their value immediately after the flag name.
| `lrlex.lexerkind` | [LexerKind](lexcompatibility.md#lexerkinds) | &cross; |
| `lrlex.posix_escapes`[^†] | bool | &cross; |
| `lrlex.allow_wholeline_comments`[^‡] | bool | &cross; |
| `lrlex.ignore_verbatim_code` | bool | &cross; |
| `regex.case_insensitive` | bool | &cross; |
| `regex.dot_matches_new_line` | bool | &cross; |
| `regex.multi_line` | bool | &cross; |
Expand Down Expand Up @@ -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,
}
%%
...
Expand Down
4 changes: 4 additions & 0 deletions lrlex/src/lib/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -382,6 +383,7 @@ where
case_insensitive,
unicode,
swap_greed,
ignore_verbatim_code,
ignore_whitespace,
size_limit,
dfa_size_limit,
Expand All @@ -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());
Expand All @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions lrlex/src/lib/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct LexFlags {
pub octal: Option<bool>,
pub posix_escapes: Option<bool>,
pub allow_wholeline_comments: Option<bool>,
pub ignore_verbatim_code: Option<bool>,

// All the following values when `None` default to the `regex` crate's default value.
pub case_insensitive: Option<bool>,
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion lrlex/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 10 additions & 5 deletions lrlex/src/lib/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ where
octal,
posix_escapes,
case_insensitive,
ignore_verbatim_code,
ignore_whitespace,
swap_greed,
unicode,
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions lrpar/cttests/src/ctfails/verbatim_code.test
Original file line number Diff line number Diff line change
@@ -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 ]+ ;
15 changes: 15 additions & 0 deletions lrpar/cttests/src/ignore_verbatim_code.test
Original file line number Diff line number Diff line change
@@ -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 ]+ ;