scheme.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. module.exports = function(hljs) {
  2. var SCHEME_IDENT_RE = '[^\\(\\)\\[\\]\\{\\}",\'`;#|\\\\\\s]+';
  3. var SCHEME_SIMPLE_NUMBER_RE = '(\\-|\\+)?\\d+([./]\\d+)?';
  4. var SCHEME_COMPLEX_NUMBER_RE = SCHEME_SIMPLE_NUMBER_RE + '[+\\-]' + SCHEME_SIMPLE_NUMBER_RE + 'i';
  5. var BUILTINS = {
  6. 'builtin-name':
  7. 'case-lambda call/cc class define-class exit-handler field import ' +
  8. 'inherit init-field interface let*-values let-values let/ec mixin ' +
  9. 'opt-lambda override protect provide public rename require ' +
  10. 'require-for-syntax syntax syntax-case syntax-error unit/sig unless ' +
  11. 'when with-syntax and begin call-with-current-continuation ' +
  12. 'call-with-input-file call-with-output-file case cond define ' +
  13. 'define-syntax delay do dynamic-wind else for-each if lambda let let* ' +
  14. 'let-syntax letrec letrec-syntax map or syntax-rules \' * + , ,@ - ... / ' +
  15. '; < <= = => > >= ` abs acos angle append apply asin assoc assq assv atan ' +
  16. 'boolean? caar cadr call-with-input-file call-with-output-file ' +
  17. 'call-with-values car cdddar cddddr cdr ceiling char->integer ' +
  18. 'char-alphabetic? char-ci<=? char-ci<? char-ci=? char-ci>=? char-ci>? ' +
  19. 'char-downcase char-lower-case? char-numeric? char-ready? char-upcase ' +
  20. 'char-upper-case? char-whitespace? char<=? char<? char=? char>=? char>? ' +
  21. 'char? close-input-port close-output-port complex? cons cos ' +
  22. 'current-input-port current-output-port denominator display eof-object? ' +
  23. 'eq? equal? eqv? eval even? exact->inexact exact? exp expt floor ' +
  24. 'force gcd imag-part inexact->exact inexact? input-port? integer->char ' +
  25. 'integer? interaction-environment lcm length list list->string ' +
  26. 'list->vector list-ref list-tail list? load log magnitude make-polar ' +
  27. 'make-rectangular make-string make-vector max member memq memv min ' +
  28. 'modulo negative? newline not null-environment null? number->string ' +
  29. 'number? numerator odd? open-input-file open-output-file output-port? ' +
  30. 'pair? peek-char port? positive? procedure? quasiquote quote quotient ' +
  31. 'rational? rationalize read read-char real-part real? remainder reverse ' +
  32. 'round scheme-report-environment set! set-car! set-cdr! sin sqrt string ' +
  33. 'string->list string->number string->symbol string-append string-ci<=? ' +
  34. 'string-ci<? string-ci=? string-ci>=? string-ci>? string-copy ' +
  35. 'string-fill! string-length string-ref string-set! string<=? string<? ' +
  36. 'string=? string>=? string>? string? substring symbol->string symbol? ' +
  37. 'tan transcript-off transcript-on truncate values vector ' +
  38. 'vector->list vector-fill! vector-length vector-ref vector-set! ' +
  39. 'with-input-from-file with-output-to-file write write-char zero?'
  40. };
  41. var SHEBANG = {
  42. className: 'meta',
  43. begin: '^#!',
  44. end: '$'
  45. };
  46. var LITERAL = {
  47. className: 'literal',
  48. begin: '(#t|#f|#\\\\' + SCHEME_IDENT_RE + '|#\\\\.)'
  49. };
  50. var NUMBER = {
  51. className: 'number',
  52. variants: [
  53. { begin: SCHEME_SIMPLE_NUMBER_RE, relevance: 0 },
  54. { begin: SCHEME_COMPLEX_NUMBER_RE, relevance: 0 },
  55. { begin: '#b[0-1]+(/[0-1]+)?' },
  56. { begin: '#o[0-7]+(/[0-7]+)?' },
  57. { begin: '#x[0-9a-f]+(/[0-9a-f]+)?' }
  58. ]
  59. };
  60. var STRING = hljs.QUOTE_STRING_MODE;
  61. var REGULAR_EXPRESSION = {
  62. className: 'regexp',
  63. begin: '#[pr]x"',
  64. end: '[^\\\\]"'
  65. };
  66. var COMMENT_MODES = [
  67. hljs.COMMENT(
  68. ';',
  69. '$',
  70. {
  71. relevance: 0
  72. }
  73. ),
  74. hljs.COMMENT('#\\|', '\\|#')
  75. ];
  76. var IDENT = {
  77. begin: SCHEME_IDENT_RE,
  78. relevance: 0
  79. };
  80. var QUOTED_IDENT = {
  81. className: 'symbol',
  82. begin: '\'' + SCHEME_IDENT_RE
  83. };
  84. var BODY = {
  85. endsWithParent: true,
  86. relevance: 0
  87. };
  88. var QUOTED_LIST = {
  89. variants: [
  90. { begin: /'/ },
  91. { begin: '`' }
  92. ],
  93. contains: [
  94. {
  95. begin: '\\(', end: '\\)',
  96. contains: ['self', LITERAL, STRING, NUMBER, IDENT, QUOTED_IDENT]
  97. }
  98. ]
  99. };
  100. var NAME = {
  101. className: 'name',
  102. begin: SCHEME_IDENT_RE,
  103. lexemes: SCHEME_IDENT_RE,
  104. keywords: BUILTINS
  105. };
  106. var LAMBDA = {
  107. begin: /lambda/, endsWithParent: true, returnBegin: true,
  108. contains: [
  109. NAME,
  110. {
  111. begin: /\(/, end: /\)/, endsParent: true,
  112. contains: [IDENT],
  113. }
  114. ]
  115. };
  116. var LIST = {
  117. variants: [
  118. { begin: '\\(', end: '\\)' },
  119. { begin: '\\[', end: '\\]' }
  120. ],
  121. contains: [LAMBDA, NAME, BODY]
  122. };
  123. BODY.contains = [LITERAL, NUMBER, STRING, IDENT, QUOTED_IDENT, QUOTED_LIST, LIST].concat(COMMENT_MODES);
  124. return {
  125. illegal: /\S/,
  126. contains: [SHEBANG, NUMBER, STRING, QUOTED_IDENT, QUOTED_LIST, LIST].concat(COMMENT_MODES)
  127. };
  128. };