[vala/wip/issue/1246: 2/2] parser: Better handling of misplaced switch sections
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/issue/1246: 2/2] parser: Better handling of misplaced switch sections
- Date: Tue, 2 Nov 2021 12:22:27 +0000 (UTC)
commit 2215b53bdfa1d95e54e8363bd53e0ff834950ece
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Tue Nov 2 13:22:00 2021 +0100
parser: Better handling of misplaced switch sections
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1246
tests/Makefile.am | 1 +
tests/parser/switch-section-outside-switch.test | 5 +++++
vala/valaparser.vala | 15 +++++++++++++--
vala/valaswitchsection.vala | 6 ++++++
4 files changed, 25 insertions(+), 2 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 413132a6f..d5ddb4917 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -929,6 +929,7 @@ TESTS = \
parser/statement-keyword-as-identifier.vala \
parser/statement-outside-root.test \
parser/switch-statement.vala \
+ parser/switch-section-outside-switch.test \
parser/template.vala \
parser/tuple.vala \
parser/unsupported-property-async.test \
diff --git a/tests/parser/switch-section-outside-switch.test b/tests/parser/switch-section-outside-switch.test
new file mode 100644
index 000000000..8a888d83c
--- /dev/null
+++ b/tests/parser/switch-section-outside-switch.test
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+ case 23:
+}
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 02b69cc0a..76c2bc92e 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -1634,6 +1634,10 @@ public class Vala.Parser : CodeVisitor {
case TokenType.SWITCH:
stmt = parse_switch_statement ();
break;
+ case TokenType.CASE:
+ case TokenType.DEFAULT:
+ stmt = parse_switch_section_statement ();
+ break;
case TokenType.WHILE:
stmt = parse_while_statement ();
break;
@@ -1700,8 +1704,6 @@ public class Vala.Parser : CodeVisitor {
void parse_statements (Block block) throws ParseError {
while (current () != TokenType.CLOSE_BRACE
- && current () != TokenType.CASE
- && current () != TokenType.DEFAULT
&& current () != TokenType.EOF) {
try {
Statement stmt = null;
@@ -1733,6 +1735,15 @@ public class Vala.Parser : CodeVisitor {
case TokenType.WITH:
stmt = parse_statement (current ());
break;
+ case TokenType.CASE:
+ case TokenType.DEFAULT:
+ if (block is SwitchSection) {
+ // begin a new switch case
+ return;
+ } else {
+ stmt = parse_statement (current ());
+ }
+ break;
case TokenType.VAR:
is_decl = true;
parse_local_variable_declarations (block);
diff --git a/vala/valaswitchsection.vala b/vala/valaswitchsection.vala
index af5be68dc..a44256b85 100644
--- a/vala/valaswitchsection.vala
+++ b/vala/valaswitchsection.vala
@@ -88,6 +88,12 @@ public class Vala.SwitchSection : Block {
return !error;
}
+ if (!(parent_node is SwitchStatement)) {
+ Report.error (source_reference, "no enclosing switch statement found");
+ error = true;
+ return false;
+ }
+
foreach (SwitchLabel label in get_labels ()) {
label.check (context);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]