clojure.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. const SYMBOLSTART = 'a-zA-Z_\\-!.?+*=<>&#\'';
  11. const SYMBOL_RE = '[' + SYMBOLSTART + '][' + SYMBOLSTART + '0-9/;:]*';
  12. const globals = 'def defonce defprotocol defstruct defmulti defmethod defn- defn defmacro deftype defrecord';
  13. const keywords = {
  14. $pattern: SYMBOL_RE,
  15. 'builtin-name':
  16. // Clojure keywords
  17. globals + ' ' +
  18. 'cond apply if-not if-let if not not= =|0 <|0 >|0 <=|0 >=|0 ==|0 +|0 /|0 *|0 -|0 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. const SIMPLE_NUMBER_RE = '[-+]?\\d+(\\.\\d+)?';
  47. const SYMBOL = {
  48. begin: SYMBOL_RE,
  49. relevance: 0
  50. };
  51. const NUMBER = {
  52. className: 'number',
  53. begin: SIMPLE_NUMBER_RE,
  54. relevance: 0
  55. };
  56. const STRING = hljs.inherit(hljs.QUOTE_STRING_MODE, {
  57. illegal: null
  58. });
  59. const COMMENT = hljs.COMMENT(
  60. ';',
  61. '$',
  62. {
  63. relevance: 0
  64. }
  65. );
  66. const LITERAL = {
  67. className: 'literal',
  68. begin: /\b(true|false|nil)\b/
  69. };
  70. const COLLECTION = {
  71. begin: '[\\[\\{]',
  72. end: '[\\]\\}]'
  73. };
  74. const HINT = {
  75. className: 'comment',
  76. begin: '\\^' + SYMBOL_RE
  77. };
  78. const HINT_COL = hljs.COMMENT('\\^\\{', '\\}');
  79. const KEY = {
  80. className: 'symbol',
  81. begin: '[:]{1,2}' + SYMBOL_RE
  82. };
  83. const LIST = {
  84. begin: '\\(',
  85. end: '\\)'
  86. };
  87. const BODY = {
  88. endsWithParent: true,
  89. relevance: 0
  90. };
  91. const NAME = {
  92. keywords: keywords,
  93. className: 'name',
  94. begin: SYMBOL_RE,
  95. relevance: 0,
  96. starts: BODY
  97. };
  98. const DEFAULT_CONTAINS = [
  99. LIST,
  100. STRING,
  101. HINT,
  102. HINT_COL,
  103. COMMENT,
  104. KEY,
  105. COLLECTION,
  106. NUMBER,
  107. LITERAL,
  108. SYMBOL
  109. ];
  110. const GLOBAL = {
  111. beginKeywords: globals,
  112. lexemes: SYMBOL_RE,
  113. end: '(\\[|#|\\d|"|:|\\{|\\)|\\(|$)',
  114. contains: [
  115. {
  116. className: 'title',
  117. begin: SYMBOL_RE,
  118. relevance: 0,
  119. excludeEnd: true,
  120. // we can only have a single title
  121. endsParent: true
  122. }
  123. ].concat(DEFAULT_CONTAINS)
  124. };
  125. LIST.contains = [
  126. hljs.COMMENT('comment', ''),
  127. GLOBAL,
  128. NAME,
  129. BODY
  130. ];
  131. BODY.contains = DEFAULT_CONTAINS;
  132. COLLECTION.contains = DEFAULT_CONTAINS;
  133. HINT_COL.contains = [ COLLECTION ];
  134. return {
  135. name: 'Clojure',
  136. aliases: [ 'clj' ],
  137. illegal: /\S/,
  138. contains: [
  139. LIST,
  140. STRING,
  141. HINT,
  142. HINT_COL,
  143. COMMENT,
  144. KEY,
  145. COLLECTION,
  146. NUMBER,
  147. LITERAL
  148. ]
  149. };
  150. }
  151. module.exports = clojure;