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:
@ -134,8 +134,8 @@ impl Expression {
|
||||
}
|
||||
|
||||
if let Some(pipeline) = block.pipelines.get(0) {
|
||||
match pipeline.expressions.get(0) {
|
||||
Some(expr) => expr.has_in_variable(working_set),
|
||||
match pipeline.elements.get(0) {
|
||||
Some(element) => element.has_in_variable(working_set),
|
||||
None => false,
|
||||
}
|
||||
} else {
|
||||
@ -150,8 +150,8 @@ impl Expression {
|
||||
}
|
||||
|
||||
if let Some(pipeline) = block.pipelines.get(0) {
|
||||
match pipeline.expressions.get(0) {
|
||||
Some(expr) => expr.has_in_variable(working_set),
|
||||
match pipeline.elements.get(0) {
|
||||
Some(element) => element.has_in_variable(working_set),
|
||||
None => false,
|
||||
}
|
||||
} else {
|
||||
@ -256,7 +256,7 @@ impl Expression {
|
||||
let block = working_set.get_block(*block_id);
|
||||
|
||||
if let Some(pipeline) = block.pipelines.get(0) {
|
||||
if let Some(expr) = pipeline.expressions.get(0) {
|
||||
if let Some(expr) = pipeline.elements.get(0) {
|
||||
expr.has_in_variable(working_set)
|
||||
} else {
|
||||
false
|
||||
@ -302,10 +302,10 @@ impl Expression {
|
||||
let block = working_set.get_block(*block_id);
|
||||
|
||||
let new_expr = if let Some(pipeline) = block.pipelines.get(0) {
|
||||
if let Some(expr) = pipeline.expressions.get(0) {
|
||||
let mut new_expr = expr.clone();
|
||||
new_expr.replace_in_variable(working_set, new_var_id);
|
||||
Some(new_expr)
|
||||
if let Some(element) = pipeline.elements.get(0) {
|
||||
let mut new_element = element.clone();
|
||||
new_element.replace_in_variable(working_set, new_var_id);
|
||||
Some(new_element)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -317,7 +317,7 @@ impl Expression {
|
||||
|
||||
if let Some(new_expr) = new_expr {
|
||||
if let Some(pipeline) = block.pipelines.get_mut(0) {
|
||||
if let Some(expr) = pipeline.expressions.get_mut(0) {
|
||||
if let Some(expr) = pipeline.elements.get_mut(0) {
|
||||
*expr = new_expr
|
||||
}
|
||||
}
|
||||
@ -332,11 +332,11 @@ impl Expression {
|
||||
Expr::Closure(block_id) => {
|
||||
let block = working_set.get_block(*block_id);
|
||||
|
||||
let new_expr = if let Some(pipeline) = block.pipelines.get(0) {
|
||||
if let Some(expr) = pipeline.expressions.get(0) {
|
||||
let mut new_expr = expr.clone();
|
||||
new_expr.replace_in_variable(working_set, new_var_id);
|
||||
Some(new_expr)
|
||||
let new_element = if let Some(pipeline) = block.pipelines.get(0) {
|
||||
if let Some(element) = pipeline.elements.get(0) {
|
||||
let mut new_element = element.clone();
|
||||
new_element.replace_in_variable(working_set, new_var_id);
|
||||
Some(new_element)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -346,10 +346,10 @@ impl Expression {
|
||||
|
||||
let block = working_set.get_block_mut(*block_id);
|
||||
|
||||
if let Some(new_expr) = new_expr {
|
||||
if let Some(new_element) = new_element {
|
||||
if let Some(pipeline) = block.pipelines.get_mut(0) {
|
||||
if let Some(expr) = pipeline.expressions.get_mut(0) {
|
||||
*expr = new_expr
|
||||
if let Some(element) = pipeline.elements.get_mut(0) {
|
||||
*element = new_element
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -428,11 +428,11 @@ impl Expression {
|
||||
Expr::RowCondition(block_id) | Expr::Subexpression(block_id) => {
|
||||
let block = working_set.get_block(*block_id);
|
||||
|
||||
let new_expr = if let Some(pipeline) = block.pipelines.get(0) {
|
||||
if let Some(expr) = pipeline.expressions.get(0) {
|
||||
let mut new_expr = expr.clone();
|
||||
new_expr.replace_in_variable(working_set, new_var_id);
|
||||
Some(new_expr)
|
||||
let new_element = if let Some(pipeline) = block.pipelines.get(0) {
|
||||
if let Some(element) = pipeline.elements.get(0) {
|
||||
let mut new_element = element.clone();
|
||||
new_element.replace_in_variable(working_set, new_var_id);
|
||||
Some(new_element)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -442,10 +442,10 @@ impl Expression {
|
||||
|
||||
let block = working_set.get_block_mut(*block_id);
|
||||
|
||||
if let Some(new_expr) = new_expr {
|
||||
if let Some(new_element) = new_element {
|
||||
if let Some(pipeline) = block.pipelines.get_mut(0) {
|
||||
if let Some(expr) = pipeline.expressions.get_mut(0) {
|
||||
*expr = new_expr
|
||||
if let Some(element) = pipeline.elements.get_mut(0) {
|
||||
*element = new_element
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -499,8 +499,8 @@ impl Expression {
|
||||
let mut block = working_set.get_block(*block_id).clone();
|
||||
|
||||
for pipeline in block.pipelines.iter_mut() {
|
||||
for expr in pipeline.expressions.iter_mut() {
|
||||
expr.replace_span(working_set, replaced, new_span)
|
||||
for element in pipeline.elements.iter_mut() {
|
||||
element.replace_span(working_set, replaced, new_span)
|
||||
}
|
||||
}
|
||||
|
||||
@ -510,8 +510,8 @@ impl Expression {
|
||||
let mut block = working_set.get_block(*block_id).clone();
|
||||
|
||||
for pipeline in block.pipelines.iter_mut() {
|
||||
for expr in pipeline.expressions.iter_mut() {
|
||||
expr.replace_span(working_set, replaced, new_span)
|
||||
for element in pipeline.elements.iter_mut() {
|
||||
element.replace_span(working_set, replaced, new_span)
|
||||
}
|
||||
}
|
||||
|
||||
@ -589,8 +589,8 @@ impl Expression {
|
||||
let mut block = working_set.get_block(*block_id).clone();
|
||||
|
||||
for pipeline in block.pipelines.iter_mut() {
|
||||
for expr in pipeline.expressions.iter_mut() {
|
||||
expr.replace_span(working_set, replaced, new_span)
|
||||
for element in pipeline.elements.iter_mut() {
|
||||
element.replace_span(working_set, replaced, new_span)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,64 @@
|
||||
use std::ops::{Index, IndexMut};
|
||||
|
||||
use crate::ast::Expression;
|
||||
use crate::{ast::Expression, engine::StateWorkingSet, Span, VarId};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum PipelineElement {
|
||||
Expression(Expression),
|
||||
Redirect(Expression),
|
||||
And(Expression),
|
||||
Or(Expression),
|
||||
}
|
||||
|
||||
impl PipelineElement {
|
||||
pub fn span(&self) -> Span {
|
||||
match self {
|
||||
PipelineElement::Expression(expression)
|
||||
| PipelineElement::Redirect(expression)
|
||||
| PipelineElement::And(expression)
|
||||
| PipelineElement::Or(expression) => expression.span,
|
||||
}
|
||||
}
|
||||
pub fn has_in_variable(&self, working_set: &StateWorkingSet) -> bool {
|
||||
match self {
|
||||
PipelineElement::Expression(expression)
|
||||
| PipelineElement::Redirect(expression)
|
||||
| PipelineElement::And(expression)
|
||||
| PipelineElement::Or(expression) => expression.has_in_variable(working_set),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn replace_in_variable(&mut self, working_set: &mut StateWorkingSet, new_var_id: VarId) {
|
||||
match self {
|
||||
PipelineElement::Expression(expression)
|
||||
| PipelineElement::Redirect(expression)
|
||||
| PipelineElement::And(expression)
|
||||
| PipelineElement::Or(expression) => {
|
||||
expression.replace_in_variable(working_set, new_var_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn replace_span(
|
||||
&mut self,
|
||||
working_set: &mut StateWorkingSet,
|
||||
replaced: Span,
|
||||
new_span: Span,
|
||||
) {
|
||||
match self {
|
||||
PipelineElement::Expression(expression)
|
||||
| PipelineElement::Redirect(expression)
|
||||
| PipelineElement::And(expression)
|
||||
| PipelineElement::Or(expression) => {
|
||||
expression.replace_span(working_set, replaced, new_span)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Pipeline {
|
||||
pub expressions: Vec<Expression>,
|
||||
pub elements: Vec<PipelineElement>,
|
||||
}
|
||||
|
||||
impl Default for Pipeline {
|
||||
@ -15,34 +69,37 @@ impl Default for Pipeline {
|
||||
|
||||
impl Pipeline {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
expressions: vec![],
|
||||
}
|
||||
Self { elements: vec![] }
|
||||
}
|
||||
|
||||
pub fn from_vec(expressions: Vec<Expression>) -> Pipeline {
|
||||
Self { expressions }
|
||||
Self {
|
||||
elements: expressions
|
||||
.into_iter()
|
||||
.map(PipelineElement::Expression)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.expressions.len()
|
||||
self.elements.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.expressions.is_empty()
|
||||
self.elements.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl Index<usize> for Pipeline {
|
||||
type Output = Expression;
|
||||
type Output = PipelineElement;
|
||||
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
&self.expressions[index]
|
||||
&self.elements[index]
|
||||
}
|
||||
}
|
||||
|
||||
impl IndexMut<usize> for Pipeline {
|
||||
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
||||
&mut self.expressions[index]
|
||||
&mut self.elements[index]
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user