1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-17 20:58:08 +02:00

Rust: Add initial support for string interpolation

This commit is contained in:
Lovecraftian Horror 2023-01-30 17:35:03 -07:00 committed by Alec Thomas
parent f5f48e206a
commit 4bfa1bbb7c
4 changed files with 186 additions and 1 deletions

View File

@ -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"/>
@ -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"/>

View File

@ -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"},

View File

@ -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}");
}

View File

@ -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"}
]