-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e27e65a
commit 053016e
Showing
21 changed files
with
566 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
Prism.languages.racket = Prism.languages.extend('scheme', {}); | ||
|
||
// Add brackets to racket | ||
// The basic idea here is to go through all pattens of Scheme and replace all occurrences of "(" with the union of "(" | ||
// and "["; Similar for ")". This is a bit tricky because "(" can be escaped or inside a character set. Both cases | ||
// have to be handled differently and, of course, we don't want to destroy groups, so we can only replace literal "(" | ||
// and ")". | ||
// To do this, we use a regular expression which will parse any JS regular expression. It works because regexes are | ||
// matches from left to right and already matched text cannot be matched again. We use this to first capture all | ||
// escaped characters (not really, we don't get escape sequences but we don't need them). Because we already captured | ||
// all escaped characters, we know that any "[" character is the start of a character set, so we match that character | ||
// set whole. | ||
// With the regex parsed, we only have to replace all escaped "(" (they cannot be unescaped outside of character sets) | ||
// with /[([]/ and replace all "(" inside character sets. | ||
// Note: This method does not work for "(" that are escaped like this /\x28/ or this /\u0028/. | ||
Prism.languages.DFS(Prism.languages.racket, function (key, value) { | ||
if (Prism.util.type(value) === 'RegExp') { | ||
var source = value.source.replace(/\\(.)|\[\^?((?:\\.|[^\\\]])*)\]/g, function (m, g1, g2) { | ||
if (g1) { | ||
if (g1 === '(') { | ||
// replace all '(' characters outside character sets | ||
return '[([]'; | ||
} | ||
if (g1 === ')') { | ||
// replace all ')' characters outside character sets | ||
return '[)\\]]'; | ||
} | ||
} | ||
if (g2) { | ||
var prefix = m[1] === '^' ? '[^' : '['; | ||
return prefix + g2.replace(/\\(.)|[()]/g, function (m, g1) { | ||
if (m === '(' || g1 === '(') { | ||
// replace all '(' characters inside character sets | ||
return '(['; | ||
} | ||
if (m === ')' || g1 === ')') { | ||
// replace all ')' characters inside character sets | ||
return ')\\]'; | ||
} | ||
return m; | ||
}) + ']'; | ||
} | ||
return m; | ||
}); | ||
|
||
this[key] = RegExp(source, value.flags); | ||
} | ||
}); | ||
|
||
Prism.languages.insertBefore('racket', 'string', { | ||
'lang': { | ||
pattern: /^#lang.+/m, | ||
greedy: true, | ||
alias: 'keyword' | ||
} | ||
}); | ||
|
||
Prism.languages.rkt = Prism.languages.racket; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<h2>Full example</h2> | ||
<pre><code>; Source: https://github.com/mbutterick/pollen/blob/master/pollen/private/to-string.rkt | ||
|
||
#lang racket/base | ||
(provide (all-defined-out)) | ||
|
||
(define (to-string x) | ||
(cond | ||
[(string? x) x] | ||
[(or (null? x) (void? x)) ""] | ||
[(or (symbol? x) (number? x) (path? x) (char? x)) (format "~a" x)] | ||
;; special handling for procedures, because if a procedure reaches this func, | ||
;; it usually indicates a failed attempt to use a tag function. | ||
;; meaning, it's more useful to raise an error. | ||
[(procedure? x) (error 'pollen "Can't convert procedure ~a to string" x)] | ||
[else (format "~v" x)]))</code></pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.