Skip to content
Closed
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
13 changes: 10 additions & 3 deletions corpus/declarations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ fn accumulate(self) -> Machine<{State::Accumulate}> {}
(macro_invocation
macro: (identifier)
(token_tree
(string_literal)
(string_literal
(interpolation))
(identifier))))))
(function_item
name: (identifier)
Expand Down Expand Up @@ -1209,7 +1210,9 @@ pub enum Error {
(attribute
(identifier)
(token_tree
(string_literal)
(string_literal
(interpolation
(format_specifier)))
(identifier)
(token_tree
(integer_literal)))))
Expand All @@ -1221,7 +1224,11 @@ pub enum Error {
(attribute
(identifier)
(token_tree
(string_literal)
(string_literal
(interpolation
(identifier))
(interpolation)
(interpolation))
(identifier)
(identifier)
(identifier)
Expand Down
132 changes: 132 additions & 0 deletions corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,135 @@ false;
(boolean_literal))
(expression_statement
(boolean_literal)))

================================================================================
Format strings
================================================================================

"{}";
"{a}";
"a {a}";
"a {{}}";
"a {b}}}";
"a {{{b}";
"a {{b}}";
"a {{{b}}}";
"a {aaaa}";

--------------------------------------------------------------------------------

(source_file
(expression_statement
(string_literal
(interpolation)))
(expression_statement
(string_literal
(interpolation
(identifier))))
(expression_statement
(string_literal
(interpolation
(identifier))))
(expression_statement
(string_literal))
(expression_statement
(string_literal
(interpolation
(identifier))))
(expression_statement
(string_literal
(interpolation
(identifier))))
(expression_statement
(string_literal))
(expression_statement
(string_literal
(interpolation
(identifier))))
(expression_statement
(string_literal
(interpolation
(identifier)))))

================================================================================
Format strings with format specifiers
================================================================================
"{:}";
"{:?}";
"{:#?}";
"{a:?}";
"{a:#?}";
"{0:#?}";
"{1} {0}";
"{:b}";
"{a:b}";
"{a:#b}";
"{a:#032b}";
"{a:10.3b}";
"{a:e}";

--------------------------------------------------------------------------------

(source_file
(expression_statement
(string_literal
(interpolation
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(identifier)
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(identifier)
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(integer_literal)
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(integer_literal))
(interpolation
(integer_literal))))
(expression_statement
(string_literal
(interpolation
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(identifier)
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(identifier)
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(identifier)
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(identifier)
(format_specifier))))
(expression_statement
(string_literal
(interpolation
(identifier)
(format_specifier)))))
18 changes: 13 additions & 5 deletions corpus/macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,21 @@ a!($a $a:ident $($a);*);
(expression_statement
(macro_invocation
(identifier)
(token_tree (token_tree))))
(token_tree
(token_tree))))
(expression_statement
(macro_invocation
(identifier)
(token_tree (identifier))))
(token_tree
(identifier))))
(expression_statement
(macro_invocation
(identifier)
(token_tree (token_tree (token_tree (token_tree (identifier)))))))
(token_tree
(token_tree
(token_tree
(token_tree
(identifier)))))))
(expression_statement
(macro_invocation
(identifier)
Expand Down Expand Up @@ -213,7 +219,8 @@ macro_rules! zero_or_one {
right: (token_tree
(identifier)
(token_tree
(string_literal)
(string_literal
(interpolation))
(metavariable))))
(macro_rule
left: (token_tree_pattern
Expand All @@ -224,7 +231,8 @@ macro_rules! zero_or_one {
right: (token_tree
(identifier)
(token_tree
(string_literal)
(string_literal
(interpolation))
(metavariable)))))
(macro_definition
name: (identifier)
Expand Down
21 changes: 19 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1406,8 +1406,10 @@ module.exports = grammar({
string_literal: $ => seq(
alias(/b?"/, '"'),
repeat(choice(
$.interpolation,
$._escape_interpolation,
$.escape_sequence,
$._string_content
$._string_content,
)),
token.immediate('"')
),
Expand Down Expand Up @@ -1473,7 +1475,22 @@ module.exports = grammar({
super: $ => 'super',
crate: $ => 'crate',

metavariable: $ => /\$[a-zA-Z_]\w*/
metavariable: $ => /\$[a-zA-Z_]\w*/,

interpolation:
$ => seq(
'{',
optional(choice($.integer_literal, $.identifier)),
optional($.format_specifier),
'}'
),

_escape_interpolation: _ => choice('{{', '}}'),

format_specifier: _ => seq(
':',
optional(repeat(token(/[^{}\n]+/)))
),
}
})

Expand Down
4 changes: 4 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,7 @@
"*" @operator
"&" @operator
"'" @operator

(interpolation
"{" @punctuation.special
"}" @punctuation.special) @embedded
94 changes: 94 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -8112,6 +8112,14 @@
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "interpolation"
},
{
"type": "SYMBOL",
"name": "_escape_interpolation"
},
{
"type": "SYMBOL",
"name": "escape_sequence"
Expand Down Expand Up @@ -8448,6 +8456,92 @@
"metavariable": {
"type": "PATTERN",
"value": "\\$[a-zA-Z_]\\w*"
},
"interpolation": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "integer_literal"
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "format_specifier"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "}"
}
]
},
"_escape_interpolation": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "{{"
},
{
"type": "STRING",
"value": "}}"
}
]
},
"format_specifier": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ":"
},
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT",
"content": {
"type": "TOKEN",
"content": {
"type": "PATTERN",
"value": "[^{}\\n]+"
}
}
},
{
"type": "BLANK"
}
]
}
]
}
},
"extras": [
Expand Down
Loading