Parser refactoring for improving pipelines (#7162)

* Remove lite_parse file

* Add LiteElement to represent different pipeline elem types

* Add PipelineElement to Pipelines

* Remove lite_parse specific tests
This commit is contained in:
JT
2022-11-19 10:46:48 +13:00
committed by GitHub
parent bd30ea723e
commit 6454bf69aa
16 changed files with 1196 additions and 1099 deletions

View File

@ -310,10 +310,10 @@ mod test_examples {
let (mut block, delta) = parse(src, engine_state);
match block.pipelines.len() {
1 => {
let n_expressions = block.pipelines[0].expressions.len();
block.pipelines[0].expressions.truncate(&n_expressions - 1);
let n_expressions = block.pipelines[0].elements.len();
block.pipelines[0].elements.truncate(&n_expressions - 1);
if !block.pipelines[0].expressions.is_empty() {
if !block.pipelines[0].elements.is_empty() {
let empty_input = PipelineData::new(Span::test_data());
Some(eval_block(block, empty_input, cwd, engine_state, delta))
} else {

View File

@ -1,4 +1,4 @@
use nu_protocol::ast::{Call, Expr, Expression};
use nu_protocol::ast::{Call, Expr, Expression, PipelineElement};
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Range, ShellError, Signature, Span, Type,
@ -88,15 +88,11 @@ impl Command for FromNuon {
let (lexed, err) = nu_parser::lex(string_input.as_bytes(), 0, &[b'\n', b'\r'], &[], true);
error = error.or(err);
let (lite_block, err) = nu_parser::lite_parse(&lexed);
error = error.or(err);
let (mut block, err) =
nu_parser::parse_block(&mut working_set, &lite_block, true, &[], false);
let (mut block, err) = nu_parser::parse_block(&mut working_set, &lexed, true, &[], false);
error = error.or(err);
if let Some(pipeline) = block.pipelines.get(1) {
if let Some(expr) = pipeline.expressions.get(0) {
if let Some(element) = pipeline.elements.get(0) {
return Err(ShellError::GenericError(
"error when loading nuon text".into(),
"could not load nuon text".into(),
@ -106,7 +102,7 @@ impl Command for FromNuon {
string_input,
"error when loading".into(),
"excess values when loading".into(),
expr.span,
element.span(),
)],
));
} else {
@ -136,7 +132,7 @@ impl Command for FromNuon {
} else {
let mut pipeline = block.pipelines.remove(0);
if let Some(expr) = pipeline.expressions.get(1) {
if let Some(expr) = pipeline.elements.get(1) {
return Err(ShellError::GenericError(
"error when loading nuon text".into(),
"could not load nuon text".into(),
@ -146,12 +142,12 @@ impl Command for FromNuon {
string_input,
"error when loading".into(),
"detected a pipeline in nuon file".into(),
expr.span,
expr.span(),
)],
));
}
if pipeline.expressions.is_empty() {
if pipeline.elements.is_empty() {
Expression {
expr: Expr::Nothing,
span: head,
@ -159,7 +155,12 @@ impl Command for FromNuon {
ty: Type::Nothing,
}
} else {
pipeline.expressions.remove(0)
match pipeline.elements.remove(0) {
PipelineElement::Expression(expression)
| PipelineElement::Redirect(expression)
| PipelineElement::And(expression)
| PipelineElement::Or(expression) => expression,
}
}
};