annotate vendor/gems/coderay-1.0.0/lib/coderay/scanners/clojure.rb @ 1022:f2ec92061fca browsing

Merge from live branch
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Tue, 13 Nov 2012 10:35:40 +0000
parents cbb26bc654de
children
rev   line source
Chris@909 1 # encoding: utf-8
Chris@909 2 module CodeRay
Chris@909 3 module Scanners
Chris@909 4
Chris@909 5 # Clojure scanner by Licenser.
Chris@909 6 class Clojure < Scanner
Chris@909 7
Chris@909 8 register_for :clojure
Chris@909 9 file_extension 'clj'
Chris@909 10
Chris@909 11 SPECIAL_FORMS = %w[
Chris@909 12 def if do let quote var fn loop recur throw try catch monitor-enter monitor-exit .
Chris@909 13 new
Chris@909 14 ] # :nodoc:
Chris@909 15
Chris@909 16 CORE_FORMS = %w[
Chris@909 17 + - -> ->> .. / * <= < = == >= > accessor aclone add-classpath add-watch
Chris@909 18 agent agent-error agent-errors aget alength alias all-ns alter alter-meta!
Chris@909 19 alter-var-root amap ancestors and apply areduce array-map aset aset-boolean
Chris@909 20 aset-byte aset-char aset-double aset-float aset-int aset-long aset-short
Chris@909 21 assert assoc assoc! assoc-in associative? atom await await-for bases bean
Chris@909 22 bigdec bigint binding bit-and bit-and-not bit-clear bit-flip bit-not bit-or
Chris@909 23 bit-set bit-shift-left bit-shift-right bit-test bit-xor boolean boolean-array
Chris@909 24 booleans bound-fn bound-fn* bound? butlast byte byte-array bytes case cast char
Chris@909 25 char-array char-escape-string char-name-string char? chars class class?
Chris@909 26 clear-agent-errors clojure-version coll? comment commute comp comparator
Chris@909 27 compare compare-and-set! compile complement concat cond condp conj conj!
Chris@909 28 cons constantly construct-proxy contains? count counted? create-ns
Chris@909 29 create-struct cycle dec decimal? declare definline defmacro defmethod defmulti
Chris@909 30 defn defn- defonce defprotocol defrecord defstruct deftype delay delay?
Chris@909 31 deliver denominator deref derive descendants disj disj! dissoc dissoc!
Chris@909 32 distinct distinct? doall doc dorun doseq dosync dotimes doto double
Chris@909 33 double-array doubles drop drop-last drop-while empty empty? ensure
Chris@909 34 enumeration-seq error-handler error-mode eval even? every? extend
Chris@909 35 extend-protocol extend-type extenders extends? false? ffirst file-seq
Chris@909 36 filter find find-doc find-ns find-var first float float-array float?
Chris@909 37 floats flush fn fn? fnext for force format future future-call future-cancel
Chris@909 38 future-cancelled? future-done? future? gen-class gen-interface gensym get
Chris@909 39 get-in get-method get-proxy-class get-thread-bindings get-validator hash
Chris@909 40 hash-map hash-set identical? identity if-let if-not ifn? import in-ns
Chris@909 41 inc init-proxy instance? int int-array integer? interleave intern
Chris@909 42 interpose into into-array ints io! isa? iterate iterator-seq juxt key
Chris@909 43 keys keyword keyword? last lazy-cat lazy-seq let letfn line-seq list list*
Chris@909 44 list? load load-file load-reader load-string loaded-libs locking long
Chris@909 45 long-array longs loop macroexpand macroexpand-1 make-array make-hierarchy
Chris@909 46 map map? mapcat max max-key memfn memoize merge merge-with meta methods
Chris@909 47 min min-key mod name namespace neg? newline next nfirst nil? nnext not
Chris@909 48 not-any? not-empty not-every? not= ns ns-aliases ns-imports ns-interns
Chris@909 49 ns-map ns-name ns-publics ns-refers ns-resolve ns-unalias ns-unmap nth
Chris@909 50 nthnext num number? numerator object-array odd? or parents partial
Chris@909 51 partition pcalls peek persistent! pmap pop pop! pop-thread-bindings
Chris@909 52 pos? pr pr-str prefer-method prefers print print-namespace-doc
Chris@909 53 print-str printf println println-str prn prn-str promise proxy
Chris@909 54 proxy-mappings proxy-super push-thread-bindings pvalues quot rand
Chris@909 55 rand-int range ratio? rationalize re-find re-groups re-matcher
Chris@909 56 re-matches re-pattern re-seq read read-line read-string reduce ref
Chris@909 57 ref-history-count ref-max-history ref-min-history ref-set refer
Chris@909 58 refer-clojure reify release-pending-sends rem remove remove-all-methods
Chris@909 59 remove-method remove-ns remove-watch repeat repeatedly replace replicate
Chris@909 60 require reset! reset-meta! resolve rest restart-agent resultset-seq
Chris@909 61 reverse reversible? rseq rsubseq satisfies? second select-keys send
Chris@909 62 send-off seq seq? seque sequence sequential? set set-error-handler!
Chris@909 63 set-error-mode! set-validator! set? short short-array shorts
Chris@909 64 shutdown-agents slurp some sort sort-by sorted-map sorted-map-by
Chris@909 65 sorted-set sorted-set-by sorted? special-form-anchor special-symbol?
Chris@909 66 split-at split-with str string? struct struct-map subs subseq subvec
Chris@909 67 supers swap! symbol symbol? sync syntax-symbol-anchor take take-last
Chris@909 68 take-nth take-while test the-ns thread-bound? time to-array to-array-2d
Chris@909 69 trampoline transient tree-seq true? type unchecked-add unchecked-dec
Chris@909 70 unchecked-divide unchecked-inc unchecked-multiply unchecked-negate
Chris@909 71 unchecked-remainder unchecked-subtract underive update-in update-proxy
Chris@909 72 use val vals var-get var-set var? vary-meta vec vector vector-of vector?
Chris@909 73 when when-first when-let when-not while with-bindings with-bindings*
Chris@909 74 with-in-str with-local-vars with-meta with-open with-out-str
Chris@909 75 with-precision xml-seq zero? zipmap
Chris@909 76 ] # :nodoc:
Chris@909 77
Chris@909 78 PREDEFINED_CONSTANTS = %w[
Chris@909 79 true false nil *1 *2 *3 *agent* *clojure-version* *command-line-args*
Chris@909 80 *compile-files* *compile-path* *e *err* *file* *flush-on-newline*
Chris@909 81 *in* *ns* *out* *print-dup* *print-length* *print-level* *print-meta*
Chris@909 82 *print-readably* *read-eval* *warn-on-reflection*
Chris@909 83 ] # :nodoc:
Chris@909 84
Chris@909 85 IDENT_KIND = WordList.new(:ident).
Chris@909 86 add(SPECIAL_FORMS, :keyword).
Chris@909 87 add(CORE_FORMS, :keyword).
Chris@909 88 add(PREDEFINED_CONSTANTS, :predefined_constant)
Chris@909 89
Chris@909 90 KEYWORD_NEXT_TOKEN_KIND = WordList.new(nil).
Chris@909 91 add(%w[ def defn defn- definline defmacro defmulti defmethod defstruct defonce declare ], :function).
Chris@909 92 add(%w[ ns ], :namespace).
Chris@909 93 add(%w[ defprotocol defrecord ], :class)
Chris@909 94
Chris@909 95 BASIC_IDENTIFIER = /[a-zA-Z$%*\/_+!?&<>\-=]=?[a-zA-Z0-9$&*+!\/_?<>\-\#]*/
Chris@909 96 IDENTIFIER = /(?!-\d)(?:(?:#{BASIC_IDENTIFIER}\.)*#{BASIC_IDENTIFIER}(?:\/#{BASIC_IDENTIFIER})?\.?)|\.\.?/
Chris@909 97 SYMBOL = /::?#{IDENTIFIER}/o
Chris@909 98 DIGIT = /\d/
Chris@909 99 DIGIT10 = DIGIT
Chris@909 100 DIGIT16 = /[0-9a-f]/i
Chris@909 101 DIGIT8 = /[0-7]/
Chris@909 102 DIGIT2 = /[01]/
Chris@909 103 RADIX16 = /\#x/i
Chris@909 104 RADIX8 = /\#o/i
Chris@909 105 RADIX2 = /\#b/i
Chris@909 106 RADIX10 = /\#d/i
Chris@909 107 EXACTNESS = /#i|#e/i
Chris@909 108 SIGN = /[\+-]?/
Chris@909 109 EXP_MARK = /[esfdl]/i
Chris@909 110 EXP = /#{EXP_MARK}#{SIGN}#{DIGIT}+/
Chris@909 111 SUFFIX = /#{EXP}?/
Chris@909 112 PREFIX10 = /#{RADIX10}?#{EXACTNESS}?|#{EXACTNESS}?#{RADIX10}?/
Chris@909 113 PREFIX16 = /#{RADIX16}#{EXACTNESS}?|#{EXACTNESS}?#{RADIX16}/
Chris@909 114 PREFIX8 = /#{RADIX8}#{EXACTNESS}?|#{EXACTNESS}?#{RADIX8}/
Chris@909 115 PREFIX2 = /#{RADIX2}#{EXACTNESS}?|#{EXACTNESS}?#{RADIX2}/
Chris@909 116 UINT10 = /#{DIGIT10}+#*/
Chris@909 117 UINT16 = /#{DIGIT16}+#*/
Chris@909 118 UINT8 = /#{DIGIT8}+#*/
Chris@909 119 UINT2 = /#{DIGIT2}+#*/
Chris@909 120 DECIMAL = /#{DIGIT10}+#+\.#*#{SUFFIX}|#{DIGIT10}+\.#{DIGIT10}*#*#{SUFFIX}|\.#{DIGIT10}+#*#{SUFFIX}|#{UINT10}#{EXP}/
Chris@909 121 UREAL10 = /#{UINT10}\/#{UINT10}|#{DECIMAL}|#{UINT10}/
Chris@909 122 UREAL16 = /#{UINT16}\/#{UINT16}|#{UINT16}/
Chris@909 123 UREAL8 = /#{UINT8}\/#{UINT8}|#{UINT8}/
Chris@909 124 UREAL2 = /#{UINT2}\/#{UINT2}|#{UINT2}/
Chris@909 125 REAL10 = /#{SIGN}#{UREAL10}/
Chris@909 126 REAL16 = /#{SIGN}#{UREAL16}/
Chris@909 127 REAL8 = /#{SIGN}#{UREAL8}/
Chris@909 128 REAL2 = /#{SIGN}#{UREAL2}/
Chris@909 129 IMAG10 = /i|#{UREAL10}i/
Chris@909 130 IMAG16 = /i|#{UREAL16}i/
Chris@909 131 IMAG8 = /i|#{UREAL8}i/
Chris@909 132 IMAG2 = /i|#{UREAL2}i/
Chris@909 133 COMPLEX10 = /#{REAL10}@#{REAL10}|#{REAL10}\+#{IMAG10}|#{REAL10}-#{IMAG10}|\+#{IMAG10}|-#{IMAG10}|#{REAL10}/
Chris@909 134 COMPLEX16 = /#{REAL16}@#{REAL16}|#{REAL16}\+#{IMAG16}|#{REAL16}-#{IMAG16}|\+#{IMAG16}|-#{IMAG16}|#{REAL16}/
Chris@909 135 COMPLEX8 = /#{REAL8}@#{REAL8}|#{REAL8}\+#{IMAG8}|#{REAL8}-#{IMAG8}|\+#{IMAG8}|-#{IMAG8}|#{REAL8}/
Chris@909 136 COMPLEX2 = /#{REAL2}@#{REAL2}|#{REAL2}\+#{IMAG2}|#{REAL2}-#{IMAG2}|\+#{IMAG2}|-#{IMAG2}|#{REAL2}/
Chris@909 137 NUM10 = /#{PREFIX10}?#{COMPLEX10}/
Chris@909 138 NUM16 = /#{PREFIX16}#{COMPLEX16}/
Chris@909 139 NUM8 = /#{PREFIX8}#{COMPLEX8}/
Chris@909 140 NUM2 = /#{PREFIX2}#{COMPLEX2}/
Chris@909 141 NUM = /#{NUM10}|#{NUM16}|#{NUM8}|#{NUM2}/
Chris@909 142
Chris@909 143 protected
Chris@909 144
Chris@909 145 def scan_tokens encoder, options
Chris@909 146
Chris@909 147 state = :initial
Chris@909 148 kind = nil
Chris@909 149
Chris@909 150 until eos?
Chris@909 151
Chris@909 152 case state
Chris@909 153 when :initial
Chris@909 154 if match = scan(/ \s+ | \\\n | , /x)
Chris@909 155 encoder.text_token match, :space
Chris@909 156 elsif match = scan(/['`\(\[\)\]\{\}]|\#[({]|~@?|[@\^]/)
Chris@909 157 encoder.text_token match, :operator
Chris@909 158 elsif match = scan(/;.*/)
Chris@909 159 encoder.text_token match, :comment # TODO: recognize (comment ...) too
Chris@909 160 elsif match = scan(/\#?\\(?:newline|space|.?)/)
Chris@909 161 encoder.text_token match, :char
Chris@909 162 elsif match = scan(/\#[ft]/)
Chris@909 163 encoder.text_token match, :predefined_constant
Chris@909 164 elsif match = scan(/#{IDENTIFIER}/o)
Chris@909 165 kind = IDENT_KIND[match]
Chris@909 166 encoder.text_token match, kind
Chris@909 167 if rest? && kind == :keyword
Chris@909 168 if kind = KEYWORD_NEXT_TOKEN_KIND[match]
Chris@909 169 encoder.text_token match, :space if match = scan(/\s+/o)
Chris@909 170 encoder.text_token match, kind if match = scan(/#{IDENTIFIER}/o)
Chris@909 171 end
Chris@909 172 end
Chris@909 173 elsif match = scan(/#{SYMBOL}/o)
Chris@909 174 encoder.text_token match, :symbol
Chris@909 175 elsif match = scan(/\./)
Chris@909 176 encoder.text_token match, :operator
Chris@909 177 elsif match = scan(/ \# \^ #{IDENTIFIER} /ox)
Chris@909 178 encoder.text_token match, :type
Chris@909 179 elsif match = scan(/ (\#)? " /x)
Chris@909 180 state = self[1] ? :regexp : :string
Chris@909 181 encoder.begin_group state
Chris@909 182 encoder.text_token match, :delimiter
Chris@909 183 elsif match = scan(/#{NUM}/o) and not matched.empty?
Chris@909 184 encoder.text_token match, match[/[.e\/]/i] ? :float : :integer
Chris@909 185 else
Chris@909 186 encoder.text_token getch, :error
Chris@909 187 end
Chris@909 188
Chris@909 189 when :string, :regexp
Chris@909 190 if match = scan(/[^"\\]+|\\.?/)
Chris@909 191 encoder.text_token match, :content
Chris@909 192 elsif match = scan(/"/)
Chris@909 193 encoder.text_token match, :delimiter
Chris@909 194 encoder.end_group state
Chris@909 195 state = :initial
Chris@909 196 else
Chris@909 197 raise_inspect "else case \" reached; %p not handled." % peek(1),
Chris@909 198 encoder, state
Chris@909 199 end
Chris@909 200
Chris@909 201 else
Chris@909 202 raise 'else case reached'
Chris@909 203
Chris@909 204 end
Chris@909 205
Chris@909 206 end
Chris@909 207
Chris@909 208 if [:string, :regexp].include? state
Chris@909 209 encoder.end_group state
Chris@909 210 end
Chris@909 211
Chris@909 212 encoder
Chris@909 213
Chris@909 214 end
Chris@909 215 end
Chris@909 216 end
Chris@909 217 end