[gtk/wip/otte/css: 32/79] cssimagelinear: Use gtk_css_parser_consume_function()



commit 0d6796e76e47d20259875f9e73aafc7d24eefb7f
Author: Benjamin Otte <otte redhat com>
Date:   Sun Mar 31 13:39:58 2019 +0200

    cssimagelinear: Use gtk_css_parser_consume_function()

 gtk/gtkcssimagelinear.c | 132 +++++++++++++++++++++++++-----------------------
 1 file changed, 70 insertions(+), 62 deletions(-)
---
diff --git a/gtk/gtkcssimagelinear.c b/gtk/gtkcssimagelinear.c
index 9ca1b88bd9..ef3ab75ed1 100644
--- a/gtk/gtkcssimagelinear.c
+++ b/gtk/gtkcssimagelinear.c
@@ -254,24 +254,43 @@ gtk_css_image_linear_snapshot (GtkCssImage        *image,
     }
 }
 
-
-static gboolean
-gtk_css_image_linear_parse (GtkCssImage  *image,
-                            GtkCssParser *parser)
+static guint
+gtk_css_image_linear_parse_color_stop (GtkCssImageLinear *self,
+                                       GtkCssParser      *parser)
 {
-  GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
-  guint i;
+  GtkCssImageLinearColorStop stop;
 
-  if (_gtk_css_parser_try (parser, "repeating-linear-gradient(", TRUE))
-    linear->repeating = TRUE;
-  else if (_gtk_css_parser_try (parser, "linear-gradient(", TRUE))
-    linear->repeating = FALSE;
+  stop.color = _gtk_css_color_value_parse (parser);
+  if (stop.color == NULL)
+    return 0;
+
+  if (gtk_css_number_value_can_parse (parser))
+    {
+      stop.offset = _gtk_css_number_value_parse (parser,
+                                                 GTK_CSS_PARSE_PERCENT
+                                                 | GTK_CSS_PARSE_LENGTH);
+      if (stop.offset == NULL)
+        {
+          _gtk_css_value_unref (stop.color);
+          return 0;
+        }
+    }
   else
     {
-      _gtk_css_parser_error (parser, "Not a linear gradient");
-      return FALSE;
+      stop.offset = NULL;
     }
 
+  g_array_append_val (self->stops, stop);
+
+  return 1;
+}
+
+static guint
+gtk_css_image_linear_parse_first_arg (GtkCssImageLinear *linear,
+                                      GtkCssParser      *parser)
+{
+  guint i;
+
   if (gtk_css_parser_try_ident (parser, "to"))
     {
       for (i = 0; i < 2; i++)
@@ -281,7 +300,7 @@ gtk_css_image_linear_parse (GtkCssImage  *image,
               if (linear->side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
                 {
                   _gtk_css_parser_error (parser, "Expected 'top', 'bottom' or comma");
-                  return FALSE;
+                  return 0;
                 }
               linear->side |= (1 << GTK_CSS_LEFT);
             }
@@ -290,7 +309,7 @@ gtk_css_image_linear_parse (GtkCssImage  *image,
               if (linear->side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
                 {
                   _gtk_css_parser_error (parser, "Expected 'top', 'bottom' or comma");
-                  return FALSE;
+                  return 0;
                 }
               linear->side |= (1 << GTK_CSS_RIGHT);
             }
@@ -299,7 +318,7 @@ gtk_css_image_linear_parse (GtkCssImage  *image,
               if (linear->side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
                 {
                   _gtk_css_parser_error (parser, "Expected 'left', 'right' or comma");
-                  return FALSE;
+                  return 0;
                 }
               linear->side |= (1 << GTK_CSS_TOP);
             }
@@ -308,7 +327,7 @@ gtk_css_image_linear_parse (GtkCssImage  *image,
               if (linear->side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
                 {
                   _gtk_css_parser_error (parser, "Expected 'left', 'right' or comma");
-                  return FALSE;
+                  return 0;
                 }
               linear->side |= (1 << GTK_CSS_BOTTOM);
             }
@@ -319,71 +338,60 @@ gtk_css_image_linear_parse (GtkCssImage  *image,
       if (linear->side == 0)
         {
           _gtk_css_parser_error (parser, "Expected side that gradient should go to");
-          return FALSE;
+          return 0;
         }
 
-      if (!gtk_css_parser_try_token (parser, GTK_CSS_TOKEN_COMMA))
-        {
-          _gtk_css_parser_error (parser, "Expected a comma");
-          return FALSE;
-        }
+      return 1;
     }
   else if (gtk_css_number_value_can_parse (parser))
     {
       linear->angle = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
       if (linear->angle == NULL)
-        return FALSE;
-
-      if (!gtk_css_parser_try_token (parser, GTK_CSS_TOKEN_COMMA))
-        {
-          _gtk_css_parser_error (parser, "Expected a comma");
-          return FALSE;
-        }
+        return 0;
+      
+      return 1;
     }
   else
-    linear->side = 1 << GTK_CSS_BOTTOM;
-
-  do {
-    GtkCssImageLinearColorStop stop;
+    {
+      linear->side = 1 << GTK_CSS_BOTTOM;
+      if (!gtk_css_image_linear_parse_color_stop (linear, parser))
+        return 0;
 
-    stop.color = _gtk_css_color_value_parse (parser);
-    if (stop.color == NULL)
-      return FALSE;
+      return 2;
+    }
+}
 
-    if (gtk_css_number_value_can_parse (parser))
-      {
-        stop.offset = _gtk_css_number_value_parse (parser,
-                                                   GTK_CSS_PARSE_PERCENT
-                                                   | GTK_CSS_PARSE_LENGTH);
-        if (stop.offset == NULL)
-          {
-            _gtk_css_value_unref (stop.color);
-            return FALSE;
-          }
-      }
-    else
-      {
-        stop.offset = NULL;
-      }
+static guint
+gtk_css_image_linear_parse_arg (GtkCssParser *parser,
+                                guint         arg,
+                                gpointer      data)
+{
+  GtkCssImageLinear *self = data;
 
-    g_array_append_val (linear->stops, stop);
+  if (arg == 0)
+    return gtk_css_image_linear_parse_first_arg (self, parser);
+  else
+    return gtk_css_image_linear_parse_color_stop (self, parser);
 
-  } while (gtk_css_parser_try_token (parser, GTK_CSS_TOKEN_COMMA));
+}
 
-  if (linear->stops->len < 2)
-    {
-      _gtk_css_parser_error (parser, "%s() needs at least 2 color stops.",
-                             linear->repeating ? "repeating-linear-gradient" : "linear-gradient");
-      return FALSE;
-    }
+static gboolean
+gtk_css_image_linear_parse (GtkCssImage  *image,
+                            GtkCssParser *parser)
+{
+  GtkCssImageLinear *self = GTK_CSS_IMAGE_LINEAR (image);
 
-  if (!_gtk_css_parser_try (parser, ")", TRUE))
+  if (gtk_css_parser_has_function (parser, "repeating-linear-gradient"))
+    self->repeating = TRUE;
+  else if (gtk_css_parser_has_function (parser, "linear-gradient"))
+    self->repeating = FALSE;
+  else
     {
-      _gtk_css_parser_error (parser, "Missing closing bracket at end of linear gradient");
+      _gtk_css_parser_error (parser, "Not a linear gradient");
       return FALSE;
     }
 
-  return TRUE;
+  return gtk_css_parser_consume_function (parser, 3, G_MAXUINT, gtk_css_image_linear_parse_arg, self);
 }
 
 static void


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