Skip to content

Commit 54f907f

Browse files
committed
Implement field-like flags in lex, add a cttest.
Contrary to the comment field-like flags are used in lrlex this adds the missing implementation and adds a cttest that would have caught the mistake in either the grmtools_section.test or when building the `.test`
1 parent 1970c6d commit 54f907f

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

lrlex/src/lib/parser.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ where
304304
});
305305
}
306306
i = self.parse_spaces(i)?;
307+
if let Some(j) = self.lookahead_is(":", i) {
308+
i = j
309+
} else {
310+
return Err(LexBuildError {
311+
kind: LexErrorKind::InvalidGrmtoolsSectionValue,
312+
spans: vec![Span::new(i, i)],
313+
});
314+
}
315+
i = self.parse_spaces(i)?;
307316
let j = self.parse_digits(i)?;
308317
// This checks that the digits are valid numbers, but currently just returns `None`
309318
// when the values are actually out of range for that type. This could be improved.
@@ -340,7 +349,6 @@ where
340349
let mut grmtools_section_span_map = HashMap::new();
341350
let mut grmtools_section_lex_flags = UNSPECIFIED_LEX_FLAGS;
342351
if let Some(j) = self.lookahead_is("%grmtools", i) {
343-
// lrlex currently doesn't have any `key: value' settings.
344352
i = self.parse_ws(j)?;
345353
if let Some(j) = self.lookahead_is("{", i) {
346354
i = self.parse_ws(j)?;
@@ -1889,8 +1897,8 @@ b "A"
18891897

18901898
let src = r#"
18911899
%grmtools {
1892-
size_limit 5,
1893-
size_limit 6,
1900+
size_limit: 5,
1901+
size_limit: 6,
18941902
}
18951903
%%
18961904
. "dot";
@@ -1904,7 +1912,7 @@ b "A"
19041912

19051913
let src = r#"
19061914
%grmtools {
1907-
!size_limit 5,
1915+
!size_limit: 5,
19081916
}
19091917
%%
19101918
. "dot"

lrpar/cttests/src/lex_flags.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Lex flags in the grmtools section
2+
grammar: |
3+
%grmtools{yacckind: Original(NoAction)}
4+
%start Start
5+
%%
6+
Start: 'ANY' | 'a' | 'NL';
7+
8+
lexer: |
9+
%grmtools{!dot_matches_new_line, octal, size_limit: 1048576}
10+
%%
11+
\141 'a'
12+
. 'ANY'
13+
[\n] 'NL'

lrpar/cttests/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ lrpar_mod!("expect.y");
3535
lrlex_mod!("lexer_lifetime.l");
3636
lrpar_mod!("lexer_lifetime.y");
3737

38+
lrlex_mod!("lex_flags.l");
39+
lrpar_mod!("lex_flags.y");
40+
3841
lrlex_mod!("multitypes.l");
3942
lrpar_mod!("multitypes.y");
4043

@@ -47,6 +50,9 @@ lrpar_mod!("parseparam_copy.y");
4750
lrlex_mod!("passthrough.l");
4851
lrpar_mod!("passthrough.y");
4952

53+
lrlex_mod!("regex_opt.l");
54+
lrlex_mod!("regex_opt.y");
55+
5056
lrlex_mod!("span.l");
5157
lrpar_mod!("span.y");
5258

@@ -394,6 +400,28 @@ fn test_grmtools_section() {
394400
}
395401
}
396402

403+
// regex options set through the builder methods.
404+
#[test]
405+
fn test_regex_opt() {
406+
let lexerdef = regex_opt_l::lexerdef();
407+
let lexer = lexerdef.lexer("a");
408+
match regex_opt_y::parse(&lexer) {
409+
ref errs if errs.is_empty() => (),
410+
e => panic!("{:?}", e),
411+
}
412+
}
413+
414+
// Lex flags set through the grmtools section.
415+
#[test]
416+
fn test_lex_flags() {
417+
let lexerdef = lex_flags_l::lexerdef();
418+
let lexer = lexerdef.lexer("a");
419+
match lex_flags_y::parse(&lexer) {
420+
ref errs if errs.is_empty() => (),
421+
e => panic!("{:?}", e),
422+
}
423+
}
424+
397425
// Codegen failure tests
398426
#[cfg(test)]
399427
generate_codegen_fail_tests!("src/ctfails/*.test");

lrpar/cttests/src/regex_opt.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test NoAction using the calculator grammar
1+
name: Test regex options via builder.
22
yacckind: Original(YaccOriginalActionKind::NoAction)
33
lex_flags: ['!dot_matches_new_line', 'octal']
44
grammar: |

0 commit comments

Comments
 (0)