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