Sha256: 6e78328e2208d5542dda49e959a6e58b2969bab47385c05831e604325ddee3ba

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

require 'jsduck/lexer'
require 'jsduck/doc_parser'

module JsDuck

  class CssParser
    def initialize(input)
      @lex = Lexer.new(input)
      @doc_parser = DocParser.new(:css)
      @docs = []
    end

    # Parses the whole CSS block and returns same kind of structure
    # that JavaScript parser does.
    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

    # <code-block> := <mixin> | <nop>
    def code_block
      if look("@", "mixin")
        mixin
      else
        {:type => :nop}
      end
    end

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

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

    # Matches all arguments, returns the value of last match
    # When the whole sequence doesn't match, throws exception
    def match(*args)
      if look(*args)
        last = nil
        args.length.times { last = @lex.next }
        last
      else
        throw "Expected: " + args.join(", ")
      end
    end

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

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
jsduck-3.0.pre lib/jsduck/css_parser.rb
jsduck-2.0.pre4 lib/jsduck/css_parser.rb
jsduck-0.6.1 lib/jsduck/css_parser.rb
jsduck-2.0.pre2 lib/jsduck/css_parser.rb
jsduck-2.0.pre lib/jsduck/css_parser.rb
jsduck-0.6 lib/jsduck/css_parser.rb