Commit 72cde69424eed49e16f222891bad8840c06e6e7e

Golmote 2015-01-06T20:22:24

Groovy fix : added support for shebang, fixed single-line strings and updated example page

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&lt;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