1 | 'use strict'
|
---|
2 |
|
---|
3 | module.exports = scheme
|
---|
4 | scheme.displayName = 'scheme'
|
---|
5 | scheme.aliases = []
|
---|
6 | function scheme(Prism) {
|
---|
7 | ;(function (Prism) {
|
---|
8 | Prism.languages.scheme = {
|
---|
9 | // this supports "normal" single-line comments:
|
---|
10 | // ; comment
|
---|
11 | // and (potentially nested) multiline comments:
|
---|
12 | // #| comment #| nested |# still comment |#
|
---|
13 | // (only 1 level of nesting is supported)
|
---|
14 | comment:
|
---|
15 | /;.*|#;\s*(?:\((?:[^()]|\([^()]*\))*\)|\[(?:[^\[\]]|\[[^\[\]]*\])*\])|#\|(?:[^#|]|#(?!\|)|\|(?!#)|#\|(?:[^#|]|#(?!\|)|\|(?!#))*\|#)*\|#/,
|
---|
16 | string: {
|
---|
17 | pattern: /"(?:[^"\\]|\\.)*"/,
|
---|
18 | greedy: true
|
---|
19 | },
|
---|
20 | symbol: {
|
---|
21 | pattern: /'[^()\[\]#'\s]+/,
|
---|
22 | greedy: true
|
---|
23 | },
|
---|
24 | char: {
|
---|
25 | pattern:
|
---|
26 | /#\\(?:[ux][a-fA-F\d]+\b|[-a-zA-Z]+\b|[\uD800-\uDBFF][\uDC00-\uDFFF]|\S)/,
|
---|
27 | greedy: true
|
---|
28 | },
|
---|
29 | 'lambda-parameter': [
|
---|
30 | // https://www.cs.cmu.edu/Groups/AI/html/r4rs/r4rs_6.html#SEC30
|
---|
31 | {
|
---|
32 | pattern:
|
---|
33 | /((?:^|[^'`#])[(\[]lambda\s+)(?:[^|()\[\]'\s]+|\|(?:[^\\|]|\\.)*\|)/,
|
---|
34 | lookbehind: true
|
---|
35 | },
|
---|
36 | {
|
---|
37 | pattern: /((?:^|[^'`#])[(\[]lambda\s+[(\[])[^()\[\]']+/,
|
---|
38 | lookbehind: true
|
---|
39 | }
|
---|
40 | ],
|
---|
41 | keyword: {
|
---|
42 | pattern:
|
---|
43 | /((?:^|[^'`#])[(\[])(?:begin|case(?:-lambda)?|cond(?:-expand)?|define(?:-library|-macro|-record-type|-syntax|-values)?|defmacro|delay(?:-force)?|do|else|except|export|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]|$)/,
|
---|
44 | lookbehind: true
|
---|
45 | },
|
---|
46 | builtin: {
|
---|
47 | // all functions of the base library of R7RS plus some of built-ins of R5Rs
|
---|
48 | pattern:
|
---|
49 | /((?:^|[^'`#])[(\[])(?: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]|$)/,
|
---|
50 | lookbehind: true
|
---|
51 | },
|
---|
52 | operator: {
|
---|
53 | pattern: /((?:^|[^'`#])[(\[])(?:[-+*%/]|[<>]=?|=>?)(?=[()\[\]\s]|$)/,
|
---|
54 | lookbehind: true
|
---|
55 | },
|
---|
56 | number: {
|
---|
57 | // The number pattern from [the R7RS spec](https://small.r7rs.org/attachment/r7rs.pdf).
|
---|
58 | //
|
---|
59 | // <number> := <num 2>|<num 8>|<num 10>|<num 16>
|
---|
60 | // <num R> := <prefix R><complex R>
|
---|
61 | // <complex R> := <real R>(?:@<real R>|<imaginary R>)?|<imaginary R>
|
---|
62 | // <imaginary R> := [+-](?:<ureal R>|(?:inf|nan)\.0)?i
|
---|
63 | // <real R> := [+-]?<ureal R>|[+-](?:inf|nan)\.0
|
---|
64 | // <ureal R> := <uint R>(?:\/<uint R>)?
|
---|
65 | // | <decimal R>
|
---|
66 | //
|
---|
67 | // <decimal 10> := (?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?
|
---|
68 | // <uint R> := <digit R>+
|
---|
69 | // <prefix R> := <radix R>(?:#[ei])?|(?:#[ei])?<radix R>
|
---|
70 | // <radix 2> := #b
|
---|
71 | // <radix 8> := #o
|
---|
72 | // <radix 10> := (?:#d)?
|
---|
73 | // <radix 16> := #x
|
---|
74 | // <digit 2> := [01]
|
---|
75 | // <digit 8> := [0-7]
|
---|
76 | // <digit 10> := \d
|
---|
77 | // <digit 16> := [0-9a-f]
|
---|
78 | //
|
---|
79 | // The problem with this grammar is that the resulting regex is way to complex, so we simplify by grouping all
|
---|
80 | // non-decimal bases together. This results in a decimal (dec) and combined binary, octal, and hexadecimal (box)
|
---|
81 | // pattern:
|
---|
82 | pattern: RegExp(
|
---|
83 | SortedBNF({
|
---|
84 | '<ureal dec>':
|
---|
85 | /\d+(?:\/\d+)|(?:\d+(?:\.\d*)?|\.\d+)(?:[esfdl][+-]?\d+)?/.source,
|
---|
86 | '<real dec>': /[+-]?<ureal dec>|[+-](?:inf|nan)\.0/.source,
|
---|
87 | '<imaginary dec>': /[+-](?:<ureal dec>|(?:inf|nan)\.0)?i/.source,
|
---|
88 | '<complex dec>':
|
---|
89 | /<real dec>(?:@<real dec>|<imaginary dec>)?|<imaginary dec>/
|
---|
90 | .source,
|
---|
91 | '<num dec>': /(?:#d(?:#[ei])?|#[ei](?:#d)?)?<complex dec>/.source,
|
---|
92 | '<ureal box>': /[0-9a-f]+(?:\/[0-9a-f]+)?/.source,
|
---|
93 | '<real box>': /[+-]?<ureal box>|[+-](?:inf|nan)\.0/.source,
|
---|
94 | '<imaginary box>': /[+-](?:<ureal box>|(?:inf|nan)\.0)?i/.source,
|
---|
95 | '<complex box>':
|
---|
96 | /<real box>(?:@<real box>|<imaginary box>)?|<imaginary box>/
|
---|
97 | .source,
|
---|
98 | '<num box>': /#[box](?:#[ei])?|(?:#[ei])?#[box]<complex box>/
|
---|
99 | .source,
|
---|
100 | '<number>': /(^|[()\[\]\s])(?:<num dec>|<num box>)(?=[()\[\]\s]|$)/
|
---|
101 | .source
|
---|
102 | }),
|
---|
103 | 'i'
|
---|
104 | ),
|
---|
105 | lookbehind: true
|
---|
106 | },
|
---|
107 | boolean: {
|
---|
108 | pattern: /(^|[()\[\]\s])#(?:[ft]|false|true)(?=[()\[\]\s]|$)/,
|
---|
109 | lookbehind: true
|
---|
110 | },
|
---|
111 | function: {
|
---|
112 | pattern:
|
---|
113 | /((?:^|[^'`#])[(\[])(?:[^|()\[\]'\s]+|\|(?:[^\\|]|\\.)*\|)(?=[()\[\]\s]|$)/,
|
---|
114 | lookbehind: true
|
---|
115 | },
|
---|
116 | identifier: {
|
---|
117 | pattern: /(^|[()\[\]\s])\|(?:[^\\|]|\\.)*\|(?=[()\[\]\s]|$)/,
|
---|
118 | lookbehind: true,
|
---|
119 | greedy: true
|
---|
120 | },
|
---|
121 | punctuation: /[()\[\]']/
|
---|
122 | }
|
---|
123 | /**
|
---|
124 | * Given a topologically sorted BNF grammar, this will return the RegExp source of last rule of the grammar.
|
---|
125 | *
|
---|
126 | * @param {Record<string, string>} grammar
|
---|
127 | * @returns {string}
|
---|
128 | */
|
---|
129 | function SortedBNF(grammar) {
|
---|
130 | for (var key in grammar) {
|
---|
131 | grammar[key] = grammar[key].replace(/<[\w\s]+>/g, function (key) {
|
---|
132 | return '(?:' + grammar[key].trim() + ')'
|
---|
133 | })
|
---|
134 | } // return the last item
|
---|
135 | return grammar[key]
|
---|
136 | }
|
---|
137 | })(Prism)
|
---|
138 | }
|
---|