syntax: handle labels for 'do'
authorJean Privat <jean@pryen.org>
Mon, 27 Jul 2009 04:23:24 +0000 (00:23 -0400)
committerJean Privat <jean@pryen.org>
Mon, 27 Jul 2009 07:36:36 +0000 (03:36 -0400)
Error message for 'break closures' was changed and reused for 'do'.

Signed-off-by: Jean Privat <jean@pryen.org>

14 files changed:
src/syntax/escape.nit
src/syntax/icode_generation.nit
src/syntax/typing.nit
tests/base_label_do.nit [new file with mode: 0644]
tests/sav/base_closure_break_alt6.sav
tests/sav/base_closure_break_alt7.sav
tests/sav/base_label_do.sav [new file with mode: 0644]
tests/sav/base_label_do_alt1.sav [new file with mode: 0644]
tests/sav/base_label_do_alt2.sav [new file with mode: 0644]
tests/sav/base_label_do_alt3.sav [new file with mode: 0644]
tests/sav/base_label_do_alt4.sav [new file with mode: 0644]
tests/sav/base_label_do_alt5.sav [new file with mode: 0644]
tests/sav/base_label_do_alt6.sav [new file with mode: 0644]
tests/sav/base_label_do_alt7.sav [new file with mode: 0644]

index 9cc40a7..fbd8a77 100644 (file)
@@ -88,7 +88,9 @@ end
 ###############################################################################
 
 # A escapable block correspond to a block statement where break and/or continue can by used
-# For and while use this class. closures uses the EscapableClosure subclass.
+# 'for' and 'while' use this class
+# 'do' uses the BreakOnlyEscapableBlock subclass
+# closures uses the EscapableClosure subclass
 class EscapableBlock
        # The syntax node of the block
        readable var _node: ANode
@@ -114,6 +116,14 @@ class EscapableBlock
        end
 end
 
+# specific EscapableBlock where only break can be used
+class BreakOnlyEscapableBlock
+special EscapableBlock
+       redef fun is_break_block: Bool do return true
+
+       init(node: ANode) do super
+end
+
 # specific EscapableBlock for closures
 class EscapableClosure
 special EscapableBlock
index 43662f9..c9d4438 100644 (file)
@@ -630,7 +630,15 @@ end
 redef class ADoExpr
        redef fun generate_icode(v)
        do
+               var seq_old = v.seq
+               var seq = new ISeq
+               v.stmt(seq)
+               escapable.break_seq = seq
+               v.seq = seq
+
                v.generate_stmt(n_block)
+
+               v.seq = seq_old
                return null
        end
 end
index 4038123..4608ccd 100644 (file)
@@ -414,7 +414,7 @@ redef class AContinueExpr
                if esc == null then return
 
                if esc.is_break_block then
-                       v.error(self, "Error: 'continue' forbiden in break blocks.")
+                       v.error(self, "Error: cannot 'continue', only 'break'.")
                        return
                end
 
@@ -458,6 +458,23 @@ redef class AAbortExpr
        end
 end
 
+redef class ADoExpr
+       # The corresponding escapable block
+       readable var _escapable: nullable EscapableBlock
+
+       redef fun accept_typing(v)
+       do
+               var escapable = new BreakOnlyEscapableBlock(self)
+               _escapable = escapable
+               v.escapable_ctx.push(escapable, n_label)
+
+               super
+
+               v.escapable_ctx.pop
+               _is_typed = true
+       end
+end
+
 redef class AIfExpr
        redef fun accept_typing(v)
        do
diff --git a/tests/base_label_do.nit b/tests/base_label_do.nit
new file mode 100644 (file)
index 0000000..efb6c82
--- /dev/null
@@ -0,0 +1,38 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import kernel
+
+1.output
+do
+       2.output
+       do
+               3.output
+               do end label l3
+               #alt1#break label l2
+               #alt2#break label l3
+               #alt3#break label l4
+               #alt4#break
+               #alt5#continue label l1
+               break label l1
+               4.output
+       end label l2#!alt6#
+       #alt6#end label l1
+       5.output
+end label l1
+6.output
+
+#alt7#do end label l1
index 80d327a..de5aa6b 100644 (file)
@@ -1 +1 @@
-alt/base_closure_break_alt6.nit:40,3--10: Error: 'continue' forbiden in break blocks.
+alt/base_closure_break_alt6.nit:40,3--10: Error: cannot 'continue', only 'break'.
index 4cf1e7c..07f91e2 100644 (file)
@@ -1 +1 @@
-alt/base_closure_break_alt7.nit:41,3--14: Error: 'continue' forbiden in break blocks.
+alt/base_closure_break_alt7.nit:41,3--14: Error: cannot 'continue', only 'break'.
diff --git a/tests/sav/base_label_do.sav b/tests/sav/base_label_do.sav
new file mode 100644 (file)
index 0000000..c65db77
--- /dev/null
@@ -0,0 +1,4 @@
+1
+2
+3
+6
diff --git a/tests/sav/base_label_do_alt1.sav b/tests/sav/base_label_do_alt1.sav
new file mode 100644 (file)
index 0000000..150a5fb
--- /dev/null
@@ -0,0 +1,5 @@
+1
+2
+3
+5
+6
diff --git a/tests/sav/base_label_do_alt2.sav b/tests/sav/base_label_do_alt2.sav
new file mode 100644 (file)
index 0000000..004f21e
--- /dev/null
@@ -0,0 +1 @@
+alt/base_label_do_alt2.nit:26,9--16: Syntax error: invalid label l3.
diff --git a/tests/sav/base_label_do_alt3.sav b/tests/sav/base_label_do_alt3.sav
new file mode 100644 (file)
index 0000000..db31e51
--- /dev/null
@@ -0,0 +1 @@
+alt/base_label_do_alt3.nit:27,9--16: Syntax error: invalid label l4.
diff --git a/tests/sav/base_label_do_alt4.sav b/tests/sav/base_label_do_alt4.sav
new file mode 100644 (file)
index 0000000..150a5fb
--- /dev/null
@@ -0,0 +1,5 @@
+1
+2
+3
+5
+6
diff --git a/tests/sav/base_label_do_alt5.sav b/tests/sav/base_label_do_alt5.sav
new file mode 100644 (file)
index 0000000..f40de95
--- /dev/null
@@ -0,0 +1 @@
+alt/base_label_do_alt5.nit:29,3--19: Error: cannot 'continue', only 'break'.
diff --git a/tests/sav/base_label_do_alt6.sav b/tests/sav/base_label_do_alt6.sav
new file mode 100644 (file)
index 0000000..f86d21c
--- /dev/null
@@ -0,0 +1 @@
+alt/base_label_do_alt6.nit:32,6--13: Syntax error: label l1 already defined at 34,5--12.
diff --git a/tests/sav/base_label_do_alt7.sav b/tests/sav/base_label_do_alt7.sav
new file mode 100644 (file)
index 0000000..b0b9dbb
--- /dev/null
@@ -0,0 +1 @@
+alt/base_label_do_alt7.nit:38,8--15: Syntax error: label l1 already defined at 35,5--12.