Skip to content

Commit

Permalink
Rust: Add initial support for string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev authored and alecthomas committed Jan 31, 2023
1 parent f5f48e2 commit 4bfa1bb
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lexers/embedded/rust.xml
Expand Up @@ -105,6 +105,24 @@
<pop depth="1"/>
</rule>
</state>
<state name="formatted_string">
<rule pattern="&#34;">
<token type="LiteralString"/>
<pop depth="1"/>
</rule>
<rule pattern="\\[&#39;&#34;\\nrt]|\\(?=\n)|\\x[0-7][0-9a-fA-F]|\\0|\\u\{[0-9a-fA-F]{1,6}\}|\{\{|\}\}">
<token type="LiteralStringEscape"/>
</rule>
<rule pattern="\{[^}]*\}">
<token type="LiteralStringInterpol"/>
</rule>
<rule pattern="[^\\&#34;\{\}]+">
<token type="LiteralString"/>
</rule>
<rule pattern="\\">
<token type="LiteralString"/>
</rule>
</state>
<state name="string">
<rule pattern="&#34;">
<token type="LiteralString"/>
Expand Down Expand Up @@ -294,6 +312,16 @@
<rule pattern="\b(r#)?_?([A-Z][A-Z0-9_]*){2,}\b">
<token type="NameConstant"/>
</rule>
<rule pattern="((?:e?print(?:ln)?|format(?:_args)?|panic|todo|un(?:reachable|implemented))!)(\s*)(\()(\s*)(&#34;)">
<bygroups>
<token type="NameFunctionMagic"/>
<token type="TextWhitespace"/>
<token type="Punctuation"/>
<token type="TextWhitespace"/>
<token type="LiteralString"/>
</bygroups>
<push state="formatted_string"/>
</rule>
<rule pattern="([a-zA-Z_]\w*!)(\s*)(\(|\[|\{)">
<bygroups>
<token type="NameFunctionMagic"/>
Expand Down
4 changes: 3 additions & 1 deletion lexers/testdata/rust/rust.expected
Expand Up @@ -60,7 +60,9 @@
{"type":"TextWhitespace","value":" "},
{"type":"NameFunctionMagic","value":"println!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\"rect1 is {:?}\""},
{"type":"LiteralString","value":"\"rect1 is "},
{"type":"LiteralStringInterpol","value":"{:?}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"rect1"},
Expand Down
29 changes: 29 additions & 0 deletions lexers/testdata/rust/string_interpol.actual
@@ -0,0 +1,29 @@
fn main() {
let foo = 'x';

println!(
"
Kitchen sink
{{ }} - Escaped
{{{}}} - Escaped with inner interpol
{foo:#?} - Ident with pretty debug format
{0:-<5} - Positional with complex fill/alignment
",
foo,
);

// Unconventional formatting
println! ( "Hello, {foo}!");

// Exhausting the supported macros
eprintln!("{foo}");
eprint!("{foo}");
println!("{foo}");
print!("{foo}");
format!("{foo}");
format_args!("{foo}");
panic!("{foo}");
todo!("{foo}");
unreachable!("{foo}");
unimplemented!("{foo}");
}
126 changes: 126 additions & 0 deletions lexers/testdata/rust/string_interpol.expected
@@ -0,0 +1,126 @@
[
{"type":"Keyword","value":"fn"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"main"},
{"type":"Punctuation","value":"()"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"KeywordDeclaration","value":"let"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"foo"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralStringChar","value":"'x'"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"NameFunctionMagic","value":"println!"},
{"type":"Punctuation","value":"("},
{"type":"TextWhitespace","value":"\n "},
{"type":"LiteralString","value":"\"\n Kitchen sink\n "},
{"type":"LiteralStringEscape","value":"{{"},
{"type":"LiteralString","value":" "},
{"type":"LiteralStringEscape","value":"}}"},
{"type":"LiteralString","value":" - Escaped\n "},
{"type":"LiteralStringEscape","value":"{{"},
{"type":"LiteralStringInterpol","value":"{}"},
{"type":"LiteralStringEscape","value":"}}"},
{"type":"LiteralString","value":" - Escaped with inner interpol\n "},
{"type":"LiteralStringInterpol","value":"{foo:#?}"},
{"type":"LiteralString","value":" - Ident with pretty debug format\n "},
{"type":"LiteralStringInterpol","value":"{0:-\u003c5}"},
{"type":"LiteralString","value":" - Positional with complex fill/alignment\n \""},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"foo"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"CommentSingle","value":"// Unconventional formatting\n"},
{"type":"TextWhitespace","value":" "},
{"type":"NameFunctionMagic","value":"println!"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Punctuation","value":"("},
{"type":"TextWhitespace","value":"\t"},
{"type":"LiteralString","value":"\"Hello, "},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"!\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"CommentSingle","value":"// Exhausting the supported macros\n"},
{"type":"TextWhitespace","value":" "},
{"type":"NameFunctionMagic","value":"eprintln!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\""},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameFunctionMagic","value":"eprint!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\""},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameFunctionMagic","value":"println!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\""},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameFunctionMagic","value":"print!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\""},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameFunctionMagic","value":"format!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\""},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameFunctionMagic","value":"format_args!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\""},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameFunctionMagic","value":"panic!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\""},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameFunctionMagic","value":"todo!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\""},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameFunctionMagic","value":"unreachable!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\""},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameFunctionMagic","value":"unimplemented!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\""},
{"type":"LiteralStringInterpol","value":"{foo}"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n"}
]

0 comments on commit 4bfa1bb

Please sign in to comment.