Skip to content

Commit

Permalink
makes slash operator default lhs value to an empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Krieg committed Aug 26, 2022
1 parent 35fb61a commit eca4f9f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/string_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions tests/slash_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit eca4f9f

Please sign in to comment.