Skip to content

Commit

Permalink
fileset, revset, templater: add support for single-quoted raw string …
Browse files Browse the repository at this point in the history
…literals

Since fileset/revset/template expressions are specified as command-line
arguments, it's sometimes convenient to use single quotes instead of double
quotes. Various scripting languages parse single-quoted strings in various ways,
but I choose the TOML rule because it's simple and practically useful. TOML is
our config language, so copying the TOML syntax would be less surprising than
borrowing it from another language.

toml-lang/toml#188
  • Loading branch information
yuja committed Apr 25, 2024
1 parent a74bf89 commit 5b769c5
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
expressions](docs/filesets.md). Note that filesets are currently experimental,
but will be enabled by default in a future release.

* Revsets and templates now support single-quoted raw string literals.

* `jj prev` and `jj next` now work when the working copy revision is a merge.

* Operation objects in templates now have a `snapshot() -> Boolean` method that
Expand Down
4 changes: 4 additions & 0 deletions cli/src/template.pest
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ string_content_char = @{ !("\"" | "\\") ~ ANY }
string_content = @{ string_content_char+ }
string_literal = ${ "\"" ~ (string_content | string_escape)* ~ "\"" }

raw_string_content = @{ (!"'" ~ ANY)* }
raw_string_literal = ${ "'" ~ raw_string_content ~ "'" }

