scheme.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Language: Scheme
  3. Description: Scheme is a programming language in the Lisp family.
  4. (keywords based on http://community.schemewiki.org/?scheme-keywords)
  5. Author: JP Verkamp <me@jverkamp.com>
  6. Contributors: Ivan Sagalaev <maniac@softwaremaniacs.org>
  7. Origin: clojure.js
  8. Website: http://community.schemewiki.org/?what-is-scheme
  9. Category: lisp
  10. */
  11. function scheme(hljs) {
  12. const SCHEME_IDENT_RE = '[^\\(\\)\\[\\]\\{\\}",\'`;#|\\\\\\s]+';
  13. const SCHEME_SIMPLE_NUMBER_RE = '(-|\\+)?\\d+([./]\\d+)?';
  14. const SCHEME_COMPLEX_NUMBER_RE = SCHEME_SIMPLE_NUMBER_RE + '[+\\-]' + SCHEME_SIMPLE_NUMBER_RE + 'i';
  15. const KEYWORDS = {
  16. $pattern: SCHEME_IDENT_RE,
  17. built_in:
  18. 'case-lambda call/cc class define-class exit-handler field import '
  19. + 'inherit init-field interface let*-values let-values let/ec mixin '
  20. + 'opt-lambda override protect provide public rename require '
  21. + 'require-for-syntax syntax syntax-case syntax-error unit/sig unless '
  22. + 'when with-syntax and begin call-with-current-continuation '
  23. + 'call-with-input-file call-with-output-file case cond define '
  24. + 'define-syntax delay do dynamic-wind else for-each if lambda let let* '
  25. + 'let-syntax letrec letrec-syntax map or syntax-rules \' * + , ,@ - ... / '
  26. + '; < <= = => > >= ` abs acos angle append apply asin assoc assq assv atan '
  27. + 'boolean? caar cadr call-with-input-file call-with-output-file '
  28. + 'call-with-values car cdddar cddddr cdr ceiling char->integer '
  29. + 'char-alphabetic? char-ci<=? char-ci<? char-ci=? char-ci>=? char-ci>? '
  30. + 'char-downcase char-lower-case? char-numeric? char-ready? char-upcase '
  31. + 'char-upper-case? char-whitespace? char<=? char<? char=? char>=? char>? '
  32. + 'char? close-input-port close-output-port complex? cons cos '
  33. + 'current-input-port current-output-port denominator display eof-object? '
  34. + 'eq? equal? eqv? eval even? exact->inexact exact? exp expt floor '
  35. + 'force gcd imag-part inexact->exact inexact? input-port? integer->char '
  36. + 'integer? interaction-environment lcm length list list->string '
  37. + 'list->vector list-ref list-tail list? load log magnitude make-polar '
  38. + 'make-rectangular make-string make-vector max member memq memv min '
  39. + 'modulo negative? newline not null-environment null? number->string '
  40. + 'number? numerator odd? open-input-file open-output-file output-port? '
  41. + 'pair? peek-char port? positive? procedure? quasiquote quote quotient '
  42. + 'rational? rationalize read read-char real-part real? remainder reverse '
  43. + 'round scheme-report-environment set! set-car! set-cdr! sin sqrt string '
  44. + 'string->list string->number string->symbol string-append string-ci<=? '
  45. + 'string-ci<? string-ci=? string-ci>=? string-ci>? string-copy '
  46. + 'string-fill! string-length string-ref string-set! string<=? string<? '
  47. + 'string=? string>=? string>? string? substring symbol->string symbol? '
  48. + 'tan transcript-off transcript-on truncate values vector '
  49. + 'vector->list vector-fill! vector-length vector-ref vector-set! '
  50. + 'with-input-from-file with-output-to-file write write-char zero?'
  51. };
  52. const LITERAL = {
  53. className: 'literal',
  54. begin: '(#t|#f|#\\\\' + SCHEME_IDENT_RE + '|#\\\\.)'
  55. };
  56. const NUMBER = {
  57. className: 'number',
  58. variants: [
  59. {
  60. begin: SCHEME_SIMPLE_NUMBER_RE,
  61. relevance: 0
  62. },
  63. {
  64. begin: SCHEME_COMPLEX_NUMBER_RE,
  65. relevance: 0
  66. },
  67. { begin: '#b[0-1]+(/[0-1]+)?' },
  68. { begin: '#o[0-7]+(/[0-7]+)?' },
  69. { begin: '#x[0-9a-f]+(/[0-9a-f]+)?' }
  70. ]
  71. };
  72. const STRING = hljs.QUOTE_STRING_MODE;
  73. const COMMENT_MODES = [
  74. hljs.COMMENT(
  75. ';',
  76. '$',
  77. { relevance: 0 }
  78. ),
  79. hljs.COMMENT('#\\|', '\\|#')
  80. ];
  81. const IDENT = {
  82. begin: SCHEME_IDENT_RE,
  83. relevance: 0
  84. };
  85. const QUOTED_IDENT = {
  86. className: 'symbol',
  87. begin: '\'' + SCHEME_IDENT_RE
  88. };
  89. const BODY = {
  90. endsWithParent: true,
  91. relevance: 0
  92. };
  93. const QUOTED_LIST = {
  94. variants: [
  95. { begin: /'/ },
  96. { begin: '`' }
  97. ],
  98. contains: [
  99. {
  100. begin: '\\(',
  101. end: '\\)',
  102. contains: [
  103. 'self',
  104. LITERAL,
  105. STRING,
  106. NUMBER,
  107. IDENT,
  108. QUOTED_IDENT
  109. ]
  110. }
  111. ]
  112. };
  113. const NAME = {
  114. className: 'name',
  115. relevance: 0,
  116. begin: SCHEME_IDENT_RE,
  117. keywords: KEYWORDS
  118. };
  119. const LAMBDA = {
  120. begin: /lambda/,
  121. endsWithParent: true,
  122. returnBegin: true,
  123. contains: [
  124. NAME,
  125. {
  126. endsParent: true,
  127. variants: [
  128. {
  129. begin: /\(/,
  130. end: /\)/
  131. },
  132. {
  133. begin: /\[/,
  134. end: /\]/
  135. }
  136. ],
  137. contains: [ IDENT ]
  138. }
  139. ]
  140. };
  141. const LIST = {
  142. variants: [
  143. {
  144. begin: '\\(',
  145. end: '\\)'
  146. },
  147. {
  148. begin: '\\[',
  149. end: '\\]'
  150. }
  151. ],
  152. contains: [
  153. LAMBDA,
  154. NAME,
  155. BODY
  156. ]
  157. };
  158. BODY.contains = [
  159. LITERAL,
  160. NUMBER,
  161. STRING,
  162. IDENT,
  163. QUOTED_IDENT,
  164. QUOTED_LIST,
  165. LIST
  166. ].concat(COMMENT_MODES);
  167. return {
  168. name: 'Scheme',
  169. aliases: ['scm'],
  170. illegal: /\S/,
  171. contains: [
  172. hljs.SHEBANG(),
  173. NUMBER,
  174. STRING,
  175. QUOTED_IDENT,
  176. QUOTED_LIST,
  177. LIST
  178. ].concat(COMMENT_MODES)
  179. };
  180. }
  181. export { scheme as default };