Skip to content

Commit 51b4b97

Browse files
committed
emit error for unused header values
1 parent 3994571 commit 51b4b97

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lrlex/src/main.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use getopts::Options;
22
use std::{
33
env,
44
error::Error,
5+
fmt,
56
fs::File,
67
io::{stderr, stdin, Read, Write},
78
path::Path,
@@ -17,6 +18,22 @@ use lrpar::{
1718

1819
const ERROR: &str = "[Error]";
1920

21+
/// A string which uses `Display` for it's `Debug` impl.
22+
struct ErrorString(String);
23+
impl fmt::Display for ErrorString {
24+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25+
let ErrorString(s) = self;
26+
write!(f, "{}", s)
27+
}
28+
}
29+
impl fmt::Debug for ErrorString {
30+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31+
let ErrorString(s) = self;
32+
write!(f, "{}", s)
33+
}
34+
}
35+
impl Error for ErrorString {}
36+
2037
fn usage(prog: &str, msg: &str) {
2138
let path = Path::new(prog);
2239
let leaf = match path.file_name() {
@@ -109,9 +126,18 @@ fn main() -> Result<(), Box<dyn Error>> {
109126
lexerdef
110127
}
111128
_ => {
112-
return Err("Unrecognized lexer kind")?;
129+
return Err(ErrorString("Unrecognized lexer kind".to_string()))?;
113130
}
114131
};
132+
{
133+
let unused_header_values = header.unused();
134+
if !unused_header_values.is_empty() {
135+
return Err(ErrorString(format!(
136+
"Unused header values: {}",
137+
unused_header_values.join(", ")
138+
)))?;
139+
}
140+
}
115141
let input = &read_file(&matches.free[1]);
116142
for r in lexerdef.lexer(input).iter() {
117143
match r {

0 commit comments

Comments
 (0)