[vala/wip/issue/327: 27/27] Cleaning



commit ec959b678f84c36051b44d046b7b5e6e473b20ea
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Wed Apr 22 22:33:53 2020 +0200

    Cleaning

 vala/valaparser.vala        | 45 +++++++++++++----------------
 vala/valawithstatement.vala | 70 +++++++++------------------------------------
 2 files changed, 33 insertions(+), 82 deletions(-)
---
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 0b847e145..2398bbfdc 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -1908,7 +1908,7 @@ public class Vala.Parser : CodeVisitor {
                expect (TokenType.SEMICOLON);
        }
 
-       LocalVariable parse_local_variable (DataType? variable_type) throws ParseError {
+       LocalVariable parse_local_variable (DataType? variable_type, bool expect_initializer = false) throws 
ParseError {
                var begin = get_location ();
                string id = parse_identifier ();
                var type = parse_inline_array_type (variable_type);
@@ -1917,6 +1917,10 @@ public class Vala.Parser : CodeVisitor {
                Expression initializer = null;
                if (accept (TokenType.ASSIGN)) {
                        initializer = parse_expression ();
+               } else if (expect_initializer) {
+                       report_parse_error (new ParseError.SYNTAX ("expected initializer"));
+                       prev ();
+                       initializer = new InvalidExpression ();
                }
                return new LocalVariable (type, id, initializer, src);
        }
@@ -2264,47 +2268,38 @@ public class Vala.Parser : CodeVisitor {
                return new DeleteStatement (expr, src);
        }
 
-       inline void expect_or_throw (TokenType type) throws ParseError {
-               // Don't continue on any minor issue as the with-syntax is a fragile left recursion
-               if (!accept (type)) {
-                       throw new ParseError.SYNTAX (@"expected $(type.to_string ())");
-               }
-       }
-
        Statement? parse_with_statement () throws ParseError {
                var begin = get_location ();
-               expect_or_throw (TokenType.WITH);
-               expect_or_throw (TokenType.OPEN_PARENS);
+               expect (TokenType.WITH);
+               expect (TokenType.OPEN_PARENS);
                var expr_or_decl = get_location ();
 
-               DataType? variable_type = null;
-               string? variable_name = null;
+               LocalVariable? local = null;
 
                // Try "with (expr)"
                Expression expr = parse_expression ();
                if (!accept (TokenType.CLOSE_PARENS)) {
                        // Try "with (var identifier = expr)"
                        rollback (expr_or_decl);
-                       if (accept (TokenType.VAR)) {
-                               variable_name = parse_identifier ();
-                               expect_or_throw (TokenType.ASSIGN);
+                       DataType variable_type;
+                       if (accept (TokenType.UNOWNED) && accept (TokenType.VAR)) {
+                               variable_type = new VarType (false);
                        } else {
-                               // Try "with (type identifier = expr)"
-                               variable_type = parse_type (true, true);
-                               variable_name = parse_identifier ();
-                               if (!accept (TokenType.ASSIGN)) {
-                                       // Fallback to "with (expr)"
-                                       rollback (expr_or_decl);
+                               rollback (expr_or_decl);
+                               if (accept (TokenType.VAR)) {
+                                       variable_type = new VarType ();
+                               } else {
+                                       variable_type = parse_type (true, true);
                                }
                        }
-
-                       expr = parse_expression ();
-                       expect_or_throw (TokenType.CLOSE_PARENS);
+                       local = parse_local_variable (variable_type, true);
+                       expr = local.initializer;
+                       expect (TokenType.CLOSE_PARENS);
                }
 
                var src = get_src (begin);
                var body = parse_embedded_statement ("with", false);
-               return new WithStatement (variable_type, variable_name, expr, body, src);
+               return new WithStatement (local, expr, body, src);
        }
 
        string parse_attribute_value () throws ParseError {
diff --git a/vala/valawithstatement.vala b/vala/valawithstatement.vala
index ecc79b1b5..f873e600c 100644
--- a/vala/valawithstatement.vala
+++ b/vala/valawithstatement.vala
@@ -21,8 +21,6 @@
  */
 
 public class Vala.WithStatement : Block {
-       private static int next_with_id = 0;
-
        /**
         * Expression representing the type of body's dominant scope.
         */
@@ -34,28 +32,10 @@ public class Vala.WithStatement : Block {
                }
        }
 
-       /**
-        * Specifies the with-variable type.
-        */
-       public DataType? type_reference {
-               get { return _data_type; }
-               private set {
-                       _data_type = value;
-                       if (_data_type != null) {
-                               _data_type.parent_node = this;
-                       }
-               }
-       }
-
-       /**
-        * Specifies the with-variable name.
-        */
-       public string? with_variable_name { get; private set; }
-
        /**
         * Specifies the with-variable.
         */
-       public LocalVariable with_variable { get; private set; }
+       public LocalVariable? with_variable { get; private set; }
 
        /**
         * The block which dominant scope is type of expression.
@@ -64,23 +44,18 @@ public class Vala.WithStatement : Block {
                get { return _body; }
                private set {
                        _body = value;
-                       if (_body != null) {
-                               _body.parent_node = this;
-                       }
+                       _body.parent_node = this;
                }
        }
 
        Expression _expression;
        Block _body;
-       DataType? _data_type;
 
-       public WithStatement (DataType? type_reference, string? variable_name, Expression expression,
-                       Block body, SourceReference? source_reference = null) {
+       public WithStatement (LocalVariable? variable, Expression expression, Block body, SourceReference? 
source_reference = null) {
                base (source_reference);
+               this.with_variable = variable;
                this.expression = expression;
                this.body = body;
-               this.type_reference = type_reference;
-               this.with_variable_name = variable_name;
        }
 
        public override void accept (CodeVisitor visitor) {
@@ -92,25 +67,11 @@ public class Vala.WithStatement : Block {
                        expression.accept (visitor);
                }
 
-               if (type_reference != null) {
-                       type_reference.accept (visitor);
+               if (with_variable != null) {
+                       with_variable.accept (visitor);
                }
 
-               if (body != null) {
-                       body.accept (visitor);
-               }
-       }
-
-       public override void replace_expression (Expression old_node, Expression new_node) {
-               if (expression == old_node) {
-                       expression = new_node;
-               }
-       }
-
-       public override void replace_type (DataType old_type, DataType new_type) {
-               if (type_reference == old_type) {
-                       type_reference = new_type;
-               }
+               body.accept (visitor);
        }
 
        bool is_object_or_value_type (DataType? type) {
@@ -140,20 +101,15 @@ public class Vala.WithStatement : Block {
                        error = true;
                        Report.error (expression.source_reference, "with statement expects an object or basic 
type");
                        return false;
-               } else if (type_reference != null && !expression.value_type.compatible (type_reference)) {
-                       error = true;
-                       Report.error (expression.source_reference, "Cannot convert from `%s' to `%s'".printf 
(expression.value_type.to_string (), type_reference.to_string ()));
-                       return false;
-               }
-
-               if (type_reference == null) {
-                       type_reference = expression.value_type.copy ();
                }
 
                var local_var = expression.symbol_reference as LocalVariable;
-               if (with_variable_name != null || local_var == null) {
-                       var n = with_variable_name ?? "_with_local%d_".printf (next_with_id++);
-                       local_var = new LocalVariable (type_reference, n, expression, source_reference);
+               if (with_variable != null || local_var == null) {
+                       if (with_variable == null) {
+                               local_var = new LocalVariable (expression.value_type.copy (), get_temp_name 
(), expression, source_reference);
+                       } else {
+                               local_var = with_variable;
+                       }
                        body.insert_statement (0, new DeclarationStatement (local_var, source_reference));
                }
                with_variable = local_var;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]