[macruby-changes] [4624] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:50:16 PDT 2010


Revision: 4624
          http://trac.macosforge.org/projects/ruby/changeset/4624
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:50:15 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Return whether or not the source contains a syntax error.

From: Eloy Duran <eloy.de.enige at gmail.com>

Modified Paths:
--------------
    DietRB/trunk/lib/irb/source.rb
    DietRB/trunk/spec/source_spec.rb

Modified: DietRB/trunk/lib/irb/source.rb
===================================================================
--- DietRB/trunk/lib/irb/source.rb	2010-10-08 10:50:06 UTC (rev 4623)
+++ DietRB/trunk/lib/irb/source.rb	2010-10-08 10:50:15 UTC (rev 4624)
@@ -29,7 +29,7 @@
     
     # Reflects on the accumulated source to see if it's a valid code block.
     def valid?
-      reflect.valid?
+      reflect.code_block?
     end
     
     # Returns a Reflector for the accumulated source and caches it.
@@ -41,7 +41,8 @@
       def initialize(source)
         super
         @level = 0
-        @valid = !parse.nil?
+        @syntax_error = false
+        @code_block = !parse.nil?
       end
       
       # Returns the code block indentation level.
@@ -63,10 +64,32 @@
       # This however is:
       #
       #   def foo; p :ok; end
-      def valid?
-        @valid
+      def code_block?
+        @code_block
       end
       
+      # Returns whether or not the source contains a syntax error. However, it
+      # ignores a syntax error resulting in a code block not ending yet.
+      #
+      # For example, this contains a syntax error:
+      #
+      #   def; foo
+      #
+      # This does not:
+      #
+      #   def foo
+      def syntax_error?
+        @syntax_error
+      end
+      
+      UNEXPECTED_END = "syntax error, unexpected $end"
+      
+      def on_parse_error(error) #:nodoc:
+        if code_block? || !error.start_with?(UNEXPECTED_END)
+          @syntax_error = true
+        end
+      end
+      
       INCREASE_LEVEL_KEYWORDS = %w{ class module def begin if unless case while for do }
       
       def on_kw(token) #:nodoc:

Modified: DietRB/trunk/spec/source_spec.rb
===================================================================
--- DietRB/trunk/spec/source_spec.rb	2010-10-08 10:50:06 UTC (rev 4623)
+++ DietRB/trunk/spec/source_spec.rb	2010-10-08 10:50:15 UTC (rev 4624)
@@ -83,17 +83,31 @@
   end
 end
 
+class Should
+  alias have be
+end
+
 describe "IRB::Source::Reflector" do
   def reflect(source)
     IRB::Source::Reflector.new(source)
   end
   
   it "returns whether or not the source is a valid code block" do
-    reflect("def foo").should.not.be.valid
-    reflect("def foo; p :ok").should.not.be.valid
-    reflect("def foo; p :ok; end").should.be.valid
+    reflect("def foo").should.not.be.code_block
+    reflect("def foo; p :ok").should.not.be.code_block
+    reflect("def foo; p :ok; end").should.be.code_block
   end
   
+  it "returns whether or not the source contains a syntax error, except a code block not ending" do
+    reflect("def;").should.have.syntax_error
+    reflect("def;").should.have.syntax_error
+    reflect("def foo").should.not.have.syntax_error
+    reflect("class A; }").should.have.syntax_error
+    reflect("class A; {" ).should.not.have.syntax_error
+    reflect("class A def foo").should.have.syntax_error
+    reflect("class A; def foo" ).should.not.have.syntax_error
+  end
+  
   it "returns the code block indentation level" do
     reflect("").level.should == 0
     reflect("class A").level.should == 1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/ced1d257/attachment-0001.html>


More information about the macruby-changes mailing list