Commit ac0b7080 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Lexer no more depends on DocComment.

The Parser now brings Lexer and DocComment together.

- Moved some more tests to Lexer testsuite.
- The :doc_comment part of it is now more easily testable.
- Additionally assert_tokens now checks the nr of tokens.
parent eccb7874
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
require 'strscan'
require 'doc_comment'

module JsDuck

@@ -50,7 +49,7 @@ module JsDuck
        elsif @input.check(/\/\*\*/) then
          @tokens << {
            :type => :doc_comment,
            :value => DocComment.new(@input.scan_until(/\*\//))
            :value => @input.scan_until(/\*\//)
          }
        elsif @input.check(/"/) then
          @tokens << {
+2 −1
Original line number Diff line number Diff line
require 'lexer'
require 'doc_comment'

module JsDuck

@@ -11,7 +12,7 @@ module JsDuck
    def parse
      while !@lex.empty? do
        if look(:doc_comment) then
          doc = match(:doc_comment)
          doc = DocComment.new(match(:doc_comment))
          block = code_block
          if block[:type] == :function then
            doc.set_default_name(*block[:name]) if block[:name]
+0 −12
Original line number Diff line number Diff line
@@ -3,18 +3,6 @@ require "test/unit"
 
class TestJsDuck < Test::Unit::TestCase

  def test_no_doc_comments
    assert_equal([], JsDuck.parse("var foo = 8;") )
  end

  def test_singleline_comment_until_file_end
    assert_equal([], JsDuck.parse("// ") )
  end

  def test_multiline_comment_until_file_end
    assert_equal([], JsDuck.parse("/* ") )
  end

  def test_function
    docs = JsDuck.parse("
/**
+12 −7
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ class TestLexer < Test::Unit::TestCase
    expected_tokens.each do |t|
      assert_equal({:type => t[0], :value => t[1]}, lex.next(true))
    end
    assert(lex.empty?)
  end

  def test_simple
@@ -38,10 +39,10 @@ class TestLexer < Test::Unit::TestCase
    d = '"' # double-quote
    s = "'" # single-quote
    b = "\\" # backslash
    assert_tokens(d+s+d + ' "blah"', [[:string, s]])
    assert_tokens(s+d+s + ' "blah"', [[:string, d]])
    assert_tokens(d+b+d+d + ' "blah"', [[:string, d]])
    assert_tokens(s+b+s+s + ' "blah"', [[:string, s]])
    assert_tokens(d+s+d   + ' "blah"', [[:string, s], [:string, "blah"]])
    assert_tokens(s+d+s   + ' "blah"', [[:string, d], [:string, "blah"]])
    assert_tokens(d+b+d+d + ' "blah"', [[:string, d], [:string, "blah"]])
    assert_tokens(s+b+s+s + ' "blah"', [[:string, s], [:string, "blah"]])
  end

  def test_comments
@@ -49,9 +50,13 @@ class TestLexer < Test::Unit::TestCase
    assert_tokens("a /* foo */ b", [[:ident, "a"], [:ident, "b"]])
  end

  def test_comments_until_file_end
    assert_tokens("// ", [])
    assert_tokens("/* ", [])
  end

  def test_doc_comment
    lex = JsDuck::Lexer.new("/** foo */")
    assert_equal(:doc_comment, lex.next(true)[:type])
    assert_tokens("/** foo */", [[:doc_comment, "/** foo */"]])
  end
end