Repository navigation
awk: report an unreadable -f file and run --source programs - #188
VanshajPoonia wants to merge 4 commits into
Conversation
|
GNU awk testsuite comparison: |
|
thanks for picking this up! |
| pub fn include_many( | ||
| &mut self, | ||
| files: &[PathBuf], | ||
| ) -> Result<Result<&mut Ast<'a>, DiagnosticStore>, (Rc<Path>, io::Error)> { |
There was a problem hiding this comment.
a Result<Result<..>> is hard to read
could you please use a thiserror enum (or put the io error into the diagnostics) instead?
There was a problem hiding this comment.
Replaced with an IncludeError thiserror enum: CannotOpen { path, err } for a -f file that can't be read, Invalid(DiagnosticStore) for parse errors.
|
|
||
| fn include(&mut self, path: Rc<Path>, ns: Option<&'static str>) -> (Result<()>, FileCache) { | ||
| // TODO: report an unreadable @include file instead of panicking. | ||
| let source = self.preprocessor.read_to_arena(&path, self.arena).unwrap(); |
There was a problem hiding this comment.
why not return the error here too? same message as for -f, no?
we shouldn't leave an unwrap here
There was a problem hiding this comment.
No unwrap left. An @include that can't be read is now a ParsingError::CannotOpenSource diagnostic at the include path, with the same message as -f. Like every parse diagnostic on main today, it still exits 0.
| Some(code) => code.as_encoded_bytes(), | ||
| None => &source_text, | ||
| }; | ||
| parser.parse(FileCache(None), code) |
There was a problem hiding this comment.
with both -f and --source, the --source texts are silently ignored
could you please concatenate them like gawk, or at least error out?
There was a problem hiding this comment.
They're read in command-line order now, like gawk: the -f files and --source texts are parsed as one program in the order given, and adjacent --source texts stay together so a construct can span them. A test covers both orders.
e93f5d7 to
5f25bdc
Compare
|
Thanks! Squashed into one commit and rebased on main. |
|
GNU awk testsuite comparison: |
5f25bdc to
c4cce21
Compare
|
GNU awk testsuite comparison: |
|
|
||
| /// The -f files and --source texts in command-line order, as gawk reads them. | ||
| /// Adjacent --source texts stay one piece, so a construct can span them. | ||
| fn program_sources<'a>(matches: &clap::ArgMatches, args: &'a Args) -> Vec<ProgramSource<'a>> { |
There was a problem hiding this comment.
I think it would be better to attempt to do this as clap item parser, so we don't have to post-process clap's output.
There was a problem hiding this comment.
Done in ProgramSources, a clap Args impl that gives the -f files and --source texts in order. clap keeps the two options in separate lists so it still reads the indices. But main.rs is back to Args::try_parse_from.
|
GNU awk testsuite comparison: |
| .write("prog.awk", "@include \"no-such-include.awk\"\n"); | ||
| ts.cmd(crate::TESTS_BINARY) | ||
| .args(&["-f", "prog.awk"]) | ||
| .run() |
There was a problem hiding this comment.
could you please check the exit code here too?
gawk exits 2 on an @include it can't open (fatal), we exit 1 as a parse error
|
GNU awk testsuite comparison: |
|
Thanks for the review. I added the exit code check to the test. |
| } | ||
|
|
||
| /// Prints a fatal error and exits with 2, gawk's status for fatal errors. | ||
| pub fn exit_fatal(msg: impl Display) -> ! { |
There was a problem hiding this comment.
exit_fatal was removed from main in c0ac1db
could you please use exit_err(Some(format!("awk: fatal: {err}")), AWK_FATAL_CODE) in main.rs instead?
| /// Why [`Parser::include_many`] has no program to run. | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum IncludeError { | ||
| #[error("cannot open source file `{}' for reading: {}", .path.display(), read_reason(.err))] |
There was a problem hiding this comment.
same message as ParsingError::CannotOpenSource, could be dedup, no?
-f with a file that can't be read panicked, and --source was ignored. Read the -f files and --source texts in command-line order like gawk, keeping adjacent --source texts together so a construct can span them. A -f file that can't be read is fatal with gawk's message and status 2; an @include that can't be read is a diagnostic at its path. With -f or --source there is no program operand, so the first operand is an input.
Each -f file and --source text is now its own piece in include_many, so an error's line number counts from the start of its own text and each text starts in the awk namespace, like gawk. FileCache gets a Cli(n) variant so every --source text has its own diagnostics entry. The order of -f and --source now comes from a clap Args impl, so main.rs no longer reads the arg matches. --source texts are borrowed from the arguments instead of copied into the arena.
7ce3eb4 to
11c1ef0
Compare
|
GNU awk testsuite comparison: |
gawk 5.4.2 reports it as an error and exits 1, not a fatal 2 like -f.
11c1ef0 to
b6f37c4
Compare
|
GNU awk testsuite comparison: |
|
Thank you, I worked on both of the comments. On the exit code. I ran gawk 5.4.2 again. A missing |
Both ways of giving the program as an option crash as the issue shows:
-ffile that can't be read now printsawk: fatal: cannot open source file `missing.awk' for reading: No such file or directoryand exits with 2, like gawk in the issue. The parser's file reader returns the error instead of callingunwrap().-for--sourcethere is no program operand, so the first operand is an input file, as the gawk manual describes. It was taken as the program instead:awk -f prog.awk dataignoreddataand read stdin, and--sourcealone had nothing to parse.--sourcetexts are now the program. Each one is read as if it ended with a newline, which the manual also says.Left out:
-fand--sourcetogether still use only the-ffiles. An unreadable@includefile still panics (marked TODO), since I couldn't check what gawk does there.--debugwith either option still panics (#168).exit_fatalandstrip_os_errorinsrc/utils.rsare the same as in #187, so the two merge cleanly in either order.Tests: five new tests (missing
-ffile,-fand--sourceeach with an input operand,--sourcealone, two-etexts), all fail on main.cargo test --workspace230 passed,cargo clippy --workspace --all-targets -- -D warningsandcargo fmt --all -- --checkclean.Closes #171