]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
parser: Split out Parser.parse_switch_section_statement()
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 2 Nov 2021 12:22:00 +0000 (13:22 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 11 Nov 2021 14:50:33 +0000 (15:50 +0100)
vala/valaparser.vala

index f71f6e89cb4f1974ae128d769de738d01e12a206..64266126be2424d535bdc97502d143c1502aab56 100644 (file)
@@ -2121,28 +2121,34 @@ public class Vala.Parser : CodeVisitor {
                var stmt = new SwitchStatement (condition, get_src (begin));
                expect (TokenType.OPEN_BRACE);
                while (current () != TokenType.CLOSE_BRACE) {
-                       begin = get_location ();
-                       var section = new SwitchSection (get_src (begin));
-                       do {
-                               if (accept (TokenType.CASE)) {
-                                       section.add_label (new SwitchLabel (parse_expression (), get_src (begin)));
-                                       while (current () == TokenType.COMMA) {
-                                               expect (TokenType.COMMA);
-                                               section.add_label (new SwitchLabel (parse_expression (), get_src (begin)));
-                                       }
-                               } else {
-                                       expect (TokenType.DEFAULT);
-                                       section.add_label (new SwitchLabel.with_default (get_src (begin)));
-                               }
-                               expect (TokenType.COLON);
-                       } while (current () == TokenType.CASE || current () == TokenType.DEFAULT);
-                       parse_statements (section);
-                       stmt.add_section (section);
+                       stmt.add_section ((SwitchSection) parse_switch_section_statement ());
                }
                expect (TokenType.CLOSE_BRACE);
                return stmt;
        }
 
+       Statement parse_switch_section_statement () throws ParseError {
+               var begin = get_location ();
+               var section = new SwitchSection (get_src (begin));
+               do {
+                       if (accept (TokenType.CASE)) {
+                               section.add_label (new SwitchLabel (parse_expression (), get_src (begin)));
+                               while (current () == TokenType.COMMA) {
+                                       expect (TokenType.COMMA);
+                                       section.add_label (new SwitchLabel (parse_expression (), get_src (begin)));
+                               }
+                               expect (TokenType.COLON);
+                       } else if (accept (TokenType.DEFAULT)) {
+                               section.add_label (new SwitchLabel.with_default (get_src (begin)));
+                               expect (TokenType.COLON);
+                       } else {
+                               throw new ParseError.SYNTAX ("expected `case' or `default' switch label");
+                       }
+               } while (current () == TokenType.CASE || current () == TokenType.DEFAULT);
+               parse_statements (section);
+               return section;
+       }
+
        Statement parse_while_statement () throws ParseError {
                var begin = get_location ();
                expect (TokenType.WHILE);