Refactor to support multiple parse errors (#8765)

# Description

This is a pretty heavy refactor of the parser to support multiple parser
errors. It has a few issues we should address before landing:

- [x] In some cases, error quality has gotten worse `1 / "bob"` for
example
- [x] if/else isn't currently parsing correctly
- probably others

# User-Facing Changes

This may have error quality degradation as we adjust to the new error
reporting mechanism.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-utils/standard_library/tests.nu` to run the
tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
JT
2023-04-07 12:35:45 +12:00
committed by GitHub
parent e54b867e8e
commit aded2c1937
37 changed files with 2658 additions and 3379 deletions

View File

@ -64,15 +64,15 @@ pub(crate) fn parse_commandline_args(
let mut working_set = StateWorkingSet::new(engine_state);
working_set.add_decl(Box::new(Nu));
let (output, err) = parse(
let output = parse(
&mut working_set,
None,
commandline_args.as_bytes(),
false,
&[],
);
if let Some(err) = err {
report_error(&working_set, &err);
if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err);
std::process::exit(1);
}

View File

@ -3,10 +3,9 @@ use log::info;
use nu_cli::read_plugin_file;
use nu_cli::{eval_config_contents, eval_source};
use nu_command::util::report_error;
use nu_parser::ParseError;
use nu_path::canonicalize_with;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{PipelineData, Spanned};
use nu_protocol::{ParseError, PipelineData, Spanned};
use nu_utils::{get_default_config, get_default_env};
use std::fs::File;
use std::io::Write;

View File

@ -22,7 +22,7 @@ fn find_id(
location: &Value,
) -> Option<(Id, usize, Span)> {
let offset = working_set.next_span_start();
let (block, _) = parse(working_set, Some(file_path), file, false, &[]);
let block = parse(working_set, Some(file_path), file, false, &[]);
let flattened = flatten_block(working_set, &block);
@ -78,9 +78,9 @@ pub fn check(engine_state: &mut EngineState, file_path: &String) {
if let Ok(contents) = file {
let offset = working_set.next_span_start();
let (block, err) = parse(&mut working_set, Some(file_path), &contents, false, &[]);
let block = parse(&mut working_set, Some(file_path), &contents, false, &[]);
if let Some(err) = err {
for err in &working_set.parse_errors {
let mut span = err.span();
span.start -= offset;
span.end -= offset;

View File

@ -62,21 +62,21 @@ fn load_standard_library(
working_set.add_file(name.clone(), content);
let end = working_set.next_span_start();
let (_, module, comments, parse_error) = parse_module_block(
let (_, module, comments) = parse_module_block(
&mut working_set,
Span::new(start, end),
name.as_bytes(),
&[],
);
if let Some(err) = parse_error {
report_error(&working_set, &err);
if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err);
}
let (_, parse_error) = parse(&mut working_set, Some(&name), content, true, &[]);
parse(&mut working_set, Some(&name), content, true, &[]);
if let Some(err) = parse_error {
report_error(&working_set, &err);
if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err);
}
// TODO: change this when #8505 is merged

View File

@ -224,7 +224,7 @@ pub fn nu_repl() {
// Eval the REPL line
let (block, delta) = {
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(
let block = parse(
&mut working_set,
Some(&format!("line{i}")),
line.as_bytes(),
@ -232,8 +232,8 @@ pub fn nu_repl() {
&[],
);
if let Some(err) = err {
outcome_err(&engine_state, &err);
if let Some(err) = working_set.parse_errors.first() {
outcome_err(&engine_state, err);
}
(block, working_set.render())
};