source: node_modules/highlight.js/lib/languages/groovy.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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