clojure.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Language: Clojure
  3. Description: Clojure syntax (based on lisp.js)
  4. Author: mfornos
  5. Website: https://clojure.org
  6. Category: lisp
  7. */
  8. /** @type LanguageFn */
  9. function clojure(hljs) {
  10. var SYMBOLSTART = 'a-zA-Z_\\-!.?+*=<>&#\'';
  11. var SYMBOL_RE = '[' + SYMBOLSTART + '][' + SYMBOLSTART + '0-9/;:]*';
  12. var globals = 'def defonce defprotocol defstruct defmulti defmethod defn- defn defmacro deftype defrecord';
  13. var keywords = {
  14. $pattern: SYMBOL_RE,
  15. 'builtin-name':
  16. // Clojure keywords
  17. globals + ' ' +
  18. 'cond apply if-not if-let if not not= = < > <= >= == + / * - rem ' +
  19. 'quot neg? pos? delay? symbol? keyword? true? false? integer? empty? coll? list? ' +
  20. 'set? ifn? fn? associative? sequential? sorted? counted? reversible? number? decimal? ' +
  21. 'class? distinct? isa? float? rational? reduced? ratio? odd? even? char? seq? vector? ' +
  22. 'string? map? nil? contains? zero? instance? not-every? not-any? libspec? -> ->> .. . ' +
  23. 'inc compare do dotimes mapcat take remove take-while drop letfn drop-last take-last ' +
  24. 'drop-while while intern condp case reduced cycle split-at split-with repeat replicate ' +
  25. 'iterate range merge zipmap declare line-seq sort comparator sort-by dorun doall nthnext ' +
  26. 'nthrest partition eval doseq await await-for let agent atom send send-off release-pending-sends ' +
  27. 'add-watch mapv filterv remove-watch agent-error restart-agent set-error-handler error-handler ' +
  28. 'set-error-mode! error-mode shutdown-agents quote var fn loop recur throw try monitor-enter ' +
  29. 'monitor-exit macroexpand macroexpand-1 for dosync and or ' +
  30. 'when when-not when-let comp juxt partial sequence memoize constantly complement identity assert ' +
  31. 'peek pop doto proxy first rest cons cast coll last butlast ' +
  32. 'sigs reify second ffirst fnext nfirst nnext meta with-meta ns in-ns create-ns import ' +
  33. 'refer keys select-keys vals key val rseq name namespace promise into transient persistent! conj! ' +
  34. 'assoc! dissoc! pop! disj! use class type num float double short byte boolean bigint biginteger ' +
  35. 'bigdec print-method print-dup throw-if printf format load compile get-in update-in pr pr-on newline ' +
  36. 'flush read slurp read-line subvec with-open memfn time re-find re-groups rand-int rand mod locking ' +
  37. 'assert-valid-fdecl alias resolve ref deref refset swap! reset! set-validator! compare-and-set! alter-meta! ' +
  38. 'reset-meta! commute get-validator alter ref-set ref-history-count ref-min-history ref-max-history ensure sync io! ' +
  39. 'new next conj set! to-array future future-call into-array aset gen-class reduce map filter find empty ' +
  40. 'hash-map hash-set sorted-map sorted-map-by sorted-set sorted-set-by vec vector seq flatten reverse assoc dissoc list ' +
  41. 'disj get union difference intersection extend extend-type extend-protocol int nth delay count concat chunk chunk-buffer ' +
  42. 'chunk-append chunk-first chunk-rest max min dec unchecked-inc-int unchecked-inc unchecked-dec-inc unchecked-dec unchecked-negate ' +
  43. 'unchecked-add-int unchecked-add unchecked-subtract-int unchecked-subtract chunk-next chunk-cons chunked-seq? prn vary-meta ' +
  44. 'lazy-seq spread list* str find-keyword keyword symbol gensym force rationalize'
  45. };
  46. var SIMPLE_NUMBER_RE = '[-+]?\\d+(\\.\\d+)?';
  47. var SYMBOL = {
  48. begin: SYMBOL_RE,
  49. relevance: 0
  50. };
  51. var NUMBER = {
  52. className: 'number', begin: SIMPLE_NUMBER_RE,
  53. relevance: 0
  54. };
  55. var STRING = hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null});
  56. var COMMENT = hljs.COMMENT(
  57. ';',
  58. '$',
  59. {
  60. relevance: 0
  61. }
  62. );
  63. var LITERAL = {
  64. className: 'literal',
  65. begin: /\b(true|false|nil)\b/
  66. };
  67. var COLLECTION = {
  68. begin: '[\\[\\{]', end: '[\\]\\}]'
  69. };
  70. var HINT = {
  71. className: 'comment',
  72. begin: '\\^' + SYMBOL_RE
  73. };
  74. var HINT_COL = hljs.COMMENT('\\^\\{', '\\}');
  75. var KEY = {
  76. className: 'symbol',
  77. begin: '[:]{1,2}' + SYMBOL_RE
  78. };
  79. var LIST = {
  80. begin: '\\(', end: '\\)'
  81. };
  82. var BODY = {
  83. endsWithParent: true,
  84. relevance: 0
  85. };
  86. var NAME = {
  87. keywords: keywords,
  88. className: 'name', begin: SYMBOL_RE,
  89. starts: BODY
  90. };
  91. var DEFAULT_CONTAINS = [LIST, STRING, HINT, HINT_COL, COMMENT, KEY, COLLECTION, NUMBER, LITERAL, SYMBOL];
  92. var GLOBAL = {
  93. beginKeywords: globals,
  94. lexemes: SYMBOL_RE,
  95. end: '(\\[|\\#|\\d|"|:|\\{|\\)|\\(|$)',
  96. contains: [
  97. {
  98. className: 'title',
  99. begin: SYMBOL_RE,
  100. relevance: 0,
  101. excludeEnd: true,
  102. // we can only have a single title
  103. endsParent: true
  104. },
  105. ].concat(DEFAULT_CONTAINS)
  106. };
  107. LIST.contains = [hljs.COMMENT('comment', ''), GLOBAL, NAME, BODY];
  108. BODY.contains = DEFAULT_CONTAINS;
  109. COLLECTION.contains = DEFAULT_CONTAINS;
  110. HINT_COL.contains = [COLLECTION];
  111. return {
  112. name: 'Clojure',
  113. aliases: ['clj'],
  114. illegal: /\S/,
  115. contains: [LIST, STRING, HINT, HINT_COL, COMMENT, KEY, COLLECTION, NUMBER, LITERAL]
  116. };
  117. }
  118. module.exports = clojure;