pub fn parse(input: &str) -> Result<Program>Examples found in repository?
examples/parse_file.rs (line 55)
18fn run(
19 args: impl IntoIterator<Item = OsString>,
20 stdin: &mut dyn Read,
21 output: &mut dyn Write,
22) -> Result<(), Box<dyn std::error::Error>> {
23 let mut dump_ast = false;
24 let mut positional = false;
25 let mut files = Vec::new();
26 for arg in args {
27 if !positional && arg == "--" {
28 positional = true;
29 } else if !positional && arg == "--dump-ast" {
30 dump_ast = true;
31 } else if !positional && (arg == "--help" || arg == "-h") {
32 writeln!(output, "{USAGE}")?;
33 return Ok(());
34 } else if !positional && arg != "-" && arg.to_string_lossy().starts_with('-') {
35 return Err(format!("unknown option {arg:?}\n{USAGE}").into());
36 } else {
37 files.push(arg);
38 }
39 }
40 if files.is_empty() {
41 return Err(USAGE.into());
42 }
43 if files.iter().filter(|file| *file == "-").count() > 1 {
44 return Err("stdin (-) may only be read once".into());
45 }
46 let multiple = files.len() > 1;
47 for file in files {
48 let input = if file == "-" {
49 let mut input = String::new();
50 stdin.read_to_string(&mut input)?;
51 input
52 } else {
53 std::fs::read_to_string(&file).map_err(|error| format!("{file:?}: {error}"))?
54 };
55 let program = parse(&input).map_err(|error| format!("{file:?}: {error}"))?;
56 if dump_ast {
57 if multiple {
58 writeln!(output, "==> {file:?} <==")?;
59 }
60 write!(output, "{}", format_ast(&program))?;
61 }
62 }
63 Ok(())
64}