syntax: 'do' blocks only accept labelled breaks
authorJean Privat <jean@pryen.org>
Thu, 6 Aug 2009 19:10:22 +0000 (15:10 -0400)
committerJean Privat <jean@pryen.org>
Thu, 6 Aug 2009 19:10:22 +0000 (15:10 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/syntax/escape.nit
tests/sav/base_label_do_alt4.sav

index fbd8a77..eb4deb0 100644 (file)
@@ -47,13 +47,16 @@ class EscapableContext
                end
        end
 
-       # Is there no block in the stack?
-       fun is_empty: Bool do return _stack.is_empty
-
-       # Return the current block (the last stacked)
-       fun head: EscapableBlock
+       # Return the last stacked block that accepts unlabelled break/continue
+       fun head: nullable EscapableBlock
        do
-               return _stack.last
+               var i = _stack.length - 1
+               while i >= 0 do
+                       var h = _stack[i]
+                       if not (h isa BreakOnlyEscapableBlock) then return h
+                       i -= 1
+               end
+               return null
        end
 
        # Return the block associed to a label
@@ -116,7 +119,7 @@ class EscapableBlock
        end
 end
 
-# specific EscapableBlock where only break can be used
+# specific EscapableBlock where only labelled break can be used
 class BreakOnlyEscapableBlock
 special EscapableBlock
        redef fun is_break_block: Bool do return true
@@ -161,11 +164,11 @@ special ALabelable
                var nl = n_label
                if nl != null then
                        block = lctx.get_by_label(nl)
-               else if lctx.is_empty then
-                       lctx.visitor.error(self, "Syntax Error: '{kwname}' statment outside block.")
-                       return null
                else
                        block = lctx.head
+                       if block == null then
+                               lctx.visitor.error(self, "Syntax Error: '{kwname}' statment outside block.")
+                       end
                end
                _escapable = block
                return block
index 150a5fb..b93fb21 100644 (file)
@@ -1,5 +1 @@
-1
-2
-3
-5
-6
+alt/base_label_do_alt4.nit:28,3--7: Syntax Error: 'break' statment outside block.