Skip to content

Commit 5bf06cd

Browse files
committed
Add a ctfails test using %grmtools{test_files}
1 parent e1cbd4d commit 5bf06cd

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Test calculator with malformed input from %grmtools{test_files}
2+
grammar: |
3+
%grmtools {
4+
yacckind: Original(YaccOriginalActionKind::UserAction),
5+
recoverer: RecoveryKind::None,
6+
test_files: "*.bad_input"
7+
}
8+
%start Expr
9+
%actiontype Result<u64, ()>
10+
%avoid_insert 'INT'
11+
%%
12+
Expr: Expr '+' Term { Ok($1? + $3?) }
13+
| Term { $1 }
14+
;
15+
16+
Term: Term '*' Factor { Ok($1? * $3?) }
17+
| Factor { $1 }
18+
;
19+
20+
Factor: '(' Expr ')' { $2 }
21+
| 'INT' {
22+
let l = $1.map_err(|_| ())?;
23+
match $lexer.span_str(l.span()).parse::<u64>() {
24+
Ok(v) => Ok(v),
25+
Err(_) => {
26+
let ((_, col), _) = $lexer.line_col(l.span());
27+
eprintln!("Error at column {}: '{}' cannot be represented as a u64",
28+
col,
29+
$lexer.span_str(l.span()));
30+
Err(())
31+
}
32+
}
33+
}
34+
;
35+
36+
lexer: |
37+
%%
38+
[0-9]+ "INT"
39+
\+ "+"
40+
\* "*"
41+
\( "("
42+
\) ")"
43+
[\t\n ]+ ;
44+
extra_files:
45+
input1.bad_input: |
46+
(1 + 2 * 3
47+

0 commit comments

Comments
 (0)