scheme.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. 'builtin-name':
  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. {
  68. begin: '#b[0-1]+(/[0-1]+)?'
  69. },
  70. {
  71. begin: '#o[0-7]+(/[0-7]+)?'
  72. },
  73. {
  74. begin: '#x[0-9a-f]+(/[0-9a-f]+)?'
  75. }
  76. ]
  77. };
  78. const STRING = hljs.QUOTE_STRING_MODE;
  79. const COMMENT_MODES = [
  80. hljs.COMMENT(
  81. ';',
  82. '$',
  83. {
  84. relevance: 0
  85. }
  86. ),
  87. hljs.COMMENT('#\\|', '\\|#')
  88. ];
  89. const IDENT = {
  90. begin: SCHEME_IDENT_RE,
  91. relevance: 0
  92. };
  93. const QUOTED_IDENT = {
  94. className: 'symbol',
  95. begin: '\'' + SCHEME_IDENT_RE
  96. };
  97. const BODY = {
  98. endsWithParent: true,
  99. relevance: 0
  100. };
  101. const QUOTED_LIST = {
  102. variants: [
  103. {
  104. begin: /'/
  105. },
  106. {
  107. begin: '`'
  108. }
  109. ],
  110. contains: [
  111. {
  112. begin: '\\(',
  113. end: '\\)',
  114. contains: [
  115. 'self',
  116. LITERAL,
  117. STRING,
  118. NUMBER,
  119. IDENT,
  120. QUOTED_IDENT
  121. ]
  122. }
  123. ]
  124. };
  125. const NAME = {
  126. className: 'name',
  127. relevance: 0,
  128. begin: SCHEME_IDENT_RE,
  129. keywords: KEYWORDS
  130. };
  131. const LAMBDA = {
  132. begin: /lambda/,
  133. endsWithParent: true,
  134. returnBegin: true,
  135. contains: [
  136. NAME,
  137. {
  138. endsParent: true,
  139. variants: [
  140. {
  141. begin: /\(/,
  142. end: /\)/
  143. },
  144. {
  145. begin: /\[/,
  146. end: /\]/
  147. }
  148. ],
  149. contains: [ IDENT ]
  150. }
  151. ]
  152. };
  153. const LIST = {
  154. variants: [
  155. {
  156. begin: '\\(',
  157. end: '\\)'
  158. },
  159. {
  160. begin: '\\[',
  161. end: '\\]'
  162. }
  163. ],
  164. contains: [
  165. LAMBDA,
  166. NAME,
  167. BODY
  168. ]
  169. };
  170. BODY.contains = [
  171. LITERAL,
  172. NUMBER,
  173. STRING,
  174. IDENT,
  175. QUOTED_IDENT,
  176. QUOTED_LIST,
  177. LIST
  178. ].concat(COMMENT_MODES);
  179. return {
  180. name: 'Scheme',
  181. illegal: /\S/,
  182. contains: [
  183. hljs.SHEBANG(),
  184. NUMBER,
  185. STRING,
  186. QUOTED_IDENT,
  187. QUOTED_LIST,
  188. LIST
  189. ].concat(COMMENT_MODES)
  190. };
  191. }
  192. module.exports = scheme;