2021-09-06 22:02:24 +00:00
|
|
|
use nu_protocol::ast::{Block, Expr, Expression, PathMember, Pipeline, Statement};
|
2021-09-02 18:21:37 +00:00
|
|
|
use nu_protocol::{engine::StateWorkingSet, Span};
|
2021-07-22 19:50:59 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum FlatShape {
|
|
|
|
Garbage,
|
2021-07-24 05:57:17 +00:00
|
|
|
Bool,
|
2021-07-22 19:50:59 +00:00
|
|
|
Int,
|
2021-08-08 20:21:21 +00:00
|
|
|
Float,
|
2021-09-04 21:52:57 +00:00
|
|
|
Range,
|
2021-07-22 19:50:59 +00:00
|
|
|
InternalCall,
|
|
|
|
External,
|
2021-09-10 08:07:18 +00:00
|
|
|
ExternalArg,
|
2021-07-22 19:50:59 +00:00
|
|
|
Literal,
|
|
|
|
Operator,
|
|
|
|
Signature,
|
|
|
|
String,
|
2021-10-04 19:21:31 +00:00
|
|
|
Filepath,
|
|
|
|
GlobPattern,
|
2021-07-22 19:50:59 +00:00
|
|
|
Variable,
|
2021-10-11 21:17:45 +00:00
|
|
|
Flag,
|
2021-09-14 04:59:46 +00:00
|
|
|
Custom(String),
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
pub fn flatten_block(working_set: &StateWorkingSet, block: &Block) -> Vec<(Span, FlatShape)> {
|
|
|
|
let mut output = vec![];
|
|
|
|
for stmt in &block.stmts {
|
|
|
|
output.extend(flatten_statement(working_set, stmt));
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
output
|
|
|
|
}
|
2021-07-22 19:50:59 +00:00
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
pub fn flatten_statement(
|
|
|
|
working_set: &StateWorkingSet,
|
|
|
|
stmt: &Statement,
|
|
|
|
) -> Vec<(Span, FlatShape)> {
|
|
|
|
match stmt {
|
|
|
|
Statement::Pipeline(pipeline) => flatten_pipeline(working_set, pipeline),
|
|
|
|
_ => vec![],
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
}
|
2021-07-22 19:50:59 +00:00
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
pub fn flatten_expression(
|
|
|
|
working_set: &StateWorkingSet,
|
|
|
|
expr: &Expression,
|
|
|
|
) -> Vec<(Span, FlatShape)> {
|
2021-09-14 04:59:46 +00:00
|
|
|
if let Some(custom_completion) = &expr.custom_completion {
|
|
|
|
return vec![(expr.span, FlatShape::Custom(custom_completion.clone()))];
|
|
|
|
}
|
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
match &expr.expr {
|
|
|
|
Expr::BinaryOp(lhs, op, rhs) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
output.extend(flatten_expression(working_set, lhs));
|
|
|
|
output.extend(flatten_expression(working_set, op));
|
|
|
|
output.extend(flatten_expression(working_set, rhs));
|
|
|
|
output
|
|
|
|
}
|
|
|
|
Expr::Block(block_id) => flatten_block(working_set, working_set.get_block(*block_id)),
|
|
|
|
Expr::Call(call) => {
|
|
|
|
let mut output = vec![(call.head, FlatShape::InternalCall)];
|
|
|
|
for positional in &call.positional {
|
|
|
|
output.extend(flatten_expression(working_set, positional));
|
2021-07-24 05:57:17 +00:00
|
|
|
}
|
2021-10-11 21:17:45 +00:00
|
|
|
for named in &call.named {
|
|
|
|
output.push((named.0.span, FlatShape::Flag));
|
|
|
|
if let Some(expr) = &named.1 {
|
|
|
|
output.extend(flatten_expression(working_set, expr));
|
|
|
|
}
|
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
output
|
|
|
|
}
|
2021-10-08 22:30:10 +00:00
|
|
|
Expr::ExternalCall(_, name_span, args) => {
|
|
|
|
let mut output = vec![(*name_span, FlatShape::External)];
|
2021-09-10 08:07:18 +00:00
|
|
|
|
|
|
|
for arg in args {
|
2021-10-08 21:51:47 +00:00
|
|
|
//output.push((*arg, FlatShape::ExternalArg));
|
|
|
|
match arg {
|
|
|
|
Expression {
|
|
|
|
expr: Expr::String(..),
|
|
|
|
span,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
output.push((*span, FlatShape::ExternalArg));
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
output.extend(flatten_expression(working_set, arg));
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 08:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output
|
2021-09-02 08:25:22 +00:00
|
|
|
}
|
|
|
|
Expr::Garbage => {
|
|
|
|
vec![(expr.span, FlatShape::Garbage)]
|
|
|
|
}
|
|
|
|
Expr::Int(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Int)]
|
|
|
|
}
|
|
|
|
Expr::Float(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Float)]
|
2021-09-04 21:52:57 +00:00
|
|
|
}
|
2021-10-05 02:27:39 +00:00
|
|
|
Expr::ValueWithUnit(x, unit) => {
|
|
|
|
let mut output = flatten_expression(working_set, x);
|
|
|
|
output.push((unit.span, FlatShape::String));
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
2021-10-02 02:59:11 +00:00
|
|
|
Expr::CellPath(cell_path) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for path_element in &cell_path.members {
|
|
|
|
match path_element {
|
|
|
|
PathMember::String { span, .. } => output.push((*span, FlatShape::String)),
|
|
|
|
PathMember::Int { span, .. } => output.push((*span, FlatShape::Int)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
2021-09-26 18:39:19 +00:00
|
|
|
Expr::FullCellPath(cell_path) => {
|
2021-09-06 22:02:24 +00:00
|
|
|
let mut output = vec![];
|
2021-09-26 18:39:19 +00:00
|
|
|
output.extend(flatten_expression(working_set, &cell_path.head));
|
|
|
|
for path_element in &cell_path.tail {
|
2021-09-06 22:02:24 +00:00
|
|
|
match path_element {
|
|
|
|
PathMember::String { span, .. } => output.push((*span, FlatShape::String)),
|
|
|
|
PathMember::Int { span, .. } => output.push((*span, FlatShape::Int)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
2021-09-11 11:13:04 +00:00
|
|
|
Expr::Range(from, next, to, op) => {
|
2021-09-04 21:52:57 +00:00
|
|
|
let mut output = vec![];
|
|
|
|
if let Some(f) = from {
|
|
|
|
output.extend(flatten_expression(working_set, f));
|
|
|
|
}
|
2021-09-11 11:13:04 +00:00
|
|
|
if let Some(s) = next {
|
|
|
|
output.extend(vec![(op.next_op_span, FlatShape::Operator)]);
|
|
|
|
output.extend(flatten_expression(working_set, s));
|
|
|
|
}
|
|
|
|
output.extend(vec![(op.span, FlatShape::Operator)]);
|
2021-09-04 21:52:57 +00:00
|
|
|
if let Some(t) = to {
|
|
|
|
output.extend(flatten_expression(working_set, t));
|
|
|
|
}
|
|
|
|
output
|
2021-09-02 08:25:22 +00:00
|
|
|
}
|
|
|
|
Expr::Bool(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Bool)]
|
|
|
|
}
|
2021-10-04 19:21:31 +00:00
|
|
|
Expr::Filepath(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Filepath)]
|
|
|
|
}
|
|
|
|
Expr::GlobPattern(_) => {
|
|
|
|
vec![(expr.span, FlatShape::GlobPattern)]
|
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
Expr::List(list) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for l in list {
|
|
|
|
output.extend(flatten_expression(working_set, l));
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
output
|
|
|
|
}
|
|
|
|
Expr::Keyword(_, span, expr) => {
|
|
|
|
let mut output = vec![(*span, FlatShape::Operator)];
|
|
|
|
output.extend(flatten_expression(working_set, expr));
|
|
|
|
output
|
|
|
|
}
|
|
|
|
Expr::Operator(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Operator)]
|
|
|
|
}
|
|
|
|
Expr::Signature(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Signature)]
|
|
|
|
}
|
|
|
|
Expr::String(_) => {
|
|
|
|
vec![(expr.span, FlatShape::String)]
|
|
|
|
}
|
2021-09-09 21:47:20 +00:00
|
|
|
Expr::RowCondition(_, expr) => flatten_expression(working_set, expr),
|
2021-09-02 08:25:22 +00:00
|
|
|
Expr::Subexpression(block_id) => {
|
|
|
|
flatten_block(working_set, working_set.get_block(*block_id))
|
|
|
|
}
|
|
|
|
Expr::Table(headers, cells) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for e in headers {
|
|
|
|
output.extend(flatten_expression(working_set, e));
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
for row in cells {
|
|
|
|
for expr in row {
|
|
|
|
output.extend(flatten_expression(working_set, expr));
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
output
|
|
|
|
}
|
|
|
|
Expr::Var(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Variable)]
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
}
|
2021-07-22 19:50:59 +00:00
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
pub fn flatten_pipeline(
|
|
|
|
working_set: &StateWorkingSet,
|
|
|
|
pipeline: &Pipeline,
|
|
|
|
) -> Vec<(Span, FlatShape)> {
|
|
|
|
let mut output = vec![];
|
|
|
|
for expr in &pipeline.expressions {
|
|
|
|
output.extend(flatten_expression(working_set, expr))
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
output
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|