UNPKG

6.82 kBJavaScriptView Raw
1/*
2Language: D
3Author: Aleksandar Ruzicic <aleksandar@ruzicic.info>
4Description: D is a language with C-like syntax and static typing. It pragmatically combines efficiency, control, and modeling power, with safety and programmer productivity.
5Version: 1.0a
6Website: https://dlang.org
7Date: 2012-04-08
8*/
9
10/**
11 * Known issues:
12 *
13 * - invalid hex string literals will be recognized as a double quoted strings
14 * but 'x' at the beginning of string will not be matched
15 *
16 * - delimited string literals are not checked for matching end delimiter
17 * (not possible to do with js regexp)
18 *
19 * - content of token string is colored as a string (i.e. no keyword coloring inside a token string)
20 * also, content of token string is not validated to contain only valid D tokens
21 *
22 * - special token sequence rule is not strictly following D grammar (anything following #line
23 * up to the end of line is matched as special token sequence)
24 */
25
26/** @type LanguageFn */
27function d(hljs) {
28 /**
29 * Language keywords
30 *
31 * @type {Object}
32 */
33 const D_KEYWORDS = {
34 $pattern: hljs.UNDERSCORE_IDENT_RE,
35 keyword:
36 'abstract alias align asm assert auto body break byte case cast catch class ' +
37 'const continue debug default delete deprecated do else enum export extern final ' +
38 'finally for foreach foreach_reverse|10 goto if immutable import in inout int ' +
39 'interface invariant is lazy macro mixin module new nothrow out override package ' +
40 'pragma private protected public pure ref return scope shared static struct ' +
41 'super switch synchronized template this throw try typedef typeid typeof union ' +
42 'unittest version void volatile while with __FILE__ __LINE__ __gshared|10 ' +
43 '__thread __traits __DATE__ __EOF__ __TIME__ __TIMESTAMP__ __VENDOR__ __VERSION__',
44 built_in:
45 'bool cdouble cent cfloat char creal dchar delegate double dstring float function ' +
46 'idouble ifloat ireal long real short string ubyte ucent uint ulong ushort wchar ' +
47 'wstring',
48 literal:
49 'false null true'
50 };
51
52 /**
53 * Number literal regexps
54 *
55 * @type {String}
56 */
57 const decimal_integer_re = '(0|[1-9][\\d_]*)';
58 const decimal_integer_nosus_re = '(0|[1-9][\\d_]*|\\d[\\d_]*|[\\d_]+?\\d)';
59 const binary_integer_re = '0[bB][01_]+';
60 const hexadecimal_digits_re = '([\\da-fA-F][\\da-fA-F_]*|_[\\da-fA-F][\\da-fA-F_]*)';
61 const hexadecimal_integer_re = '0[xX]' + hexadecimal_digits_re;
62
63 const decimal_exponent_re = '([eE][+-]?' + decimal_integer_nosus_re + ')';
64 const decimal_float_re = '(' + decimal_integer_nosus_re + '(\\.\\d*|' + decimal_exponent_re + ')|' +
65 '\\d+\\.' + decimal_integer_nosus_re + '|' +
66 '\\.' + decimal_integer_re + decimal_exponent_re + '?' +
67 ')';
68 const hexadecimal_float_re = '(0[xX](' +
69 hexadecimal_digits_re + '\\.' + hexadecimal_digits_re + '|' +
70 '\\.?' + hexadecimal_digits_re +
71 ')[pP][+-]?' + decimal_integer_nosus_re + ')';
72
73 const integer_re = '(' +
74 decimal_integer_re + '|' +
75 binary_integer_re + '|' +
76 hexadecimal_integer_re +
77 ')';
78
79 const float_re = '(' +
80 hexadecimal_float_re + '|' +
81 decimal_float_re +
82 ')';
83
84 /**
85 * Escape sequence supported in D string and character literals
86 *
87 * @type {String}
88 */
89 const escape_sequence_re = '\\\\(' +
90 '[\'"\\?\\\\abfnrtv]|' + // common escapes
91 'u[\\dA-Fa-f]{4}|' + // four hex digit unicode codepoint
92 '[0-7]{1,3}|' + // one to three octal digit ascii char code
93 'x[\\dA-Fa-f]{2}|' + // two hex digit ascii char code
94 'U[\\dA-Fa-f]{8}' + // eight hex digit unicode codepoint
95 ')|' +
96 '&[a-zA-Z\\d]{2,};'; // named character entity
97
98 /**
99 * D integer number literals
100 *
101 * @type {Object}
102 */
103 const D_INTEGER_MODE = {
104 className: 'number',
105 begin: '\\b' + integer_re + '(L|u|U|Lu|LU|uL|UL)?',
106 relevance: 0
107 };
108
109 /**
110 * [D_FLOAT_MODE description]
111 * @type {Object}
112 */
113 const D_FLOAT_MODE = {
114 className: 'number',
115 begin: '\\b(' +
116 float_re + '([fF]|L|i|[fF]i|Li)?|' +
117 integer_re + '(i|[fF]i|Li)' +
118 ')',
119 relevance: 0
120 };
121
122 /**
123 * D character literal
124 *
125 * @type {Object}
126 */
127 const D_CHARACTER_MODE = {
128 className: 'string',
129 begin: '\'(' + escape_sequence_re + '|.)',
130 end: '\'',
131 illegal: '.'
132 };
133
134 /**
135 * D string escape sequence
136 *
137 * @type {Object}
138 */
139 const D_ESCAPE_SEQUENCE = {
140 begin: escape_sequence_re,
141 relevance: 0
142 };
143
144 /**
145 * D double quoted string literal
146 *
147 * @type {Object}
148 */
149 const D_STRING_MODE = {
150 className: 'string',
151 begin: '"',
152 contains: [D_ESCAPE_SEQUENCE],
153 end: '"[cwd]?'
154 };
155
156 /**
157 * D wysiwyg and delimited string literals
158 *
159 * @type {Object}
160 */
161 const D_WYSIWYG_DELIMITED_STRING_MODE = {
162 className: 'string',
163 begin: '[rq]"',
164 end: '"[cwd]?',
165 relevance: 5
166 };
167
168 /**
169 * D alternate wysiwyg string literal
170 *
171 * @type {Object}
172 */
173 const D_ALTERNATE_WYSIWYG_STRING_MODE = {
174 className: 'string',
175 begin: '`',
176 end: '`[cwd]?'
177 };
178
179 /**
180 * D hexadecimal string literal
181 *
182 * @type {Object}
183 */
184 const D_HEX_STRING_MODE = {
185 className: 'string',
186 begin: 'x"[\\da-fA-F\\s\\n\\r]*"[cwd]?',
187 relevance: 10
188 };
189
190 /**
191 * D delimited string literal
192 *
193 * @type {Object}
194 */
195 const D_TOKEN_STRING_MODE = {
196 className: 'string',
197 begin: 'q"\\{',
198 end: '\\}"'
199 };
200
201 /**
202 * Hashbang support
203 *
204 * @type {Object}
205 */
206 const D_HASHBANG_MODE = {
207 className: 'meta',
208 begin: '^#!',
209 end: '$',
210 relevance: 5
211 };
212
213 /**
214 * D special token sequence
215 *
216 * @type {Object}
217 */
218 const D_SPECIAL_TOKEN_SEQUENCE_MODE = {
219 className: 'meta',
220 begin: '#(line)',
221 end: '$',
222 relevance: 5
223 };
224
225 /**
226 * D attributes
227 *
228 * @type {Object}
229 */
230 const D_ATTRIBUTE_MODE = {
231 className: 'keyword',
232 begin: '@[a-zA-Z_][a-zA-Z_\\d]*'
233 };
234
235 /**
236 * D nesting comment
237 *
238 * @type {Object}
239 */
240 const D_NESTING_COMMENT_MODE = hljs.COMMENT(
241 '\\/\\+',
242 '\\+\\/',
243 {
244 contains: ['self'],
245 relevance: 10
246 }
247 );
248
249 return {
250 name: 'D',
251 keywords: D_KEYWORDS,
252 contains: [
253 hljs.C_LINE_COMMENT_MODE,
254 hljs.C_BLOCK_COMMENT_MODE,
255 D_NESTING_COMMENT_MODE,
256 D_HEX_STRING_MODE,
257 D_STRING_MODE,
258 D_WYSIWYG_DELIMITED_STRING_MODE,
259 D_ALTERNATE_WYSIWYG_STRING_MODE,
260 D_TOKEN_STRING_MODE,
261 D_FLOAT_MODE,
262 D_INTEGER_MODE,
263 D_CHARACTER_MODE,
264 D_HASHBANG_MODE,
265 D_SPECIAL_TOKEN_SEQUENCE_MODE,
266 D_ATTRIBUTE_MODE
267 ]
268 };
269}
270
271export { d as default };