clojure.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. ;(function (mod) {
  4. if (typeof exports === 'object' && typeof module === 'object')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'))
  7. else if (typeof define === 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. CodeMirror.defineMode('clojure', function (options) {
  15. var atoms = ['false', 'nil', 'true']
  16. var specialForms = ['.', 'catch', 'def', 'do', 'if', 'monitor-enter', 'monitor-exit', 'new', 'quote', 'recur', 'set!', 'throw', 'try', 'var']
  17. var coreSymbols = [
  18. '*',
  19. "*'",
  20. '*1',
  21. '*2',
  22. '*3',
  23. '*agent*',
  24. '*allow-unresolved-vars*',
  25. '*assert*',
  26. '*clojure-version*',
  27. '*command-line-args*',
  28. '*compile-files*',
  29. '*compile-path*',
  30. '*compiler-options*',
  31. '*data-readers*',
  32. '*default-data-reader-fn*',
  33. '*e',
  34. '*err*',
  35. '*file*',
  36. '*flush-on-newline*',
  37. '*fn-loader*',
  38. '*in*',
  39. '*math-context*',
  40. '*ns*',
  41. '*out*',
  42. '*print-dup*',
  43. '*print-length*',
  44. '*print-level*',
  45. '*print-meta*',
  46. '*print-namespace-maps*',
  47. '*print-readably*',
  48. '*read-eval*',
  49. '*reader-resolver*',
  50. '*source-path*',
  51. '*suppress-read*',
  52. '*unchecked-math*',
  53. '*use-context-classloader*',
  54. '*verbose-defrecords*',
  55. '*warn-on-reflection*',
  56. '+',
  57. "+'",
  58. '-',
  59. "-'",
  60. '->',
  61. '->>',
  62. '->ArrayChunk',
  63. '->Eduction',
  64. '->Vec',
  65. '->VecNode',
  66. '->VecSeq',
  67. '-cache-protocol-fn',
  68. '-reset-methods',
  69. '..',
  70. '/',
  71. '<',
  72. '<=',
  73. '=',
  74. '==',
  75. '>',
  76. '>=',
  77. 'EMPTY-NODE',
  78. 'Inst',
  79. 'StackTraceElement->vec',
  80. 'Throwable->map',
  81. 'accessor',
  82. 'aclone',
  83. 'add-classpath',
  84. 'add-watch',
  85. 'agent',
  86. 'agent-error',
  87. 'agent-errors',
  88. 'aget',
  89. 'alength',
  90. 'alias',
  91. 'all-ns',
  92. 'alter',
  93. 'alter-meta!',
  94. 'alter-var-root',
  95. 'amap',
  96. 'ancestors',
  97. 'and',
  98. 'any?',
  99. 'apply',
  100. 'areduce',
  101. 'array-map',
  102. 'as->',
  103. 'aset',
  104. 'aset-boolean',
  105. 'aset-byte',
  106. 'aset-char',
  107. 'aset-double',
  108. 'aset-float',
  109. 'aset-int',
  110. 'aset-long',
  111. 'aset-short',
  112. 'assert',
  113. 'assoc',
  114. 'assoc!',
  115. 'assoc-in',
  116. 'associative?',
  117. 'atom',
  118. 'await',
  119. 'await-for',
  120. 'await1',
  121. 'bases',
  122. 'bean',
  123. 'bigdec',
  124. 'bigint',
  125. 'biginteger',
  126. 'binding',
  127. 'bit-and',
  128. 'bit-and-not',
  129. 'bit-clear',
  130. 'bit-flip',
  131. 'bit-not',
  132. 'bit-or',
  133. 'bit-set',
  134. 'bit-shift-left',
  135. 'bit-shift-right',
  136. 'bit-test',
  137. 'bit-xor',
  138. 'boolean',
  139. 'boolean-array',
  140. 'boolean?',
  141. 'booleans',
  142. 'bound-fn',
  143. 'bound-fn*',
  144. 'bound?',
  145. 'bounded-count',
  146. 'butlast',
  147. 'byte',
  148. 'byte-array',
  149. 'bytes',
  150. 'bytes?',
  151. 'case',
  152. 'cast',
  153. 'cat',
  154. 'char',
  155. 'char-array',
  156. 'char-escape-string',
  157. 'char-name-string',
  158. 'char?',
  159. 'chars',
  160. 'chunk',
  161. 'chunk-append',
  162. 'chunk-buffer',
  163. 'chunk-cons',
  164. 'chunk-first',
  165. 'chunk-next',
  166. 'chunk-rest',
  167. 'chunked-seq?',
  168. 'class',
  169. 'class?',
  170. 'clear-agent-errors',
  171. 'clojure-version',
  172. 'coll?',
  173. 'comment',
  174. 'commute',
  175. 'comp',
  176. 'comparator',
  177. 'compare',
  178. 'compare-and-set!',
  179. 'compile',
  180. 'complement',
  181. 'completing',
  182. 'concat',
  183. 'cond',
  184. 'cond->',
  185. 'cond->>',
  186. 'condp',
  187. 'conj',
  188. 'conj!',
  189. 'cons',
  190. 'constantly',
  191. 'construct-proxy',
  192. 'contains?',
  193. 'count',
  194. 'counted?',
  195. 'create-ns',
  196. 'create-struct',
  197. 'cycle',
  198. 'dec',
  199. "dec'",
  200. 'decimal?',
  201. 'declare',
  202. 'dedupe',
  203. 'default-data-readers',
  204. 'definline',
  205. 'definterface',
  206. 'defmacro',
  207. 'defmethod',
  208. 'defmulti',
  209. 'defn',
  210. 'defn-',
  211. 'defonce',
  212. 'defprotocol',
  213. 'defrecord',
  214. 'defstruct',
  215. 'deftype',
  216. 'delay',
  217. 'delay?',
  218. 'deliver',
  219. 'denominator',
  220. 'deref',
  221. 'derive',
  222. 'descendants',
  223. 'destructure',
  224. 'disj',
  225. 'disj!',
  226. 'dissoc',
  227. 'dissoc!',
  228. 'distinct',
  229. 'distinct?',
  230. 'doall',
  231. 'dorun',
  232. 'doseq',
  233. 'dosync',
  234. 'dotimes',
  235. 'doto',
  236. 'double',
  237. 'double-array',
  238. 'double?',
  239. 'doubles',
  240. 'drop',
  241. 'drop-last',
  242. 'drop-while',
  243. 'eduction',
  244. 'empty',
  245. 'empty?',
  246. 'ensure',
  247. 'ensure-reduced',
  248. 'enumeration-seq',
  249. 'error-handler',
  250. 'error-mode',
  251. 'eval',
  252. 'even?',
  253. 'every-pred',
  254. 'every?',
  255. 'ex-data',
  256. 'ex-info',
  257. 'extend',
  258. 'extend-protocol',
  259. 'extend-type',
  260. 'extenders',
  261. 'extends?',
  262. 'false?',
  263. 'ffirst',
  264. 'file-seq',
  265. 'filter',
  266. 'filterv',
  267. 'find',
  268. 'find-keyword',
  269. 'find-ns',
  270. 'find-protocol-impl',
  271. 'find-protocol-method',
  272. 'find-var',
  273. 'first',
  274. 'flatten',
  275. 'float',
  276. 'float-array',
  277. 'float?',
  278. 'floats',
  279. 'flush',
  280. 'fn',
  281. 'fn?',
  282. 'fnext',
  283. 'fnil',
  284. 'for',
  285. 'force',
  286. 'format',
  287. 'frequencies',
  288. 'future',
  289. 'future-call',
  290. 'future-cancel',
  291. 'future-cancelled?',
  292. 'future-done?',
  293. 'future?',
  294. 'gen-class',
  295. 'gen-interface',
  296. 'gensym',
  297. 'get',
  298. 'get-in',
  299. 'get-method',
  300. 'get-proxy-class',
  301. 'get-thread-bindings',
  302. 'get-validator',
  303. 'group-by',
  304. 'halt-when',
  305. 'hash',
  306. 'hash-combine',
  307. 'hash-map',
  308. 'hash-ordered-coll',
  309. 'hash-set',
  310. 'hash-unordered-coll',
  311. 'ident?',
  312. 'identical?',
  313. 'identity',
  314. 'if-let',
  315. 'if-not',
  316. 'if-some',
  317. 'ifn?',
  318. 'import',
  319. 'in-ns',
  320. 'inc',
  321. "inc'",
  322. 'indexed?',
  323. 'init-proxy',
  324. 'inst-ms',
  325. 'inst-ms*',
  326. 'inst?',
  327. 'instance?',
  328. 'int',
  329. 'int-array',
  330. 'int?',
  331. 'integer?',
  332. 'interleave',
  333. 'intern',
  334. 'interpose',
  335. 'into',
  336. 'into-array',
  337. 'ints',
  338. 'io!',
  339. 'isa?',
  340. 'iterate',
  341. 'iterator-seq',
  342. 'juxt',
  343. 'keep',
  344. 'keep-indexed',
  345. 'key',
  346. 'keys',
  347. 'keyword',
  348. 'keyword?',
  349. 'last',
  350. 'lazy-cat',
  351. 'lazy-seq',
  352. 'let',
  353. 'letfn',
  354. 'line-seq',
  355. 'list',
  356. 'list*',
  357. 'list?',
  358. 'load',
  359. 'load-file',
  360. 'load-reader',
  361. 'load-string',
  362. 'loaded-libs',
  363. 'locking',
  364. 'long',
  365. 'long-array',
  366. 'longs',
  367. 'loop',
  368. 'macroexpand',
  369. 'macroexpand-1',
  370. 'make-array',
  371. 'make-hierarchy',
  372. 'map',
  373. 'map-entry?',
  374. 'map-indexed',
  375. 'map?',
  376. 'mapcat',
  377. 'mapv',
  378. 'max',
  379. 'max-key',
  380. 'memfn',
  381. 'memoize',
  382. 'merge',
  383. 'merge-with',
  384. 'meta',
  385. 'method-sig',
  386. 'methods',
  387. 'min',
  388. 'min-key',
  389. 'mix-collection-hash',
  390. 'mod',
  391. 'munge',
  392. 'name',
  393. 'namespace',
  394. 'namespace-munge',
  395. 'nat-int?',
  396. 'neg-int?',
  397. 'neg?',
  398. 'newline',
  399. 'next',
  400. 'nfirst',
  401. 'nil?',
  402. 'nnext',
  403. 'not',
  404. 'not-any?',
  405. 'not-empty',
  406. 'not-every?',
  407. 'not=',
  408. 'ns',
  409. 'ns-aliases',
  410. 'ns-imports',
  411. 'ns-interns',
  412. 'ns-map',
  413. 'ns-name',
  414. 'ns-publics',
  415. 'ns-refers',
  416. 'ns-resolve',
  417. 'ns-unalias',
  418. 'ns-unmap',
  419. 'nth',
  420. 'nthnext',
  421. 'nthrest',
  422. 'num',
  423. 'number?',
  424. 'numerator',
  425. 'object-array',
  426. 'odd?',
  427. 'or',
  428. 'parents',
  429. 'partial',
  430. 'partition',
  431. 'partition-all',
  432. 'partition-by',
  433. 'pcalls',
  434. 'peek',
  435. 'persistent!',
  436. 'pmap',
  437. 'pop',
  438. 'pop!',
  439. 'pop-thread-bindings',
  440. 'pos-int?',
  441. 'pos?',
  442. 'pr',
  443. 'pr-str',
  444. 'prefer-method',
  445. 'prefers',
  446. 'primitives-classnames',
  447. 'print',
  448. 'print-ctor',
  449. 'print-dup',
  450. 'print-method',
  451. 'print-simple',
  452. 'print-str',
  453. 'printf',
  454. 'println',
  455. 'println-str',
  456. 'prn',
  457. 'prn-str',
  458. 'promise',
  459. 'proxy',
  460. 'proxy-call-with-super',
  461. 'proxy-mappings',
  462. 'proxy-name',
  463. 'proxy-super',
  464. 'push-thread-bindings',
  465. 'pvalues',
  466. 'qualified-ident?',
  467. 'qualified-keyword?',
  468. 'qualified-symbol?',
  469. 'quot',
  470. 'rand',
  471. 'rand-int',
  472. 'rand-nth',
  473. 'random-sample',
  474. 'range',
  475. 'ratio?',
  476. 'rational?',
  477. 'rationalize',
  478. 're-find',
  479. 're-groups',
  480. 're-matcher',
  481. 're-matches',
  482. 're-pattern',
  483. 're-seq',
  484. 'read',
  485. 'read-line',
  486. 'read-string',
  487. 'reader-conditional',
  488. 'reader-conditional?',
  489. 'realized?',
  490. 'record?',
  491. 'reduce',
  492. 'reduce-kv',
  493. 'reduced',
  494. 'reduced?',
  495. 'reductions',
  496. 'ref',
  497. 'ref-history-count',
  498. 'ref-max-history',
  499. 'ref-min-history',
  500. 'ref-set',
  501. 'refer',
  502. 'refer-clojure',
  503. 'reify',
  504. 'release-pending-sends',
  505. 'rem',
  506. 'remove',
  507. 'remove-all-methods',
  508. 'remove-method',
  509. 'remove-ns',
  510. 'remove-watch',
  511. 'repeat',
  512. 'repeatedly',
  513. 'replace',
  514. 'replicate',
  515. 'require',
  516. 'reset!',
  517. 'reset-meta!',
  518. 'reset-vals!',
  519. 'resolve',
  520. 'rest',
  521. 'restart-agent',
  522. 'resultset-seq',
  523. 'reverse',
  524. 'reversible?',
  525. 'rseq',
  526. 'rsubseq',
  527. 'run!',
  528. 'satisfies?',
  529. 'second',
  530. 'select-keys',
  531. 'send',
  532. 'send-off',
  533. 'send-via',
  534. 'seq',
  535. 'seq?',
  536. 'seqable?',
  537. 'seque',
  538. 'sequence',
  539. 'sequential?',
  540. 'set',
  541. 'set-agent-send-executor!',
  542. 'set-agent-send-off-executor!',
  543. 'set-error-handler!',
  544. 'set-error-mode!',
  545. 'set-validator!',
  546. 'set?',
  547. 'short',
  548. 'short-array',
  549. 'shorts',
  550. 'shuffle',
  551. 'shutdown-agents',
  552. 'simple-ident?',
  553. 'simple-keyword?',
  554. 'simple-symbol?',
  555. 'slurp',
  556. 'some',
  557. 'some->',
  558. 'some->>',
  559. 'some-fn',
  560. 'some?',
  561. 'sort',
  562. 'sort-by',
  563. 'sorted-map',
  564. 'sorted-map-by',
  565. 'sorted-set',
  566. 'sorted-set-by',
  567. 'sorted?',
  568. 'special-symbol?',
  569. 'spit',
  570. 'split-at',
  571. 'split-with',
  572. 'str',
  573. 'string?',
  574. 'struct',
  575. 'struct-map',
  576. 'subs',
  577. 'subseq',
  578. 'subvec',
  579. 'supers',
  580. 'swap!',
  581. 'swap-vals!',
  582. 'symbol',
  583. 'symbol?',
  584. 'sync',
  585. 'tagged-literal',
  586. 'tagged-literal?',
  587. 'take',
  588. 'take-last',
  589. 'take-nth',
  590. 'take-while',
  591. 'test',
  592. 'the-ns',
  593. 'thread-bound?',
  594. 'time',
  595. 'to-array',
  596. 'to-array-2d',
  597. 'trampoline',
  598. 'transduce',
  599. 'transient',
  600. 'tree-seq',
  601. 'true?',
  602. 'type',
  603. 'unchecked-add',
  604. 'unchecked-add-int',
  605. 'unchecked-byte',
  606. 'unchecked-char',
  607. 'unchecked-dec',
  608. 'unchecked-dec-int',
  609. 'unchecked-divide-int',
  610. 'unchecked-double',
  611. 'unchecked-float',
  612. 'unchecked-inc',
  613. 'unchecked-inc-int',
  614. 'unchecked-int',
  615. 'unchecked-long',
  616. 'unchecked-multiply',
  617. 'unchecked-multiply-int',
  618. 'unchecked-negate',
  619. 'unchecked-negate-int',
  620. 'unchecked-remainder-int',
  621. 'unchecked-short',
  622. 'unchecked-subtract',
  623. 'unchecked-subtract-int',
  624. 'underive',
  625. 'unquote',
  626. 'unquote-splicing',
  627. 'unreduced',
  628. 'unsigned-bit-shift-right',
  629. 'update',
  630. 'update-in',
  631. 'update-proxy',
  632. 'uri?',
  633. 'use',
  634. 'uuid?',
  635. 'val',
  636. 'vals',
  637. 'var-get',
  638. 'var-set',
  639. 'var?',
  640. 'vary-meta',
  641. 'vec',
  642. 'vector',
  643. 'vector-of',
  644. 'vector?',
  645. 'volatile!',
  646. 'volatile?',
  647. 'vreset!',
  648. 'vswap!',
  649. 'when',
  650. 'when-first',
  651. 'when-let',
  652. 'when-not',
  653. 'when-some',
  654. 'while',
  655. 'with-bindings',
  656. 'with-bindings*',
  657. 'with-in-str',
  658. 'with-loading-context',
  659. 'with-local-vars',
  660. 'with-meta',
  661. 'with-open',
  662. 'with-out-str',
  663. 'with-precision',
  664. 'with-redefs',
  665. 'with-redefs-fn',
  666. 'xml-seq',
  667. 'zero?',
  668. 'zipmap',
  669. ]
  670. var haveBodyParameter = [
  671. '->',
  672. '->>',
  673. 'as->',
  674. 'binding',
  675. 'bound-fn',
  676. 'case',
  677. 'catch',
  678. 'comment',
  679. 'cond',
  680. 'cond->',
  681. 'cond->>',
  682. 'condp',
  683. 'def',
  684. 'definterface',
  685. 'defmethod',
  686. 'defn',
  687. 'defmacro',
  688. 'defprotocol',
  689. 'defrecord',
  690. 'defstruct',
  691. 'deftype',
  692. 'do',
  693. 'doseq',
  694. 'dotimes',
  695. 'doto',
  696. 'extend',
  697. 'extend-protocol',
  698. 'extend-type',
  699. 'fn',
  700. 'for',
  701. 'future',
  702. 'if',
  703. 'if-let',
  704. 'if-not',
  705. 'if-some',
  706. 'let',
  707. 'letfn',
  708. 'locking',
  709. 'loop',
  710. 'ns',
  711. 'proxy',
  712. 'reify',
  713. 'struct-map',
  714. 'some->',
  715. 'some->>',
  716. 'try',
  717. 'when',
  718. 'when-first',
  719. 'when-let',
  720. 'when-not',
  721. 'when-some',
  722. 'while',
  723. 'with-bindings',
  724. 'with-bindings*',
  725. 'with-in-str',
  726. 'with-loading-context',
  727. 'with-local-vars',
  728. 'with-meta',
  729. 'with-open',
  730. 'with-out-str',
  731. 'with-precision',
  732. 'with-redefs',
  733. 'with-redefs-fn',
  734. ]
  735. CodeMirror.registerHelper('hintWords', 'clojure', [].concat(atoms, specialForms, coreSymbols))
  736. var atom = createLookupMap(atoms)
  737. var specialForm = createLookupMap(specialForms)
  738. var coreSymbol = createLookupMap(coreSymbols)
  739. var hasBodyParameter = createLookupMap(haveBodyParameter)
  740. var delimiter = /^(?:[\\\[\]\s"(),;@^`{}~]|$)/
  741. var numberLiteral = /^(?:[+\-]?\d+(?:(?:N|(?:[eE][+\-]?\d+))|(?:\.?\d*(?:M|(?:[eE][+\-]?\d+))?)|\/\d+|[xX][0-9a-fA-F]+|r[0-9a-zA-Z]+)?(?=[\\\[\]\s"#'(),;@^`{}~]|$))/
  742. var characterLiteral = /^(?:\\(?:backspace|formfeed|newline|return|space|tab|o[0-7]{3}|u[0-9A-Fa-f]{4}|x[0-9A-Fa-f]{4}|.)?(?=[\\\[\]\s"(),;@^`{}~]|$))/
  743. // simple-namespace := /^[^\\\/\[\]\d\s"#'(),;@^`{}~.][^\\\[\]\s"(),;@^`{}~.\/]*/
  744. // simple-symbol := /^(?:\/|[^\\\/\[\]\d\s"#'(),;@^`{}~][^\\\[\]\s"(),;@^`{}~]*)/
  745. // qualified-symbol := (<simple-namespace>(<.><simple-namespace>)*</>)?<simple-symbol>
  746. var qualifiedSymbol =
  747. /^(?:(?:[^\\\/\[\]\d\s"#'(),;@^`{}~.][^\\\[\]\s"(),;@^`{}~.\/]*(?:\.[^\\\/\[\]\d\s"#'(),;@^`{}~.][^\\\[\]\s"(),;@^`{}~.\/]*)*\/)?(?:\/|[^\\\/\[\]\d\s"#'(),;@^`{}~][^\\\[\]\s"(),;@^`{}~]*)*(?=[\\\[\]\s"(),;@^`{}~]|$))/
  748. function base(stream, state) {
  749. if (stream.eatSpace() || stream.eat(',')) return ['space', null]
  750. if (stream.match(numberLiteral)) return [null, 'number']
  751. if (stream.match(characterLiteral)) return [null, 'string-2']
  752. if (stream.eat(/^"/)) return (state.tokenize = inString)(stream, state)
  753. if (stream.eat(/^[(\[{]/)) return ['open', 'bracket']
  754. if (stream.eat(/^[)\]}]/)) return ['close', 'bracket']
  755. if (stream.eat(/^;/)) {
  756. stream.skipToEnd()
  757. return ['space', 'comment']
  758. }
  759. if (stream.eat(/^[#'@^`~]/)) return [null, 'meta']
  760. var matches = stream.match(qualifiedSymbol)
  761. var symbol = matches && matches[0]
  762. if (!symbol) {
  763. // advance stream by at least one character so we don't get stuck.
  764. stream.next()
  765. stream.eatWhile(function (c) {
  766. return !is(c, delimiter)
  767. })
  768. return [null, 'error']
  769. }
  770. if (symbol === 'comment' && state.lastToken === '(') return (state.tokenize = inComment)(stream, state)
  771. if (is(symbol, atom) || symbol.charAt(0) === ':') return ['symbol', 'atom']
  772. if (is(symbol, specialForm) || is(symbol, coreSymbol)) return ['symbol', 'keyword']
  773. if (state.lastToken === '(') return ['symbol', 'builtin'] // other operator
  774. return ['symbol', 'variable']
  775. }
  776. function inString(stream, state) {
  777. var escaped = false,
  778. next
  779. while ((next = stream.next())) {
  780. if (next === '"' && !escaped) {
  781. state.tokenize = base
  782. break
  783. }
  784. escaped = !escaped && next === '\\'
  785. }
  786. return [null, 'string']
  787. }
  788. function inComment(stream, state) {
  789. var parenthesisCount = 1
  790. var next
  791. while ((next = stream.next())) {
  792. if (next === ')') parenthesisCount--
  793. if (next === '(') parenthesisCount++
  794. if (parenthesisCount === 0) {
  795. stream.backUp(1)
  796. state.tokenize = base
  797. break
  798. }
  799. }
  800. return ['space', 'comment']
  801. }
  802. function createLookupMap(words) {
  803. var obj = {}
  804. for (var i = 0; i < words.length; ++i) obj[words[i]] = true
  805. return obj
  806. }
  807. function is(value, test) {
  808. if (test instanceof RegExp) return test.test(value)
  809. if (test instanceof Object) return test.propertyIsEnumerable(value)
  810. }
  811. return {
  812. startState: function () {
  813. return {
  814. ctx: { prev: null, start: 0, indentTo: 0 },
  815. lastToken: null,
  816. tokenize: base,
  817. }
  818. },
  819. token: function (stream, state) {
  820. if (stream.sol() && typeof state.ctx.indentTo !== 'number') state.ctx.indentTo = state.ctx.start + 1
  821. var typeStylePair = state.tokenize(stream, state)
  822. var type = typeStylePair[0]
  823. var style = typeStylePair[1]
  824. var current = stream.current()
  825. if (type !== 'space') {
  826. if (state.lastToken === '(' && state.ctx.indentTo === null) {
  827. if (type === 'symbol' && is(current, hasBodyParameter)) state.ctx.indentTo = state.ctx.start + options.indentUnit
  828. else state.ctx.indentTo = 'next'
  829. } else if (state.ctx.indentTo === 'next') {
  830. state.ctx.indentTo = stream.column()
  831. }
  832. state.lastToken = current
  833. }
  834. if (type === 'open') state.ctx = { prev: state.ctx, start: stream.column(), indentTo: null }
  835. else if (type === 'close') state.ctx = state.ctx.prev || state.ctx
  836. return style
  837. },
  838. indent: function (state) {
  839. var i = state.ctx.indentTo
  840. return typeof i === 'number' ? i : state.ctx.start + 1
  841. },
  842. closeBrackets: { pairs: '()[]{}""' },
  843. lineComment: ';;',
  844. }
  845. })
  846. CodeMirror.defineMIME('text/x-clojure', 'clojure')
  847. CodeMirror.defineMIME('text/x-clojurescript', 'clojure')
  848. CodeMirror.defineMIME('application/edn', 'clojure')
  849. })