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

fileset, revset, templater: add support for single-quoted raw string literals #3568

Merged
merged 3 commits into from
Apr 25, 2024
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
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
51 changes: 43 additions & 8 deletions 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 @@ -229,6 +231,19 @@ fn parse_function_call_node(pair: Pair<Rule>) -> FilesetParseResult<FunctionCall
})
}

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:?}"),
}
}

fn parse_primary_node(pair: Pair<Rule>) -> FilesetParseResult<ExpressionNode> {
assert_eq!(pair.as_rule(), Rule::primary);
let first = pair.into_inner().next().unwrap();
Expand All @@ -244,17 +259,12 @@ fn parse_primary_node(pair: Pair<Rule>) -> FilesetParseResult<ExpressionNode> {
assert_eq!(lhs.as_rule(), Rule::strict_identifier);
assert_eq!(op.as_rule(), Rule::pattern_kind_op);
let kind = lhs.as_str();
let value = match rhs.as_rule() {
Rule::identifier => rhs.as_str().to_owned(),
Rule::string_literal => STRING_LITERAL_PARSER.parse(rhs.into_inner()),
r => panic!("unexpected string pattern rule: {r:?}"),
};
let value = parse_as_string_literal(rhs);
ExpressionKind::StringPattern { kind, value }
}
Rule::identifier => ExpressionKind::Identifier(first.as_str()),
Rule::string_literal => {
let text = STRING_LITERAL_PARSER.parse(first.into_inner());
ExpressionKind::String(text)
Rule::string_literal | Rule::raw_string_literal => {
ExpressionKind::String(parse_as_string_literal(first))
}
r => panic!("unexpected primary rule: {r:?}"),
};
Expand Down Expand Up @@ -467,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 @@ -492,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
Loading