Groovy fix : added support for shebang, fixed single-line strings and updated example page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
diff --git a/components/prism-groovy.js b/components/prism-groovy.js
index ba484c7..b8112e2 100644
--- a/components/prism-groovy.js
+++ b/components/prism-groovy.js
@@ -1,6 +1,6 @@
Prism.languages.groovy = Prism.languages.extend('clike', {
'keyword': /\b(as|def|in|abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|extends|final|finally|float|for|goto|if|implements|import|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/g,
- 'string': /("""|''')[\W\w]*?\1|("|'|\/)[\W\w]*?\2|(\$\/)(\$\/\$|[\W\w])*?\/\$/g,
+ 'string': /("""|''')[\W\w]*?\1|("|'|\/)(?:\\?.)*?\2|(\$\/)(\$\/\$|[\W\w])*?\/\$/g,
'number': /\b0b[01_]+\b|\b0x[\da-f_]+(\.[\da-f_p\-]+)?\b|\b[\d_]+(\.[\d_]+[e]?[\d]*)?[glidf]\b|[\d_]+(\.[\d_]+)?\b/gi,
'operator': {
pattern: /(^|[^.])(={0,2}~|\?\.|\*?\.@|\.&|\.{1,2}(?!\.)|\.{2}<?(?=\w)|->|\?:|[-+]{1,2}|!|<=>|>{1,3}|<{1,2}|={1,2}|&{1,2}|\|{1,2}|\?|\*{1,2}|\/|\^|%)/g,
@@ -9,6 +9,13 @@ Prism.languages.groovy = Prism.languages.extend('clike', {
'punctuation': /\.+|[{}[\];(),:$]/g
});
+Prism.languages.insertBefore('groovy', 'string', {
+ 'shebang': {
+ pattern: /#!.+/,
+ alias: 'comment'
+ }
+});
+
Prism.languages.insertBefore('groovy', 'punctuation', {
'spock-block': /\b(setup|given|when|then|and|cleanup|expect|where):/g
});
diff --git a/components/prism-groovy.min.js b/components/prism-groovy.min.js
index bc74e97..cfe0b58 100644
--- a/components/prism-groovy.min.js
+++ b/components/prism-groovy.min.js
@@ -1 +1 @@
-Prism.languages.groovy=Prism.languages.extend("clike",{keyword:/\b(as|def|in|abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|extends|final|finally|float|for|goto|if|implements|import|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/g,string:/("""|''')[\W\w]*?\1|("|'|\/)[\W\w]*?\2|(\$\/)(\$\/\$|[\W\w])*?\/\$/g,number:/\b0b[01_]+\b|\b0x[\da-f_]+(\.[\da-f_p\-]+)?\b|\b[\d_]+(\.[\d_]+[e]?[\d]*)?[glidf]\b|[\d_]+(\.[\d_]+)?\b/gi,operator:{pattern:/(^|[^.])(={0,2}~|\?\.|\*?\.@|\.&|\.{1,2}(?!\.)|\.{2}<?(?=\w)|->|\?:|[-+]{1,2}|!|<=>|>{1,3}|<{1,2}|={1,2}|&{1,2}|\|{1,2}|\?|\*{1,2}|\/|\^|%)/g,lookbehind:!0},punctuation:/\.+|[{}[\];(),:$]/g}),Prism.languages.insertBefore("groovy","punctuation",{"spock-block":/\b(setup|given|when|then|and|cleanup|expect|where):/g}),Prism.languages.insertBefore("groovy","function",{annotation:{pattern:/(^|[^.])@\w+/,lookbehind:!0}}),Prism.hooks.add("wrap",function(e){if("groovy"===e.language&&"string"===e.type){var t=e.content[0];if("'"!=t){var n=/([^\\])(\$(\{.*?\}|[\w\.]+))/;"$"===t&&(n=/([^\$])(\$(\{.*?\}|[\w\.]+))/),e.content=Prism.highlight(e.content,{expression:{pattern:n,lookbehind:!0,inside:Prism.languages.groovy}}),e.classes.push("/"===t?"regex":"gstring")}}});
\ No newline at end of file
+Prism.languages.groovy=Prism.languages.extend("clike",{keyword:/\b(as|def|in|abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|extends|final|finally|float|for|goto|if|implements|import|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/g,string:/("""|''')[\W\w]*?\1|("|'|\/)(?:\\?.)*?\2|(\$\/)(\$\/\$|[\W\w])*?\/\$/g,number:/\b0b[01_]+\b|\b0x[\da-f_]+(\.[\da-f_p\-]+)?\b|\b[\d_]+(\.[\d_]+[e]?[\d]*)?[glidf]\b|[\d_]+(\.[\d_]+)?\b/gi,operator:{pattern:/(^|[^.])(={0,2}~|\?\.|\*?\.@|\.&|\.{1,2}(?!\.)|\.{2}<?(?=\w)|->|\?:|[-+]{1,2}|!|<=>|>{1,3}|<{1,2}|={1,2}|&{1,2}|\|{1,2}|\?|\*{1,2}|\/|\^|%)/g,lookbehind:!0},punctuation:/\.+|[{}[\];(),:$]/g}),Prism.languages.insertBefore("groovy","string",{shebang:{pattern:/#!.+/,alias:"comment"}}),Prism.languages.insertBefore("groovy","punctuation",{"spock-block":/\b(setup|given|when|then|and|cleanup|expect|where):/g}),Prism.languages.insertBefore("groovy","function",{annotation:{pattern:/(^|[^.])@\w+/,lookbehind:!0}}),Prism.hooks.add("wrap",function(e){if("groovy"===e.language&&"string"===e.type){var t=e.content[0];if("'"!=t){var n=/([^\\])(\$(\{.*?\}|[\w\.]+))/;"$"===t&&(n=/([^\$])(\$(\{.*?\}|[\w\.]+))/),e.content=Prism.highlight(e.content,{expression:{pattern:n,lookbehind:!0,inside:Prism.languages.groovy}}),e.classes.push("/"===t?"regex":"gstring")}}});
\ No newline at end of file
diff --git a/examples/prism-groovy.html b/examples/prism-groovy.html
index d4f0f8f..596e74f 100644
--- a/examples/prism-groovy.html
+++ b/examples/prism-groovy.html
@@ -28,59 +28,62 @@ $/.*"(.*)".*/(.*)/$</code></pre>
'No interpolation here : ${21*2}'</code></pre>
<h2>Full example</h2>
-<pre><code>/*
- The input string to parse is from...
- <...the rest of the header comment from above...>
-*/
-boolean debugging = true
-
-if (debugging) {
- // Test data
- dfkOutput = '''
-Filesystem 1K-blocks Used Available Use% Mounted on
-/dev/sda1 4185956 1206996 2762888 31% /
-/dev/sda11 30679784 28324040 772140 98% /extra
-fas3050c-1-2.b:/vol/canis
- 10485760 6599936 3885824 63% /nfs/data_d2/dog_data
-fas6070-1-1.b:/vol/felis
- 314572800 54889600 259683200 18% /nfs/DATA-1/cat_data
-'''
-} else {
- // Real data
- dfkOutput = 'df -k'.execute().text
-}
-
-long kbAvail = 0
-
-regex = '''(?ix) # enable case-insensitive matches, extended patterns
- (\\d+) # 1: The disk space we want
- \\s+ # some whitespace
- \\d+% # a number followed by??%
- \\s+ # some more whitespace
- (/nfs/data.*) # 2: partition name'''
+<pre><code>#!/usr/bin/env groovy
+package model
-pattern = ~regex
-matcher = pattern.matcher(dfkOutput)
+import groovy.transform.CompileStatic
+import java.util.List as MyList
-if (debugging) {
- println """matcher pattern:
-/---------------------------------\\
-${matcher.pattern()}
-\\---------------------------------/"""
-
- println "match count=${matcher.getCount()}"
+trait Distributable {
+ void distribute(String version) {}
}
-for (i=0; i < matcher.getCount(); i++) {
- if (debugging) {
- println " text matched in matcher[${i}]: '" + matcher[i][0] + "'"
- println " free space in (group 1): '" + matcher[i][1] + "'"
- println " partition name (group 2): '" + matcher[i][2] + "'"
+@CompileStatic
+class Distribution implements Distributable {
+ double number = 1234.234 / 567
+ def otherNumber = 3 / 4
+ boolean archivable = condition ?: true
+ def ternary = a ? b : c
+ String name = "Guillaume"
+ Closure description = null
+ List<DownloadPackage> packages = []
+ String regex = ~/.*foo.*/
+ String multi = '''
+ multi line string
+ ''' + """
+ now with double quotes and ${gstring}
+ """ + $/
+ even with dollar slashy strings
+ /$
+
+ /**
+ * description method
+ * @param cl the closure
+ */
+ void description(Closure cl) { this.description = cl }
+
+ void version(String name, Closure versionSpec) {
+ def closure = { println "hi" } as Runnable
+
+ MyList ml = [1, 2, [a: 1, b:2,c :3]]
+ for (ch in "name") {}
+
+ // single line comment
+ DownloadPackage pkg = new DownloadPackage(version: name)
+
+ check that: true
+
+ label:
+ def clone = versionSpec.rehydrate(pkg, pkg, pkg)
+ /*
+ now clone() in a multiline comment
+ */
+ clone()
+ packages.add(pkg)
+
+ assert 4 / 2 == 2
}
- kbAvail += matcher[i][1].toLong()
-}
-
-println "KB available=${kbAvail}"</code></pre>
+}</code></pre>
<h2>Known failures</h2>
<p>There are certain edge cases where Prism will fail.
@@ -92,5 +95,5 @@ println "KB available=${kbAvail}"</code></pre>
<h3>Comment-like substrings</h3>
<pre><code>"foo /* bar */ baz"; "foo // bar";</code></pre>
-<h3>Slashy strings containing (escaped) slashes</h3>
-<pre><code>/foo\/bar/</code></pre>
\ No newline at end of file
+<h3>Two divisions on the same line</h3>
+<pre><code>2 / 3 / 4</code></pre>
\ No newline at end of file