Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makes slash operator default lhs value to an empty string #1320

Merged
merged 17 commits into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/assignment_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ impl<'src: 'run, 'run> AssignmentResolver<'src, 'run> {
self.resolve_expression(c)
}
},
Expression::Concatenation { lhs, rhs } | Expression::Join { lhs, rhs } => {
Expression::Concatenation { lhs, rhs } => {
self.resolve_expression(lhs)?;
self.resolve_expression(rhs)
}
Expression::Join { lhs, rhs } => {
if let Some(lhs) = lhs {
self.resolve_expression(lhs)?;
}
self.resolve_expression(rhs)
}
Expression::Conditional {
lhs,
rhs,
Expand Down
8 changes: 5 additions & 3 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,11 @@ impl<'src, 'run> Evaluator<'src, 'run> {
}
}
Expression::Group { contents } => self.evaluate_expression(contents),
Expression::Join { lhs, rhs } => {
Ok(self.evaluate_expression(lhs)? + "/" + &self.evaluate_expression(rhs)?)
}
Expression::Join { lhs: None, rhs } => Ok("/".to_string() + &self.evaluate_expression(rhs)?),
Expression::Join {
lhs: Some(lhs),
rhs,
} => Ok(self.evaluate_expression(lhs)? + "/" + &self.evaluate_expression(rhs)?),
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) enum Expression<'src> {
Group { contents: Box<Expression<'src>> },
/// `lhs / rhs`
Join {
lhs: Box<Expression<'src>>,
lhs: Option<Box<Expression<'src>>>,
rhs: Box<Expression<'src>>,
},
/// `"string_literal"` or `'string_literal'`
Expand All @@ -51,7 +51,11 @@ impl<'src> Display for Expression<'src> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match self {
Expression::Backtick { token, .. } => write!(f, "{}", token.lexeme()),
Expression::Join { lhs, rhs } => write!(f, "{} / {}", lhs, rhs),
Expression::Join { lhs: None, rhs } => write!(f, "/ {}", rhs),
Expression::Join {
lhs: Some(lhs),
rhs,
} => write!(f, "{} / {}", lhs, rhs),
Expression::Concatenation { lhs, rhs } => write!(f, "{} + {}", lhs, rhs),
Expression::Conditional {
lhs,
Expand Down
6 changes: 5 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ impl<'src> Node<'src> for Expression<'src> {
} => Tree::string(cooked),
Expression::Backtick { contents, .. } => Tree::atom("backtick").push(Tree::string(contents)),
Expression::Group { contents } => Tree::List(vec![contents.tree()]),
Expression::Join { lhs, rhs } => Tree::atom("/").push(lhs.tree()).push(rhs.tree()),
Expression::Join { lhs: None, rhs } => Tree::atom("/").push(rhs.tree()),
Expression::Join {
lhs: Some(lhs),
rhs,
} => Tree::atom("/").push(lhs.tree()).push(rhs.tree()),
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,15 @@ impl<'tokens, 'src> Parser<'tokens, 'src> {

let expression = if self.accepted_keyword(Keyword::If)? {
self.parse_conditional()?
} else if self.accepted(Slash)? {
let lhs = None;
let rhs = Box::new(self.parse_expression()?);
Expression::Join { lhs, rhs }
} else {
let value = self.parse_value()?;

if self.accepted(Slash)? {
let lhs = Box::new(value);
let lhs = Some(Box::new(value));
let rhs = Box::new(self.parse_expression()?);
Expression::Join { lhs, rhs }
} else if self.accepted(Plus)? {
Expand Down Expand Up @@ -1989,6 +1993,7 @@ mod tests {
Identifier,
ParenL,
ParenR,
Slash,
erikkrieg marked this conversation as resolved.
Show resolved Hide resolved
StringToken,
],
found: Eof,
Expand All @@ -2008,6 +2013,7 @@ mod tests {
Identifier,
ParenL,
ParenR,
Slash,
StringToken,
],
found: InterpolationEnd,
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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can pub(crate) be removed now?

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 {
erikkrieg marked this conversation as resolved.
Show resolved Hide resolved
Self {
delimiter,
indented,
Expand Down
4 changes: 2 additions & 2 deletions src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub enum Expression {
operator: ConditionalOperator,
},
Join {
lhs: Box<Expression>,
lhs: Option<Box<Expression>>,
rhs: Box<Expression>,
},
String {
Expand Down Expand Up @@ -258,7 +258,7 @@ impl Expression {
rhs: Box::new(Expression::new(rhs)),
},
Join { lhs, rhs } => Expression::Join {
lhs: Box::new(Expression::new(lhs)),
lhs: lhs.as_ref().map(|lhs| Box::new(Expression::new(lhs))),
rhs: Box::new(Expression::new(rhs)),
},
Conditional {
Expand Down
8 changes: 7 additions & 1 deletion src/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ impl<'expression, 'src> Iterator for Variables<'expression, 'src> {
self.stack.push(lhs);
}
Expression::Variable { name, .. } => return Some(name.token()),
Expression::Concatenation { lhs, rhs } | Expression::Join { lhs, rhs } => {
Expression::Concatenation { lhs, rhs } => {
self.stack.push(rhs);
self.stack.push(lhs);
}
Expression::Join { lhs, rhs } => {
self.stack.push(rhs);
if let Some(lhs) = lhs {
self.stack.push(lhs);
}
}
Expression::Group { contents } => {
self.stack.push(contents);
}
Expand Down
74 changes: 74 additions & 0 deletions tests/slash_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,45 @@ fn twice() {
.run();
}

#[test]
fn no_lhs_once() {
Test::new()
.justfile("x := / 'a'")
.args(&["--evaluate", "x"])
.stdout("/a")
.run();
}

#[test]
fn no_lhs_twice() {
Test::new()
.justfile("x := / 'a' / 'b'")
.args(&["--evaluate", "x"])
.stdout("/a/b")
.run();
Test::new()
.justfile("x := // 'a'")
.args(&["--evaluate", "x"])
.stdout("//a")
.run();
}

#[test]
fn no_rhs_once() {
Test::new()
.justfile("x := 'a' /")
.stderr(
"
error: Expected backtick, identifier, '(', '/', or string, but found end of file
|
1 | x := 'a' /
| ^
",
)
.status(EXIT_FAILURE)
.run();
}

#[test]
fn default_un_parenthesized() {
Test::new()
Expand All @@ -39,6 +78,27 @@ fn default_un_parenthesized() {
.run();
}

#[test]
fn no_lhs_un_parenthesized() {
Test::new()
.justfile(
"
foo x=/ 'a' / 'b':
echo {{x}}
",
)
.stderr(
"
error: Expected backtick, identifier, '(', or string, but found '/'
|
1 | foo x=/ 'a' / 'b':
| ^
",
)
.status(EXIT_FAILURE)
.run();
}

#[test]
fn default_parenthesized() {
Test::new()
Expand All @@ -52,3 +112,17 @@ fn default_parenthesized() {
.stdout("a/b\n")
.run();
}

#[test]
fn no_lhs_parenthesized() {
Test::new()
.justfile(
"
foo x=(/ 'a' / 'b'):
echo {{x}}
",
)
.stderr("echo /a/b\n")
.stdout("/a/b\n")
.run();
}