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

Add / operator #1237

Merged
merged 6 commits into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions GRAMMAR.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ setting : 'set' 'dotenv-load' boolean?
boolean : ':=' ('true' | 'false')

expression : 'if' condition '{' expression '}' 'else' '{' expression '}'
| value '/' expression
| value '+' expression
| value

Expand Down
38 changes: 33 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -803,11 +803,12 @@ Starting server with database localhost:6379 on port 1337…

### Variables and Substitution

Variables, strings, concatenation, and substitution using `{{…}}` are supported:
Variables, strings, concatenation, path joining, and substitution using `{{…}}` are supported:

```make
tmpdir := `mktemp`
version := "0.2.7"
tardir := "awesomesauce-" + version
tardir := tmpdir / "awesomesauce-" + version
tarball := tardir + ".tar.gz"

publish:
Expand All @@ -819,6 +820,33 @@ publish:
rm -rf {{tarball}} {{tardir}}
```

#### Joining Paths

The `/` operator can be used to join two strings with a slash:

```make
foo := "a" / "b"
```

```
$ just --evaluate foo
a/b
```

Note that a `/` is added even if one is already present:

```make
foo := "a/"
bar := foo / "b"
```

```
$ just --evaluate bar
a//b
```

The `/` operator uses the `/` character, even on Windows. Thus, using the `/` operator should be avoided with paths that use universal naming convention (UNC), i.e., those that start with `\?`, since forward slashes are not supported with UNC paths.

#### Escaping `{{`

To write a recipe containing `{{`, use `{{{{`:
Expand Down Expand Up @@ -1064,7 +1092,7 @@ These functions can fail, for example if a path does not have an extension, whic

##### Infallible

- `join(a, b…)` - Join path `a` with path `b`. `join("foo/bar", "baz")` is `foo/bar/baz`. Accepts two or more arguments.
- `join(a, b…)` - *This function uses `/` on Unix and `\` on Windows, which can be lead to unwanted behavior. The `/` operator, e.g., `a / b`, which always uses `/`, should be considered as a replacement unless `\`s are specifically desired on Windows.* Join path `a` with path `b`. `join("foo/bar", "baz")` is `foo/bar/baz`. Accepts two or more arguments.

- `clean(path)` - Simplify `path` by removing extra path separators, intermediate `.` components, and `..` where possible. `clean("foo//bar")` is `foo/bar`, `clean("foo/..")` is `.`, `clean("foo/./bar")` is `foo/bar`.

Expand Down Expand Up @@ -1360,12 +1388,12 @@ Testing server:unit…
./test --tests unit server
```

Default values may be arbitrary expressions, but concatenations must be parenthesized:
Default values may be arbitrary expressions, but concatenations or path joins must be parenthesized:

```make
arch := "wasm"

test triple=(arch + "-unknown-unknown"):
test triple=(arch + "-unknown-unknown") input=(arch / "input.dat"):
./test {{triple}}
```

Expand Down
3 changes: 0 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ check: fmt clippy test forbid
git diff --no-ext-diff --quiet --exit-code
VERSION=`sed -En 's/version[[:space:]]*=[[:space:]]*"([^"]+)"/\1/p' Cargo.toml | head -1`
grep "^\[$VERSION\]" CHANGELOG.md
cargo +nightly generate-lockfile -Z minimal-versions
cargo test
git checkout Cargo.lock

