Skip to content

Commit c646846

Browse files
committed
Fix string interpolation def
1 parent 3d8947c commit c646846

File tree

3 files changed

+84
-70
lines changed

3 files changed

+84
-70
lines changed

components/prism-v.js

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,79 @@
1-
Prism.languages.v = Prism.languages.extend('clike', {
2-
'string': [
3-
{
4-
pattern: /`(?:\\\`|\\?[^\`]{1,2})`/, // using {1,2} instead of `u` flag for compatibility
5-
alias: 'rune'
6-
},
7-
{
8-
pattern: /r?(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
9-
alias: 'quoted-string',
10-
greedy: true,
11-
inside: {
12-
'interpolation': {
13-
pattern: /((?:^|[^\\])(?:\\{2})*)\$(?:\{[^{}]*\}|\w+(?:\.\w+(?:\([^\(\)]*\))?|\[[^\[\]]+\])*)/,
14-
lookbehind: true,
15-
inside: {
16-
'interpolation-variable': {
17-
pattern: /^\$\w[\s\S]*$/,
18-
alias: 'variable'
19-
},
20-
'interpolation-punctuation': {
21-
pattern: /^\${|}$/,
22-
alias: 'punctuation'
23-
},
24-
'interpolation-expression': {
25-
pattern: /[\s\S]+/,
26-
inside: Prism.languages.v
1+
(function(Prism) {
2+
var interpolationExpr = {
3+
pattern: /[\s\S]+/,
4+
inside: null
5+
};
6+
7+
Prism.languages.v = Prism.languages.extend('clike', {
8+
'string': [
9+
{
10+
pattern: /`(?:\\\`|\\?[^\`]{1,2})`/, // using {1,2} instead of `u` flag for compatibility
11+
alias: 'rune'
12+
},
13+
{
14+
pattern: /r?(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
15+
alias: 'quoted-string',
16+
greedy: true,
17+
inside: {
18+
'interpolation': {
19+
pattern: /((?:^|[^\\])(?:\\{2})*)\$(?:\{[^{}]*\}|\w+(?:\.\w+(?:\([^\(\)]*\))?|\[[^\[\]]+\])*)/,
20+
lookbehind: true,
21+
inside: {
22+
'interpolation-variable': {
23+
pattern: /^\$\w[\s\S]*$/,
24+
alias: 'variable'
25+
},
26+
'interpolation-punctuation': {
27+
pattern: /^\${|}$/,
28+
alias: 'punctuation'
29+
},
30+
'interpolation-expression': interpolationExpr
2731
}
2832
}
2933
}
3034
}
31-
}
32-
],
33-
'class-name': {
34-
pattern: /(\b(?:enum|interface|struct|type)\s+)\w+/,
35-
lookbehind: true
36-
},
37-
'keyword': /(?:\b(?:as|asm|assert|atomic|break|chan|const|continue|defer|else|embed|enum|fn|for|__global|go(?:to)?|if|import|in|interface|is|lock|match|module|mut|none|or|pub|return|rlock|select|shared|sizeof|static|struct|type(?:of)?|union|unsafe)|\$(?:if|else|for)|#(?:include|flag))\b/,
38-
'number': /\b(?:0x[a-f\d]+(?:_[a-f\d]+)*|0b[01]+(?:_[01]+)*|0o[0-7]+(?:_[0-7]+)*|\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?)\b/i,
39-
'operator': /~|\?|[*\/%^!=]=?|\+[=+]?|-[=-]?|\|[=|]?|&(?:=|&|\^=?)?|>(?:>=?|=)?|<(?:<=?|=|-)?|:=|\.\.\.?/,
40-
'builtin': /\b(?:any(?:_int|_float)?|bool|byte(?:ptr)?|charptr|f(?:32|64)|i(?:8|16|nt|64|128)|rune|size_t|string|u(?:16|32|64|128)|voidptr)\b/
41-
});
35+
],
36+
'class-name': {
37+
pattern: /(\b(?:enum|interface|struct|type)\s+)(?:C\.)?[\w]+/,
38+
lookbehind: true
39+
},
40+
'keyword': /(?:\b(?:as|asm|assert|atomic|break|chan|const|continue|defer|else|embed|enum|fn|for|__global|go(?:to)?|if|import|in|interface|is|lock|match|module|mut|none|or|pub|return|rlock|select|shared|sizeof|static|struct|type(?:of)?|union|unsafe)|\$(?:if|else|for)|#(?:include|flag))\b/,
41+
'number': /\b(?:0x[a-f\d]+(?:_[a-f\d]+)*|0b[01]+(?:_[01]+)*|0o[0-7]+(?:_[0-7]+)*|\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?)\b/i,
42+
'operator': /~|\?|[*\/%^!=]=?|\+[=+]?|-[=-]?|\|[=|]?|&(?:=|&|\^=?)?|>(?:>=?|=)?|<(?:<=?|=|-)?|:=|\.\.\.?/,
43+
'builtin': /\b(?:any(?:_int|_float)?|bool|byte(?:ptr)?|charptr|f(?:32|64)|i(?:8|16|nt|64|128)|rune|size_t|string|u(?:16|32|64|128)|voidptr)\b/
44+
});
4245

43-
Prism.languages.insertBefore('v', 'operator', {
44-
'attribute': {
45-
pattern: /^\s*\[(?:deprecated|unsafe_fn|typedef|live|inline|flag|ref_only|windows_stdcall|direct_array_access)\]/m,
46-
alias: 'annotation',
47-
inside: {
48-
'punctuation': /[\[\]]/,
49-
'keyword': /\w+/
50-
}
51-
},
52-
'generic': {
53-
pattern: /\<\w+\>(?=\s*[\)\{])/,
54-
inside: {
55-
'punctuation': /[<>]/,
56-
'class-name': /\w+/
46+
interpolationExpr.inside = Prism.languages.v;
47+
48+
Prism.languages.insertBefore('v', 'operator', {
49+
'attribute': {
50+
pattern: /^\s*\[(?:deprecated|unsafe_fn|typedef|live|inline|flag|ref_only|windows_stdcall|direct_array_access)\]/m,
51+
alias: 'annotation',
52+
inside: {
53+
'punctuation': /[\[\]]/,
54+
'keyword': /\w+/
55+
}
56+
},
57+
'generic': {
58+
pattern: /\<\w+\>(?=\s*[\)\{])/,
59+
inside: {
60+
'punctuation': /[<>]/,
61+
'class-name': /\w+/
62+
}
5763
}
58-
}
59-
});
60-
61-
Prism.languages.insertBefore('v', 'function', {
62-
'generic-function': {
63-
// e.g. foo<T>( ...
64-
pattern: /\w+\s*<\w+>(?=\()/,
65-
inside: {
66-
'function': /^\w+/,
67-
'generic': {
68-
pattern: /<\w+>/,
69-
inside: Prism.languages.v.generic.inside
64+
});
65+
66+
Prism.languages.insertBefore('v', 'function', {
67+
'generic-function': {
68+
// e.g. foo<T>( ...
69+
pattern: /\w+\s*<\w+>(?=\()/,
70+
inside: {
71+
'function': /^\w+/,
72+
'generic': {
73+
pattern: /<\w+>/,
74+
inside: Prism.languages.v.generic.inside
75+
}
7076
}
7177
}
72-
}
73-
});
78+
});
79+
})(Prism);

components/prism-v.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/v/string_feature.test

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"https://example.com"
22
'single quote'
33
'age = $user.age'
4-
'x = ${x:4.2f}'
4+
'[${int(x):-10}]'
55
r'hello\nworld'
66
`🚀`
77
`\``
@@ -20,13 +20,21 @@ r'hello\nworld'
2020
"'"
2121
]],
2222
["string", [
23-
"'x = ",
23+
"'[",
2424
["interpolation", [
2525
["interpolation-punctuation", "${"],
26-
["interpolation-expression", "x:4.2f"],
26+
["interpolation-expression", [
27+
["function", "int"],
28+
["punctuation", "("],
29+
"x",
30+
["punctuation", ")"],
31+
["punctuation", ":"],
32+
["operator", "-"],
33+
["number", "10"]
34+
]],
2735
["interpolation-punctuation", "}"]
2836
]],
29-
"'"
37+
"]'"
3038
]],
3139
["string", ["r'hello\\nworld'"]],
3240
["string", "`🚀`"],

0 commit comments

Comments
 (0)