integer_literal = @{
ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*
| "0"
Expand Down Expand Up @@ -59,6 +62,7 @@ primary = _{
| lambda
| identifier
| string_literal
| raw_string_literal
| integer_literal
}

Expand Down
26 changes: 26 additions & 0 deletions cli/src/template_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ impl Rule {
Rule::string_content_char => None,
Rule::string_content => None,
Rule::string_literal => None,
Rule::raw_string_content => None,
Rule::raw_string_literal => None,
Rule::integer_literal => None,
Rule::identifier => None,
Rule::concat_op => Some("++"),
Expand Down Expand Up @@ -383,6 +385,12 @@ fn parse_term_node(pair: Pair<Rule>) -> TemplateParseResult<ExpressionNode> {
let text = STRING_LITERAL_PARSER.parse(expr.into_inner());
ExpressionNode::new(ExpressionKind::String(text), span)
}
Rule::raw_string_literal => {
let (content,) = expr.into_inner().collect_tuple().unwrap();
assert_eq!(content.as_rule(), Rule::raw_string_content);
let text = content.as_str().to_owned();
ExpressionNode::new(ExpressionKind::String(text), span)
}
Rule::integer_literal => {
let value = expr.as_str().parse().map_err(|err| {
TemplateParseError::expression("Invalid integer literal", span).with_source(err)
Expand Down Expand Up @@ -1178,6 +1186,24 @@ mod tests {
parse_into_kind(r#" "\y" "#),
Err(TemplateParseErrorKind::SyntaxError),
);

// Single-quoted raw string
assert_eq!(
parse_into_kind(r#" '' "#),
Ok(ExpressionKind::String("".to_owned())),
);
assert_eq!(
parse_into_kind(r#" 'a\n' "#),
Ok(ExpressionKind::String(r"a\n".to_owned())),
);
assert_eq!(
parse_into_kind(r#" '\' "#),
Ok(ExpressionKind::String(r"\".to_owned())),
);
assert_eq!(
parse_into_kind(r#" '"' "#),
Ok(ExpressionKind::String(r#"""#.to_owned())),
);
}

#[test]
Expand Down
8 changes: 5 additions & 3 deletions docs/filesets.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ ui.allow-filesets = true
```

Many `jj` commands accept fileset expressions as positional arguments. File
names passed to these commands must be quoted if they contain whitespace or meta
characters. However, as a special case, quotes can be omitted if the expression
has no operators nor function calls. For example:
names passed to these commands [must be quoted][string-literals] if they contain
whitespace or meta characters. However, as a special case, quotes can be omitted
if the expression has no operators nor function calls. For example:

* `jj diff 'Foo Bar'` (shell quotes are required, but inner quotes are optional)
* `jj diff '~"Foo Bar"'` (both shell and inner quotes are required)
* `jj diff '"Foo(1)"'` (both shell and inner quotes are required)

[string-literals]: templates.md#string-literals

## File patterns

The following patterns are supported:
Expand Down
10 changes: 6 additions & 4 deletions docs/revsets.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ A full change ID refers to all visible commits with that change ID (there is
typically only one visible commit with a given change ID). A unique prefix of
the full change ID can also be used. It is an error to use a non-unique prefix.

Use double quotes to prevent a symbol from being interpreted as an expression.
For example, `"x-"` is the symbol `x-`, not the parents of symbol `x`.
Taking shell quoting into account, you may need to use something like
`jj log -r '"x-"'`.
Use [single or double quotes][string-literals] to prevent a symbol from being
interpreted as an expression. For example, `"x-"` is the symbol `x-`, not the
parents of symbol `x`. Taking shell quoting into account, you may need to use
something like `jj log -r '"x-"'`.

[string-literals]: templates.md#string-literals

### Priority

Expand Down
21 changes: 16 additions & 5 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,22 @@ defined.

#### String literals

String literals must be surrounded by double quotes (`"`). The following escape
sequences starting with a backslash have their usual meaning: `\"`, `\\`, `\n`,
`\r`, `\t`, `\0`. Other escape sequences are not supported. Any UTF-8 characters
are allowed inside a string literal, with two exceptions: unescaped `"`-s and
uses of `\` that don't form a valid escape sequence.
String literals must be surrounded by single or double quotes (`'` or `"`).
A double-quoted string literal supports the following escape sequences:

* `\"`: double quote
* `\\`: backslash
* `\t`: horizontal tab
* `\r`: carriage return
* `\n`: new line
* `\0`: null

Other escape sequences are not supported. Any UTF-8 characters are allowed
inside a string literal, with two exceptions: unescaped `"`-s and uses of `\`
that don't form a valid escape sequence.

A single-quoted string literal has no escape syntax. `'` can't be expressed
inside a single-quoted string literal.

### Template type

Expand Down
10 changes: 9 additions & 1 deletion lib/src/fileset.pest
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ string_content_char = @{ !("\"" | "\\") ~ ANY }
string_content = @{ string_content_char+ }
string_literal = ${ "\"" ~ (string_content | string_escape)* ~ "\"" }

raw_string_content = @{ (!"'" ~ ANY)* }
raw_string_literal = ${ "'" ~ raw_string_content ~ "'" }

pattern_kind_op = { ":" }

negate_op = { "~" }
Expand All @@ -57,7 +60,11 @@ function_arguments = {
}

// TODO: change rhs to string_literal to require quoting? #2101
string_pattern = { strict_identifier ~ pattern_kind_op ~ (identifier | string_literal) }
string_pattern = {
strict_identifier
~ pattern_kind_op
~ (identifier | string_literal | raw_string_literal)
}
bare_string_pattern = { strict_identifier ~ pattern_kind_op ~ bare_string }

primary = {
Expand All @@ -66,6 +73,7 @@ primary = {
| string_pattern
| identifier
| string_literal
| raw_string_literal
}

expression = {
Expand Down
36 changes: 35 additions & 1 deletion lib/src/fileset_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ impl Rule {
Rule::string_content_char => None,
Rule::string_content => None,
Rule::string_literal => None,
Rule::raw_string_content => None,
Rule::raw_string_literal => None,
Rule::pattern_kind_op => Some(":"),
Rule::negate_op => Some("~"),
Rule::union_op => Some("|"),
Expand Down Expand Up @@ -233,6 +235,11 @@ fn parse_as_string_literal(pair: Pair<Rule>) -> String {
match pair.as_rule() {
Rule::identifier => pair.as_str().to_owned(),
Rule::string_literal => STRING_LITERAL_PARSER.parse(pair.into_inner()),
Rule::raw_string_literal => {
let (content,) = pair.into_inner().collect_tuple().unwrap();
assert_eq!(content.as_rule(), Rule::raw_string_content);
content.as_str().to_owned()
}
r => panic!("unexpected string literal rule: {r:?}"),
}
}
Expand All @@ -256,7 +263,9 @@ fn parse_primary_node(pair: Pair<Rule>) -> FilesetParseResult<ExpressionNode> {
ExpressionKind::StringPattern { kind, value }
}
Rule::identifier => ExpressionKind::Identifier(first.as_str()),
Rule::string_literal => ExpressionKind::String(parse_as_string_literal(first)),
Rule::string_literal | Rule::raw_string_literal => {
ExpressionKind::String(parse_as_string_literal(first))
}
r => panic!("unexpected primary rule: {r:?}"),
};
Ok(ExpressionNode::new(expr, span))
Expand Down Expand Up @@ -468,6 +477,24 @@ mod tests {
parse_into_kind(r#" "\y" "#),
Err(FilesetParseErrorKind::SyntaxError)
);

// Single-quoted raw string
assert_eq!(
parse_into_kind(r#" '' "#),
Ok(ExpressionKind::String("".to_owned())),
);
assert_eq!(
parse_into_kind(r#" 'a\n' "#),
Ok(ExpressionKind::String(r"a\n".to_owned())),
);
assert_eq!(
parse_into_kind(r#" '\' "#),
Ok(ExpressionKind::String(r"\".to_owned())),
);
assert_eq!(
parse_into_kind(r#" '"' "#),
Ok(ExpressionKind::String(r#"""#.to_owned())),
);
}

#[test]
Expand All @@ -493,6 +520,13 @@ mod tests {
value: "".to_owned()
})
);
assert_eq!(
parse_into_kind(r#" foo:'\' "#),
Ok(ExpressionKind::StringPattern {
kind: "foo",
value: r"\".to_owned()
})
);
assert_eq!(
parse_into_kind(r#" foo: "#),
Err(FilesetParseErrorKind::SyntaxError)
Expand Down
4 changes: 4 additions & 0 deletions lib/src/revset.pest
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ identifier = @{
symbol = {
identifier
| string_literal
| raw_string_literal
}

string_escape = @{ "\\" ~ ("t" | "r" | "n" | "0" | "\"" | "\\") }
string_content_char = @{ !("\"" | "\\") ~ ANY }
string_content = @{ string_content_char+ }
string_literal = ${ "\"" ~ (string_content | string_escape)* ~ "\"" }

raw_string_content = @{ (!"'" ~ ANY)* }
raw_string_literal = ${ "'" ~ raw_string_content ~ "'" }

at_op = { "@" }
pattern_kind_op = { ":" }

Expand Down
38 changes: 33 additions & 5 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ impl Rule {
Rule::string_content_char => None,
Rule::string_content => None,
Rule::string_literal => None,
Rule::raw_string_content => None,
Rule::raw_string_literal => None,
Rule::at_op => Some("@"),
Rule::pattern_kind_op => Some(":"),
Rule::parents_op => Some("-"),
Expand Down Expand Up @@ -1114,6 +1116,11 @@ fn parse_symbol_rule_as_literal(mut pairs: Pairs<Rule>) -> String {
match first.as_rule() {
Rule::identifier => first.as_str().to_owned(),
Rule::string_literal => STRING_LITERAL_PARSER.parse(first.into_inner()),
Rule::raw_string_literal => {
let (content,) = first.into_inner().collect_tuple().unwrap();
assert_eq!(content.as_rule(), Rule::raw_string_content);
content.as_str().to_owned()
}
_ => {
panic!("unexpected symbol parse rule: {:?}", first.as_str());
}
Expand Down Expand Up @@ -2839,6 +2846,13 @@ mod tests {
"foo bar".to_string()
))
);
assert_eq!(
parse(r#"'foo bar'@'bar baz'"#),
Ok(RevsetExpression::remote_symbol(
"foo bar".to_string(),
"bar baz".to_string()
))
);
// Quoted "@" is not interpreted as a working copy or remote symbol
assert_eq!(
parse(r#""@""#),
Expand Down Expand Up @@ -2895,6 +2909,7 @@ mod tests {
assert_eq!(parse("(foo)"), Ok(foo_symbol.clone()));
// Parse a quoted symbol
assert_eq!(parse("\"foo\""), Ok(foo_symbol.clone()));
assert_eq!(parse("'foo'"), Ok(foo_symbol.clone()));
// Parse the "parents" operator
assert_eq!(parse("foo-"), Ok(foo_symbol.parents()));
// Parse the "children" operator
Expand Down Expand Up @@ -3070,19 +3085,26 @@ mod tests {

#[test]
fn test_parse_string_literal() {
let branches_expr =
|s: &str| RevsetExpression::branches(StringPattern::Substring(s.to_owned()));

// "\<char>" escapes
assert_eq!(
parse(r#"branches("\t\r\n\"\\\0")"#),
Ok(RevsetExpression::branches(StringPattern::Substring(
"\t\r\n\"\\\0".to_owned()
)))
Ok(branches_expr("\t\r\n\"\\\0"))
);

// Invalid "\<char>" escape
assert_eq!(
parse(r#"branches("\y")"#),
Err(RevsetParseErrorKind::SyntaxError)
);

// Single-quoted raw string
assert_eq!(parse(r#"branches('')"#), Ok(branches_expr("")));
assert_eq!(parse(r#"branches('a\n')"#), Ok(branches_expr(r"a\n")));
assert_eq!(parse(r#"branches('\')"#), Ok(branches_expr(r"\")));
assert_eq!(parse(r#"branches('"')"#), Ok(branches_expr(r#"""#)));
}

#[test]
Expand Down Expand Up @@ -3117,6 +3139,12 @@ mod tests {
"foo".to_owned()
)))
);
assert_eq!(
parse(r#"branches(exact:'\')"#),
Ok(RevsetExpression::branches(StringPattern::Exact(
r"\".to_owned()
)))
);
assert_eq!(
parse(r#"branches(bad:"foo")"#),
Err(RevsetParseErrorKind::InvalidFunctionArguments {
Expand Down Expand Up @@ -3426,8 +3454,8 @@ mod tests {

// String literal should not be substituted with alias.
assert_eq!(
parse_with_aliases(r#"A|"A""#, [("A", "a")]).unwrap(),
parse("a|A").unwrap()
parse_with_aliases(r#"A|"A"|'A'"#, [("A", "a")]).unwrap(),
parse("a|A|A").unwrap()
);

// Alias can be substituted to string literal.
Expand Down

0 comments on commit 5b769c5

Please sign in to comment.