Skip to content

Commit 5617765

Browse files
Groovy: Added string interpolation without hook (#3366)
1 parent 1c533f4 commit 5617765

File tree

6 files changed

+213
-182
lines changed

6 files changed

+213
-182
lines changed

components/prism-groovy.js

+55-58
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,65 @@
1-
Prism.languages.groovy = Prism.languages.extend('clike', {
2-
'string': [
3-
{
1+
(function (Prism) {
2+
3+
var interpolation = {
4+
pattern: /((?:^|[^\\$])(?:\\{2})*)\$(?:\w+|\{[^{}]*\})/,
5+
lookbehind: true,
6+
inside: {
7+
'interpolation-punctuation': {
8+
pattern: /^\$\{?|\}$/,
9+
alias: 'punctuation'
10+
},
11+
'expression': {
12+
pattern: /[\s\S]+/,
13+
inside: null // see below
14+
}
15+
}
16+
};
17+
18+
Prism.languages.groovy = Prism.languages.extend('clike', {
19+
'string': {
420
// https://groovy-lang.org/syntax.html#_dollar_slashy_string
5-
pattern: /("""|''')(?:[^\\]|\\[\s\S])*?\1|\$\/(?:[^/$]|\$(?:[/$]|(?![/$]))|\/(?!\$))*\/\$/,
21+
pattern: /'''(?:[^\\]|\\[\s\S])*?'''|'(?:\\.|[^\\'\r\n])*'/,
622
greedy: true
723
},
8-
{
24+
'keyword': /\b(?:abstract|as|assert|boolean|break|byte|case|catch|char|class|const|continue|def|default|do|double|else|enum|extends|final|finally|float|for|goto|if|implements|import|in|instanceof|int|interface|long|native|new|package|private|protected|public|return|short|static|strictfp|super|switch|synchronized|this|throw|throws|trait|transient|try|void|volatile|while)\b/,
25+
'number': /\b(?:0b[01_]+|0x[\da-f_]+(?:\.[\da-f_p\-]+)?|[\d_]+(?:\.[\d_]+)?(?:e[+-]?\d+)?)[glidf]?\b/i,
26+
'operator': {
27+
pattern: /(^|[^.])(?:~|==?~?|\?[.:]?|\*(?:[.=]|\*=?)?|\.[@&]|\.\.<|\.\.(?!\.)|-[-=>]?|\+[+=]?|!=?|<(?:<=?|=>?)?|>(?:>>?=?|=)?|&[&=]?|\|[|=]?|\/=?|\^=?|%=?)/,
28+
lookbehind: true
29+
},
30+
'punctuation': /\.+|[{}[\];(),:$]/
31+
});
32+
33+
Prism.languages.insertBefore('groovy', 'string', {
34+
'shebang': {
35+
pattern: /#!.+/,
36+
alias: 'comment',
37+
greedy: true
38+
},
39+
'interpolation-string': {
940
// TODO: Slash strings (e.g. /foo/) can contain line breaks but this will cause a lot of trouble with
1041
// simple division (see JS regex), so find a fix maybe?
11-
pattern: /(["'/])(?:\\.|(?!\1)[^\\\r\n])*\1/,
12-
greedy: true
42+
pattern: /"""(?:[^\\]|\\[\s\S])*?"""|(["/])(?:\\.|(?!\1)[^\\\r\n])*\1|\$\/(?:[^/$]|\$(?:[/$]|(?![/$]))|\/(?!\$))*\/\$/,
43+
greedy: true,
44+
inside: {
45+
'interpolation': interpolation,
46+
'string': /[\s\S]+/
47+
}
1348
}
14-
],
15-
'keyword': /\b(?:abstract|as|assert|boolean|break|byte|case|catch|char|class|const|continue|def|default|do|double|else|enum|extends|final|finally|float|for|goto|if|implements|import|in|instanceof|int|interface|long|native|new|package|private|protected|public|return|short|static|strictfp|super|switch|synchronized|this|throw|throws|trait|transient|try|void|volatile|while)\b/,
16-
'number': /\b(?:0b[01_]+|0x[\da-f_]+(?:\.[\da-f_p\-]+)?|[\d_]+(?:\.[\d_]+)?(?:e[+-]?\d+)?)[glidf]?\b/i,
17-
'operator': {
18-
pattern: /(^|[^.])(?:~|==?~?|\?[.:]?|\*(?:[.=]|\*=?)?|\.[@&]|\.\.<|\.\.(?!\.)|-[-=>]?|\+[+=]?|!=?|<(?:<=?|=>?)?|>(?:>>?=?|=)?|&[&=]?|\|[|=]?|\/=?|\^=?|%=?)/,
19-
lookbehind: true
20-
},
21-
'punctuation': /\.+|[{}[\];(),:$]/
22-
});
23-
24-
Prism.languages.insertBefore('groovy', 'string', {
25-
'shebang': {
26-
pattern: /#!.+/,
27-
alias: 'comment'
28-
}
29-
});
30-
31-
Prism.languages.insertBefore('groovy', 'punctuation', {
32-
'spock-block': /\b(?:and|cleanup|expect|given|setup|then|when|where):/
33-
});
34-
35-
Prism.languages.insertBefore('groovy', 'function', {
36-
'annotation': {
37-
pattern: /(^|[^.])@\w+/,
38-
lookbehind: true,
39-
alias: 'punctuation'
40-
}
41-
});
49+
});
4250

43-
// Handle string interpolation
44-
Prism.hooks.add('wrap', function (env) {
45-
if (env.language === 'groovy' && env.type === 'string') {
46-
var delimiter = env.content[0];
51+
Prism.languages.insertBefore('groovy', 'punctuation', {
52+
'spock-block': /\b(?:and|cleanup|expect|given|setup|then|when|where):/
53+
});
4754

48-
if (delimiter != "'") {
49-
var pattern = /([^\\])(?:\$(?:\{.*?\}|[\w.]+))/;
50-
if (delimiter === '$') {
51-
pattern = /([^\$])(?:\$(?:\{.*?\}|[\w.]+))/;
52-
}
53-
54-
// To prevent double HTML-encoding we have to decode env.content first
55-
env.content = env.content.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
55+
Prism.languages.insertBefore('groovy', 'function', {
56+
'annotation': {
57+
pattern: /(^|[^.])@\w+/,
58+
lookbehind: true,
59+
alias: 'punctuation'
60+
}
61+
});
5662

57-
env.content = Prism.highlight(env.content, {
58-
'expression': {
59-
pattern: pattern,
60-
lookbehind: true,
61-
inside: Prism.languages.groovy
62-
}
63-
});
63+
interpolation.inside.expression.inside = Prism.languages.groovy;
6464

65-
env.classes.push(delimiter === '/' ? 'regex' : 'gstring');
66-
}
67-
}
68-
});
65+
}(Prism));

components/prism-groovy.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/groovy+sas/groovy_inclusion.test

+51-45
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,63 @@ quit;
1717

1818
[
1919
["step", "proc groovy"],
20-
["proc-args",
21-
[
22-
["arg", "classpath"],
23-
["operator", "="],
24-
["arg-value", "cp"],
25-
["punctuation", ";"]
26-
]
27-
],
28-
[
29-
"proc-groovy", [
30-
["comment", "/* Testing a comment */"],
31-
["submit-statement", "submit parseonly"],
32-
[
33-
"groovy", [
34-
["punctuation", ";"],
35-
["keyword", "class"],
36-
["class-name", ["Speaker"]],
37-
["punctuation", "{"],
38-
["keyword", "def"],
39-
["function", "say"],
40-
["punctuation", "("],
41-
" word ",
42-
["punctuation", ")"],
43-
["punctuation", "{"],
44-
"\r\n println ",
45-
["string", "\"----> \\\"${word}\\\"\""],
46-
["punctuation", "}"],
47-
["punctuation", "}"]
48-
]
49-
],
50-
["submit-statement", "endsubmit"],
51-
["punctuation", ";"]
52-
]
53-
],
20+
["proc-args", [
21+
["arg", "classpath"],
22+
["operator", "="],
23+
["arg-value", "cp"],
24+
["punctuation", ";"]
25+
]],
26+
["proc-groovy", [
27+
["comment", "/* Testing a comment */"],
28+
29+
["submit-statement", "submit parseonly"],
30+
["groovy", [
31+
["punctuation", ";"],
32+
33+
["keyword", "class"],
34+
["class-name", ["Speaker"]],
35+
["punctuation", "{"],
36+
37+
["keyword", "def"],
38+
["function", "say"],
39+
["punctuation", "("],
40+
" word ",
41+
["punctuation", ")"],
42+
["punctuation", "{"],
43+
44+
"\r\n println ",
45+
["interpolation-string", [
46+
["string", "\"----> \\\""],
47+
["interpolation", [
48+
["interpolation-punctuation", "${"],
49+
["expression", ["word"]],
50+
["interpolation-punctuation", "}"]
51+
]],
52+
["string", "\\\"\""]
53+
]],
54+
55+
["punctuation", "}"],
56+
57+
["punctuation", "}"]
58+
]],
59+
["submit-statement", "endsubmit"],
60+
["punctuation", ";"]
61+
]],
5462
["step", "quit"],
5563
["punctuation", ";"],
64+
5665
["step", "proc groovy"],
57-
["proc-args",
58-
[
59-
["arg", "classpath"],
60-
["operator", "="],
61-
["arg-value", "cp"],
62-
["punctuation", ";"]
63-
]
64-
],
65-
["proc-groovy",[
66+
["proc-args", [
67+
["arg", "classpath"],
68+
["operator", "="],
69+
["arg-value", "cp"],
70+
["punctuation", ";"]
71+
]],
72+
["proc-groovy", [
6673
["keyword", "eval"],
6774
["string", "\"s = new Speaker(); s.say( \"\"Hi\"\" )\""],
6875
["punctuation", ";"]
69-
]
70-
],
76+
]],
7177
["step", "quit"],
7278
["punctuation", ";"]
7379
]

tests/languages/groovy/issue1049.html.test

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
----------------------------------------------------
99

10-
<span class="token string gstring">"&amp;amp;"</span>
11-
<span class="token string gstring">"&amp;amp;&amp;amp;"</span>
12-
<span class="token string gstring">"&amp;lt;"</span>
13-
<span class="token string gstring">"&amp;lt;&amp;lt;"</span>
14-
<span class="token string gstring">"&amp;amp;lt;"</span>
15-
<span class="token string gstring">"&amp;gt;"</span>
10+
<span class="token interpolation-string"><span class="token string">"&amp;amp;"</span></span>
11+
<span class="token interpolation-string">
12+
<span class="token string">"&amp;amp;&amp;amp;"</span>
13+
</span>
14+
<span class="token interpolation-string"><span class="token string">"&amp;lt;"</span></span>
15+
<span class="token interpolation-string"><span class="token string">"&amp;lt;&amp;lt;"</span></span>
16+
<span class="token interpolation-string"><span class="token string">"&amp;amp;lt;"</span></span>
17+
<span class="token interpolation-string"><span class="token string">"&amp;gt;"</span></span>

0 commit comments

Comments
 (0)