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