diff --git a/lib/jsduck/js_parser.rb b/lib/jsduck/js_parser.rb index d15d2fc0157691e9e719e8fb3608f95a8a249ac2..b71989db5c4d073f568a7afae4e881428061d206 100644 --- a/lib/jsduck/js_parser.rb +++ b/lib/jsduck/js_parser.rb @@ -73,9 +73,9 @@ module JsDuck # := | | | # | def code_block - if look("function") + if look(:function) function - elsif look("var") + elsif look(:var) var_declaration elsif ext_look(:ns, ".", "define", "(", :string) ext_define @@ -86,7 +86,7 @@ module JsDuck elsif look(",", :ident, ":") || look(",", :string, ":") match(",") property_literal - elsif look(:ident) || look("this") + elsif look(:ident) || look(:this) maybe_assignment elsif look(:string) {:type => :assignment, :left => [match(:string)[:value]]} @@ -97,7 +97,7 @@ module JsDuck # := "function" [ ] def function - match("function") + match(:function) return { :type => :function, :name => look(:ident) ? match(:ident)[:value] : "", @@ -124,7 +124,7 @@ module JsDuck # := "var" def var_declaration - match("var") + match(:var) maybe_assignment end @@ -144,8 +144,9 @@ module JsDuck # := [ "this" | ] [ "." ]* def ident_chain - if look("this") - chain = [match("this")[:value]] + if look(:this) + match(:this) + chain = ["this"] else chain = [match(:ident)[:value]] end @@ -158,7 +159,7 @@ module JsDuck # := | | | def expression - if look("function") + if look(:function) function elsif ext_look(:ns, ".", "extend") ext_extend diff --git a/lib/jsduck/lexer.rb b/lib/jsduck/lexer.rb index b0605fbbf534ea558d2195326c52a3f16846ea65..e6c54480a6efac4445d77195e70634a4199d3daf 100644 --- a/lib/jsduck/lexer.rb +++ b/lib/jsduck/lexer.rb @@ -9,12 +9,14 @@ module JsDuck # # - :number -- 25 # - :string -- "Hello world" - # - :keyword -- "typeof" # - :ident -- "foo" # - :regex -- "/abc/i" # - :operator -- "+" # - :doc_comment -- "/** My comment */" # + # Plus a separate types for all keywords: :if, :while, :function, ... + # For keywords the type and value are the same. + # # Notice that doc-comments are recognized as tokens while normal # comments are ignored just as whitespace. # @@ -120,9 +122,10 @@ module JsDuck } elsif @input.check(/[a-zA-Z_$]/) value = @input.scan(/[$\w]+/) + kw = KEYWORDS[value] return { - :type => KEYWORDS[value] ? :keyword : :ident, - :value => value + :type => kw || :ident, + :value => kw || value } elsif @input.check(/'/) return { @@ -189,7 +192,7 @@ module JsDuck value = @previous_token[:value] if type == :ident || type == :number return false - elsif type == :keyword && value == "this" + elsif type == :this return false elsif type == :operator && (value == ")" || value == "]") return false @@ -217,31 +220,31 @@ module JsDuck }x KEYWORDS = { - "break" => true, - "case" => true, - "catch" => true, - "continue" => true, - "default" => true, - "delete" => true, - "do" => true, - "else" => true, - "finally" => true, - "for" => true, - "function" => true, - "if" => true, - "in" => true, - "instanceof" => true, - "new" => true, - "return" => true, - "switch" => true, - "this" => true, - "throw" => true, - "try" => true, - "typeof" => true, - "var" => true, - "void" => true, - "while" => true, - "with" => true, + "break" => :break, + "case" => :case, + "catch" => :catch, + "continue" => :continue, + "default" => :default, + "delete" => :delete, + "do" => :do, + "else" => :else, + "finally" => :finally, + "for" => :for, + "function" => :function, + "if" => :if, + "in" => :in, + "instanceof" => :instanceof, + "new" => :new, + "return" => :return, + "switch" => :switch, + "this" => :this, + "throw" => :throw, + "try" => :try, + "typeof" => :typeof, + "var" => :var, + "void" => :void, + "while" => :while, + "with" => :with, } end diff --git a/spec/aggregator_methods_and_events_spec.rb b/spec/aggregator_methods_and_events_spec.rb index 62bfc3611dcadbc5bc9a4d1dac2c22dbd7a11663..712b392d872d9e4a98a3d27f417b103fd83b25a3 100644 --- a/spec/aggregator_methods_and_events_spec.rb +++ b/spec/aggregator_methods_and_events_spec.rb @@ -151,6 +151,16 @@ describe JsDuck::Aggregator do end end + describe "doc-comment followed by 'function'" do + before do + @doc = parse("/** Some function */ 'function';")[0] + end + + it "isn't detected as method" do + @doc[:tagname].should_not == :method + end + end + describe "explicit @method without @param-s" do before do @doc = parse(<<-EOS)[0] diff --git a/spec/lexer_spec.rb b/spec/lexer_spec.rb index 3aac17cfb9a1528dd4ddc80456fede04121633f0..fd1fe667aed925981d90d9d78708a04d44cf1ad7 100644 --- a/spec/lexer_spec.rb +++ b/spec/lexer_spec.rb @@ -17,7 +17,7 @@ describe JsDuck::Lexer do it "tokenizes simple expression" do lex("var foo = 8;").should == [ - [:keyword, "var"], + [:var, :var], [:ident, "foo"], [:operator, "="], [:number, "8"], @@ -49,7 +49,7 @@ describe JsDuck::Lexer do it "when regex after return" do lex("return /foo/.test;").should == [ - [:keyword, "return"], + [:return, :return], [:regex, "/foo/"], [:operator, "."], [:ident, "test"], @@ -59,7 +59,7 @@ describe JsDuck::Lexer do it "when regex after typeof" do lex("typeof /foo/;").should == [ - [:keyword, "typeof"], + [:typeof, :typeof], [:regex, "/foo/"], [:operator, ";"] ] @@ -67,7 +67,7 @@ describe JsDuck::Lexer do it "when division after this" do lex("this / 3").should == [ - [:keyword, "this"], + [:this, :this], [:operator, "/"], [:number, "3"] ]