Commit 30945e01 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Use JsLiteralParser in DocParser.

Minor modifications to JsLiteralParser to make it fail when
syntax error encountered while parsing object literal.
parent 701aa31c
Loading
Loading
Loading
Loading
+4 −82
Original line number Diff line number Diff line
require 'strscan'
require 'jsduck/js_literal_parser'
require 'jsduck/js_literal_builder'

module JsDuck

@@ -391,88 +393,8 @@ module JsDuck
    end

    def literal
      skip_horiz_white
      if look(/[0-9]/)
        number_literal
      elsif look(/["']/)
        string_literal
      elsif look(/\//)
        regex_literal
      elsif look(/\[/)
        array_literal
      elsif look(/\{/)
        object_literal
      elsif look(/true|false/)
        boolean_literal
      end
    end

    def string_literal
      if look(/"/)
        match(/"([^"\\]|\\.)*"/)
      elsif look(/'/)
        match(/'([^'\\]|\\.)*'/)
      end
    end

    def regex_literal
      match(/\/([^\/\\]|\\.)*\/[gim]*/)
    end

    def number_literal
      match(/[0-9]+(\.[0-9]*)?/)
    end

    def boolean_literal
      match(/true|false/)
    end

    def array_literal
      match(/\[/)
      r = []
      lit = literal
      while lit
        r << lit
        skip_horiz_white
        match(/,/)
        lit = literal
      end
      match(/\]/)
      "[" + r.join(", ") + "]"
    end

    def object_literal
      match(/\{/)
      r = []
      lit = object_literal_pair
      while lit
        r << lit
        skip_horiz_white
        match(/,/)
        lit = object_literal_pair
      end
      match(/\}/)
      "{" + r.join(", ") + "}"
    end

    def object_literal_pair
      skip_horiz_white
      if look(/\w/)
        key = ident
      elsif look(/['"]/)
        key = string_literal
      else
        return
      end

      skip_horiz_white
      match(/:/)

      skip_horiz_white
      value = literal
      return if !value

      key + ": " + value
      lit = JsLiteralParser.new(@input).literal
      lit ? JsLiteralBuilder.new.to_s(lit) : nil
    end

    # matches {...} and returns text inside brackets
+4 −3
Original line number Diff line number Diff line
@@ -57,11 +57,14 @@ module JsDuck
    def object_literal
      match("{")
      r = []
      while (lit = object_literal_pair)
      while (look(:ident) || look(:string))
        lit = object_literal_pair
        return unless lit
        r << lit
        break unless look(",")
        match(",")
      end

      return unless look("}")
      match("}")
      return {:type => :object, :value => r}
@@ -72,8 +75,6 @@ module JsDuck
        key = match(:ident)
      elsif look(:string)
        key = match(:string)
      else
        return
      end

      return unless look(":")
+6 −6
Original line number Diff line number Diff line
@@ -203,8 +203,8 @@ describe JsDuck::Aggregator do
         */
      EOS
    end
    it "has as much of the array as possible for default value" do
      @doc[:default].should == '[]'
    it "has nil as default value" do
      @doc[:default].should == nil
    end
  end

@@ -216,8 +216,8 @@ describe JsDuck::Aggregator do
         */
      EOS
    end
    it "has as much of the object as possible for default value" do
      @doc[:default].should == '{ho: 5}'
    it "has nil as default value" do
      @doc[:default].should == nil
    end
  end

@@ -229,8 +229,8 @@ describe JsDuck::Aggregator do
         */
      EOS
    end
    it "has as much of the object as possible for default value" do
      @doc[:default].should == '{ho: 5}'
    it "has nil as default value" do
      @doc[:default].should == nil
    end
  end