prism-groovy.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. Prism.languages.groovy = Prism.languages.extend('clike', {
  2. 'string': [
  3. {
  4. // https://groovy-lang.org/syntax.html#_dollar_slashy_string
  5. pattern: /("""|''')(?:[^\\]|\\[\s\S])*?\1|\$\/(?:[^/$]|\$(?:[/$]|(?![/$]))|\/(?!\$))*\/\$/,
  6. greedy: true
  7. },
  8. {
  9. // TODO: Slash strings (e.g. /foo/) can contain line breaks but this will cause a lot of trouble with
  10. // simple division (see JS regex), so find a fix maybe?
  11. pattern: /(["'/])(?:\\.|(?!\1)[^\\\r\n])*\1/,
  12. greedy: true
  13. }
  14. ],
  15. '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/,
  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. Prism.languages.insertBefore('groovy', 'string', {
  24. 'shebang': {
  25. pattern: /#!.+/,
  26. alias: 'comment'
  27. }
  28. });
  29. Prism.languages.insertBefore('groovy', 'punctuation', {
  30. 'spock-block': /\b(?:setup|given|when|then|and|cleanup|expect|where):/
  31. });
  32. Prism.languages.insertBefore('groovy', 'function', {
  33. 'annotation': {
  34. pattern: /(^|[^.])@\w+/,
  35. lookbehind: true,
  36. alias: 'punctuation'
  37. }
  38. });
  39. // Handle string interpolation
  40. Prism.hooks.add('wrap', function(env) {
  41. if (env.language === 'groovy' && env.type === 'string') {
  42. var delimiter = env.content[0];
  43. if (delimiter != "'") {
  44. var pattern = /([^\\])(?:\$(?:\{.*?\}|[\w.]+))/;
  45. if (delimiter === '$') {
  46. pattern = /([^\$])(?:\$(?:\{.*?\}|[\w.]+))/;
  47. }
  48. // To prevent double HTML-encoding we have to decode env.content first
  49. env.content = env.content.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
  50. env.content = Prism.highlight(env.content, {
  51. 'expression': {
  52. pattern: pattern,
  53. lookbehind: true,
  54. inside: Prism.languages.groovy
  55. }
  56. });
  57. env.classes.push(delimiter === '/' ? 'regex' : 'gstring');
  58. }
  59. }
  60. });