Loading lib/jsduck/lexer.rb +17 −3 Original line number Diff line number Diff line Loading @@ -127,12 +127,12 @@ module JsDuck elsif @input.check(/'/) return { :type => :string, :value => @input.scan(/'([^'\\]|\\.)*'/m).sub(/^'(.*)'$/m, "\\1") :value => @input.scan(/'([^'\\]|\\.)*('|\Z)/m).gsub(/\A'|'\Z/m, "") } elsif @input.check(/"/) return { :type => :string, :value => @input.scan(/"([^"\\]|\\.)*"/m).sub(/^"(.*)"$/m, "\\1") :value => @input.scan(/"([^"\\]|\\.)*("|\Z)/m).gsub(/\A"|"\Z/m, "") } elsif @input.check(/\//) # Several things begin with dash: Loading @@ -153,7 +153,7 @@ module JsDuck elsif regex? return { :type => :regex, :value => @input.scan(/\/([^\/\\]|\\.)*\/[gim]*/) :value => @input.scan(META_REGEX) } else return { Loading Loading @@ -202,6 +202,20 @@ module JsDuck @input.scan(/\s+/) end # A regex to match a regex META_REGEX = %r{ / (?# beginning ) ( [^/\[\\] (?# any character except \ / [ ) | \\. (?# an escaping \ followed by any character ) | \[ ([^\]\\]|\\.)* \] (?# [...] containing any characters including / ) (?# except \ ] which have to be escaped ) )* (/[gim]*|\Z) (?# ending + modifiers ) }x KEYWORDS = { "break" => true, "case" => true, Loading spec/lexer_spec.rb +16 −0 Original line number Diff line number Diff line Loading @@ -74,6 +74,10 @@ describe JsDuck::Lexer do end end it "allows [/] inside regex" do lex("/ [/] /").should == [[:regex, "/ [/] /"]] end describe "identifies strings" do before do Loading Loading @@ -151,6 +155,18 @@ describe JsDuck::Lexer do it "doc-comment" do lex("/** ").should == [[:doc_comment, "/** ", 1]] end it "regex" do lex("/[a-z] ").should == [[:regex, "/[a-z] "]] end it "single-quoted string" do lex("' ").should == [[:string, " "]] end it "double-quoted string" do lex('" ').should == [[:string, " "]] end end describe "passing StringScanner to constructor" do Loading Loading
lib/jsduck/lexer.rb +17 −3 Original line number Diff line number Diff line Loading @@ -127,12 +127,12 @@ module JsDuck elsif @input.check(/'/) return { :type => :string, :value => @input.scan(/'([^'\\]|\\.)*'/m).sub(/^'(.*)'$/m, "\\1") :value => @input.scan(/'([^'\\]|\\.)*('|\Z)/m).gsub(/\A'|'\Z/m, "") } elsif @input.check(/"/) return { :type => :string, :value => @input.scan(/"([^"\\]|\\.)*"/m).sub(/^"(.*)"$/m, "\\1") :value => @input.scan(/"([^"\\]|\\.)*("|\Z)/m).gsub(/\A"|"\Z/m, "") } elsif @input.check(/\//) # Several things begin with dash: Loading @@ -153,7 +153,7 @@ module JsDuck elsif regex? return { :type => :regex, :value => @input.scan(/\/([^\/\\]|\\.)*\/[gim]*/) :value => @input.scan(META_REGEX) } else return { Loading Loading @@ -202,6 +202,20 @@ module JsDuck @input.scan(/\s+/) end # A regex to match a regex META_REGEX = %r{ / (?# beginning ) ( [^/\[\\] (?# any character except \ / [ ) | \\. (?# an escaping \ followed by any character ) | \[ ([^\]\\]|\\.)* \] (?# [...] containing any characters including / ) (?# except \ ] which have to be escaped ) )* (/[gim]*|\Z) (?# ending + modifiers ) }x KEYWORDS = { "break" => true, "case" => true, Loading
spec/lexer_spec.rb +16 −0 Original line number Diff line number Diff line Loading @@ -74,6 +74,10 @@ describe JsDuck::Lexer do end end it "allows [/] inside regex" do lex("/ [/] /").should == [[:regex, "/ [/] /"]] end describe "identifies strings" do before do Loading Loading @@ -151,6 +155,18 @@ describe JsDuck::Lexer do it "doc-comment" do lex("/** ").should == [[:doc_comment, "/** ", 1]] end it "regex" do lex("/[a-z] ").should == [[:regex, "/[a-z] "]] end it "single-quoted string" do lex("' ").should == [[:string, " "]] end it "double-quoted string" do lex('" ').should == [[:string, " "]] end end describe "passing StringScanner to constructor" do Loading