forked from extern/nushell
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:
@ -1,7 +1,7 @@
|
||||
use nu_parser::ParseError;
|
||||
use nu_parser::*;
|
||||
use nu_protocol::{
|
||||
ast::{Expr, Expression},
|
||||
ast::{Expr, Expression, PipelineElement},
|
||||
engine::{Command, EngineState, Stack, StateWorkingSet},
|
||||
Signature, SyntaxShape,
|
||||
};
|
||||
@ -54,10 +54,10 @@ pub fn parse_int() {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Int(3),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -72,7 +72,11 @@ pub fn parse_binary_with_hex_format() {
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert_eq!(expressions[0].expr, Expr::Binary(vec![0x13]))
|
||||
if let PipelineElement::Expression(expr) = &expressions[0] {
|
||||
assert_eq!(expr.expr, Expr::Binary(vec![0x13]))
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -86,7 +90,11 @@ pub fn parse_binary_with_incomplete_hex_format() {
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert_eq!(expressions[0].expr, Expr::Binary(vec![0x03]))
|
||||
if let PipelineElement::Expression(expr) = &expressions[0] {
|
||||
assert_eq!(expr.expr, Expr::Binary(vec![0x03]))
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -100,7 +108,11 @@ pub fn parse_binary_with_binary_format() {
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert_eq!(expressions[0].expr, Expr::Binary(vec![0b10101000]))
|
||||
if let PipelineElement::Expression(expr) = &expressions[0] {
|
||||
assert_eq!(expr.expr, Expr::Binary(vec![0b10101000]))
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -114,7 +126,11 @@ pub fn parse_binary_with_incomplete_binary_format() {
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert_eq!(expressions[0].expr, Expr::Binary(vec![0b00000010]))
|
||||
if let PipelineElement::Expression(expr) = &expressions[0] {
|
||||
assert_eq!(expr.expr, Expr::Binary(vec![0b00000010]))
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -128,7 +144,11 @@ pub fn parse_binary_with_octal_format() {
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert_eq!(expressions[0].expr, Expr::Binary(vec![0o250]))
|
||||
if let PipelineElement::Expression(expr) = &expressions[0] {
|
||||
assert_eq!(expr.expr, Expr::Binary(vec![0o250]))
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -142,7 +162,11 @@ pub fn parse_binary_with_incomplete_octal_format() {
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert_eq!(expressions[0].expr, Expr::Binary(vec![0o2]))
|
||||
if let PipelineElement::Expression(expr) = &expressions[0] {
|
||||
assert_eq!(expr.expr, Expr::Binary(vec![0o2]))
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -156,7 +180,11 @@ pub fn parse_binary_with_invalid_octal_format() {
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(!matches!(&expressions[0].expr, Expr::Binary(_)))
|
||||
if let PipelineElement::Expression(expr) = &expressions[0] {
|
||||
assert!(!matches!(&expr.expr, Expr::Binary(_)))
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -172,7 +200,11 @@ pub fn parse_binary_with_multi_byte_char() {
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(!matches!(&expressions[0].expr, Expr::Binary(_)))
|
||||
if let PipelineElement::Expression(expr) = &expressions[0] {
|
||||
assert!(!matches!(&expr.expr, Expr::Binary(_)))
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -186,10 +218,11 @@ pub fn parse_string() {
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert_eq!(
|
||||
expressions[0].expr,
|
||||
Expr::String("hello nushell".to_string())
|
||||
)
|
||||
if let PipelineElement::Expression(expr) = &expressions[0] {
|
||||
assert_eq!(expr.expr, Expr::String("hello nushell".to_string()))
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -209,10 +242,11 @@ pub fn parse_escaped_string() {
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert_eq!(
|
||||
expressions[0].expr,
|
||||
Expr::String("hello nushell".to_string())
|
||||
)
|
||||
if let PipelineElement::Expression(expr) = &expressions[0] {
|
||||
assert_eq!(expr.expr, Expr::String("hello nushell".to_string()))
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -231,10 +265,10 @@ pub fn parse_call() {
|
||||
let expressions = &block[0];
|
||||
assert_eq!(expressions.len(), 1);
|
||||
|
||||
if let Expression {
|
||||
if let PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
} = &expressions[0]
|
||||
}) = &expressions[0]
|
||||
{
|
||||
assert_eq!(call.decl_id, 0);
|
||||
}
|
||||
@ -337,10 +371,10 @@ fn test_nothing_comparisson_eq() {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
&expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::BinaryOp(..),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -357,10 +391,10 @@ fn test_nothing_comparisson_neq() {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
&expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::BinaryOp(..),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -382,7 +416,7 @@ mod range {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Range(
|
||||
Some(_),
|
||||
None,
|
||||
@ -393,7 +427,7 @@ mod range {
|
||||
}
|
||||
),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -411,7 +445,7 @@ mod range {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Range(
|
||||
Some(_),
|
||||
None,
|
||||
@ -422,7 +456,7 @@ mod range {
|
||||
}
|
||||
),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -440,7 +474,7 @@ mod range {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Range(
|
||||
Some(_),
|
||||
None,
|
||||
@ -451,7 +485,7 @@ mod range {
|
||||
}
|
||||
),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -469,7 +503,7 @@ mod range {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Range(
|
||||
Some(_),
|
||||
None,
|
||||
@ -480,7 +514,7 @@ mod range {
|
||||
}
|
||||
),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -500,7 +534,7 @@ mod range {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Range(
|
||||
Some(_),
|
||||
None,
|
||||
@ -511,7 +545,7 @@ mod range {
|
||||
}
|
||||
),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -537,7 +571,7 @@ mod range {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Range(
|
||||
Some(_),
|
||||
None,
|
||||
@ -548,7 +582,7 @@ mod range {
|
||||
}
|
||||
),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -566,7 +600,7 @@ mod range {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Range(
|
||||
Some(_),
|
||||
None,
|
||||
@ -577,7 +611,7 @@ mod range {
|
||||
}
|
||||
),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -595,7 +629,7 @@ mod range {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Range(
|
||||
None,
|
||||
None,
|
||||
@ -606,7 +640,7 @@ mod range {
|
||||
}
|
||||
),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -624,7 +658,7 @@ mod range {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Range(
|
||||
Some(_),
|
||||
None,
|
||||
@ -635,7 +669,7 @@ mod range {
|
||||
}
|
||||
),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -653,7 +687,7 @@ mod range {
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(matches!(
|
||||
expressions[0],
|
||||
Expression {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Range(
|
||||
Some(_),
|
||||
Some(_),
|
||||
@ -664,7 +698,7 @@ mod range {
|
||||
}
|
||||
),
|
||||
..
|
||||
}
|
||||
})
|
||||
))
|
||||
}
|
||||
|
||||
@ -997,8 +1031,11 @@ mod input_types {
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 3);
|
||||
|
||||
match &expressions[0].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[0] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set
|
||||
.find_decl(b"ls", &Type::Any)
|
||||
.expect("Error merging delta");
|
||||
@ -1007,16 +1044,22 @@ mod input_types {
|
||||
_ => panic!("Expected expression Call not found"),
|
||||
}
|
||||
|
||||
match &expressions[1].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[1] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set.find_decl(b"to-custom", &Type::Any).unwrap();
|
||||
assert_eq!(call.decl_id, expected_id)
|
||||
}
|
||||
_ => panic!("Expected expression Call not found"),
|
||||
}
|
||||
|
||||
match &expressions[2].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[2] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set
|
||||
.find_decl(b"group-by", &Type::Custom("custom".into()))
|
||||
.unwrap();
|
||||
@ -1041,8 +1084,11 @@ mod input_types {
|
||||
assert!(block.len() == 3);
|
||||
|
||||
let expressions = &block[2];
|
||||
match &expressions[1].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[1] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set
|
||||
.find_decl(b"agg", &Type::Custom("custom".into()))
|
||||
.unwrap();
|
||||
@ -1066,8 +1112,11 @@ mod input_types {
|
||||
assert!(block.len() == 2);
|
||||
|
||||
let expressions = &block[1];
|
||||
match &expressions[1].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[1] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set
|
||||
.find_decl(b"agg", &Type::Custom("custom".into()))
|
||||
.unwrap();
|
||||
@ -1092,8 +1141,11 @@ mod input_types {
|
||||
assert!(block.len() == 3);
|
||||
|
||||
let expressions = &block[1];
|
||||
match &expressions[1].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[1] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set.find_decl(b"to-custom", &Type::Any).unwrap();
|
||||
assert_eq!(call.decl_id, expected_id)
|
||||
}
|
||||
@ -1101,8 +1153,11 @@ mod input_types {
|
||||
}
|
||||
|
||||
let expressions = &block[2];
|
||||
match &expressions[1].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[1] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set.find_decl(b"to-custom", &Type::Any).unwrap();
|
||||
assert_eq!(call.decl_id, expected_id)
|
||||
}
|
||||
@ -1126,16 +1181,22 @@ mod input_types {
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 2);
|
||||
|
||||
match &expressions[0].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[0] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set.find_decl(b"ls", &Type::Any).unwrap();
|
||||
assert_eq!(call.decl_id, expected_id)
|
||||
}
|
||||
_ => panic!("Expected expression Call not found"),
|
||||
}
|
||||
|
||||
match &expressions[1].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[1] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set.find_decl(b"group-by", &Type::Any).unwrap();
|
||||
assert_eq!(call.decl_id, expected_id)
|
||||
}
|
||||
@ -1159,8 +1220,11 @@ mod input_types {
|
||||
engine_state.merge_delta(delta).unwrap();
|
||||
|
||||
let expressions = &block[0];
|
||||
match &expressions[3].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[3] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let arg = &call.arguments[0];
|
||||
match arg {
|
||||
Argument::Positional(a) => match &a.expr {
|
||||
@ -1171,8 +1235,11 @@ mod input_types {
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 2);
|
||||
|
||||
match &expressions[1].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[1] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let working_set = StateWorkingSet::new(&engine_state);
|
||||
let expected_id =
|
||||
working_set.find_decl(b"min", &Type::Any).unwrap();
|
||||
@ -1206,8 +1273,11 @@ mod input_types {
|
||||
assert!(block.len() == 1);
|
||||
|
||||
let expressions = &block[0];
|
||||
match &expressions[2].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[2] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set
|
||||
.find_decl(b"with-column", &Type::Custom("custom".into()))
|
||||
.unwrap();
|
||||
@ -1216,8 +1286,11 @@ mod input_types {
|
||||
_ => panic!("Expected expression Call not found"),
|
||||
}
|
||||
|
||||
match &expressions[3].expr {
|
||||
Expr::Call(call) => {
|
||||
match &expressions[3] {
|
||||
PipelineElement::Expression(Expression {
|
||||
expr: Expr::Call(call),
|
||||
..
|
||||
}) => {
|
||||
let expected_id = working_set
|
||||
.find_decl(b"collect", &Type::Custom("custom".into()))
|
||||
.unwrap();
|
||||
|
Reference in New Issue
Block a user