UNPKG

2.54 kBJavaScriptView Raw
1Prism.languages.racket = Prism.languages.extend('scheme', {
2 'lambda-parameter': {
3 // the racket lambda syntax is a lot more complex, so we won't even attempt to capture it.
4 // this will just prevent false positives of the `function` pattern
5 pattern: /(\(lambda\s+\()[^()'\s]+/,
6 lookbehind: true
7 }
8});
9
10// Add brackets to racket
11// The basic idea here is to go through all pattens of Scheme and replace all occurrences of "(" with the union of "("
12// and "["; Similar for ")". This is a bit tricky because "(" can be escaped or inside a character set. Both cases
13// have to be handled differently and, of course, we don't want to destroy groups, so we can only replace literal "("
14// and ")".
15// To do this, we use a regular expression which will parse any JS regular expression. It works because regexes are
16// matches from left to right and already matched text cannot be matched again. We use this to first capture all
17// escaped characters (not really, we don't get escape sequences but we don't need them). Because we already captured
18// all escaped characters, we know that any "[" character is the start of a character set, so we match that character
19// set whole.
20// With the regex parsed, we only have to replace all escaped "(" (they cannot be unescaped outside of character sets)
21// with /[([]/ and replace all "(" inside character sets.
22// Note: This method does not work for "(" that are escaped like this /\x28/ or this /\u0028/.
23Prism.languages.DFS(Prism.languages.racket, function (key, value) {
24 if (Prism.util.type(value) === 'RegExp') {
25 var source = value.source.replace(/\\(.)|\[\^?((?:\\.|[^\\\]])*)\]/g, function (m, g1, g2) {
26 if (g1) {
27 if (g1 === '(') {
28 // replace all '(' characters outside character sets
29 return '[([]';
30 }
31 if (g1 === ')') {
32 // replace all ')' characters outside character sets
33 return '[)\\]]';
34 }
35 }
36 if (g2) {
37 var prefix = m[1] === '^' ? '[^' : '[';
38 return prefix + g2.replace(/\\(.)|[()]/g, function (m, g1) {
39 if (m === '(' || g1 === '(') {
40 // replace all '(' characters inside character sets
41 return '([';
42 }
43 if (m === ')' || g1 === ')') {
44 // replace all ')' characters inside character sets
45 return ')\\]';
46 }
47 return m;
48 }) + ']';
49 }
50 return m;
51 });
52
53 this[key] = RegExp(source, value.flags);
54 }
55});
56
57Prism.languages.insertBefore('racket', 'string', {
58 'lang': {
59 pattern: /^#lang.+/m,
60 greedy: true,
61 alias: 'keyword'
62 }
63});
64
65Prism.languages.rkt = Prism.languages.racket;