Class: JsDuck::CssParser

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/css_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(input, options = {}) ⇒ CssParser

Returns a new instance of CssParser.



6
7
8
9
# File 'lib/jsduck/css_parser.rb', line 6

def initialize(input, options = {})
  @lex = CssLexer.new(input)
  @docs = []
end

Instance Method Details

#code_blockObject

<code-block> := <mixin-declaration> | <var-declaration> | <property>



31
32
33
34
35
36
37
38
39
40
# File 'lib/jsduck/css_parser.rb', line 31

def code_block
  if look("@mixin")
    mixin_declaration
  elsif look(:var, ":")
    var_declaration
  else
    # Default to property like in JsParser.
    {:tagname => :property}
  end
end

#css_valueObject

<css-value> := …anything up to… [ “;” | “}” | “!default” ]



65
66
67
68
69
70
71
# File 'lib/jsduck/css_parser.rb', line 65

def css_value
  val = []
  while !look(";") && !look("}") && !look("!", "default")
    val << @lex.next(true)
  end
  val
end

#look(*args) ⇒ Object



114
115
116
# File 'lib/jsduck/css_parser.rb', line 114

def look(*args)
  @lex.look(*args)
end

#match(*args) ⇒ Object

Matches all arguments, returns the value of last match When the whole sequence doesn’t match, throws exception



104
105
106
107
108
109
110
111
112
# File 'lib/jsduck/css_parser.rb', line 104

def match(*args)
  if look(*args)
    last = nil
    args.length.times { last = @lex.next }
    last
  else
    throw "Expected: " + args.join(", ")
  end
end

#mixin_declarationObject

<mixin-declaration> := “@mixin” <ident>



43
44
45
46
47
48
49
# File 'lib/jsduck/css_parser.rb', line 43

def mixin_declaration
  match("@mixin")
  return {
    :tagname => :css_mixin,
    :name => look(:ident) ? match(:ident) : nil,
  }
end

#parseObject

Parses the whole CSS block and returns same kind of structure that JavaScript parser does.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jsduck/css_parser.rb', line 13

def parse
  while !@lex.empty? do
    if look(:doc_comment)
      comment = @lex.next(true)
      @docs << {
        :comment => comment[:value],
        :linenr => comment[:linenr],
        :code => code_block,
        :type => :doc_comment,
      }
    else
      @lex.next
    end
  end
  @docs
end

#value_type(val) ⇒ Object

Determines type of CSS value



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
# File 'lib/jsduck/css_parser.rb', line 74

def value_type(val)
  case val[0][:type]
  when :number
    "number"
  when :dimension
    "length"
  when :percentage
    "percentage"
  when :string
    "string"
  when :hash
    "color"
  when :ident
    case val[0][:value]
    when "true", "false"
      return "boolean"
    when "rgb", "rgba", "hsl", "hsla"
      return "color"
    when "black", "silver", "gray", "white", "maroon",
      "red", "purple", "fuchsia", "green", "lime", "olive",
      "yellow", "navy", "blue", "teal", "aqua", "orange"
      return "color"
    when "transparent"
      return "color"
    end
  end
end

#var_declarationObject

<var-declaration> := <var> “:” <css-value>



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jsduck/css_parser.rb', line 52

def var_declaration
  name = match(:var)
  match(":")
  value_list = css_value
  return {
    :tagname => :css_var,
    :name => name,
    :default => value_list.map {|v| v[:value] }.join(" "),
    :type => value_type(value_list),
  }
end