# publish current GitHub master branch
publish:
Expand Down
2 changes: 1 addition & 1 deletion src/assignment_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<'src: 'run, 'run> AssignmentResolver<'src, 'run> {
self.resolve_expression(c)
}
},
Expression::Concatenation { lhs, rhs } => {
Expression::Concatenation { lhs, rhs } | Expression::Join { lhs, rhs } => {
self.resolve_expression(lhs)?;
self.resolve_expression(rhs)
}
Expand Down
3 changes: 3 additions & 0 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ 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)?)
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ pub(crate) enum Expression<'src> {
},
/// `(contents)`
Group { contents: Box<Expression<'src>> },
/// `lhs / rhs`
Join {
lhs: Box<Expression<'src>>,
rhs: Box<Expression<'src>>,
},
/// `"string_literal"` or `'string_literal'`
StringLiteral { string_literal: StringLiteral<'src> },
/// `variable`
Expand All @@ -46,6 +51,7 @@ 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::Concatenation { lhs, rhs } => write!(f, "{} + {}", lhs, rhs),
Expression::Conditional {
lhs,
Expand Down Expand Up @@ -86,6 +92,13 @@ impl<'src> Serialize for Expression<'src> {
seq.serialize_element(rhs)?;
seq.end()
}
Self::Join { lhs, rhs } => {
let mut seq = serializer.serialize_seq(None)?;
seq.serialize_element("join")?;
seq.serialize_element(lhs)?;
seq.serialize_element(rhs)?;
seq.end()
}
Self::Conditional {
lhs,
rhs,
Expand Down
2 changes: 2 additions & 0 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ impl<'src> Lexer<'src> {
'*' => self.lex_single(Asterisk),
'+' => self.lex_single(Plus),
',' => self.lex_single(Comma),
'/' => self.lex_single(Slash),
':' => self.lex_colon(),
'=' => self.lex_choices('=', &[('=', EqualsEquals), ('~', EqualsTilde)], Equals),
'@' => self.lex_single(At),
Expand Down Expand Up @@ -942,6 +943,7 @@ mod tests {
ParenL => "(",
ParenR => ")",
Plus => "+",
Slash => "/",
Whitespace => " ",

// Empty lexemes
Expand Down
1 change: 1 addition & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ 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()),
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,11 @@ impl<'tokens, 'src> Parser<'tokens, 'src> {
} else {
let value = self.parse_value()?;

if self.accepted(Plus)? {
if self.accepted(Slash)? {
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 rhs = Box::new(self.parse_expression()?);
Expression::Concatenation { lhs, rhs }
Expand Down
8 changes: 8 additions & 0 deletions src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ pub enum Expression {
otherwise: Box<Expression>,
operator: ConditionalOperator,
},
Join {
lhs: Box<Expression>,
rhs: Box<Expression>,
},
String {
text: String,
},
Expand Down Expand Up @@ -253,6 +257,10 @@ impl Expression {
lhs: Box::new(Expression::new(lhs)),
rhs: Box::new(Expression::new(rhs)),
},
Join { lhs, rhs } => Expression::Join {
lhs: Box::new(Expression::new(lhs)),
rhs: Box::new(Expression::new(rhs)),
},
Conditional {
lhs,
operator,
Expand Down
2 changes: 2 additions & 0 deletions src/token_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) enum TokenKind {
ParenL,
ParenR,
Plus,
Slash,
StringToken,
Text,
Unspecified,
Expand Down Expand Up @@ -71,6 +72,7 @@ impl Display for TokenKind {
ParenL => "'('",
ParenR => "')'",
Plus => "'+'",
Slash => "'/'",
StringToken => "string",
Text => "command text",
Unspecified => "unspecified",
Expand Down
2 changes: 1 addition & 1 deletion src/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'expression, 'src> Iterator for Variables<'expression, 'src> {
self.stack.push(lhs);
}
Expression::Variable { name, .. } => return Some(name.token()),
Expression::Concatenation { lhs, rhs } => {
Expression::Concatenation { lhs, rhs } | Expression::Join { lhs, rhs } => {
self.stack.push(rhs);
self.stack.push(lhs);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ test! {
",
stdout: "",
stderr: "
error: Expected '!=', '==', '=~', or '+', but found identifier
error: Expected '!=', '==', '=~', '+', or '/', but found identifier
|
1 | a := if '' a '' { '' } else { b }
| ^
Expand Down
2 changes: 1 addition & 1 deletion tests/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test! {
}

test! {
name: evaluate_single,
name: evaluate_single_free,
justfile: "
a := 'x'
b := 'y'
Expand Down
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod search;
mod shebang;
mod shell;
mod show;
mod slash_operator;
mod string;
mod sublime_syntax;
mod subsequents;
Expand Down
54 changes: 54 additions & 0 deletions tests/slash_operator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use super::*;

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

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

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

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