Class: JsDuck::Css::Parser

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Parser.



7
8
9
10
# File 'lib/jsduck/css/parser.rb', line 7

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

Instance Method Details

#code_blockObject

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



32
33
34
35
36
37
38
39
40
41
# File 'lib/jsduck/css/parser.rb', line 32

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

#css_valueObject

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



66
67
68
69
70
71
72
# File 'lib/jsduck/css/parser.rb', line 66

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

#look(*args) ⇒ Object



115
116
117
# File 'lib/jsduck/css/parser.rb', line 115

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



105
106
107
108
109
110
111
112
113
# File 'lib/jsduck/css/parser.rb', line 105

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>



44
45
46
47
48
49
50
# File 'lib/jsduck/css/parser.rb', line 44

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.



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

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



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
# File 'lib/jsduck/css/parser.rb', line 75

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>



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

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