groovy.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Language: Groovy
  3. Author: Guillaume Laforge <glaforge@gmail.com>
  4. Description: Groovy programming language implementation inspired from Vsevolod's Java mode
  5. Website: https://groovy-lang.org
  6. Category: system
  7. */
  8. function variants(variants, obj = {}) {
  9. obj.variants = variants;
  10. return obj;
  11. }
  12. function groovy(hljs) {
  13. const regex = hljs.regex;
  14. const IDENT_RE = '[A-Za-z0-9_$]+';
  15. const COMMENT = variants([
  16. hljs.C_LINE_COMMENT_MODE,
  17. hljs.C_BLOCK_COMMENT_MODE,
  18. hljs.COMMENT(
  19. '/\\*\\*',
  20. '\\*/',
  21. {
  22. relevance: 0,
  23. contains: [
  24. {
  25. // eat up @'s in emails to prevent them to be recognized as doctags
  26. begin: /\w+@/,
  27. relevance: 0
  28. },
  29. {
  30. className: 'doctag',
  31. begin: '@[A-Za-z]+'
  32. }
  33. ]
  34. }
  35. )
  36. ]);
  37. const REGEXP = {
  38. className: 'regexp',
  39. begin: /~?\/[^\/\n]+\//,
  40. contains: [ hljs.BACKSLASH_ESCAPE ]
  41. };
  42. const NUMBER = variants([
  43. hljs.BINARY_NUMBER_MODE,
  44. hljs.C_NUMBER_MODE
  45. ]);
  46. const STRING = variants([
  47. {
  48. begin: /"""/,
  49. end: /"""/
  50. },
  51. {
  52. begin: /'''/,
  53. end: /'''/
  54. },
  55. {
  56. begin: "\\$/",
  57. end: "/\\$",
  58. relevance: 10
  59. },
  60. hljs.APOS_STRING_MODE,
  61. hljs.QUOTE_STRING_MODE
  62. ],
  63. { className: "string" }
  64. );
  65. const CLASS_DEFINITION = {
  66. match: [
  67. /(class|interface|trait|enum|record|extends|implements)/,
  68. /\s+/,
  69. hljs.UNDERSCORE_IDENT_RE
  70. ],
  71. scope: {
  72. 1: "keyword",
  73. 3: "title.class",
  74. }
  75. };
  76. const TYPES = [
  77. "byte",
  78. "short",
  79. "char",
  80. "int",
  81. "long",
  82. "boolean",
  83. "float",
  84. "double",
  85. "void"
  86. ];
  87. const KEYWORDS = [
  88. // groovy specific keywords
  89. "def",
  90. "as",
  91. "in",
  92. "assert",
  93. "trait",
  94. // common keywords with Java
  95. "abstract",
  96. "static",
  97. "volatile",
  98. "transient",
  99. "public",
  100. "private",
  101. "protected",
  102. "synchronized",
  103. "final",
  104. "class",
  105. "interface",
  106. "enum",
  107. "if",
  108. "else",
  109. "for",
  110. "while",
  111. "switch",
  112. "case",
  113. "break",
  114. "default",
  115. "continue",
  116. "throw",
  117. "throws",
  118. "try",
  119. "catch",
  120. "finally",
  121. "implements",
  122. "extends",
  123. "new",
  124. "import",
  125. "package",
  126. "return",
  127. "instanceof",
  128. "var"
  129. ];
  130. return {
  131. name: 'Groovy',
  132. keywords: {
  133. "variable.language": 'this super',
  134. literal: 'true false null',
  135. type: TYPES,
  136. keyword: KEYWORDS
  137. },
  138. contains: [
  139. hljs.SHEBANG({
  140. binary: "groovy",
  141. relevance: 10
  142. }),
  143. COMMENT,
  144. STRING,
  145. REGEXP,
  146. NUMBER,
  147. CLASS_DEFINITION,
  148. {
  149. className: 'meta',
  150. begin: '@[A-Za-z]+',
  151. relevance: 0
  152. },
  153. {
  154. // highlight map keys and named parameters as attrs
  155. className: 'attr',
  156. begin: IDENT_RE + '[ \t]*:',
  157. relevance: 0
  158. },
  159. {
  160. // catch middle element of the ternary operator
  161. // to avoid highlight it as a label, named parameter, or map key
  162. begin: /\?/,
  163. end: /:/,
  164. relevance: 0,
  165. contains: [
  166. COMMENT,
  167. STRING,
  168. REGEXP,
  169. NUMBER,
  170. 'self'
  171. ]
  172. },
  173. {
  174. // highlight labeled statements
  175. className: 'symbol',
  176. begin: '^[ \t]*' + regex.lookahead(IDENT_RE + ':'),
  177. excludeBegin: true,
  178. end: IDENT_RE + ':',
  179. relevance: 0
  180. }
  181. ],
  182. illegal: /#|<\//
  183. };
  184. }
  185. export { groovy as default };