mirror of
https://github.com/nushell/nushell
synced 2025-01-13 13:49:21 +00:00
commit
f31c08e941
11 changed files with 4006 additions and 1760 deletions
70
src/cli.rs
70
src/cli.rs
|
@ -10,8 +10,8 @@ use crate::evaluate::Scope;
|
||||||
crate use crate::format::{EntriesListView, GenericView};
|
crate use crate::format::{EntriesListView, GenericView};
|
||||||
use crate::git::current_branch;
|
use crate::git::current_branch;
|
||||||
use crate::object::Value;
|
use crate::object::Value;
|
||||||
use crate::parser::ast::{Expression, Leaf};
|
use crate::parser::ast::{Expression, Leaf, RawExpression};
|
||||||
use crate::parser::{Args, ParsedCommand, Pipeline};
|
use crate::parser::{Args, Pipeline};
|
||||||
use crate::stream::empty_stream;
|
use crate::stream::empty_stream;
|
||||||
|
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
@ -324,43 +324,51 @@ fn classify_command(
|
||||||
// let command_name = &command.name[..];
|
// let command_name = &command.name[..];
|
||||||
// let args = &command.args;
|
// let args = &command.args;
|
||||||
|
|
||||||
if let Expression::Call(call) = command {
|
if let Expression {
|
||||||
|
expr: RawExpression::Call(call),
|
||||||
|
..
|
||||||
|
} = command
|
||||||
|
{
|
||||||
match (&call.name, &call.args) {
|
match (&call.name, &call.args) {
|
||||||
(Expression::Leaf(Leaf::Bare(name)), args) => {
|
(
|
||||||
match context.has_command(&name.to_string()) {
|
Expression {
|
||||||
true => {
|
expr: RawExpression::Leaf(Leaf::Bare(name)),
|
||||||
let command = context.get_command(&name.to_string());
|
..
|
||||||
let config = command.config();
|
},
|
||||||
let scope = Scope::empty();
|
args,
|
||||||
|
) => match context.has_command(&name.to_string()) {
|
||||||
|
true => {
|
||||||
|
let command = context.get_command(&name.to_string());
|
||||||
|
let config = command.config();
|
||||||
|
let scope = Scope::empty();
|
||||||
|
|
||||||
let args = match args {
|
let args = match args {
|
||||||
Some(args) => config.evaluate_args(args.iter(), &scope)?,
|
Some(args) => config.evaluate_args(args.iter(), &scope)?,
|
||||||
None => Args::default(),
|
None => Args::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(ClassifiedCommand::Internal(InternalCommand {
|
Ok(ClassifiedCommand::Internal(InternalCommand {
|
||||||
command,
|
command,
|
||||||
args,
|
args,
|
||||||
}))
|
}))
|
||||||
}
|
|
||||||
false => {
|
|
||||||
let arg_list_strings: Vec<String> = match args {
|
|
||||||
Some(args) => args.iter().map(|i| i.as_external_arg()).collect(),
|
|
||||||
None => vec![],
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(ClassifiedCommand::External(ExternalCommand {
|
|
||||||
name: name.to_string(),
|
|
||||||
args: arg_list_strings,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
false => {
|
||||||
|
let arg_list_strings: Vec<String> = match args {
|
||||||
|
Some(args) => args.iter().map(|i| i.as_external_arg()).collect(),
|
||||||
|
None => vec![],
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(ClassifiedCommand::External(ExternalCommand {
|
||||||
|
name: name.to_string(),
|
||||||
|
args: arg_list_strings,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
(_, None) => Err(ShellError::string(
|
(_, None) => Err(ShellError::string(
|
||||||
"Unimplemented command that is just an expression (1)",
|
"Unimplemented command that is just an expression (1)",
|
||||||
)),
|
)),
|
||||||
(_, Some(args)) => Err(ShellError::string("Unimplemented dynamic command")),
|
(_, Some(_)) => Err(ShellError::string("Unimplemented dynamic command")),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::string(&format!(
|
Err(ShellError::string(&format!(
|
||||||
|
|
|
@ -76,6 +76,7 @@ crate struct ClassifiedPipeline {
|
||||||
}
|
}
|
||||||
|
|
||||||
crate enum ClassifiedCommand {
|
crate enum ClassifiedCommand {
|
||||||
|
#[allow(unused)]
|
||||||
Expr(Expression),
|
Expr(Expression),
|
||||||
Internal(InternalCommand),
|
Internal(InternalCommand),
|
||||||
External(ExternalCommand),
|
External(ExternalCommand),
|
||||||
|
|
|
@ -63,6 +63,7 @@ pub struct ShellDiagnostic {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ShellDiagnostic {
|
impl ShellDiagnostic {
|
||||||
|
#[allow(unused)]
|
||||||
crate fn simple_diagnostic(
|
crate fn simple_diagnostic(
|
||||||
span: impl Into<Span>,
|
span: impl Into<Span>,
|
||||||
source: impl Into<String>,
|
source: impl Into<String>,
|
||||||
|
|
|
@ -22,15 +22,15 @@ impl Scope {
|
||||||
|
|
||||||
crate fn evaluate_expr(expr: &ast::Expression, scope: &Scope) -> Result<Value, ShellError> {
|
crate fn evaluate_expr(expr: &ast::Expression, scope: &Scope) -> Result<Value, ShellError> {
|
||||||
use ast::*;
|
use ast::*;
|
||||||
match expr {
|
match &expr.expr {
|
||||||
Expression::Call(c) => Err(ShellError::unimplemented("Evaluating call expression")),
|
RawExpression::Call(_) => Err(ShellError::unimplemented("Evaluating call expression")),
|
||||||
Expression::Leaf(l) => Ok(evaluate_leaf(l)),
|
RawExpression::Leaf(l) => Ok(evaluate_leaf(l)),
|
||||||
Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),
|
RawExpression::Parenthesized(p) => evaluate_expr(&p.expr, scope),
|
||||||
Expression::Flag(f) => Ok(Value::Primitive(Primitive::String(f.print()))),
|
RawExpression::Flag(f) => Ok(Value::Primitive(Primitive::String(f.print()))),
|
||||||
Expression::Block(b) => evaluate_block(&b, scope),
|
RawExpression::Block(b) => evaluate_block(&b, scope),
|
||||||
Expression::Path(p) => evaluate_path(&p, scope),
|
RawExpression::Path(p) => evaluate_path(&p, scope),
|
||||||
Expression::Binary(b) => evaluate_binary(b, scope),
|
RawExpression::Binary(b) => evaluate_binary(b, scope),
|
||||||
Expression::VariableReference(r) => evaluate_reference(r, scope),
|
RawExpression::VariableReference(r) => evaluate_reference(r, scope),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ fn evaluate_binary(binary: &ast::Binary, scope: &Scope) -> Result<Value, ShellEr
|
||||||
let left = evaluate_expr(&binary.left, scope)?;
|
let left = evaluate_expr(&binary.left, scope)?;
|
||||||
let right = evaluate_expr(&binary.right, scope)?;
|
let right = evaluate_expr(&binary.right, scope)?;
|
||||||
|
|
||||||
match left.compare(binary.operator, &right) {
|
match left.compare(&binary.operator, &right) {
|
||||||
Some(v) => Ok(Value::boolean(v)),
|
Some(v) => Ok(Value::boolean(v)),
|
||||||
None => Err(ShellError::TypeError(format!(
|
None => Err(ShellError::TypeError(format!(
|
||||||
"Can't compare {} and {}",
|
"Can't compare {} and {}",
|
||||||
|
@ -83,8 +83,8 @@ fn evaluate_path(path: &ast::Path, scope: &Scope) -> Result<Value, ShellError> {
|
||||||
let mut seen = vec![];
|
let mut seen = vec![];
|
||||||
|
|
||||||
for name in path.tail() {
|
for name in path.tail() {
|
||||||
let next = value.get_data_by_key(&name);
|
let next = value.get_data_by_key(&name.item);
|
||||||
seen.push(name);
|
seen.push(name.item.clone());
|
||||||
|
|
||||||
match next {
|
match next {
|
||||||
None => {
|
None => {
|
||||||
|
|
|
@ -134,9 +134,10 @@ impl Deserialize<'de> for Block {
|
||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
Ok(Block::new(ast::Expression::Leaf(ast::Leaf::String(
|
let mut builder = ast::ExpressionBuilder::new();
|
||||||
format!("Unserializable block"),
|
let expr: ast::Expression = builder.string("Unserializable block");
|
||||||
))))
|
|
||||||
|
Ok(Block::new(expr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,7 +220,7 @@ impl Value {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn compare(&self, operator: ast::Operator, other: &Value) -> Option<bool> {
|
crate fn compare(&self, operator: &ast::Operator, other: &Value) -> Option<bool> {
|
||||||
match operator {
|
match operator {
|
||||||
_ => {
|
_ => {
|
||||||
let coerced = coerce_compare(self, other)?;
|
let coerced = coerce_compare(self, other)?;
|
||||||
|
|
|
@ -5,7 +5,7 @@ crate mod parser;
|
||||||
crate mod registry;
|
crate mod registry;
|
||||||
crate mod span;
|
crate mod span;
|
||||||
|
|
||||||
crate use ast::{ParsedCommand, Pipeline};
|
crate use ast::Pipeline;
|
||||||
crate use registry::{Args, CommandConfig};
|
crate use registry::{Args, CommandConfig};
|
||||||
|
|
||||||
use crate::errors::ShellError;
|
use crate::errors::ShellError;
|
||||||
|
@ -33,7 +33,7 @@ pub fn parse(input: &str) -> Result<Pipeline, ShellError> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::parser::ast::{bare, flag, short, unit, var, Expression, Operator, Pipeline};
|
use crate::parser::ast::Pipeline;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
fn assert_parse(source: &str, expected: Pipeline) {
|
fn assert_parse(source: &str, expected: Pipeline) {
|
||||||
|
@ -61,30 +61,44 @@ mod tests {
|
||||||
let printed = parsed.print();
|
let printed = parsed.print();
|
||||||
|
|
||||||
assert_eq!(parsed, expected);
|
assert_eq!(parsed, expected);
|
||||||
assert_eq!(source, printed);
|
assert_eq!(printed, source);
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! commands {
|
macro_rules! commands {
|
||||||
( $( ( $name:tt $( $command:expr)* ) )|* ) => {
|
( $( ( $name:tt $( $command:ident ( $arg:expr ) )* ) )|* ) => {{
|
||||||
Pipeline::new(vec![
|
use $crate::parser::ast::{Expression, ExpressionBuilder};
|
||||||
|
let mut builder = crate::parser::ast::ExpressionBuilder::new();
|
||||||
|
|
||||||
|
builder.pipeline(vec![
|
||||||
$(
|
$(
|
||||||
command!($name $($command)*)
|
(command!($name $($command($arg))*) as (&dyn Fn(&mut ExpressionBuilder) -> Expression))
|
||||||
),*
|
),*
|
||||||
])
|
])
|
||||||
}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! command {
|
macro_rules! command {
|
||||||
($name:ident $( $command:expr )*) => {
|
($name:ident) => {
|
||||||
Expression::call(Expression::bare(stringify!($name)), vec![ $($command.into()),* ])
|
&|b: &mut $crate::parser::ast::ExpressionBuilder| b.call((
|
||||||
|
&|b: &mut $crate::parser::ast::ExpressionBuilder| b.bare(stringify!($name)),
|
||||||
|
vec![]
|
||||||
|
))
|
||||||
};
|
};
|
||||||
|
|
||||||
($name:ident $( $command:expr )*) => {
|
($name:ident $( $command:ident ( $body:expr ) )*) => {{
|
||||||
Expression::call(Expression::bare(stringify!($name)), vec![ $($command.into()),* ])
|
use $crate::parser::ast::{Expression, ExpressionBuilder};
|
||||||
|
&|b: &mut ExpressionBuilder| b.call((
|
||||||
|
(&|b: &mut ExpressionBuilder| b.bare(stringify!($name))) as (&dyn Fn(&mut ExpressionBuilder) -> Expression),
|
||||||
|
vec![$( (&|b: &mut ExpressionBuilder| b.$command($body)) as &dyn Fn(&mut ExpressionBuilder) -> Expression ),* ]))
|
||||||
|
|
||||||
|
}};
|
||||||
|
|
||||||
|
($name:ident $( $command:ident ( $body:expr ) )*) => {
|
||||||
|
&|b: &mut $crate::parser::ast::ExpressionBuilder| b.call(|b| b.bare(stringify!($name)), vec![ $( |b| b.$command($body) ),* ])
|
||||||
};
|
};
|
||||||
|
|
||||||
($name:tt $( $command:expr )*) => {
|
($name:tt $( $command:ident ( $body:expr ) )*) => {
|
||||||
Expression::call(Expression::bare($name), vec![ $($command.into()),* ])
|
&|b: &mut $crate::parser::ast::ExpressionBuilder| b.call((&|b| b.bare($name), vec![ $( &|b| b.$command($body) ),* ]))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +114,7 @@ mod tests {
|
||||||
commands![
|
commands![
|
||||||
(open bare("Cargo.toml"))
|
(open bare("Cargo.toml"))
|
||||||
| (select bare("package.authors"))
|
| (select bare("package.authors"))
|
||||||
| ("split-row" " ")
|
| ("split-row" string(" "))
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -122,7 +136,7 @@ mod tests {
|
||||||
|
|
||||||
assert_parse(
|
assert_parse(
|
||||||
"open Cargo.toml -r",
|
"open Cargo.toml -r",
|
||||||
commands![(open bare("Cargo.toml") short("r"))],
|
commands![(open bare("Cargo.toml") shorthand("r"))],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_parse(
|
assert_parse(
|
||||||
|
@ -132,7 +146,7 @@ mod tests {
|
||||||
|
|
||||||
assert_parse(
|
assert_parse(
|
||||||
r#"config --get "ignore dups" | format-list"#,
|
r#"config --get "ignore dups" | format-list"#,
|
||||||
commands![(config flag("get") "ignore dups") | ("format-list")],
|
commands![(config flag("get") string("ignore dups")) | ("format-list")],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_parse(
|
assert_parse(
|
||||||
|
@ -147,16 +161,16 @@ mod tests {
|
||||||
|
|
||||||
assert_parse(
|
assert_parse(
|
||||||
"config --set tabs 2",
|
"config --set tabs 2",
|
||||||
commands![(config flag("set") bare("tabs") 2)],
|
commands![(config flag("set") bare("tabs") int(2))],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_parse(
|
assert_parse(
|
||||||
r#"ls | skip 1 | first 2 | select "file name" | rm $it"#,
|
r#"ls | skip 1 | first 2 | select "file name" | rm $it"#,
|
||||||
commands![
|
commands![
|
||||||
(ls)
|
(ls)
|
||||||
| (skip 1)
|
| (skip int(1))
|
||||||
| (first 2)
|
| (first int(2))
|
||||||
| (select "file name")
|
| (select string("file name"))
|
||||||
| (rm var("it"))
|
| (rm var("it"))
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
@ -165,7 +179,7 @@ mod tests {
|
||||||
r#"git branch --merged | split-row "`n" | where $it != "* master""#,
|
r#"git branch --merged | split-row "`n" | where $it != "* master""#,
|
||||||
commands![
|
commands![
|
||||||
// TODO: Handle escapes correctly. Should we do ` escape because of paths?
|
// TODO: Handle escapes correctly. Should we do ` escape because of paths?
|
||||||
(git bare("branch") flag("merged")) | ("split-row" "`n") | (where binary(var("it"), "!=", "* master"))
|
(git bare("branch") flag("merged")) | ("split-row" string("`n")) | (where binary((&|b| b.var("it"), &|b| b.op("!="), &|b| b.string("* master"))))
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -175,7 +189,7 @@ mod tests {
|
||||||
(open bare("input2.json"))
|
(open bare("input2.json"))
|
||||||
| ("from-json")
|
| ("from-json")
|
||||||
| (select bare("glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso"))
|
| (select bare("glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso"))
|
||||||
| (where binary(var("it"), ">", "GML"))
|
| (where binary((&|b| b.var("it"), &|b| b.op(">"), &|b| b.string("GML"))))
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -189,16 +203,15 @@ mod tests {
|
||||||
assert_parse(
|
assert_parse(
|
||||||
"ls | where size < 1KB",
|
"ls | where size < 1KB",
|
||||||
commands![
|
commands![
|
||||||
(ls) | (where binary(bare("size"), "<", unit(1, "KB")))
|
(ls) | (where binary((&|b| b.bare("size"), &|b| b.op("<"), &|b| b.unit((1, "KB")))))
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
fn binary(
|
assert_parse(
|
||||||
left: impl Into<Expression>,
|
"ls | where { $it.size > 100 }",
|
||||||
op: impl Into<Operator>,
|
commands![
|
||||||
right: impl Into<Expression>,
|
(ls) | (where block(&|b| b.binary((&|b| b.path((&|b| b.var("it"), vec!["size"])), &|b| b.op(">"), &|b| b.int(100)))))
|
||||||
) -> Expression {
|
],
|
||||||
Expression::binary(left, op, right)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::parser::lexer::SpannedToken;
|
use crate::parser::lexer::{Span, Spanned};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use adhoc_derive::FromStr;
|
use adhoc_derive::FromStr;
|
||||||
use derive_new::new;
|
use derive_new::new;
|
||||||
|
@ -7,6 +7,381 @@ use serde_derive::{Deserialize, Serialize};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
#[derive(new)]
|
||||||
|
pub struct ExpressionBuilder {
|
||||||
|
#[new(default)]
|
||||||
|
pos: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
impl ExpressionBuilder {
|
||||||
|
pub fn op(&mut self, input: impl Into<Operator>) -> Spanned<Operator> {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
let (start, end) = self.consume(input.as_str());
|
||||||
|
|
||||||
|
self.pos = end;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_op(input, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_op(input: impl Into<Operator>, start: usize, end: usize) -> Spanned<Operator> {
|
||||||
|
Spanned {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
item: input.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn string(&mut self, input: impl Into<String>) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
let (start, _) = self.consume("\"");
|
||||||
|
self.consume(&input);
|
||||||
|
let (_, end) = self.consume("\"");
|
||||||
|
self.pos = end;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_string(input, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_string(input: impl Into<String>, start: usize, end: usize) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Leaf(Leaf::String(input)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bare(&mut self, input: impl Into<Bare>) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
let (start, end) = self.consume(&input.body);
|
||||||
|
self.pos = end;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_bare(input, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_bare(input: impl Into<Bare>, start: usize, end: usize) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Leaf(Leaf::Bare(input)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn boolean(&mut self, input: impl Into<bool>) -> Expression {
|
||||||
|
let boolean = input.into();
|
||||||
|
|
||||||
|
let (start, end) = match boolean {
|
||||||
|
true => self.consume("$yes"),
|
||||||
|
false => self.consume("$no"),
|
||||||
|
};
|
||||||
|
|
||||||
|
self.pos = end;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_boolean(boolean, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_boolean(input: impl Into<bool>, start: usize, end: usize) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Leaf(Leaf::Boolean(input)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn int(&mut self, input: impl Into<i64>) -> Expression {
|
||||||
|
let int = input.into();
|
||||||
|
|
||||||
|
let (start, end) = self.consume(&int.to_string());
|
||||||
|
self.pos = end;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_int(int, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_int(input: impl Into<i64>, start: usize, end: usize) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Leaf(Leaf::Int(input)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unit(&mut self, input: (impl Into<i64>, impl Into<Unit>)) -> Expression {
|
||||||
|
let (int, unit) = (input.0.into(), input.1.into());
|
||||||
|
|
||||||
|
let (start, _) = self.consume(&int.to_string());
|
||||||
|
let (_, end) = self.consume(&unit.to_string());
|
||||||
|
self.pos = end;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_unit((int, unit), start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_unit(
|
||||||
|
input: (impl Into<i64>, impl Into<Unit>),
|
||||||
|
start: usize,
|
||||||
|
end: usize,
|
||||||
|
) -> Expression {
|
||||||
|
let (int, unit) = (input.0.into(), input.1.into());
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Leaf(Leaf::Unit(int, unit)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn flag(&mut self, input: impl Into<String>) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
let (start, _) = self.consume("--");
|
||||||
|
let (_, end) = self.consume(&input);
|
||||||
|
self.pos = end;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_flag(input, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_flag(input: impl Into<String>, start: usize, end: usize) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Flag(Flag::Longhand(input)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn shorthand(&mut self, input: impl Into<String>) -> Expression {
|
||||||
|
let int = input.into();
|
||||||
|
|
||||||
|
let size = int.to_string().len();
|
||||||
|
|
||||||
|
let start = self.pos;
|
||||||
|
let end = self.pos + size + 1;
|
||||||
|
self.pos = end;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_shorthand(int, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_shorthand(input: impl Into<String>, start: usize, end: usize) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Flag(Flag::Shorthand(input)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parens(
|
||||||
|
&mut self,
|
||||||
|
input: impl FnOnce(&mut ExpressionBuilder) -> Expression,
|
||||||
|
) -> Expression {
|
||||||
|
let (start, _) = self.consume("(");
|
||||||
|
let input = input(self);
|
||||||
|
let (_, end) = self.consume(")");
|
||||||
|
self.pos = end;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_parens(input, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_parens(input: Expression, start: usize, end: usize) -> Expression {
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Parenthesized(Box::new(Parenthesized::new(input))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn block(&mut self, input: &dyn Fn(&mut ExpressionBuilder) -> Expression) -> Expression {
|
||||||
|
let (start, _) = self.consume("{ ");
|
||||||
|
let input = input(self);
|
||||||
|
let (_, end) = self.consume(" }");
|
||||||
|
self.pos = end;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_block(input, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_block(input: Expression, start: usize, end: usize) -> Expression {
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Block(Box::new(Block::new(input))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn binary(
|
||||||
|
&mut self,
|
||||||
|
input: (
|
||||||
|
&dyn Fn(&mut ExpressionBuilder) -> Expression,
|
||||||
|
&dyn Fn(&mut ExpressionBuilder) -> Spanned<Operator>,
|
||||||
|
&dyn Fn(&mut ExpressionBuilder) -> Expression,
|
||||||
|
),
|
||||||
|
) -> Expression {
|
||||||
|
let start = self.pos;
|
||||||
|
|
||||||
|
let left = (input.0)(self);
|
||||||
|
self.consume(" ");
|
||||||
|
let operator = (input.1)(self);
|
||||||
|
self.consume(" ");
|
||||||
|
let right = (input.2)(self);
|
||||||
|
|
||||||
|
let end = self.pos;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_binary((left, operator, right), start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_binary(
|
||||||
|
input: (
|
||||||
|
impl Into<Expression>,
|
||||||
|
impl Into<Spanned<Operator>>,
|
||||||
|
impl Into<Expression>,
|
||||||
|
),
|
||||||
|
start: usize,
|
||||||
|
end: usize,
|
||||||
|
) -> Expression {
|
||||||
|
let binary = Binary::new(input.0, input.1.into(), input.2);
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Binary(Box::new(binary)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn path(
|
||||||
|
&mut self,
|
||||||
|
input: (
|
||||||
|
&dyn Fn(&mut ExpressionBuilder) -> Expression,
|
||||||
|
Vec<impl Into<String>>,
|
||||||
|
),
|
||||||
|
) -> Expression {
|
||||||
|
let start = self.pos;
|
||||||
|
|
||||||
|
let head = (input.0)(self);
|
||||||
|
|
||||||
|
let mut tail = vec![];
|
||||||
|
|
||||||
|
for item in input.1 {
|
||||||
|
self.consume(".");
|
||||||
|
let item = item.into();
|
||||||
|
let (start, end) = self.consume(&item);
|
||||||
|
tail.push(Spanned::new(Span::from((start, end)), item));
|
||||||
|
}
|
||||||
|
|
||||||
|
let end = self.pos;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_path((head, tail), start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_path(
|
||||||
|
input: (impl Into<Expression>, Vec<Spanned<String>>),
|
||||||
|
start: usize,
|
||||||
|
end: usize,
|
||||||
|
) -> Expression {
|
||||||
|
let path = Path::new(input.0.into(), input.1);
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Path(Box::new(path)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call(
|
||||||
|
&mut self,
|
||||||
|
input: (
|
||||||
|
&(dyn Fn(&mut ExpressionBuilder) -> Expression),
|
||||||
|
Vec<&dyn Fn(&mut ExpressionBuilder) -> Expression>,
|
||||||
|
),
|
||||||
|
) -> Expression {
|
||||||
|
let start = self.pos;
|
||||||
|
|
||||||
|
let name = (&input.0)(self);
|
||||||
|
|
||||||
|
let mut args = vec![];
|
||||||
|
|
||||||
|
for item in input.1 {
|
||||||
|
self.consume(" ");
|
||||||
|
args.push(item(self));
|
||||||
|
}
|
||||||
|
|
||||||
|
let end = self.pos;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_call((name, args), start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_call(input: impl Into<Call>, start: usize, end: usize) -> Expression {
|
||||||
|
let call = input.into();
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr: RawExpression::Call(Box::new(call)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn var(&mut self, input: impl Into<String>) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
let (start, _) = self.consume("$");
|
||||||
|
let (_, end) = self.consume(&input);
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_var(input, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_var(input: impl Into<String>, start: usize, end: usize) -> Expression {
|
||||||
|
let input = input.into();
|
||||||
|
|
||||||
|
let expr = match &input[..] {
|
||||||
|
"it" => RawExpression::VariableReference(Variable::It),
|
||||||
|
_ => RawExpression::VariableReference(Variable::Other(input)),
|
||||||
|
};
|
||||||
|
|
||||||
|
Expression {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
expr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pipeline(
|
||||||
|
&mut self,
|
||||||
|
input: Vec<&dyn Fn(&mut ExpressionBuilder) -> Expression>,
|
||||||
|
) -> Pipeline {
|
||||||
|
let start = self.pos;
|
||||||
|
|
||||||
|
let mut exprs = vec![];
|
||||||
|
let mut input = input.into_iter();
|
||||||
|
|
||||||
|
let next = input.next().unwrap();
|
||||||
|
exprs.push(next(self));
|
||||||
|
|
||||||
|
for item in input {
|
||||||
|
self.consume(" | ");
|
||||||
|
exprs.push(item(self));
|
||||||
|
}
|
||||||
|
|
||||||
|
let end = self.pos;
|
||||||
|
|
||||||
|
ExpressionBuilder::spanned_pipeline(exprs, start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spanned_pipeline(input: Vec<Expression>, start: usize, end: usize) -> Pipeline {
|
||||||
|
Pipeline {
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
commands: input,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sp(&mut self) {
|
||||||
|
self.consume(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ws(&mut self, input: &str) {
|
||||||
|
self.consume(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn consume(&mut self, input: &str) -> (usize, usize) {
|
||||||
|
let start = self.pos;
|
||||||
|
self.pos += input.len();
|
||||||
|
(start, self.pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
|
||||||
pub enum Operator {
|
pub enum Operator {
|
||||||
Equal,
|
Equal,
|
||||||
|
@ -19,13 +394,17 @@ pub enum Operator {
|
||||||
|
|
||||||
impl Operator {
|
impl Operator {
|
||||||
pub fn print(&self) -> String {
|
pub fn print(&self) -> String {
|
||||||
|
self.as_str().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_str(&self) -> &str {
|
||||||
match *self {
|
match *self {
|
||||||
Operator::Equal => "==".to_string(),
|
Operator::Equal => "==",
|
||||||
Operator::NotEqual => "!=".to_string(),
|
Operator::NotEqual => "!=",
|
||||||
Operator::LessThan => "<".to_string(),
|
Operator::LessThan => "<",
|
||||||
Operator::GreaterThan => ">".to_string(),
|
Operator::GreaterThan => ">",
|
||||||
Operator::LessThanOrEqual => "<=".to_string(),
|
Operator::LessThanOrEqual => "<=",
|
||||||
Operator::GreaterThanOrEqual => ">=".to_string(),
|
Operator::GreaterThanOrEqual => ">=",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,140 +431,127 @@ impl FromStr for Operator {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||||
pub enum Expression {
|
pub struct Expression {
|
||||||
|
crate expr: RawExpression,
|
||||||
|
crate span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::Deref for Expression {
|
||||||
|
type Target = RawExpression;
|
||||||
|
|
||||||
|
fn deref(&self) -> &RawExpression {
|
||||||
|
&self.expr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Expression {
|
||||||
|
crate fn print(&self) -> String {
|
||||||
|
self.expr.print()
|
||||||
|
}
|
||||||
|
|
||||||
|
crate fn as_external_arg(&self) -> String {
|
||||||
|
self.expr.as_external_arg()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||||
|
pub enum RawExpression {
|
||||||
Leaf(Leaf),
|
Leaf(Leaf),
|
||||||
Flag(Flag),
|
Flag(Flag),
|
||||||
Parenthesized(Box<Parenthesized>),
|
Parenthesized(Box<Parenthesized>),
|
||||||
Block(Box<Block>),
|
Block(Box<Block>),
|
||||||
Binary(Box<Binary>),
|
Binary(Box<Binary>),
|
||||||
Path(Box<Path>),
|
Path(Box<Path>),
|
||||||
Call(Box<ParsedCommand>),
|
Call(Box<Call>),
|
||||||
VariableReference(Variable),
|
VariableReference(Variable),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&str> for Expression {
|
impl RawExpression {
|
||||||
fn from(input: &str) -> Expression {
|
// crate fn leaf(leaf: impl Into<Leaf>) -> Expression {
|
||||||
Expression::Leaf(Leaf::String(input.into()))
|
// Expression::Leaf(leaf.into())
|
||||||
}
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
impl From<String> for Expression {
|
// crate fn flag(flag: impl Into<Flag>) -> Expression {
|
||||||
fn from(input: String) -> Expression {
|
// Expression::Flag(flag.into())
|
||||||
Expression::Leaf(Leaf::String(input.into()))
|
// }
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<i64> for Expression {
|
// crate fn call(head: Expression, tail: Vec<Expression>) -> Expression {
|
||||||
fn from(input: i64) -> Expression {
|
// if tail.len() == 0 {
|
||||||
Expression::Leaf(Leaf::Int(input.into()))
|
// Expression::Call(Box::new(ParsedCommand::new(head.into(), None)))
|
||||||
}
|
// } else {
|
||||||
}
|
// Expression::Call(Box::new(ParsedCommand::new(head.into(), Some(tail))))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
impl From<BarePath> for Expression {
|
// crate fn binary(
|
||||||
fn from(input: BarePath) -> Expression {
|
// left: impl Into<Expression>,
|
||||||
Expression::Leaf(Leaf::Bare(input))
|
// operator: impl Into<Operator>,
|
||||||
}
|
// right: impl Into<Expression>,
|
||||||
}
|
// ) -> Expression {
|
||||||
|
// Expression::Binary(Box::new(Binary {
|
||||||
|
// left: left.into(),
|
||||||
|
// operator: operator.into(),
|
||||||
|
// right: right.into(),
|
||||||
|
// }))
|
||||||
|
// }
|
||||||
|
|
||||||
impl From<Variable> for Expression {
|
// crate fn block(expr: impl Into<Expression>) -> Expression {
|
||||||
fn from(input: Variable) -> Expression {
|
// Expression::Block(Box::new(Block::new(expr.into())))
|
||||||
Expression::VariableReference(input)
|
// }
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Flag> for Expression {
|
|
||||||
fn from(input: Flag) -> Expression {
|
|
||||||
Expression::Flag(input)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Binary> for Expression {
|
|
||||||
fn from(input: Binary) -> Expression {
|
|
||||||
Expression::Binary(Box::new(input))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Expression {
|
|
||||||
crate fn leaf(leaf: impl Into<Leaf>) -> Expression {
|
|
||||||
Expression::Leaf(leaf.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
crate fn flag(flag: impl Into<Flag>) -> Expression {
|
|
||||||
Expression::Flag(flag.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
crate fn call(head: Expression, tail: Vec<Expression>) -> Expression {
|
|
||||||
if tail.len() == 0 {
|
|
||||||
Expression::Call(Box::new(ParsedCommand::new(head.into(), None)))
|
|
||||||
} else {
|
|
||||||
Expression::Call(Box::new(ParsedCommand::new(head.into(), Some(tail))))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
crate fn binary(
|
|
||||||
left: impl Into<Expression>,
|
|
||||||
operator: impl Into<Operator>,
|
|
||||||
right: impl Into<Expression>,
|
|
||||||
) -> Expression {
|
|
||||||
Expression::Binary(Box::new(Binary {
|
|
||||||
left: left.into(),
|
|
||||||
operator: operator.into(),
|
|
||||||
right: right.into(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
crate fn block(expr: impl Into<Expression>) -> Expression {
|
|
||||||
Expression::Block(Box::new(Block::new(expr.into())))
|
|
||||||
}
|
|
||||||
|
|
||||||
crate fn print(&self) -> String {
|
crate fn print(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Expression::Call(c) => c.print(),
|
RawExpression::Call(c) => c.print(),
|
||||||
Expression::Leaf(l) => l.print(),
|
RawExpression::Leaf(l) => l.print(),
|
||||||
Expression::Flag(f) => f.print(),
|
RawExpression::Flag(f) => f.print(),
|
||||||
Expression::Parenthesized(p) => p.print(),
|
RawExpression::Parenthesized(p) => p.print(),
|
||||||
Expression::Block(b) => b.print(),
|
RawExpression::Block(b) => b.print(),
|
||||||
Expression::VariableReference(r) => r.print(),
|
RawExpression::VariableReference(r) => r.print(),
|
||||||
Expression::Path(p) => p.print(),
|
RawExpression::Path(p) => p.print(),
|
||||||
Expression::Binary(b) => b.print(),
|
RawExpression::Binary(b) => b.print(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn as_external_arg(&self) -> String {
|
crate fn as_external_arg(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Expression::Call(c) => c.as_external_arg(),
|
RawExpression::Call(c) => c.as_external_arg(),
|
||||||
Expression::Leaf(l) => l.as_external_arg(),
|
RawExpression::Leaf(l) => l.as_external_arg(),
|
||||||
Expression::Flag(f) => f.as_external_arg(),
|
RawExpression::Flag(f) => f.as_external_arg(),
|
||||||
Expression::Parenthesized(p) => p.as_external_arg(),
|
RawExpression::Parenthesized(p) => p.as_external_arg(),
|
||||||
Expression::Block(b) => b.as_external_arg(),
|
RawExpression::Block(b) => b.as_external_arg(),
|
||||||
Expression::VariableReference(r) => r.as_external_arg(),
|
RawExpression::VariableReference(r) => r.as_external_arg(),
|
||||||
Expression::Path(p) => p.as_external_arg(),
|
RawExpression::Path(p) => p.as_external_arg(),
|
||||||
Expression::Binary(b) => b.as_external_arg(),
|
RawExpression::Binary(b) => b.as_external_arg(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn bare(path: impl Into<BarePath>) -> Expression {
|
|
||||||
Expression::Leaf(Leaf::Bare(path.into()))
|
|
||||||
}
|
|
||||||
|
|
||||||
crate fn as_string(&self) -> Option<String> {
|
crate fn as_string(&self) -> Option<String> {
|
||||||
match self {
|
match self {
|
||||||
Expression::Leaf(Leaf::String(s)) => Some(s.to_string()),
|
RawExpression::Leaf(Leaf::String(s)) => Some(s.to_string()),
|
||||||
Expression::Leaf(Leaf::Bare(path)) => Some(path.to_string()),
|
RawExpression::Leaf(Leaf::Bare(path)) => Some(path.to_string()),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
crate fn as_bare(&self) -> Option<String> {
|
crate fn as_bare(&self) -> Option<String> {
|
||||||
match self {
|
match self {
|
||||||
Expression::Leaf(Leaf::Bare(p)) => Some(p.to_string()),
|
RawExpression::Leaf(Leaf::Bare(p)) => Some(p.to_string()),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
crate fn as_block(&self) -> Option<Block> {
|
||||||
|
match self {
|
||||||
|
RawExpression::Block(block) => Some(*block.clone()),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn is_flag(&self, value: &str) -> bool {
|
crate fn is_flag(&self, value: &str) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Expression::Flag(Flag::Longhand(f)) if value == f => true,
|
RawExpression::Flag(Flag::Longhand(f)) if value == f => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,7 +593,7 @@ pub struct Path {
|
||||||
head: Expression,
|
head: Expression,
|
||||||
|
|
||||||
#[get = "crate"]
|
#[get = "crate"]
|
||||||
tail: Vec<String>,
|
tail: Vec<Spanned<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Path {
|
impl Path {
|
||||||
|
@ -235,7 +601,7 @@ impl Path {
|
||||||
let mut out = self.head.print();
|
let mut out = self.head.print();
|
||||||
|
|
||||||
for item in self.tail.iter() {
|
for item in self.tail.iter() {
|
||||||
out.push_str(&format!(".{}", item));
|
out.push_str(&format!(".{}", item.item));
|
||||||
}
|
}
|
||||||
|
|
||||||
out
|
out
|
||||||
|
@ -245,7 +611,7 @@ impl Path {
|
||||||
let mut out = self.head.as_external_arg();
|
let mut out = self.head.as_external_arg();
|
||||||
|
|
||||||
for item in self.tail.iter() {
|
for item in self.tail.iter() {
|
||||||
out.push_str(&format!(".{}", item));
|
out.push_str(&format!(".{}", item.item));
|
||||||
}
|
}
|
||||||
|
|
||||||
out
|
out
|
||||||
|
@ -258,24 +624,7 @@ pub enum Variable {
|
||||||
Other(String),
|
Other(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
crate fn var(name: &str) -> Expression {
|
|
||||||
match name {
|
|
||||||
"it" => Expression::VariableReference(Variable::It),
|
|
||||||
other => Expression::VariableReference(Variable::Other(other.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Variable {
|
impl Variable {
|
||||||
crate fn from_str(input: &str) -> Expression {
|
|
||||||
match input {
|
|
||||||
"it" => Expression::VariableReference(Variable::It),
|
|
||||||
"yes" => Expression::Leaf(Leaf::Boolean(true)),
|
|
||||||
"no" => Expression::Leaf(Leaf::Boolean(false)),
|
|
||||||
other => Expression::VariableReference(Variable::Other(other.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print(&self) -> String {
|
fn print(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Variable::It => format!("$it"),
|
Variable::It => format!("$it"),
|
||||||
|
@ -288,45 +637,34 @@ impl Variable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bare(s: impl Into<String>) -> BarePath {
|
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, new)]
|
||||||
BarePath {
|
pub struct Bare {
|
||||||
head: s.into(),
|
body: String,
|
||||||
tail: vec![],
|
}
|
||||||
|
|
||||||
|
impl From<String> for Bare {
|
||||||
|
fn from(input: String) -> Bare {
|
||||||
|
Bare { body: input }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
impl From<&str> for Bare {
|
||||||
pub struct BarePath {
|
fn from(input: &str) -> Bare {
|
||||||
head: String,
|
Bare {
|
||||||
tail: Vec<String>,
|
body: input.to_string(),
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Into<String>> From<T> for BarePath {
|
|
||||||
fn from(input: T) -> BarePath {
|
|
||||||
BarePath {
|
|
||||||
head: input.into(),
|
|
||||||
tail: vec![],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BarePath {
|
impl Bare {
|
||||||
crate fn from_token(head: SpannedToken) -> BarePath {
|
crate fn from_string(string: impl Into<String>) -> Bare {
|
||||||
BarePath {
|
Bare {
|
||||||
head: head.to_string(),
|
body: string.into(),
|
||||||
tail: vec![],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
crate fn from_tokens(head: SpannedToken, tail: Vec<SpannedToken>) -> BarePath {
|
|
||||||
BarePath {
|
|
||||||
head: head.to_string(),
|
|
||||||
tail: tail.iter().map(|i| i.to_string()).collect(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn to_string(&self) -> String {
|
crate fn to_string(&self) -> String {
|
||||||
bare_string(&self.head, &self.tail)
|
self.body.to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -363,30 +701,28 @@ impl Unit {
|
||||||
Unit::PB => size * 1024 * 1024 * 1024 * 1024 * 1024,
|
Unit::PB => size * 1024 * 1024 * 1024 * 1024 * 1024,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
crate fn to_string(&self) -> &str {
|
||||||
pub fn unit(num: i64, unit: impl Into<Unit>) -> Expression {
|
match self {
|
||||||
Expression::Leaf(Leaf::Unit(num, unit.into()))
|
Unit::B => "B",
|
||||||
|
Unit::KB => "KB",
|
||||||
|
Unit::MB => "MB",
|
||||||
|
Unit::GB => "GB",
|
||||||
|
Unit::TB => "TB",
|
||||||
|
Unit::PB => "PB",
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||||
pub enum Leaf {
|
pub enum Leaf {
|
||||||
String(String),
|
String(String),
|
||||||
Bare(BarePath),
|
Bare(Bare),
|
||||||
|
|
||||||
#[allow(unused)]
|
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
Int(i64),
|
Int(i64),
|
||||||
Unit(i64, Unit),
|
Unit(i64, Unit),
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn bare_string(head: &String, tail: &Vec<String>) -> String {
|
|
||||||
let mut out = vec![head.clone()];
|
|
||||||
out.extend(tail.clone());
|
|
||||||
itertools::join(out, ".")
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Leaf {
|
impl Leaf {
|
||||||
fn print(&self) -> String {
|
fn print(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
|
@ -412,14 +748,14 @@ impl Leaf {
|
||||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||||
pub struct Binary {
|
pub struct Binary {
|
||||||
crate left: Expression,
|
crate left: Expression,
|
||||||
crate operator: Operator,
|
crate operator: Spanned<Operator>,
|
||||||
crate right: Expression,
|
crate right: Expression,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Binary {
|
impl Binary {
|
||||||
crate fn new(
|
crate fn new(
|
||||||
left: impl Into<Expression>,
|
left: impl Into<Expression>,
|
||||||
operator: Operator,
|
operator: Spanned<Operator>,
|
||||||
right: impl Into<Expression>,
|
right: impl Into<Expression>,
|
||||||
) -> Binary {
|
) -> Binary {
|
||||||
Binary {
|
Binary {
|
||||||
|
@ -456,16 +792,6 @@ pub enum Flag {
|
||||||
Longhand(String),
|
Longhand(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
crate fn flag(s: &str) -> Flag {
|
|
||||||
Flag::Longhand(s.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
crate fn short(s: &str) -> Flag {
|
|
||||||
Flag::Shorthand(s.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Flag {
|
impl Flag {
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
crate fn print(&self) -> String {
|
crate fn print(&self) -> String {
|
||||||
|
@ -482,12 +808,34 @@ impl Flag {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, new)]
|
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, new)]
|
||||||
pub struct ParsedCommand {
|
pub struct Call {
|
||||||
crate name: Expression,
|
crate name: Expression,
|
||||||
crate args: Option<Vec<Expression>>,
|
crate args: Option<Vec<Expression>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParsedCommand {
|
impl From<(Expression, Vec<Expression>)> for Call {
|
||||||
|
fn from(input: (Expression, Vec<Expression>)) -> Call {
|
||||||
|
Call {
|
||||||
|
name: input.0,
|
||||||
|
args: if input.1.len() == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(input.1)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Expression> for Call {
|
||||||
|
fn from(input: Expression) -> Call {
|
||||||
|
Call {
|
||||||
|
name: input,
|
||||||
|
args: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Call {
|
||||||
fn as_external_arg(&self) -> String {
|
fn as_external_arg(&self) -> String {
|
||||||
let mut out = vec![];
|
let mut out = vec![];
|
||||||
|
|
||||||
|
@ -517,35 +865,26 @@ impl ParsedCommand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&str> for ParsedCommand {
|
|
||||||
fn from(input: &str) -> ParsedCommand {
|
|
||||||
ParsedCommand {
|
|
||||||
name: Expression::Leaf(Leaf::Bare(bare(input))),
|
|
||||||
args: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<(&str, Vec<Expression>)> for ParsedCommand {
|
|
||||||
fn from(input: (&str, Vec<Expression>)) -> ParsedCommand {
|
|
||||||
ParsedCommand {
|
|
||||||
name: Expression::bare(input.0),
|
|
||||||
args: Some(input.1),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(new, Debug, Eq, PartialEq)]
|
#[derive(new, Debug, Eq, PartialEq)]
|
||||||
pub struct Pipeline {
|
pub struct Pipeline {
|
||||||
crate commands: Vec<Expression>,
|
crate commands: Vec<Expression>,
|
||||||
|
crate span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pipeline {
|
impl Pipeline {
|
||||||
crate fn from_parts(command: Expression, rest: Vec<Expression>) -> Pipeline {
|
crate fn from_parts(
|
||||||
|
command: Expression,
|
||||||
|
rest: Vec<Expression>,
|
||||||
|
start: usize,
|
||||||
|
end: usize,
|
||||||
|
) -> Pipeline {
|
||||||
let mut commands = vec![command];
|
let mut commands = vec![command];
|
||||||
commands.extend(rest);
|
commands.extend(rest);
|
||||||
|
|
||||||
Pipeline { commands }
|
Pipeline {
|
||||||
|
commands,
|
||||||
|
span: Span::from((start, end)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
|
|
|
@ -317,8 +317,8 @@ impl logos::Extras for LexerState {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
|
||||||
pub struct Span {
|
pub struct Span {
|
||||||
start: usize,
|
crate start: usize,
|
||||||
end: usize,
|
crate end: usize,
|
||||||
// source: &'source str,
|
// source: &'source str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,6 +374,26 @@ impl language_reporting::ReportingSpan for Span {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(new, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
|
pub struct Spanned<T> {
|
||||||
|
crate span: Span,
|
||||||
|
crate item: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> std::ops::Deref for Spanned<T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &T {
|
||||||
|
&self.item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Spanned<T> {
|
||||||
|
crate fn from_item(item: T, span: Span) -> Spanned<T> {
|
||||||
|
Spanned { span, item }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(new, Debug, Clone, Eq, PartialEq)]
|
#[derive(new, Debug, Clone, Eq, PartialEq)]
|
||||||
pub struct SpannedToken<'source> {
|
pub struct SpannedToken<'source> {
|
||||||
crate span: Span,
|
crate span: Span,
|
||||||
|
@ -382,6 +402,10 @@ pub struct SpannedToken<'source> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SpannedToken<'source> {
|
impl SpannedToken<'source> {
|
||||||
|
crate fn to_spanned_string(&self) -> Spanned<String> {
|
||||||
|
Spanned::from_item(self.slice.to_string(), self.span)
|
||||||
|
}
|
||||||
|
|
||||||
crate fn to_string(&self) -> String {
|
crate fn to_string(&self) -> String {
|
||||||
self.slice.to_string()
|
self.slice.to_string()
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use crate::parser::ast::*;
|
use crate::parser::ast::*;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::parser::lexer::{SpannedToken, Token};
|
use crate::parser::lexer::{SpannedToken, Spanned, Span, Token};
|
||||||
use byte_unit::Byte;
|
use byte_unit::Byte;
|
||||||
|
|
||||||
// nu's grammar is a little bit different from a lot of other languages, to better match
|
// nu's grammar is a little bit different from a lot of other languages, to better match
|
||||||
|
@ -27,61 +27,69 @@ use byte_unit::Byte;
|
||||||
grammar<'input>;
|
grammar<'input>;
|
||||||
|
|
||||||
pub Pipeline: Pipeline = {
|
pub Pipeline: Pipeline = {
|
||||||
<first:PipelineElement> <rest: ( "|" <PipelineElement> )*> => Pipeline::from_parts(first, rest),
|
<l: @L> <first:PipelineElement> <rest: ( "|" <PipelineElement> )*> <r: @R> => Pipeline::from_parts(first, rest, l, r),
|
||||||
}
|
}
|
||||||
|
|
||||||
PipelineElement: Expression = {
|
PipelineElement: Expression = {
|
||||||
<Bare> => Expression::call(Expression::bare(<>), vec![]),
|
<l: @L> <bare: BareExpression> <r: @R> => ExpressionBuilder::spanned_call((bare, vec![]), l, r),
|
||||||
<SingleExpression> => <>,
|
<SingleExpression> => <>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// A leaf expression is a single logical token that directly represents an expression
|
// A leaf expression is a single logical token that directly represents an expression
|
||||||
LeafExpression: Expression = {
|
LeafExpression: Expression = {
|
||||||
<String> => <>,
|
<String>,
|
||||||
<Int> => Expression::leaf(Leaf::Int(<>)),
|
<l: @L> <int: Int> <r: @R> => ExpressionBuilder::spanned_int(int, l, r),
|
||||||
<UnitsNum> => <>,
|
<UnitsNum>,
|
||||||
<Var> => <>,
|
<Var>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub Call: Expression = {
|
pub Call: Expression = {
|
||||||
<expr:Expression> <rest:SingleCallArgument> => Expression::call(expr, vec![rest]),
|
<l: @L> <expr:Expression> <rest:SingleCallArgument> <r: @R> => ExpressionBuilder::spanned_call((expr, vec![rest]), l, r),
|
||||||
<expr:Expression> <first:CallArgument> <rest:( <CallArgument> )+> => Expression::call(expr, { let mut rest = rest; let mut v = vec![first]; v.append(&mut rest); v }),
|
<l: @L> <expr:Expression> <first:CallArgument> <rest:( <CallArgument> )+> <r: @R> => ExpressionBuilder::spanned_call((expr, { let mut rest = rest; let mut v = vec![first]; v.append(&mut rest); v }), l, r),
|
||||||
<expr:Bare> <rest:SingleCallArgument> => Expression::call(Expression::bare(expr), vec![rest]),
|
<l: @L> <expr:BareExpression> <rest:SingleCallArgument> <r: @R> => ExpressionBuilder::spanned_call((expr, vec![rest]), l, r),
|
||||||
<expr:Bare> <first:CallArgument> <rest:( <CallArgument> )+> => Expression::call(Expression::bare(expr), { let mut v = vec![first]; let mut rest = rest; v.append(&mut rest); v }),
|
<l: @L> <expr:BareExpression> <first:CallArgument> <rest:( <CallArgument> )+> <r: @R> => ExpressionBuilder::spanned_call((expr, { let mut v = vec![first]; let mut rest = rest; v.append(&mut rest); v }), l, r),
|
||||||
}
|
}
|
||||||
|
|
||||||
Binary: Expression = {
|
Binary: Expression = {
|
||||||
<left:ArgumentExpression> <op:Operator> <right:ArgumentExpression> => Expression::binary(left, op, right),
|
<l: @L> <left:ArgumentExpression> <op:SpannedOperator> <right:ArgumentExpression> <r: @R> => ExpressionBuilder::spanned_binary((left, op, right), l, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// In a block, a single bare word is interpreted as a call:
|
// In a block, a single bare word is interpreted as a call:
|
||||||
//
|
//
|
||||||
// foreach { ls }
|
// foreach { ls }
|
||||||
Block: Expression = {
|
Block: Expression = {
|
||||||
"{" <SingleExpression> "}" => Expression::block(<>),
|
<l: @L> "{" <expr: SingleExpression> "}" <r: @R> => ExpressionBuilder::spanned_block(expr, l, r),
|
||||||
"{" <Bare> "}" => Expression::block(Expression::call(Expression::bare(<>), vec![])),
|
<l: @L> "{" <bare: BareExpression> "}" <r: @R> => {
|
||||||
|
let call = ExpressionBuilder::spanned_call(bare.clone(), bare.span.start, bare.span.end);
|
||||||
|
ExpressionBuilder::spanned_block(call, l, r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// An `Expression` is the most general kind of expression. It can go anywhere, even right next to another expression, and
|
// An `Expression` is the most general kind of expression. It can go anywhere, even right next to another expression, and
|
||||||
// even as the first part of a call.
|
// even as the first part of a call.
|
||||||
Expression: Expression = {
|
MemberHeadExpression: Expression = {
|
||||||
<LeafExpression> => <>,
|
<LeafExpression> => <>,
|
||||||
<Block> => <>,
|
<Block> => <>,
|
||||||
"(" <Call> ")" => <>,
|
<l: @L> "(" <expr: Call> ")" <r: @R> => ExpressionBuilder::spanned_call(expr, l, r),
|
||||||
"(" <Bare> ")" => Expression::call(Expression::bare(<>), vec![]),
|
<l: @L> "(" <expr: BareExpression> ")" <r: @R> => ExpressionBuilder::spanned_call((expr, vec![]), l, r),
|
||||||
"(" <Binary> ")" => <>,
|
<l: @L> "(" <expr:Binary> ")" <r: @R> => ExpressionBuilder::spanned_parens(expr, l, r),
|
||||||
|
}
|
||||||
|
|
||||||
|
Expression: Expression = {
|
||||||
|
<MemberHeadExpression> => <>,
|
||||||
|
<l: @L> <expr:MemberHeadExpression> <rest: ( "???." <"member"> )+> <r: @R> => ExpressionBuilder::spanned_path((expr, rest.iter().map(|i| i.to_spanned_string()).collect()), l, r),
|
||||||
}
|
}
|
||||||
|
|
||||||
// An `ArgumentExpression` is an expression that appears in an argument list. It includes all of `Expression`, and
|
// An `ArgumentExpression` is an expression that appears in an argument list. It includes all of `Expression`, and
|
||||||
// bare words are interpreted as strings.
|
// bare words are interpreted as strings.
|
||||||
ArgumentExpression: Expression = {
|
ArgumentExpression: Expression = {
|
||||||
<Expression>,
|
<Expression>,
|
||||||
<Bare> => Expression::bare(<>),
|
<BareExpression>,
|
||||||
}
|
}
|
||||||
|
|
||||||
CallArgument: Expression = {
|
CallArgument: Expression = {
|
||||||
<ArgumentExpression> => <>,
|
<ArgumentExpression>,
|
||||||
<Flag> => Expression::flag(<>),
|
<Flag>,
|
||||||
}
|
}
|
||||||
|
|
||||||
SingleCallArgument: Expression = {
|
SingleCallArgument: Expression = {
|
||||||
|
@ -101,14 +109,22 @@ SingleExpression: Expression = {
|
||||||
<Binary>,
|
<Binary>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BareExpression: Expression = {
|
||||||
|
<l: @L> <bare: Bare> <r: @R> => ExpressionBuilder::spanned_bare(bare, l, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
SpannedOperator: Spanned<Operator> = {
|
||||||
|
<l: @L> <op: Operator> <r: @R> => Spanned::from_item(op, Span::from((l, r)))
|
||||||
|
}
|
||||||
|
|
||||||
// === LOGICAL TOKENS === //
|
// === LOGICAL TOKENS === //
|
||||||
|
|
||||||
// A logical token may be composed of more than one raw token, but the tokens must be emitted
|
// A logical token may be composed of more than one raw token, but the tokens must be emitted
|
||||||
// from the stream in exactly one sequence. This allows us to use parser infrastructure to
|
// from the stream in exactly one sequence. This allows us to use parser infrastructure to
|
||||||
// compose tokens without the risk that these logical tokens will introduce ambiguities.
|
// compose tokens without the risk that these logical tokens will introduce ambiguities.
|
||||||
|
|
||||||
Bare: BarePath = {
|
Bare: Bare = {
|
||||||
<head: "bare"> => BarePath::from_token(head)
|
<head: "bare"> => Bare::from_string(head.as_slice())
|
||||||
}
|
}
|
||||||
|
|
||||||
// A member is a special token that represents bare words or string literals immediate
|
// A member is a special token that represents bare words or string literals immediate
|
||||||
|
@ -129,25 +145,25 @@ Operator: Operator = {
|
||||||
}
|
}
|
||||||
|
|
||||||
Int: i64 = {
|
Int: i64 = {
|
||||||
<"num"> => i64::from_str(<>.as_slice()).unwrap()
|
<n: "num"> => i64::from_str(<>.as_slice()).unwrap(),
|
||||||
}
|
}
|
||||||
|
|
||||||
UnitsNum: Expression = {
|
UnitsNum: Expression = {
|
||||||
<num: Int> <unit: "unit"> => Expression::leaf(Leaf::Unit(num, Unit::from_str(unit.as_slice()).unwrap()))
|
<l: @L> <num: Int> <unit: "unit"> <r: @R> => ExpressionBuilder::spanned_unit((num, Unit::from_str(unit.as_slice()).unwrap()), l, r),
|
||||||
}
|
}
|
||||||
|
|
||||||
String: Expression = {
|
String: Expression = {
|
||||||
<"sqstring"> => <>.as_slice()[1..(<>.as_slice().len() - 1)].to_string().into(),
|
<l: @L> <s: "sqstring"> <r: @R> => ExpressionBuilder::spanned_string(&s.as_slice()[1..(s.as_slice().len() - 1)], l, r),
|
||||||
<"dqstring"> => <>.as_slice()[1..(<>.as_slice().len() - 1)].to_string().into()
|
<l: @L> <s: "dqstring"> <r: @R> => ExpressionBuilder::spanned_string(&s.as_slice()[1..(s.as_slice().len() - 1)], l, r),
|
||||||
}
|
}
|
||||||
|
|
||||||
Flag: Flag = {
|
Flag: Expression = {
|
||||||
"-" <Bare> => Flag::Shorthand(<>.to_string()),
|
<l: @L> "-" <b: Bare> <r: @R> => ExpressionBuilder::spanned_shorthand(b.to_string(), l, r),
|
||||||
"--" <Bare> => Flag::Longhand(<>.to_string()),
|
<l: @L> "--" <b: Bare> <r: @R> => ExpressionBuilder::spanned_flag(b.to_string(), l, r),
|
||||||
}
|
}
|
||||||
|
|
||||||
Var: Expression = {
|
Var: Expression = {
|
||||||
"$" <"variable"> => Variable::from_str(<>.as_slice()).into(),
|
<l: @L> "$" <v: "variable"> <r: @R> => ExpressionBuilder::spanned_var(v.as_slice(), l, r),
|
||||||
}
|
}
|
||||||
|
|
||||||
extern {
|
extern {
|
||||||
|
|
4701
src/parser/parser.rs
4701
src/parser/parser.rs
File diff suppressed because it is too large
Load diff
|
@ -41,21 +41,31 @@ impl PositionalType {
|
||||||
match self {
|
match self {
|
||||||
PositionalType::Value(_) => evaluate_expr(&arg, scope),
|
PositionalType::Value(_) => evaluate_expr(&arg, scope),
|
||||||
PositionalType::Block(_) => match arg {
|
PositionalType::Block(_) => match arg {
|
||||||
ast::Expression::Block(b) => Ok(Value::block(b.expr)),
|
ast::Expression {
|
||||||
ast::Expression::Binary(b) => {
|
expr: ast::RawExpression::Block(b),
|
||||||
if let Some(s) = b.left.as_string() {
|
..
|
||||||
Ok(Value::block(ast::Expression::Binary(Box::new(
|
} => Ok(Value::block(b.expr)),
|
||||||
ast::Binary::new(
|
ast::Expression {
|
||||||
ast::Expression::Path(Box::new(ast::Path::new(
|
expr: ast::RawExpression::Binary(binary),
|
||||||
ast::Expression::VariableReference(ast::Variable::It),
|
..
|
||||||
vec![s],
|
} => {
|
||||||
))),
|
// TODO: Use original spans
|
||||||
b.operator,
|
let mut b = ast::ExpressionBuilder::new();
|
||||||
b.right,
|
if let Some(s) = binary.left.as_string() {
|
||||||
),
|
Ok(Value::block(b.binary((
|
||||||
|
&|b| b.path((&|b| b.var("it"), vec![s.clone()])),
|
||||||
|
&|_| binary.operator.clone(),
|
||||||
|
&|_| binary.right.clone(),
|
||||||
))))
|
))))
|
||||||
} else {
|
} else {
|
||||||
Ok(Value::block(ast::Expression::Binary(b)))
|
let mut b = ast::ExpressionBuilder::new();
|
||||||
|
let expr = b.binary((
|
||||||
|
&|_| binary.left.clone(),
|
||||||
|
&|_| binary.operator.clone(),
|
||||||
|
&|_| binary.right.clone(),
|
||||||
|
));
|
||||||
|
|
||||||
|
Ok(Value::block(expr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
other => Ok(Value::block(other)), // other =>
|
other => Ok(Value::block(other)), // other =>
|
||||||
|
@ -192,13 +202,13 @@ fn extract_named(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expect_simple_expr(expr: ast::Expression) -> Result<Value, ShellError> {
|
fn expect_simple_expr(expr: ast::Expression) -> Result<Value, ShellError> {
|
||||||
match expr {
|
match &*expr {
|
||||||
ast::Expression::Leaf(l) => Ok(match l {
|
ast::RawExpression::Leaf(l) => Ok(match l {
|
||||||
ast::Leaf::Bare(s) => Value::string(s.to_string()),
|
ast::Leaf::Bare(s) => Value::string(s.to_string()),
|
||||||
ast::Leaf::String(s) => Value::string(s),
|
ast::Leaf::String(s) => Value::string(s),
|
||||||
ast::Leaf::Boolean(b) => Value::boolean(b),
|
ast::Leaf::Boolean(b) => Value::boolean(*b),
|
||||||
ast::Leaf::Int(i) => Value::int(i),
|
ast::Leaf::Int(i) => Value::int(*i),
|
||||||
ast::Leaf::Unit(i, unit) => unit.compute(i),
|
ast::Leaf::Unit(i, unit) => unit.compute(*i),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// TODO: Diagnostic
|
// TODO: Diagnostic
|
||||||
|
|
Loading…
Reference in a new issue