Class: CodeRay::Scanners::CSS

Inherits:
Scanner
  • Object
show all
Defined in:
lib/coderay/scanners/css.rb

Defined Under Namespace

Modules: RE

Constant Summary collapse

KINDS_NOT_LOC =
[
  :comment,
  :class, :pseudo_class, :type,
  :constant, :directive,
  :key, :value, :operator, :color, :float, :content, :delimiter,
  :error, :important,
]

Constants inherited from Scanner

Scanner::DEFAULT_OPTIONS, Scanner::ScanError

Instance Method Summary collapse

Methods inherited from Scanner

#column, #each, file_extension, #initialize, #lang, #line, #marshal_dump, #marshal_load, normify, #reset, streamable?, #streaming?, #string=, #tokenize, #tokens

Methods included from Plugin

#helper, #included, #plugin_host, #plugin_id, #register_for, #title

Constructor Details

This class inherits a constructor from CodeRay::Scanners::Scanner

Instance Method Details

#scan_tokens(tokens, options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/coderay/scanners/css.rb', line 52

def scan_tokens tokens, options
  
  value_expected = nil
  states = [:initial]

  until eos?

    kind = nil
    match = nil

    if scan(/\s+/)
      kind = :space

    elsif case states.last
      when :initial, :media
        if scan(/(?>#{RE::Ident})(?!\()|\*/ox)
          kind = :type
        elsif scan RE::Class
          kind = :class
        elsif scan RE::Id
          kind = :constant
        elsif scan RE::PseudoClass
          kind = :pseudo_class
        elsif match = scan(RE::AttributeSelector)
          # TODO: Improve highlighting inside of attribute selectors.
          tokens << [:open, :string]
          tokens << [match[0,1], :delimiter]
          tokens << [match[1..-2], :content] if match.size > 2
          tokens << [match[-1,1], :delimiter] if match[-1] == ?]
          tokens << [:close, :string]
          next
        elsif match = scan(/@media/)
          kind = :directive
          states.push :media_before_name
        end
      
      when :block
        if scan(/(?>#{RE::Ident})(?!\()/ox)
          if value_expected
            kind = :value
          else
            kind = :key
          end
        end

      when :media_before_name
        if scan RE::Ident
          kind = :type
          states[-1] = :media_after_name
        end
      
      when :media_after_name
        if scan(/\{/)
          kind = :operator
          states[-1] = :media
        end
      
      when :comment
        if scan(/(?:[^*\s]|\*(?!\/))+/)
          kind = :comment
        elsif scan(/\*\//)
          kind = :comment
          states.pop
        elsif scan(/\s+/)
          kind = :space
        end

      else
        raise_inspect 'Unknown state', tokens

      end

    elsif scan(/\/\*/)
      kind = :comment
      states.push :comment

    elsif scan(/\{/)
      value_expected = false
      kind = :operator
      states.push :block

    elsif scan(/\}/)
      value_expected = false
      if states.last == :block || states.last == :media
        kind = :operator
        states.pop
      else
        kind = :error
      end

    elsif match = scan(/#{RE::String}/o)
      tokens << [:open, :string]
      tokens << [match[0, 1], :delimiter]
      tokens << [match[1..-2], :content] if match.size > 2
      tokens << [match[-1, 1], :delimiter] if match.size >= 2
      tokens << [:close, :string]
      next

    elsif match = scan(/#{RE::Function}/o)
      tokens << [:open, :string]
      start = match[/^\w+\(/]
      tokens << [start, :delimiter]
      if match[-1] == ?)
        tokens << [match[start.size..-2], :content]
        tokens << [')', :delimiter]
      else
        tokens << [match[start.size..-1], :content]
      end
      tokens << [:close, :string]
      next

    elsif scan(/(?: #{RE::Dimension} | #{RE::Percentage} | #{RE::Num} )/ox)
      kind = :float

    elsif scan(/#{RE::Color}/o)
      kind = :color

    elsif scan(/! *important/)
      kind = :important

    elsif scan(/rgb\([^()\n]*\)?/)
      kind = :color

    elsif scan(/#{RE::AtKeyword}/o)
      kind = :directive

    elsif match = scan(/ [+>:;,.=()\/] /x)
      if match == ':'
        value_expected = true
      elsif match == ';'
        value_expected = false
      end
      kind = :operator

    else
      getch
      kind = :error

    end

    match ||= matched
    if $CODERAY_DEBUG and not kind
      raise_inspect 'Error token %p in line %d' %
        [[match, kind], line], tokens
    end
    raise_inspect 'Empty token', tokens unless match

    tokens << [match, kind]

  end

  tokens
end