mirror of
https://github.com/nushell/nushell
synced 2025-01-13 13:49:21 +00:00
Fix bare word .
Addresses `git add .`
This commit is contained in:
parent
9a639fd27b
commit
9e9c0b9811
5 changed files with 443 additions and 341 deletions
|
@ -20,6 +20,10 @@ crate fn evaluate_expr(expr: &ast::Expression, scope: &Scope) -> Result<Value, S
|
||||||
match expr {
|
match expr {
|
||||||
Expression::Leaf(l) => Ok(evaluate_leaf(l)),
|
Expression::Leaf(l) => Ok(evaluate_leaf(l)),
|
||||||
Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),
|
Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),
|
||||||
|
Expression::Flag(f) => Err(ShellError::string(format!(
|
||||||
|
"can't evaluate the flag {}",
|
||||||
|
f.print()
|
||||||
|
))),
|
||||||
Expression::Block(b) => evaluate_block(&b, scope),
|
Expression::Block(b) => evaluate_block(&b, scope),
|
||||||
Expression::Path(p) => evaluate_path(&p, scope),
|
Expression::Path(p) => evaluate_path(&p, scope),
|
||||||
Expression::Binary(b) => evaluate_binary(b, scope),
|
Expression::Binary(b) => evaluate_binary(b, scope),
|
||||||
|
|
|
@ -45,6 +45,7 @@ impl FromStr for Operator {
|
||||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||||
pub enum Expression {
|
pub enum Expression {
|
||||||
Leaf(Leaf),
|
Leaf(Leaf),
|
||||||
|
Flag(Flag),
|
||||||
Parenthesized(Box<Parenthesized>),
|
Parenthesized(Box<Parenthesized>),
|
||||||
Block(Box<Block>),
|
Block(Box<Block>),
|
||||||
Binary(Box<Binary>),
|
Binary(Box<Binary>),
|
||||||
|
@ -56,6 +57,7 @@ impl Expression {
|
||||||
crate fn print(&self) -> String {
|
crate fn print(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Expression::Leaf(l) => l.print(),
|
Expression::Leaf(l) => l.print(),
|
||||||
|
Expression::Flag(f) => f.print(),
|
||||||
Expression::Parenthesized(p) => p.print(),
|
Expression::Parenthesized(p) => p.print(),
|
||||||
Expression::Block(b) => b.print(),
|
Expression::Block(b) => b.print(),
|
||||||
Expression::VariableReference(r) => r.print(),
|
Expression::VariableReference(r) => r.print(),
|
||||||
|
@ -67,6 +69,7 @@ impl Expression {
|
||||||
crate fn as_external_arg(&self) -> String {
|
crate fn as_external_arg(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Expression::Leaf(l) => l.as_external_arg(),
|
Expression::Leaf(l) => l.as_external_arg(),
|
||||||
|
Expression::Flag(f) => f.as_external_arg(),
|
||||||
Expression::Parenthesized(p) => p.as_external_arg(),
|
Expression::Parenthesized(p) => p.as_external_arg(),
|
||||||
Expression::Block(b) => b.as_external_arg(),
|
Expression::Block(b) => b.as_external_arg(),
|
||||||
Expression::VariableReference(r) => r.as_external_arg(),
|
Expression::VariableReference(r) => r.as_external_arg(),
|
||||||
|
@ -262,7 +265,7 @@ impl Binary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||||
pub enum Flag {
|
pub enum Flag {
|
||||||
Shorthand(String),
|
Shorthand(String),
|
||||||
Longhand(String),
|
Longhand(String),
|
||||||
|
@ -270,12 +273,17 @@ pub enum Flag {
|
||||||
|
|
||||||
impl Flag {
|
impl Flag {
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
fn print(&self) -> String {
|
crate fn print(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Flag::Shorthand(s) => format!("-{}", s),
|
Flag::Shorthand(s) => format!("-{}", s),
|
||||||
Flag::Longhand(s) => format!("--{}", s),
|
Flag::Longhand(s) => format!("--{}", s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
crate fn as_external_arg(&self) -> String {
|
||||||
|
self.print()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(new, Debug, Clone)]
|
#[derive(new, Debug, Clone)]
|
||||||
|
|
|
@ -92,7 +92,7 @@ impl TopToken {
|
||||||
Dollar => Token::Dollar,
|
Dollar => Token::Dollar,
|
||||||
Bare => Token::Bare,
|
Bare => Token::Bare,
|
||||||
Pipe => Token::Pipe,
|
Pipe => Token::Pipe,
|
||||||
Dot => Token::Dot,
|
Dot => Token::Bare,
|
||||||
OpenBrace => Token::OpenBrace,
|
OpenBrace => Token::OpenBrace,
|
||||||
CloseBrace => Token::CloseBrace,
|
CloseBrace => Token::CloseBrace,
|
||||||
OpenParen => Token::OpenParen,
|
OpenParen => Token::OpenParen,
|
||||||
|
@ -180,7 +180,7 @@ impl AfterVariableToken {
|
||||||
|
|
||||||
let result = match self {
|
let result = match self {
|
||||||
END => return None,
|
END => return None,
|
||||||
Dot => Token::Dot,
|
Dot => Token::PathDot,
|
||||||
Whitespace => Token::Whitespace,
|
Whitespace => Token::Whitespace,
|
||||||
Error => unreachable!("Don't call to_token with the error variant"),
|
Error => unreachable!("Don't call to_token with the error variant"),
|
||||||
};
|
};
|
||||||
|
@ -340,6 +340,7 @@ impl SpannedToken<'source> {
|
||||||
pub enum Token {
|
pub enum Token {
|
||||||
Variable,
|
Variable,
|
||||||
Dot,
|
Dot,
|
||||||
|
PathDot,
|
||||||
Member,
|
Member,
|
||||||
Num,
|
Num,
|
||||||
SQString,
|
SQString,
|
||||||
|
@ -445,7 +446,6 @@ impl Iterator for Lexer<'source> {
|
||||||
return Some(Err(lex_error(&range, self.lexer.source)))
|
return Some(Err(lex_error(&range, self.lexer.source)))
|
||||||
}
|
}
|
||||||
AfterVariableToken::Whitespace if !self.whitespace => self.next(),
|
AfterVariableToken::Whitespace if !self.whitespace => self.next(),
|
||||||
|
|
||||||
other => return spanned(other.to_token()?, slice, &range),
|
other => return spanned(other.to_token()?, slice, &range),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -547,6 +547,7 @@ mod tests {
|
||||||
enum TokenDesc {
|
enum TokenDesc {
|
||||||
Ws,
|
Ws,
|
||||||
Member,
|
Member,
|
||||||
|
PathDot,
|
||||||
Top(TopToken),
|
Top(TopToken),
|
||||||
Var(VariableToken),
|
Var(VariableToken),
|
||||||
}
|
}
|
||||||
|
@ -576,6 +577,10 @@ mod tests {
|
||||||
TokenDesc::Ws => {
|
TokenDesc::Ws => {
|
||||||
SpannedToken::new(Span::new(range), self.source, Token::Whitespace)
|
SpannedToken::new(Span::new(range), self.source, Token::Whitespace)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TokenDesc::PathDot => {
|
||||||
|
SpannedToken::new(Span::new(range), self.source, Token::PathDot)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -651,42 +656,45 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_tokenize_path() {
|
fn test_tokenize_path() {
|
||||||
assert_lex("$var.bar", tokens![ "$" Var("var") "." Member("bar") ]);
|
assert_lex("$var.bar", tokens![ "$" Var("var") "???." Member("bar") ]);
|
||||||
assert_lex("$it.bar", tokens![ "$" Var("it") "." Member("bar") ]);
|
assert_lex("$it.bar", tokens![ "$" Var("it") "???." Member("bar") ]);
|
||||||
assert_lex("$var. bar", tokens![ "$" Var("var") "." SP Member("bar") ]);
|
assert_lex(
|
||||||
assert_lex("$it. bar", tokens![ "$" Var("it") "." SP Member("bar") ]);
|
"$var. bar",
|
||||||
|
tokens![ "$" Var("var") "???." SP Member("bar") ],
|
||||||
|
);
|
||||||
|
assert_lex("$it. bar", tokens![ "$" Var("it") "???." SP Member("bar") ]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_tokenize_operator() {
|
fn test_tokenize_operator() {
|
||||||
assert_lex(
|
assert_lex(
|
||||||
"$it.cpu > 10",
|
"$it.cpu > 10",
|
||||||
tokens![ "$" Var("it") "." Member("cpu") SP ">" SP Num("10") ],
|
tokens![ "$" Var("it") "???." Member("cpu") SP ">" SP Num("10") ],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_lex(
|
assert_lex(
|
||||||
"$it.cpu < 10",
|
"$it.cpu < 10",
|
||||||
tokens![ "$" Var("it") "." Member("cpu") SP "<" SP Num("10") ],
|
tokens![ "$" Var("it") "???." Member("cpu") SP "<" SP Num("10") ],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_lex(
|
assert_lex(
|
||||||
"$it.cpu >= 10",
|
"$it.cpu >= 10",
|
||||||
tokens![ "$" Var("it") "." Member("cpu") SP ">=" SP Num("10") ],
|
tokens![ "$" Var("it") "???." Member("cpu") SP ">=" SP Num("10") ],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_lex(
|
assert_lex(
|
||||||
"$it.cpu <= 10",
|
"$it.cpu <= 10",
|
||||||
tokens![ "$" Var("it") "." Member("cpu") SP "<=" SP Num("10") ],
|
tokens![ "$" Var("it") "???." Member("cpu") SP "<=" SP Num("10") ],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_lex(
|
assert_lex(
|
||||||
"$it.cpu == 10",
|
"$it.cpu == 10",
|
||||||
tokens![ "$" Var("it") "." Member("cpu") SP "==" SP Num("10") ],
|
tokens![ "$" Var("it") "???." Member("cpu") SP "==" SP Num("10") ],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_lex(
|
assert_lex(
|
||||||
"$it.cpu != 10",
|
"$it.cpu != 10",
|
||||||
tokens![ "$" Var("it") "." Member("cpu") SP "!=" SP Num("10") ],
|
tokens![ "$" Var("it") "???." Member("cpu") SP "!=" SP Num("10") ],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -699,13 +707,18 @@ mod tests {
|
||||||
|
|
||||||
assert_lex(
|
assert_lex(
|
||||||
"ls | where { $it.cpu > 10 }",
|
"ls | where { $it.cpu > 10 }",
|
||||||
tokens![ Bare("ls") SP "|" SP Bare("where") SP "{" SP "$" Var("it") "." Member("cpu") SP ">" SP Num("10") SP "}" ],
|
tokens![ Bare("ls") SP "|" SP Bare("where") SP "{" SP "$" Var("it") "???." Member("cpu") SP ">" SP Num("10") SP "}" ],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_lex(
|
assert_lex(
|
||||||
"open input2.json | from-json | select glossary",
|
"open input2.json | from-json | select glossary",
|
||||||
tokens![ Bare("open") SP Bare("input2") "." Member("json") SP "|" SP Bare("from-json") SP "|" SP Bare("select") SP Bare("glossary") ],
|
tokens![ Bare("open") SP Bare("input2") "???." Member("json") SP "|" SP Bare("from-json") SP "|" SP Bare("select") SP Bare("glossary") ],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert_lex(
|
||||||
|
"git add . -v",
|
||||||
|
tokens![ Bare("git") SP Bare("add") SP Bare(".") SP "-" Bare("v") ],
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tok(name: &str, value: &'source str) -> TestToken<'source> {
|
fn tok(name: &str, value: &'source str) -> TestToken<'source> {
|
||||||
|
@ -722,7 +735,10 @@ mod tests {
|
||||||
|
|
||||||
fn tk(name: &'source str) -> TestToken<'source> {
|
fn tk(name: &'source str) -> TestToken<'source> {
|
||||||
let token = match name {
|
let token = match name {
|
||||||
|
"???." => return TestToken::new(TokenDesc::PathDot, "."),
|
||||||
"." => TopToken::Dot,
|
"." => TopToken::Dot,
|
||||||
|
"--" => TopToken::DashDash,
|
||||||
|
"-" => TopToken::Dash,
|
||||||
"$" => TopToken::Dollar,
|
"$" => TopToken::Dollar,
|
||||||
"|" => TopToken::Pipe,
|
"|" => TopToken::Pipe,
|
||||||
"{" => TopToken::OpenBrace,
|
"{" => TopToken::OpenBrace,
|
||||||
|
|
|
@ -52,10 +52,11 @@ WholeExpression: Expression = {
|
||||||
PathHead: Expression = {
|
PathHead: Expression = {
|
||||||
<WholeExpression>,
|
<WholeExpression>,
|
||||||
<BarePath> => Expression::Leaf(Leaf::Bare(<>)),
|
<BarePath> => Expression::Leaf(Leaf::Bare(<>)),
|
||||||
|
<Flag> => Expression::Flag(<>),
|
||||||
}
|
}
|
||||||
|
|
||||||
PathExpression: Expression = {
|
PathExpression: Expression = {
|
||||||
<head:WholeExpression> <tail: ( "." <Member> )+> => Expression::Path(Box::new(Path::new(head, tail)))
|
<head:WholeExpression> <tail: ( "???." <Member> )+> => Expression::Path(Box::new(Path::new(head, tail)))
|
||||||
}
|
}
|
||||||
|
|
||||||
Expr: Expression = {
|
Expr: Expression = {
|
||||||
|
@ -92,7 +93,7 @@ String: String = {
|
||||||
}
|
}
|
||||||
|
|
||||||
BarePath: BarePath = {
|
BarePath: BarePath = {
|
||||||
<head: "bare"> <tail: ( "." <"member"> )*> => BarePath::from_tokens(head, tail)
|
<head: "bare"> <tail: ( "???." <"member"> )*> => BarePath::from_tokens(head, tail)
|
||||||
}
|
}
|
||||||
|
|
||||||
Int: i64 = {
|
Int: i64 = {
|
||||||
|
@ -119,6 +120,7 @@ extern {
|
||||||
"-" => SpannedToken { token: Token::Dash, .. },
|
"-" => SpannedToken { token: Token::Dash, .. },
|
||||||
"--" => SpannedToken { token: Token::DashDash, .. },
|
"--" => SpannedToken { token: Token::DashDash, .. },
|
||||||
"$" => SpannedToken { token: Token::Dollar, .. },
|
"$" => SpannedToken { token: Token::Dollar, .. },
|
||||||
|
"???." => SpannedToken { token: Token::PathDot, .. },
|
||||||
"num" => SpannedToken { token: Token::Num, .. },
|
"num" => SpannedToken { token: Token::Num, .. },
|
||||||
"member" => SpannedToken { token: Token::Member, .. },
|
"member" => SpannedToken { token: Token::Member, .. },
|
||||||
"variable" => SpannedToken { token: Token::Variable, .. },
|
"variable" => SpannedToken { token: Token::Variable, .. },
|
||||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue