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