build: set version to 0.5
[vis.git] / lua / lexers / clojure.lua
blob26bddec57d05f815032efa706d191707aafbe4ec
1 -- Copyright 2006-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- Clojure LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, R, S = lpeg.P, lpeg.R, lpeg.S
8 local M = {_NAME = 'clojure'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local line_comment = ';' * l.nonnewline^0
15 local block_comment = '#_(' * (l.any - ')')^0 * P(')')
16 local comment = token(l.COMMENT, line_comment + block_comment)
18 -- Strings.
19 local string = token(l.STRING, l.delimited_range('"'))
21 -- Numbers.
22 local number = token(l.NUMBER, P('-')^-1 * l.digit^1 * (S('./') * l.digit^1)^-1)
24 -- Keywords.
25 local keyword = token(l.KEYWORD, word_match({
26 'fn','try','catch','finaly','defonce',
27 'and', 'case', 'cond', 'def', 'defn', 'defmacro',
28 'do', 'else', 'when', 'when-let', 'if-let', 'if', 'let', 'loop',
29 'or', 'recur', 'quote',
30 }, '-*!'))
32 -- Functions.
33 local func = token(l.FUNCTION, word_match({
34 '*', '+', '-', '->ArrayChunk', '->Eduction', '->Vec',
35 '->VecNode', '->VecSeq', '/', '<', '<=', '=', '==', '>', '>=',
36 'StackTraceElement->vec', 'Throwable->map', 'accessor', 'aclone',
37 'add-classpath', 'add-watch', 'agent', 'agent-error', 'agent-errors', 'aget',
38 'alength', 'alias', 'all-ns', 'alter', 'alter-meta!', 'alter-var-root',
39 'ancestors', 'any?', 'apply', 'array-map', 'aset', 'aset-boolean',
40 'aset-byte', 'aset-char', 'aset-double', 'aset-float', 'aset-int',
41 'aset-long', 'aset-short', 'assoc', 'assoc!', 'assoc-in', 'associative?',
42 'atom', 'await', 'await-for', 'bases', 'bean', 'bigdec', 'bigint',
43 'biginteger', 'bit-and', 'bit-and-not', 'bit-clear', 'bit-flip', 'bit-not',
44 'bit-or', 'bit-set', 'bit-shift-left', 'bit-shift-right', 'bit-test',
45 'bit-xor', 'boolean', 'boolean-array', 'boolean?', 'booleans', 'bound-fn*',
46 'bound?', 'bounded-count', 'butlast', 'byte', 'byte-array', 'bytes', 'bytes?',
47 'cast', 'cat', 'char', 'char-array', 'char?', 'chars', 'class', 'class?',
48 'clear-agent-errors', 'clojure-version', 'coll?', 'commute', 'comp',
49 'comparator', 'compare', 'compare-and-set!', 'compile', 'complement',
50 'completing', 'concat', 'conj', 'conj!', 'cons', 'constantly',
51 'construct-proxy', 'contains?', 'count', 'counted?', 'create-ns',
52 'create-struct', 'cycle', 'dec', 'decimal?', 'dedupe', 'delay?',
53 'deliver', 'denominator', 'deref', 'derive', 'descendants', 'disj', 'disj!',
54 'dissoc', 'dissoc!', 'distinct', 'distinct?', 'doall', 'dorun', 'double',
55 'double-array', 'double?', 'doubles', 'drop', 'drop-last', 'drop-while',
56 'eduction', 'empty', 'empty?', 'ensure', 'ensure-reduced', 'enumeration-seq',
57 'error-handler', 'error-mode', 'eval', 'even?', 'every-pred', 'every?',
58 'ex-data', 'ex-info', 'extend', 'extenders', 'extends?', 'false?', 'ffirst',
59 'file-seq', 'filter', 'filterv', 'find', 'find-keyword', 'find-ns',
60 'find-var', 'first', 'flatten', 'float', 'float-array', 'float?', 'floats',
61 'flush', 'fn?', 'fnext', 'fnil', 'force', 'format', 'frequencies',
62 'future-call', 'future-cancel', 'future-cancelled?', 'future-done?',
63 'future?', 'gensym', 'get', 'get-in', 'get-method', 'get-proxy-class',
64 'get-thread-bindings', 'get-validator', 'group-by', 'halt-when', 'hash',
65 'hash-map', 'hash-ordered-coll', 'hash-set', 'hash-unordered-coll', 'ident?',
66 'identical?', 'identity', 'ifn?', 'in-ns', 'inc', 'inc', 'indexed?',
67 'init-proxy', 'inst-ms', 'inst?', 'instance?', 'int', 'int-array', 'int?',
68 'integer?', 'interleave', 'intern', 'interpose', 'into', 'into-array', 'ints',
69 'isa?', 'iterate', 'iterator-seq', 'juxt', 'keep', 'keep-indexed', 'key',
70 'keys', 'keyword', 'keyword?', 'last', 'line-seq', 'list', 'list*', 'list?',
71 'load', 'load-file', 'load-reader', 'load-string', 'loaded-libs', 'long',
72 'long-array', 'longs', 'macroexpand', 'macroexpand-1', 'make-array',
73 'make-hierarchy', 'map', 'map-entry?', 'map-indexed', 'map?', 'mapcat',
74 'mapv', 'max', 'max-key', 'memoize', 'merge', 'merge-with', 'meta', 'methods',
75 'min', 'min-key', 'mix-collection-hash', 'mod', 'name', 'namespace',
76 'namespace-munge', 'nat-int?', 'neg-int?', 'neg?', 'newline', 'next',
77 'nfirst', 'nil?', 'nnext', 'not', 'not-any?', 'not-empty', 'not-every?',
78 'not=', 'ns-aliases', 'ns-imports', 'ns-interns', 'ns-map', 'ns-name',
79 'ns-publics', 'ns-refers', 'ns-resolve', 'ns-unalias', 'ns-unmap', 'nth',
80 'nthnext', 'nthrest', 'num', 'number?', 'numerator', 'object-array', 'odd?',
81 'parents', 'partial', 'partition', 'partition-all', 'partition-by', 'pcalls',
82 'peek', 'persistent!', 'pmap', 'pop', 'pop!', 'pop-thread-bindings',
83 'pos-int?', 'pos?', 'pr-str', 'prefer-method', 'prefers', 'print',
84 'print-str', 'printf', 'println', 'println-str', 'prn', 'prn-str', 'promise',
85 'proxy-mappings', 'push-thread-bindings', 'qualified-ident?',
86 'qualified-keyword?', 'qualified-symbol?', 'quot', 'rand', 'rand-int',
87 'rand-nth', 'random-sample', 'range', 'ratio?', 'rational?', 'rationalize',
88 're-find', 're-groups', 're-matcher', 're-matches', 're-pattern', 're-seq',
89 'read', 'read-line', 'read-string', 'reader-conditional',
90 'reader-conditional?', 'realized?', 'record?', 'reduce', 'reduce-kv',
91 'reduced', 'reduced?', 'reductions', 'ref', 'ref-history-count',
92 'ref-max-history', 'ref-min-history', 'ref-set', 'refer',
93 'release-pending-sends', 'rem', 'remove', 'remove-all-methods',
94 'remove-method', 'remove-ns', 'remove-watch', 'repeat', 'repeatedly',
95 'replace', 'replicate', 'require', 'reset!', 'reset-meta!', 'reset-vals!',
96 'resolve', 'rest', 'restart-agent', 'resultset-seq', 'reverse', 'reversible?',
97 'rseq', 'rsubseq', 'run!', 'satisfies?', 'second', 'select-keys', 'send',
98 'send-off', 'send-via', 'seq', 'seq?', 'seqable?', 'seque', 'sequence',
99 'sequential?', 'set', 'set-agent-send-executor!',
100 'set-agent-send-off-executor!', 'set-error-handler!', 'set-error-mode!',
101 'set-validator!', 'set?', 'short', 'short-array', 'shorts', 'shuffle',
102 'shutdown-agents', 'simple-ident?', 'simple-keyword?', 'simple-symbol?',
103 'slurp', 'some', 'some-fn', 'some?', 'sort', 'sort-by', 'sorted-map',
104 'sorted-map-by', 'sorted-set', 'sorted-set-by', 'sorted?', 'special-symbol?',
105 'spit', 'split-at', 'split-with', 'str', 'string?', 'struct', 'struct-map',
106 'subs', 'subseq', 'subvec', 'supers', 'swap!', 'swap-vals!', 'symbol',
107 'symbol?', 'tagged-literal', 'tagged-literal?', 'take', 'take-last',
108 'take-nth', 'take-while', 'test', 'the-ns', 'thread-bound?', 'to-array',
109 'to-array-2d', 'trampoline', 'transduce', 'transient', 'tree-seq', 'true?',
110 'type', 'unchecked-add', 'unchecked-add-int', 'unchecked-byte',
111 'unchecked-char', 'unchecked-dec', 'unchecked-dec-int',
112 'unchecked-divide-int', 'unchecked-double', 'unchecked-float',
113 'unchecked-inc', 'unchecked-inc-int', 'unchecked-int', 'unchecked-long',
114 'unchecked-multiply', 'unchecked-multiply-int', 'unchecked-negate',
115 'unchecked-negate-int', 'unchecked-remainder-int', 'unchecked-short',
116 'unchecked-subtract', 'unchecked-subtract-int', 'underive', 'unreduced',
117 'unsigned-bit-shift-right', 'update', 'update-in', 'update-proxy', 'uri?',
118 'use', 'uuid?', 'val', 'vals', 'var-get', 'var-set', 'var?', 'vary-meta',
119 'vec', 'vector', 'vector-of', 'vector?', 'volatile!', 'volatile?', 'vreset!',
120 'with-bindings*', 'with-meta', 'with-redefs-fn', 'xml-seq', 'zero?', 'zipmap',
121 'diff-similar', 'equality-partition', 'diff', 'inspect', 'inspect-table',
122 'inspect-tree', '', 'validated', 'browse-url', 'as-file', 'as-url',
123 'make-input-stream', 'make-output-stream', 'make-reader', 'make-writer',
124 'as-relative-path', 'copy', 'delete-file', 'file', 'input-stream',
125 'make-parents', 'output-stream', 'reader', 'resource', 'writer',
126 'add-local-javadoc', 'add-remote-javadoc', 'javadoc', 'sh', 'demunge',
127 'load-script', 'main', 'repl', 'repl-caught', 'repl-exception', 'repl-prompt',
128 'repl-read', 'root-cause', 'skip-if-eol', 'skip-whitespace',
129 'stack-element-str', 'cl-format', 'fresh-line', 'get-pretty-writer', 'pprint',
130 'pprint-indent', 'pprint-newline', 'pprint-tab', 'print-table',
131 'set-pprint-dispatch', 'write', 'write-out', 'resolve-class', 'do-reflect',
132 'typename', '->AsmReflector', '->Constructor', '->Field', '->JavaReflector',
133 '->Method', 'map->Constructor', 'map->Field', 'map->Method', 'reflect',
134 'type-reflect', 'apropos', 'dir-fn', 'find-doc', 'pst', 'set-break-handler!',
135 'source-fn', 'thread-stopper', 'difference', 'index', 'intersection', 'join',
136 'map-invert', 'project', 'rename', 'rename-keys', 'select', 'subset?',
137 'superset?', 'union', 'e', 'print-cause-trace', 'print-stack-trace',
138 'print-throwable', 'print-trace-element', 'blank?', 'capitalize',
139 'ends-with?', 'escape', 'includes?', 'index-of', 'last-index-of',
140 'lower-case', 're-quote-replacement', 'replace-first', 'split', 'split-lines',
141 'starts-with?', 'trim', 'trim-newline', 'triml', 'trimr', 'upper-case',
142 'apply-template', 'assert-any', 'assert-predicate', 'compose-fixtures',
143 'do-report', 'file-position', 'function?', 'get-possibly-unbound-var',
144 'inc-report-counter', 'join-fixtures', 'run-all-tests', 'run-tests',
145 'successful?', 'test-all-vars', 'test-ns', 'test-vars',
146 'testing-contexts-str', 'testing-vars-str', 'keywordize-keys',
147 'macroexpand-all', 'postwalk', 'postwalk-demo', 'postwalk-replace', 'prewalk',
148 'prewalk-demo', 'prewalk-replace', 'stringify-keys', 'walk', 'append-child',
149 'branch?', 'children', 'down', 'edit', 'end?', 'insert-child', 'insert-left',
150 'insert-right', 'left', 'leftmost', 'lefts', 'make-node', 'node', 'path',
151 'prev', 'right', 'rightmost', 'rights', 'root', 'seq-zip', 'up', 'vector-zip',
152 'xml-zip', 'zipper'
153 }, '-/<>!?=#\''))
155 -- Identifiers.
156 local word = (l.alpha + S('-!?*$=-')) * (l.alnum + S('.-!?*$+-'))^0
157 local identifier = token(l.IDENTIFIER, word)
159 -- Operators.
160 local operator = token(l.OPERATOR, S('`@()'))
162 -- Clojure keywords
163 local clojure_keyword = token('clojure_keyword', ':' * S(':')^-1 * word * ('/' * word )^-1)
164 local clojure_symbol = token('clojure_symbol', "\'" * word * ('/' * word )^-1 )
166 M._rules = {
167 {'whitespace', ws},
168 {'keyword', keyword},
169 {'func', func},
170 {'identifier', identifier},
171 {'string', string},
172 {'comment', comment},
173 {'number', number},
174 {'operator', operator},
175 {"clojure_keyword", clojure_keyword},
176 {"clojure_symbol", clojure_symbol}
180 M._tokenstyles = {
181 clojure_keyword = l.STYLE_TYPE,
182 clojure_symbol = l.STYLE_TYPE..',bold',
185 M._foldsymbols = {
186 _patterns = {'[%(%)%[%]{}]', ';'},
187 [l.OPERATOR] = {
188 ['('] = 1, [')'] = -1, ['['] = 1, [']'] = -1, ['{'] = 1, ['}'] = -1
190 [l.COMMENT] = {['#_('] = 1, [';'] = l.fold_line_comments(';')}
193 return M