Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheme: Improvements #2263

Merged
merged 6 commits into from Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 28 additions & 4 deletions components/prism-scheme.js
@@ -1,13 +1,22 @@
Prism.languages.scheme = {
'comment': /;.*/,
'string': {
pattern: /"(?:[^"\\]|\\.)*"|'[^()#'\s]+/,
pattern: /"(?:[^"\\]|\\.)*"/,
greedy: true
},
'symbol': {
pattern: /'[^()#'\s]+/,
greedy: true
},
'character': {
pattern: /#\\(?:[ux][a-fA-F\d]+|[a-zA-Z]+|\S)/,
pattern: /#\\(?:[ux][a-fA-F\d]+|[-a-zA-Z]+|\S)/,
greedy: true,
alias: 'string'
},
'lambda-parameter': {
pattern: /(\(lambda\s+\()[^()'\s]+/,
lookbehind: true
},
'keyword': {
pattern: /(\()(?:define(?:-syntax|-library|-values)?|(?:case-)?lambda|let(?:\*|rec)?(?:-values)?|else|if|cond|begin|delay(?:-force)?|parameterize|guard|set!|(?:quasi-)?quote|syntax-rules)(?=[()\s])/,
lookbehind: true
Expand All @@ -17,7 +26,22 @@ Prism.languages.scheme = {
lookbehind: true
},
'number': {
pattern: /([\s()])[-+]?(?:\d+\/\d+|\d*\.?\d+(?:\s*[-+]\s*\d*\.?\d+i)?)\b/,
// This pattern (apart from the lookarounds) works like this:
//
// Decimal numbers
// <dec real> := \d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+
// <dec complex> := <dec real>(?:[+-]<dec real>i)?|<dec real>i
// <dec prefix> := (?:#d(?:#[ei])?|#[ei](?:#d)?)?
// <dec number> := <dec prefix>[+-]?<complex>
//
// Binary, octal, and hexadecimal numbers
// <b.o.x. real> := [\da-fA-F]+(?:\/[\da-fA-F]+)?
// <b.o.x. complex> := <b.o.x. real>(?:[+-]<b.o.x. real>i)?|<b.o.x. real>i
// <b.o.x. prefix> := #[box](?:#[ei])?|#[ei](?:#[box])?
// <b.o.x. number> := <b.o.x. prefix>[+-]?<b.o.x. complex>
//
// <number> := <dec number>|<b.o.x. number>
pattern: /([\s()])(?:(?:#d(?:#[ei])?|#[ei](?:#d)?)?[+-]?(?:(?:\d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+)(?:[+-](?:\d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+)i)?|(?:\d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+)i)|(?:#[box](?:#[ei])?|#[ei](?:#[box])?)[+-]?(?:[\da-fA-F]+(?:\/[\da-fA-F]+)?(?:[+-][\da-fA-F]+(?:\/[\da-fA-F]+)?i)?|[\da-fA-F]+(?:\/[\da-fA-F]+)?i))(?=[\s()]|$)/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this documentation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I don't.
It explains the pattern by showing its simpler components, yes, but how do you know that the pattern is actually what's written there? I actually manually "generated" the pattern by substituting the rules myself, so I can tell you it's a pain... especially if you change something...

I'd rather have the pattern generated from a BNF.
We could implement a BNF-like syntax with #2190 and a syntax like this:

bnf({
	// Decimal numbers
	'dec real'    : /\d*\.?\d+(?:[eE][+-]?\d+)?|\d+\/\d+/.source,
	'dec complex' : /<dec real>(?:[+-]<dec real>i)?|<dec real>i/.source,
	'dec prefix'  : /(?:#d(?:#[ei])?|#[ei](?:#d)?)?/.source,
	'dec number'  : /<dec prefix>[+-]?<complex>/.source,
	
	// Binary, octal, and hexadecimal numbers
	'box real'    : /[\da-fA-F]+(?:\/[\da-fA-F]+)?/.source,
	'box complex' : /<box real>(?:[+-]<box real>i)?|<box real>i/.source,
	'box prefix'  : /#[box](?:#[ei])?|#[ei](?:#[box])?/.source,
	'box number'  : /<box prefix>[+-]?<box complex>/.source,

	'out'         : /<dec number>|<box number>/.source,
}); // returns a RegExp

But this has a whole range of problems associated with it one of them being that minification basically impossible.

lookbehind: true
},
'boolean': /#[tf]/,
Expand All @@ -26,7 +50,7 @@ Prism.languages.scheme = {
lookbehind: true
},
'function': {
pattern: /(\()[^()'\s]+(?=[()\s)]|$)/,
pattern: /(\()[^()'\s]+(?=[()\s]|$)/,
lookbehind: true
},
'punctuation': /[()']/
Expand Down
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.

14 changes: 14 additions & 0 deletions known-failures.html
Expand Up @@ -287,6 +287,20 @@ <h3>Nested block comments</h3>
</section>


<section class="language-scheme">

<h3>The first argument of <code>case-lambda</code> argument lists are highlighted as functions</h3>
<pre><code>(define plus
(case-lambda
(() 0)
((x) x)
((x y) (+ x y))
((x y z) (+ (+ x y) z))
(args (apply + args))))</code></pre>

</section>


<section class="language-swift">

<h3>Nested block comments</h3>
Expand Down
34 changes: 32 additions & 2 deletions tests/languages/scheme/character_feature.test
@@ -1,5 +1,15 @@
#\a
#\space
#\a ; lowercase letter
#\A ; uppercase letter
#\( ; left parenthesis
#\space ; the space character
#\newline ; the newline character

#\c-a ; Control-a
#\meta-b ; Meta-b
#\c-s-m-h-a ; Control-Meta-Super-Hyper-A

#\; #\' #\"

#\u0041
#\x10FFFF
#\λ
Expand All @@ -9,7 +19,27 @@

[
["character", "#\\a"],
["comment", "; lowercase letter"],
["character", "#\\A"],
["comment", "; uppercase letter"],
["character", "#\\("],
["comment", "; left parenthesis"],
["character", "#\\space"],
["comment", "; the space character"],
["character", "#\\newline"],
["comment", "; the newline character"],

["character", "#\\c-a"],
["comment", "; Control-a"],
["character", "#\\meta-b"],
["comment", "; Meta-b"],
["character", "#\\c-s-m-h-a"],
["comment", "; Control-Meta-Super-Hyper-A"],

["character", "#\\;"],
["character", "#\\'"],
["character", "#\\\""],

["character", "#\\u0041"],
["character", "#\\x10FFFF"],
["character", "#\\λ"],
Expand Down
21 changes: 21 additions & 0 deletions tests/languages/scheme/lambda_parameter_feature.test
@@ -0,0 +1,21 @@
(lambda (foo bar) (concat foo bar))

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

[
["punctuation", "("],
["keyword", "lambda"],
["punctuation", "("],
["lambda-parameter", "foo"],
" bar",
["punctuation", ")"],
["punctuation", "("],
["function", "concat"],
" foo bar",
["punctuation", ")"],
["punctuation", ")"]
]

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

Checks for lambda parameters.
98 changes: 87 additions & 11 deletions tests/languages/scheme/number_feature.test
@@ -1,19 +1,95 @@
(foo 42)
(foo 3.14159)
(foo 42 +42 -42)
(foo 1e3 +1e3 -1e3)
(foo 1e+3 1e-3 3.14159 3.14159e-1)
(foo 8/3)
(foo 3+4i)
(foo 2.5+0.0i)
(foo 3+0i)
(foo 3+4i 2.5+0.0i 2.5+0.0i -2.5e4+0.0e4i 3+0i -2e-5i)
(list 10i +10i -10i 10.10i 10+10i 10.10+10.10i 10-10i 10e+10i 10+10e+10i)

(list #d123 #e#d123e-4 #d#i12 #i-1.234i)

(list #xBAD #b1110011 #o777)
(list #i#x10 #i#x10+10i #b10+10i)

; not a number but a symbol
(define 1+2 10)

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

[
["punctuation", "("], ["function", "foo"], ["number", "42"], ["punctuation", ")"],
["punctuation", "("], ["function", "foo"], ["number", "3.14159"], ["punctuation", ")"],
["punctuation", "("], ["function", "foo"], ["number", "8/3"], ["punctuation", ")"],
["punctuation", "("], ["function", "foo"], ["number", "3+4i"], ["punctuation", ")"],
["punctuation", "("], ["function", "foo"], ["number", "2.5+0.0i"], ["punctuation", ")"],
["punctuation", "("], ["function", "foo"], ["number", "3+0i"], ["punctuation", ")"]
["punctuation", "("],
["function", "foo"],
["number", "42"],
["number", "+42"],
["number", "-42"],
["punctuation", ")"],

["punctuation", "("],
["function", "foo"],
["number", "1e3"],
["number", "+1e3"],
["number", "-1e3"],
["punctuation", ")"],

["punctuation", "("],
["function", "foo"],
["number", "1e+3"],
["number", "1e-3"],
["number", "3.14159"],
["number", "3.14159e-1"],
["punctuation", ")"],

["punctuation", "("],
["function", "foo"],
["number", "8/3"],
["punctuation", ")"],

["punctuation", "("],
["function", "foo"],
["number", "3+4i"],
["number", "2.5+0.0i"],
["number", "2.5+0.0i"],
["number", "-2.5e4+0.0e4i"],
["number", "3+0i"],
["number", "-2e-5i"],
["punctuation", ")"],

["punctuation", "("],
["builtin", "list"],
["number", "10i"],
["number", "+10i"],
["number", "-10i"],
["number", "10.10i"],
["number", "10+10i"],
["number", "10.10+10.10i"],
["number", "10-10i"],
["number", "10e+10i"],
["number", "10+10e+10i"],
["punctuation", ")"],

["punctuation", "("],
["builtin", "list"],
["number", "#d123"],
["number", "#e#d123e-4"],
["number", "#d#i12"],
["number", "#i-1.234i"],
["punctuation", ")"],

["punctuation", "("],
["builtin", "list"],
["number", "#xBAD"],
["number", "#b1110011"],
["number", "#o777"],
["punctuation", ")"],

["punctuation", "("],
["builtin", "list"],
["number", "#i#x10"],
["number", "#i#x10+10i"],
["number", "#b10+10i"],
["punctuation", ")"],

["comment", "; not a number but a symbol"],
["punctuation", "("], ["keyword", "define"], " 1+2 ", ["number", "10"], ["punctuation", ")"]
]

----------------------------------------------------
Expand Down
8 changes: 2 additions & 6 deletions tests/languages/scheme/string_feature.test
Expand Up @@ -4,19 +4,15 @@
multi
line
"
'turkey
(define a 'foo)

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

[
["string", "\"\""],
["string", "\"fo\\\"obar\""],
["string", "\"\r\nmulti\r\nline\r\n\""],
["string", "'turkey"],
["punctuation", "("], ["keyword", "define"], " a ", ["string","'foo"], ["punctuation",")"]
["string", "\"\r\nmulti\r\nline\r\n\""]
]

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

Checks for strings and symbols.
Checks for strings.
13 changes: 13 additions & 0 deletions tests/languages/scheme/symbol_feature.test
@@ -0,0 +1,13 @@
'turkey
(define a 'foo)

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

[
["symbol", "'turkey"],
["punctuation", "("], ["keyword", "define"], " a ", ["symbol","'foo"], ["punctuation",")"]
]

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

Checks for symbols.