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.



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

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

Instance Method Details

#code_blockObject

<code-block> := <mixin> | <nop>



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

def code_block
  if look("@", "mixin")
    mixin
  else
    {:type => :nop}
  end
end

#css_identObject

<css-ident> := <ident> [ “-” <ident> ]*



50
51
52
53
54
55
56
# File 'lib/jsduck/css_parser.rb', line 50

def css_ident
  chain = [match(:ident)]
  while look("-", :ident) do
    chain << match("-", :ident)
  end
  return chain.join("-")
end

#look(*args) ⇒ Object



70
71
72
# File 'lib/jsduck/css_parser.rb', line 70

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



60
61
62
63
64
65
66
67
68
# File 'lib/jsduck/css_parser.rb', line 60

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

#mixinObject

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



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

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

#parseObject

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



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

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