nushell/crates/nu-parser/src/flatten.rs

221 lines
7.0 KiB
Rust
Raw Normal View History

2021-09-07 00:02:24 +02:00
use nu_protocol::ast::{Block, Expr, Expression, PathMember, Pipeline, Statement};
2021-09-02 20:21:37 +02:00
use nu_protocol::{engine::StateWorkingSet, Span};
2021-07-22 21:50:59 +02:00
2021-10-13 19:53:27 +02:00
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
2021-07-22 21:50:59 +02:00
pub enum FlatShape {
Garbage,
2021-07-24 07:57:17 +02:00
Bool,
2021-07-22 21:50:59 +02:00
Int,
2021-08-08 22:21:21 +02:00
Float,
Range,
2021-07-22 21:50:59 +02:00
InternalCall,
External,
ExternalArg,
2021-07-22 21:50:59 +02:00
Literal,
Operator,
Signature,
String,
2021-10-04 21:21:31 +02:00
Filepath,
GlobPattern,
2021-07-22 21:50:59 +02:00
Variable,
2021-10-11 23:17:45 +02:00
Flag,
Custom(String),
2021-07-22 21:50:59 +02:00
}
2021-09-02 10:25:22 +02: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 21:50:59 +02:00
}
2021-09-02 10:25:22 +02:00
output
}
2021-07-22 21:50:59 +02:00
2021-09-02 10:25:22 +02: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 21:50:59 +02:00
}
2021-09-02 10:25:22 +02:00
}
2021-07-22 21:50:59 +02:00
2021-09-02 10:25:22 +02:00
pub fn flatten_expression(
working_set: &StateWorkingSet,
expr: &Expression,
) -> Vec<(Span, FlatShape)> {
if let Some(custom_completion) = &expr.custom_completion {
return vec![(expr.span, FlatShape::Custom(custom_completion.clone()))];
}
2021-09-02 10:25:22 +02: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)];
2021-10-13 19:53:27 +02:00
let mut args = vec![];
2021-09-02 10:25:22 +02:00
for positional in &call.positional {
2021-10-13 19:53:27 +02:00
args.extend(flatten_expression(working_set, positional));
2021-07-24 07:57:17 +02:00
}
2021-10-11 23:17:45 +02:00
for named in &call.named {
2021-10-13 19:53:27 +02:00
args.push((named.0.span, FlatShape::Flag));
2021-10-11 23:17:45 +02:00
if let Some(expr) = &named.1 {
2021-10-13 19:53:27 +02:00
args.extend(flatten_expression(working_set, expr));
2021-10-11 23:17:45 +02:00
}
}
2021-10-13 19:53:27 +02:00
// sort these since flags and positional args can be intermixed
args.sort();
output.extend(args);
2021-09-02 10:25:22 +02:00
output
}
2021-10-09 00:30:10 +02:00
Expr::ExternalCall(_, name_span, args) => {
let mut output = vec![(*name_span, FlatShape::External)];
for arg in args {
2021-10-08 23:51:47 +02: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));
}
}
}
output
2021-09-02 10:25:22 +02:00
}
Expr::Garbage => {
vec![(expr.span, FlatShape::Garbage)]
}
Expr::Int(_) => {
vec![(expr.span, FlatShape::Int)]
}
Expr::Float(_) => {
vec![(expr.span, FlatShape::Float)]
}
2021-10-05 04:27:39 +02:00
Expr::ValueWithUnit(x, unit) => {
let mut output = flatten_expression(working_set, x);
output.push((unit.span, FlatShape::String));
output
}
2021-10-02 04:59:11 +02: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 20:39:19 +02:00
Expr::FullCellPath(cell_path) => {
2021-09-07 00:02:24 +02:00
let mut output = vec![];
2021-09-26 20:39:19 +02:00
output.extend(flatten_expression(working_set, &cell_path.head));
for path_element in &cell_path.tail {
2021-09-07 00:02:24 +02:00
match path_element {
PathMember::String { span, .. } => output.push((*span, FlatShape::String)),
PathMember::Int { span, .. } => output.push((*span, FlatShape::Int)),
}
}
output
}
Expr::Range(from, next, to, op) => {
let mut output = vec![];
if let Some(f) = from {
output.extend(flatten_expression(working_set, f));
}
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)]);
if let Some(t) = to {
output.extend(flatten_expression(working_set, t));
}
output
2021-09-02 10:25:22 +02:00
}
Expr::Bool(_) => {
vec![(expr.span, FlatShape::Bool)]
}
2021-10-04 21:21:31 +02:00
Expr::Filepath(_) => {
vec![(expr.span, FlatShape::Filepath)]
}
Expr::GlobPattern(_) => {
vec![(expr.span, FlatShape::GlobPattern)]
}
2021-09-02 10:25:22 +02:00
Expr::List(list) => {
let mut output = vec![];
for l in list {
output.extend(flatten_expression(working_set, l));
2021-07-22 21:50:59 +02:00
}
2021-09-02 10:25:22 +02:00
output
}
2021-11-11 00:14:00 +01:00
Expr::Record(list) => {
let mut output = vec![];
for l in list {
output.extend(flatten_expression(working_set, &l.0));
output.extend(flatten_expression(working_set, &l.1));
}
output
}
2021-09-02 10:25:22 +02:00
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 23:47:20 +02:00
Expr::RowCondition(_, expr) => flatten_expression(working_set, expr),
2021-09-02 10:25:22 +02: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 21:50:59 +02:00
}
2021-09-02 10:25:22 +02:00
for row in cells {
for expr in row {
output.extend(flatten_expression(working_set, expr));
2021-07-22 21:50:59 +02:00
}
}
2021-09-02 10:25:22 +02:00
output
}
2021-10-25 22:04:23 +02:00
Expr::Var(_) | Expr::VarDecl(_) => {
2021-09-02 10:25:22 +02:00
vec![(expr.span, FlatShape::Variable)]
2021-07-22 21:50:59 +02:00
}
}
2021-09-02 10:25:22 +02:00
}
2021-07-22 21:50:59 +02:00
2021-09-02 10:25:22 +02: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 21:50:59 +02:00
}
2021-09-02 10:25:22 +02:00
output
2021-07-22 21:50:59 +02:00
}