vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

clojure.lua

(10370B)


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