Skip to content

Commit

Permalink
Merge pull request #466 from ratmice/stdin
Browse files Browse the repository at this point in the history
Allow Stdin to be used as input file for lrlex/nimbleparse
  • Loading branch information
ltratt authored Aug 22, 2024
2 parents 24b81a0 + 06ea4d6 commit 1221326
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lrlex/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use getopts::Options;
use std::{
env,
fs::File,
io::{stderr, Read, Write},
io::{stdin, stderr, Read, Write},
path::Path,
process,
str::FromStr,
Expand All @@ -26,14 +26,18 @@ fn usage(prog: &str, msg: &str) {
}

fn read_file(path: &str) -> String {
let mut s = String::new();
if path == "-" {
stdin().read_to_string(&mut s).unwrap();
return s;
}
let mut f = match File::open(path) {
Ok(r) => r,
Err(e) => {
writeln!(stderr(), "Can't open file {}: {}", path, e).ok();
process::exit(1);
}
};
let mut s = String::new();
f.read_to_string(&mut s).unwrap();
s
}
Expand Down
8 changes: 7 additions & 1 deletion nimbleparse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,13 @@ fn main() {
}
}

let input = read_file(&matches.free[2]);
let input = if &matches.free[2] == "-" {
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
s
} else {
read_file(&matches.free[2])
};
let lexer = lexerdef.lexer(&input);
let pb = RTParserBuilder::new(&grm, &stable).recoverer(recoverykind);
let (pt, errs) = pb.parse_generictree(&lexer);
Expand Down

0 comments on commit 1221326

Please sign in to comment.