Skip to content

Commit

Permalink
Scheme: Added support for R7RS syntax (#2525)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Aug 30, 2020
1 parent fa2225f commit e4f6cca
Show file tree
Hide file tree
Showing 10 changed files with 523 additions and 108 deletions.
33 changes: 22 additions & 11 deletions components/prism-scheme.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Prism.languages.scheme = {
'comment': /;.*/,
// this supports "normal" single-line comments:
// ; comment
// and (potentially nested) multiline comments:
// #| comment #| nested |# still comment |#
// (only 1 level of nesting is supported)
'comment': /;.*|#;\s*\((?:[^()]|\([^()]*\))*\)|#\|(?:[^#|]|#(?!\|)|\|(?!#)|#\|(?:[^#|]|#(?!\|)|\|(?!#))*\|#)*\|#/,
'string': {
pattern: /"(?:[^"\\]|\\.)*"/,
greedy: true
Expand All @@ -9,14 +14,14 @@ Prism.languages.scheme = {
greedy: true
},
'character': {
pattern: /#\\(?:[ux][a-fA-F\d]+|[-a-zA-Z]+|\S)/,
pattern: /#\\(?:[ux][a-fA-F\d]+\b|[-a-zA-Z]+\b|\S)/,
greedy: true,
alias: 'string'
},
'lambda-parameter': [
// https://www.cs.cmu.edu/Groups/AI/html/r4rs/r4rs_6.html#SEC30
{
pattern: /(\(lambda\s+)[^()'\s]+/,
pattern: /(\(lambda\s+)(?:[^|()'\s]+|\|(?:[^\\|]|\\.)*\|)/,
lookbehind: true
},
{
Expand All @@ -25,11 +30,16 @@ Prism.languages.scheme = {
}
],
'keyword': {
pattern: /(\()(?:define(?:-library|-macro|-syntax|-values)?|defmacro|(?:case-)?lambda|let(?:(?:\*|rec)?(?:-values)?|-syntax|rec-syntax)|else|if|cond|begin|delay(?:-force)?|parameterize|guard|set!|(?:quasi-)?quote|syntax-(?:case|rules))(?=[()\s]|$)/,
pattern: /(\()(?:begin|case(?:-lambda)?|cond(?:-expand)?|define(?:-library|-macro|-record-type|-syntax|-values)?|defmacro|delay(?:-force)?|do|else|export|except|guard|if|import|include(?:-ci|-library-declarations)?|lambda|let(?:rec)?(?:-syntax|-values|\*)?|let\*-values|only|parameterize|prefix|(?:quasi-?)?quote|rename|set!|syntax-(?:case|rules)|unless|unquote(?:-splicing)?|when)(?=[()\s]|$)/,
lookbehind: true
},
'builtin': {
pattern: /(\()(?:(?:cons|car|cdr|list|call-with-current-continuation|call\/cc|append|abs|apply|eval)\b|null\?|pair\?|boolean\?|eof-object\?|char\?|procedure\?|number\?|port\?|string\?|vector\?|symbol\?|bytevector\?)(?=[()\s]|$)/,
// all functions of the base library of R7RS plus some of built-ins of R5Rs
pattern: /(\()(?:abs|and|append|apply|assoc|ass[qv]|binary-port\?|boolean=?\?|bytevector(?:-append|-copy|-copy!|-length|-u8-ref|-u8-set!|\?)?|caar|cadr|call-with-(?:current-continuation|port|values)|call\/cc|car|cdar|cddr|cdr|ceiling|char(?:->integer|-ready\?|\?|<\?|<=\?|=\?|>\?|>=\?)|close-(?:input-port|output-port|port)|complex\?|cons|current-(?:error|input|output)-port|denominator|dynamic-wind|eof-object\??|eq\?|equal\?|eqv\?|error|error-object(?:-irritants|-message|\?)|eval|even\?|exact(?:-integer-sqrt|-integer\?|\?)?|expt|features|file-error\?|floor(?:-quotient|-remainder|\/)?|flush-output-port|for-each|gcd|get-output-(?:bytevector|string)|inexact\??|input-port(?:-open\?|\?)|integer(?:->char|\?)|lcm|length|list(?:->string|->vector|-copy|-ref|-set!|-tail|\?)?|make-(?:bytevector|list|parameter|string|vector)|map|max|member|memq|memv|min|modulo|negative\?|newline|not|null\?|number(?:->string|\?)|numerator|odd\?|open-(?:input|output)-(?:bytevector|string)|or|output-port(?:-open\?|\?)|pair\?|peek-char|peek-u8|port\?|positive\?|procedure\?|quotient|raise|raise-continuable|rational\?|rationalize|read-(?:bytevector|bytevector!|char|error\?|line|string|u8)|real\?|remainder|reverse|round|set-c[ad]r!|square|string(?:->list|->number|->symbol|->utf8|->vector|-append|-copy|-copy!|-fill!|-for-each|-length|-map|-ref|-set!|\?|<\?|<=\?|=\?|>\?|>=\?)?|substring|symbol(?:->string|\?|=\?)|syntax-error|textual-port\?|truncate(?:-quotient|-remainder|\/)?|u8-ready\?|utf8->string|values|vector(?:->list|->string|-append|-copy|-copy!|-fill!|-for-each|-length|-map|-ref|-set!|\?)?|with-exception-handler|write-(?:bytevector|char|string|u8)|zero\?)(?=[()\s]|$)/,
lookbehind: true
},
'operator': {
pattern: /(\()(?:[-+*%/]|[<>]=?|=>?)(?=[()\s]|$)/,
lookbehind: true
},
'number': {
Expand All @@ -52,16 +62,17 @@ Prism.languages.scheme = {
lookbehind: true
},
'boolean': {
pattern: /(^|[\s()])#[ft](?=[()\s]|$)/,
lookbehind: true
},
'operator': {
pattern: /(\()(?:[-+*%\/]|[<>]=?|=>?)(?=[()\s]|$)/,
pattern: /(^|[\s()])#(?:[ft]|false|true)(?=[()\s]|$)/,
lookbehind: true
},
'function': {
pattern: /(\()[^()'\s]+(?=[()\s]|$)/,
pattern: /(\()(?:[^|()'\s]+|\|(?:[^\\|]|\\.)*\|)(?=[()\s]|$)/,
lookbehind: true
},
'identifier': {
pattern: /(^|[\s()])\|(?:[^\\|]|\\.)*\|(?=[()\s]|$)/,
lookbehind: true,
greedy: true
},
'punctuation': /[()']/
};
2 changes: 1 addition & 1 deletion components/prism-scheme.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions tests/languages/racket/function_feature.test
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(fl= 1 2)
(flmin 2 3)
(exact? 2)
(inexact->exact 3)
(!fact)
(** 10)
(exact?
(**
(defined foo)

[fl= 1 2]
[flmin 2 3]
[exact? 2]
[inexact->exact 3]
[!fact]
[** 10]
[exact?
[**
[defined foo]

Expand All @@ -23,21 +19,17 @@
[
["punctuation", "("], ["function", "fl="], ["number", "1"], ["number", "2"], ["punctuation", ")"],
["punctuation", "("], ["function", "flmin"], ["number", "2"], ["number", "3"], ["punctuation", ")"],
["punctuation", "("], ["function", "exact?"], ["number", "2"], ["punctuation", ")"],
["punctuation", "("], ["function", "inexact->exact"], ["number", "3"], ["punctuation", ")"],
["punctuation", "("], ["function", "!fact"], ["punctuation", ")"],
["punctuation", "("], ["function", "**"], ["number", "10"], ["punctuation", ")"],
["punctuation", "("], ["function", "exact?"],
["punctuation", "("], ["function", "**"],
["punctuation", "("], ["function", "defined"], " foo", ["punctuation", ")"],

["punctuation", "["], ["function", "fl="], ["number", "1"], ["number", "2"], ["punctuation", "]"],
["punctuation", "["], ["function", "flmin"], ["number", "2"], ["number", "3"], ["punctuation", "]"],
["punctuation", "["], ["function", "exact?"], ["number", "2"], ["punctuation", "]"],
["punctuation", "["], ["function", "inexact->exact"], ["number", "3"], ["punctuation", "]"],
["punctuation", "["], ["function", "!fact"], ["punctuation", "]"],
["punctuation", "["], ["function", "**"], ["number", "10"], ["punctuation", "]"],
["punctuation", "["], ["function", "exact?"],
["punctuation", "["], ["function", "**"],
["punctuation", "["], ["function", "defined"], " foo", ["punctuation", "]"]
]
Expand Down
8 changes: 3 additions & 5 deletions tests/languages/scheme/boolean_feature.test
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#t
#f

; not a boolean
#true
#false

----------------------------------------------------

[
["boolean", "#t"],
["boolean", "#f"],

["comment", "; not a boolean"],
"\r\n#true"
["boolean", "#true"],
["boolean", "#false"]
]

----------------------------------------------------
Expand Down
Loading

0 comments on commit e4f6cca

Please sign in to comment.