From eca4f9f195356ded09779c6481566733e280ed09 Mon Sep 17 00:00:00 2001 From: Erik Krieg Date: Fri, 26 Aug 2022 00:47:33 -0400 Subject: [PATCH] makes slash operator default lhs value to an empty string --- src/parser.rs | 17 +++++++++++++---- src/string_kind.rs | 4 ++-- tests/slash_operator.rs | 9 +++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 00ec7e7edf..d696014c3f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -405,18 +405,27 @@ impl<'tokens, 'src> Parser<'tokens, 'src> { let expression = if self.accepted_keyword(Keyword::If)? { self.parse_conditional()? } else { - let value = self.parse_value()?; - + let value = self.parse_value(); if self.accepted(Slash)? { + // Enables Slash operator to build absolute paths, like `/ "Users"`, by + // setting a default empty string on the lhs if the resulting expression + // is not Ok. + let value = value.unwrap_or(Expression::StringLiteral { + string_literal: StringLiteral { + kind: StringKind::new(string_kind::StringDelimiter::QuoteDouble, false), + raw: "", + cooked: "".to_string(), + }, + }); let lhs = Box::new(value); let rhs = Box::new(self.parse_expression()?); Expression::Join { lhs, rhs } } else if self.accepted(Plus)? { - let lhs = Box::new(value); + let lhs = Box::new(value?); let rhs = Box::new(self.parse_expression()?); Expression::Concatenation { lhs, rhs } } else { - value + value? } }; diff --git a/src/string_kind.rs b/src/string_kind.rs index edfbe46ce3..dbb5496d07 100644 --- a/src/string_kind.rs +++ b/src/string_kind.rs @@ -7,7 +7,7 @@ pub(crate) struct StringKind { } #[derive(Debug, PartialEq, Clone, Copy, Ord, PartialOrd, Eq)] -enum StringDelimiter { +pub(crate) enum StringDelimiter { Backtick, QuoteDouble, QuoteSingle, @@ -26,7 +26,7 @@ impl StringKind { Self::new(StringDelimiter::QuoteSingle, false), ]; - const fn new(delimiter: StringDelimiter, indented: bool) -> Self { + pub(crate) const fn new(delimiter: StringDelimiter, indented: bool) -> Self { Self { delimiter, indented, diff --git a/tests/slash_operator.rs b/tests/slash_operator.rs index 5995a32e7b..9168fbee1e 100644 --- a/tests/slash_operator.rs +++ b/tests/slash_operator.rs @@ -18,6 +18,15 @@ fn twice() { .run(); } +#[test] +fn no_lhs() { + Test::new() + .justfile("x := / 'a'") + .args(&["--evaluate", "x"]) + .stdout("/a") + .run(); +} + #[test] fn default_un_parenthesized() { Test::new()