@readme/syntax-highlighter 12.0.2 → 12.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,599 @@
1
+ /* eslint-disable operator-assignment */
2
+ /* eslint-disable prefer-template */
3
+ /* eslint-disable object-shorthand */
4
+ /* eslint-disable no-return-assign */
5
+ /* eslint-disable unicorn/no-unsafe-regex */
6
+ /* eslint-disable no-unused-expressions */
7
+ /* eslint-disable consistent-return */
8
+ /* eslint-disable no-prototype-builtins */
9
+ /* eslint-disable no-useless-escape */
10
+ /* eslint-disable eqeqeq */
11
+ /* eslint-disable no-use-before-define */
12
+ /* eslint-disable global-require */
13
+
14
+ /**
15
+ * This mode has been adapted from the source of `codemirror-solidity` as the way that library is
16
+ * structured makes it impossible for us load as an isolated mode for our Codemirror needs. The
17
+ * source is mostly presented as-is with the exception of a few areas adapted into to our coding
18
+ * standards (such as using `const` instead of `let` for immutable data).
19
+ *
20
+ * @license MIT
21
+ * @see {@link https://github.com/alincode/codemirror-solidity}
22
+ */
23
+ (function (mod) {
24
+ if (typeof exports === 'object' && typeof module === 'object') {
25
+ // CommonJS
26
+ mod(require('codemirror/lib/codemirror'));
27
+ // eslint-disable-next-line no-undef
28
+ } else if (typeof define === 'function' && define.amd) {
29
+ // AMD
30
+ define(['codemirror/lib/codemirror'], mod); // eslint-disable-line no-undef
31
+ } else {
32
+ // Plain browser env
33
+ mod(CodeMirror); // eslint-disable-line no-undef
34
+ }
35
+ })(function (CodeMirror) {
36
+ CodeMirror.defineMode('solidity', function (config) {
37
+ const indentUnit = config.indentUnit;
38
+
39
+ // let functionKeyword = 'function'
40
+ // let functionNameKeyword = 'Name'
41
+ // let leftBracketSign = '('
42
+ // let rightBracketSign = ')'
43
+ // let functionVariableName = 'variable'
44
+ // let keyWordContract = 'contract'
45
+
46
+ const keywords = {
47
+ pragma: true,
48
+ solidity: true,
49
+ import: true,
50
+ as: true,
51
+ from: true,
52
+ contract: true,
53
+ constructor: true,
54
+ is: true,
55
+ function: true,
56
+ modifier: true,
57
+ // modifiers
58
+ pure: true,
59
+ view: true,
60
+ payable: true,
61
+ constant: true,
62
+ anonymous: true,
63
+ indexed: true,
64
+ returns: true,
65
+ return: true,
66
+ event: true,
67
+ struct: true,
68
+ mapping: true,
69
+ interface: true,
70
+ using: true,
71
+ library: true,
72
+ storage: true,
73
+ memory: true,
74
+ calldata: true,
75
+ public: true,
76
+ private: true,
77
+ external: true,
78
+ internal: true,
79
+ emit: true,
80
+ assembly: true,
81
+ abstract: true,
82
+ after: true,
83
+ catch: true,
84
+ final: true,
85
+ in: true,
86
+ inline: true,
87
+ let: true,
88
+ match: true,
89
+ null: true,
90
+ of: true,
91
+ relocatable: true,
92
+ static: true,
93
+ try: true,
94
+ typeof: true,
95
+ var: true,
96
+ };
97
+
98
+ const keywordsSpecial = {
99
+ pragma: true,
100
+ returns: true,
101
+ address: true,
102
+ contract: true,
103
+ function: true,
104
+ struct: true,
105
+ };
106
+
107
+ const keywordsEtherUnit = {
108
+ wei: true,
109
+ szabo: true,
110
+ finney: true,
111
+ ether: true,
112
+ };
113
+ const keywordsTimeUnit = {
114
+ seconds: true,
115
+ minutes: true,
116
+ hours: true,
117
+ days: true,
118
+ weeks: true,
119
+ };
120
+ const keywordsBlockAndTransactionProperties = {
121
+ block: ['coinbase', 'difficulty', 'gaslimit', 'number', 'timestamp'],
122
+ msg: ['data', 'sender', 'sig', 'value'],
123
+ tx: ['gasprice', 'origin'],
124
+ };
125
+ const keywordsMoreBlockAndTransactionProperties = {
126
+ now: true,
127
+ gasleft: true,
128
+ blockhash: true,
129
+ };
130
+ const keywordsErrorHandling = {
131
+ assert: true,
132
+ require: true,
133
+ revert: true,
134
+ throw: true,
135
+ };
136
+ const keywordsMathematicalAndCryptographicFuctions = {
137
+ addmod: true,
138
+ mulmod: true,
139
+ keccak256: true,
140
+ sha256: true,
141
+ ripemd160: true,
142
+ ecrecover: true,
143
+ };
144
+ const keywordsContractRelated = {
145
+ this: true,
146
+ selfdestruct: true,
147
+ super: true,
148
+ };
149
+ const keywordsTypeInformation = { type: true };
150
+ const keywordsContractList = {};
151
+
152
+ const keywordsControlStructures = {
153
+ if: true,
154
+ else: true,
155
+ while: true,
156
+ do: true,
157
+ for: true,
158
+ break: true,
159
+ continue: true,
160
+ switch: true,
161
+ case: true,
162
+ default: true,
163
+ };
164
+
165
+ const keywordsValueTypes = {
166
+ bool: true,
167
+ byte: true,
168
+ string: true,
169
+ enum: true,
170
+ address: true,
171
+ };
172
+
173
+ const keywordsV0505NewReserve = {
174
+ alias: true,
175
+ apply: true,
176
+ auto: true,
177
+ copyof: true,
178
+ define: true,
179
+ immutable: true,
180
+ implements: true,
181
+ macro: true,
182
+ mutable: true,
183
+ override: true,
184
+ partial: true,
185
+ promise: true,
186
+ reference: true,
187
+ sealed: true,
188
+ sizeof: true,
189
+ supports: true,
190
+ typedef: true,
191
+ unchecked: true,
192
+ };
193
+
194
+ const keywordsAbiEncodeDecodeFunctions = {
195
+ abi: ['decode', 'encodePacked', 'encodeWithSelector', 'encodeWithSignature', 'encode'],
196
+ };
197
+
198
+ const keywordsMembersOfAddressType = ['transfer', 'send', 'balance', 'call', 'delegatecall', 'staticcall'];
199
+
200
+ const natSpecTags = ['title', 'author', 'notice', 'dev', 'param', 'return'];
201
+
202
+ // let functionStructureStage = [{
203
+ // function: ['function', 'returns']
204
+ // },
205
+ // leftBracketSign,
206
+ // parameterName,
207
+ // parameterValue,
208
+ // rightBracketSign,
209
+ // ];
210
+
211
+ const atoms = {
212
+ delete: true,
213
+ new: true,
214
+ true: true,
215
+ false: true,
216
+ // "iota": true, "nil": true, "append": true,
217
+ // "cap": true, "close": true, "complex": true, "copy": true, "imag": true,
218
+ // "make": true, "panic": true, "print": true,
219
+ // "println": true, "real": true, "recover": true
220
+ };
221
+
222
+ const isOperatorChar = /[+\-*&^%:=<>!|\/~]/;
223
+ const isNegativeChar = /[-]/;
224
+
225
+ let curPunc;
226
+
227
+ function tokenBase(stream, state) {
228
+ let ch = stream.next();
229
+
230
+ if (ch == '"' || ch == "'" || ch == '`') {
231
+ state.tokenize = tokenString(ch);
232
+ return state.tokenize(stream, state);
233
+ }
234
+
235
+ if (isVersion(stream, state)) return 'version';
236
+
237
+ if (
238
+ ch == '.' &&
239
+ keywordsMembersOfAddressType.some(function (item) {
240
+ return stream.match(`${item}`);
241
+ })
242
+ )
243
+ return 'addressFunction';
244
+
245
+ if (isNumber(ch, stream)) return 'number';
246
+
247
+ if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
248
+ return updateGarmmer(ch, state);
249
+ }
250
+
251
+ if (ch == '/') {
252
+ if (stream.eat('*')) {
253
+ state.tokenize = tokenComment;
254
+ return tokenComment(stream, state);
255
+ }
256
+ if (stream.match(/\/{2}/)) {
257
+ while ((ch = stream.next())) {
258
+ if (ch == '@') {
259
+ stream.backUp(1);
260
+ state.grammar = 'doc';
261
+ break;
262
+ }
263
+ }
264
+ return 'doc';
265
+ }
266
+
267
+ if (stream.eat('/')) {
268
+ stream.skipToEnd();
269
+ return 'comment';
270
+ }
271
+ }
272
+
273
+ if (isNegativeChar.test(ch)) {
274
+ if (isNumber(stream.peek(), stream)) return 'number';
275
+ return 'operator';
276
+ }
277
+
278
+ if (isOperatorChar.test(ch)) {
279
+ stream.eatWhile(isOperatorChar);
280
+ return 'operator';
281
+ }
282
+ stream.eatWhile(/[\w\$_\xa1-\uffff]/);
283
+
284
+ const cur = stream.current();
285
+
286
+ if (state.grammar == 'doc') {
287
+ if (
288
+ natSpecTags.some(function (item) {
289
+ return cur == `@${item}`;
290
+ })
291
+ )
292
+ return 'docReserve';
293
+ return 'doc';
294
+ }
295
+
296
+ if (cur === 'solidity' && state.lastToken == 'pragma') {
297
+ state.lastToken = state.lastToken + ' ' + cur;
298
+ }
299
+
300
+ if (keywords.propertyIsEnumerable(cur)) {
301
+ if (cur == 'case' || cur == 'default') curPunc = 'case';
302
+ if (keywordsSpecial.propertyIsEnumerable(cur)) state.lastToken = cur;
303
+ // if (cur == 'function' && state.para == 'parameterMode')
304
+ return 'keyword';
305
+ }
306
+
307
+ if (keywordsEtherUnit.propertyIsEnumerable(cur)) return 'etherUnit';
308
+ if (keywordsContractRelated.propertyIsEnumerable(cur)) return 'contractRelated';
309
+ if (
310
+ keywordsControlStructures.propertyIsEnumerable(cur) ||
311
+ keywordsTypeInformation.propertyIsEnumerable(cur) ||
312
+ keywordsV0505NewReserve.propertyIsEnumerable(cur)
313
+ )
314
+ return 'keyword';
315
+ if (
316
+ keywordsValueTypes.propertyIsEnumerable(cur) ||
317
+ keywordsTimeUnit.propertyIsEnumerable(cur) ||
318
+ isValidInteger(cur) ||
319
+ isValidBytes(cur) ||
320
+ isValidFixed(cur)
321
+ ) {
322
+ state.lastToken = state.lastToken + 'variable';
323
+ return 'keyword';
324
+ }
325
+
326
+ if (atoms.propertyIsEnumerable(cur)) return 'atom';
327
+ if (keywordsErrorHandling.propertyIsEnumerable(cur)) return 'errorHandling';
328
+ if (keywordsMathematicalAndCryptographicFuctions.propertyIsEnumerable(cur)) return 'mathematicalAndCryptographic';
329
+
330
+ if (
331
+ keywordsMoreBlockAndTransactionProperties.propertyIsEnumerable(cur) ||
332
+ (keywordsBlockAndTransactionProperties.hasOwnProperty(cur) &&
333
+ keywordsBlockAndTransactionProperties[cur].some(function (item) {
334
+ return stream.match(`.${item}`);
335
+ }))
336
+ )
337
+ return 'variable-2';
338
+
339
+ if (
340
+ keywordsAbiEncodeDecodeFunctions.hasOwnProperty(cur) &&
341
+ keywordsAbiEncodeDecodeFunctions[cur].some(function (item) {
342
+ return stream.match(`.${item}`);
343
+ })
344
+ )
345
+ return 'abi';
346
+
347
+ const style = updateHexLiterals(cur, stream);
348
+ if (style != null) return style;
349
+
350
+ if (
351
+ (state.lastToken == 'functionName(' || state.lastToken == 'returns(') &&
352
+ keywordsContractList.propertyIsEnumerable(cur)
353
+ ) {
354
+ state.lastToken += 'variable';
355
+ return 'variable';
356
+ }
357
+ if (state.lastToken == 'function') {
358
+ state.lastToken = 'functionName';
359
+ if (state.para == null) {
360
+ state.grammar = 'function';
361
+ state.para = '';
362
+ }
363
+ // state.parasMode = isNaN(state.parasMode) ? 1 : state.functionLayerCount++;
364
+ state.para += 'functionName';
365
+ return 'functionName';
366
+ }
367
+
368
+ if (state.lastToken == 'functionName(variable') {
369
+ state.lastToken = 'functionName(';
370
+ return 'parameterValue';
371
+ }
372
+
373
+ if (state.lastToken == 'returns(variable') {
374
+ state.lastToken = 'returns(';
375
+ return 'parameterValue';
376
+ }
377
+
378
+ if (state.lastToken == 'address' && cur == 'payable') {
379
+ state.lastToken = 'address payable';
380
+ }
381
+ if (state.lastToken == 'contract' || state.lastToken == 'struct') {
382
+ keywordsContractList[cur] = true;
383
+ state.lastToken = null;
384
+ }
385
+ if (state.grammar == 'function') {
386
+ return 'parameterValue';
387
+ }
388
+
389
+ return 'variable';
390
+ }
391
+
392
+ function tokenString(quote) {
393
+ return function (stream, state) {
394
+ let escaped = false;
395
+ let next;
396
+ let end = false;
397
+ while ((next = stream.next()) != null) {
398
+ if (next == quote && !escaped) {
399
+ end = true;
400
+ break;
401
+ }
402
+ escaped = !escaped && quote != '`' && next == '\\';
403
+ }
404
+ if (end || !(escaped || quote == '`')) state.tokenize = tokenBase;
405
+ return 'string';
406
+ };
407
+ }
408
+
409
+ function tokenComment(stream, state) {
410
+ let maybeEnd = false;
411
+ let ch;
412
+ while ((ch = stream.next())) {
413
+ if (ch == '/' && maybeEnd) {
414
+ state.tokenize = tokenBase;
415
+ break;
416
+ }
417
+ maybeEnd = ch == '*';
418
+ }
419
+ return 'comment';
420
+ }
421
+
422
+ function isVersion(stream, state) {
423
+ if (state.lastToken == 'pragma solidity') {
424
+ state.lastToken = null;
425
+ return (
426
+ !state.startOfLine &&
427
+ (stream.match(/[\^{0}][0-9\.]+/) || stream.match(/[\>\=]+?[\s]*[0-9\.]+[\s]*[\<]?[\s]*[0-9\.]+/))
428
+ );
429
+ }
430
+ }
431
+
432
+ function isNumber(ch, stream) {
433
+ if (/[\d\.]/.test(ch)) {
434
+ if (ch == '.') {
435
+ stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/);
436
+ } else if (ch == '0') {
437
+ stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/);
438
+ } else {
439
+ stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/);
440
+ }
441
+ return true;
442
+ }
443
+ }
444
+
445
+ function isValidInteger(token) {
446
+ if (token.match(/^[u]?int/)) {
447
+ if (token.indexOf('t') + 1 == token.length) return true;
448
+ const numberPart = token.substr(token.indexOf('t') + 1, token.length);
449
+ return numberPart % 8 === 0 && numberPart <= 256;
450
+ }
451
+ }
452
+
453
+ function isValidBytes(token) {
454
+ if (token.match(/^bytes/)) {
455
+ if (token.indexOf('s') + 1 == token.length) return true;
456
+ const bytesPart = token.substr(token.indexOf('s') + 1, token.length);
457
+ return bytesPart <= 32;
458
+ }
459
+ }
460
+
461
+ function isValidFixed(token) {
462
+ if (token.match(/^[u]?fixed([0-9]+x[0-9]+)?/)) {
463
+ if (token.indexOf('d') + 1 == token.length) return true;
464
+ const numberPart = token.substr(token.indexOf('d') + 1, token.length).split('x');
465
+ return numberPart[0] % 8 === 0 && numberPart[0] <= 256 && numberPart[1] <= 80;
466
+ }
467
+ }
468
+
469
+ function updateHexLiterals(token, stream) {
470
+ if (token.match(/^hex/) && stream.peek() == '"') {
471
+ let maybeEnd = false;
472
+ let ch;
473
+ let hexValue = '';
474
+ let stringAfterHex = '';
475
+ while ((ch = stream.next())) {
476
+ stringAfterHex += ch;
477
+ if (ch == '"' && maybeEnd) {
478
+ hexValue = stringAfterHex.substring(1, stringAfterHex.length - 1);
479
+ if (hexValue.match(/^[0-9a-fA-F]+$/)) {
480
+ return 'number';
481
+ }
482
+
483
+ stream.backUp(stringAfterHex.length);
484
+ break;
485
+ }
486
+ maybeEnd = maybeEnd || ch == '"';
487
+ }
488
+ }
489
+ }
490
+
491
+ function updateGarmmer(ch, state) {
492
+ if (ch == ',' && state.para == 'functionName(variable') {
493
+ state.para = 'functionName(';
494
+ }
495
+ if (state.para != null && state.para.startsWith('functionName')) {
496
+ if (ch == ')') {
497
+ if (state.para.endsWith('(')) {
498
+ state.para = state.para.substr(0, state.para.length - 1);
499
+ if (state.para == 'functionName') state.grammar = '';
500
+ }
501
+ } else if (ch == '(') {
502
+ state.para += ch;
503
+ }
504
+ }
505
+
506
+ if (ch == '(' && state.lastToken == 'functionName') {
507
+ state.lastToken += ch;
508
+ } else if (ch == ')' && state.lastToken == 'functionName(') {
509
+ state.lastToken = null;
510
+ } else if (ch == '(' && state.lastToken == 'returns') {
511
+ state.lastToken += ch;
512
+ } else if (ch == ')' && (state.lastToken == 'returns(' || state.lastToken == 'returns(variable')) {
513
+ state.lastToken = null;
514
+ }
515
+ if (ch == '(' && state.lastToken == 'address') {
516
+ state.lastToken += ch;
517
+ }
518
+ curPunc = ch;
519
+ return null;
520
+ }
521
+
522
+ function Context(indented, column, type, align, prev) {
523
+ this.indented = indented;
524
+ this.column = column;
525
+ this.type = type;
526
+ this.align = align;
527
+ this.prev = prev;
528
+ }
529
+ function pushContext(state, col, type) {
530
+ return (state.context = new Context(state.indented, col, type, null, state.context));
531
+ }
532
+ function popContext(state) {
533
+ if (!state.context.prev) return;
534
+ const t = state.context.type;
535
+ if (t == ')' || t == ']' || t == '}') state.indented = state.context.indented;
536
+ return (state.context = state.context.prev);
537
+ }
538
+
539
+ // Interface
540
+ return {
541
+ startState: function (basecolumn) {
542
+ return {
543
+ tokenize: null,
544
+ context: new Context((basecolumn || 0) - indentUnit, 0, 'top', false),
545
+ indented: 0,
546
+ startOfLine: true,
547
+ };
548
+ },
549
+
550
+ token: function (stream, state) {
551
+ const ctx = state.context;
552
+ if (stream.sol()) {
553
+ if (ctx.align == null) ctx.align = false;
554
+ state.indented = stream.indentation();
555
+ state.startOfLine = true;
556
+ if (ctx.type == 'case') ctx.type = '}';
557
+ if (state.grammar == 'doc') state.grammar = null;
558
+ }
559
+ if (stream.eatSpace()) return null;
560
+ curPunc = null;
561
+ const style = (state.tokenize || tokenBase)(stream, state);
562
+
563
+ if (style == 'comment') return style;
564
+ if (ctx.align == null) ctx.align = true;
565
+
566
+ if (curPunc == '{') pushContext(state, stream.column(), '}');
567
+ else if (curPunc == '[') pushContext(state, stream.column(), ']');
568
+ else if (curPunc == '(') pushContext(state, stream.column(), ')');
569
+ else if (curPunc == 'case') ctx.type = 'case';
570
+ else if (curPunc == '}' && ctx.type == '}') popContext(state);
571
+ else if (curPunc == ctx.type) popContext(state);
572
+ state.startOfLine = false;
573
+ return style;
574
+ },
575
+
576
+ indent: function (state, textAfter) {
577
+ if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
578
+ const ctx = state.context;
579
+ const firstChar = textAfter && textAfter.charAt(0);
580
+ if (ctx.type == 'case' && /^(?:case|default)\b/.test(textAfter)) {
581
+ state.context.type = '}';
582
+ return ctx.indented;
583
+ }
584
+ const closing = firstChar == ctx.type;
585
+ if (ctx.align) return ctx.column + (closing ? 0 : 1);
586
+ return ctx.indented + (closing ? 0 : indentUnit);
587
+ },
588
+
589
+ electricChars: '{}):',
590
+ closeBrackets: '()[]{}\'\'""``',
591
+ fold: 'brace',
592
+ blockCommentStart: '/*',
593
+ blockCommentEnd: '*/',
594
+ lineComment: '//',
595
+ };
596
+ });
597
+
598
+ CodeMirror.defineMIME('text/x-solidity', 'solidity');
599
+ });
@@ -76,8 +76,8 @@ const modes = {
76
76
  scala: ['clike', 'text/x-scala'],
77
77
  scss: 'css',
78
78
  sh: 'shell',
79
- sol: 'clike',
80
- solidity: 'clike',
79
+ sol: 'solidity',
80
+ solidity: 'solidity',
81
81
  sql: ['sql', 'text/x-sql'],
82
82
  sqlite: ['sql', 'text/x-sqlite'],
83
83
  styl: 'css',