]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
parser: Clean up creation of constant declaration
authorRico Tzschichholz <ricotz@ubuntu.com>
Sun, 6 Feb 2022 13:29:55 +0000 (14:29 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 9 Feb 2022 21:26:27 +0000 (22:26 +0100)
vala/valaparser.vala

index 8661565f34ff4916713f55baef86d7982c06f77c..4f9b99160aff0d2212b96412a1c681b64b87cc92 100644 (file)
@@ -2879,6 +2879,7 @@ public class Vala.Parser : CodeVisitor {
                string id = parse_identifier ();
 
                type = parse_inline_array_type (type);
+               var src = get_src (begin);
 
                // constant arrays don't own their element
                unowned ArrayType? array_type = type as ArrayType;
@@ -2886,7 +2887,13 @@ public class Vala.Parser : CodeVisitor {
                        array_type.element_type.value_owned = false;
                }
 
-               var c = new Constant (id, type, null, get_src (begin), comment);
+               Expression? initializer = null;
+               if (accept (TokenType.ASSIGN)) {
+                       initializer = parse_expression ();
+               }
+               expect (TokenType.SEMICOLON);
+
+               var c = new Constant (id, type, initializer, src, comment);
                c.access = access;
                if (ModifierFlags.EXTERN in flags) {
                        c.is_extern = true;
@@ -2904,11 +2911,6 @@ public class Vala.Parser : CodeVisitor {
                        Report.error (c.source_reference, "`owned' is not allowed on constants");
                }
 
-               if (accept (TokenType.ASSIGN)) {
-                       c.value = parse_expression ();
-               }
-               expect (TokenType.SEMICOLON);
-
                parent.add_constant (c);
        }