From d2595afe452314d3daeb1bde9d02ccc52f00a427 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 6 Aug 2009 15:10:22 -0400 Subject: [PATCH] syntax: 'do' blocks only accept labelled breaks Signed-off-by: Jean Privat --- src/syntax/escape.nit | 23 +++++++++++++---------- tests/sav/base_label_do_alt4.sav | 6 +----- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/syntax/escape.nit b/src/syntax/escape.nit index fbd8a77..eb4deb0 100644 --- a/src/syntax/escape.nit +++ b/src/syntax/escape.nit @@ -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 diff --git a/tests/sav/base_label_do_alt4.sav b/tests/sav/base_label_do_alt4.sav index 150a5fb..b93fb21 100644 --- a/tests/sav/base_label_do_alt4.sav +++ b/tests/sav/base_label_do_alt4.sav @@ -1,5 +1 @@ -1 -2 -3 -5 -6 +alt/base_label_do_alt4.nit:28,3--7: Syntax Error: 'break' statment outside block. -- 1.7.9.5