[gtk/wip/otte/css: 50/66] cssparser: Improve location APIs



commit b7c5accb139ef13799945e98c7052b2e150edae7
Author: Benjamin Otte <otte redhat com>
Date:   Wed Apr 10 16:02:07 2019 +0200

    cssparser: Improve location APIs
    
    1. Export multiple locations
    2. Return the location instead of passing one in

 gtk/css/gtkcssparser.c        | 75 +++++++++++++++++++++++++++++++++++++++----
 gtk/css/gtkcssparserprivate.h |  6 ++--
 gtk/gtkcsssection.c           |  6 ++--
 3 files changed, 76 insertions(+), 11 deletions(-)
---
diff --git a/gtk/css/gtkcssparser.c b/gtk/css/gtkcssparser.c
index 2f02b33022..8162c42fa6 100644
--- a/gtk/css/gtkcssparser.c
+++ b/gtk/css/gtkcssparser.c
@@ -24,6 +24,7 @@
 
 #include "gtkcssenums.h"
 #include "gtkcsserror.h"
+#include "gtkcsslocationprivate.h"
 
 typedef struct _GtkCssParserBlock GtkCssParserBlock;
 
@@ -197,17 +198,79 @@ gtk_css_parser_resolve_url (GtkCssParser *self,
 }
 
 /**
- * gtk_css_parser_get_location:
+ * gtk_css_parser_get_start_location:
+ * @self: a #GtkCssParser
+ *
+ * Queries the location of the current token.
+ *
+ * This function will return the location of the start of the
+ * current token. In the case a token has been consumed, but no
+ * new token has been queried yet via gtk_css_parser_peek_token()
+ * or gtk_css_parser_get_token(), the previous token's start
+ * location will be returned.
+ *
+ * This function may return the same location as
+ * gtk_css_parser_get_end_location() - in particular at the
+ * beginning and end of the document.
+ *
+ * Returns: the start location
+ **/
+const GtkCssLocation *
+gtk_css_parser_get_start_location (GtkCssParser *self)
+{
+  return &self->location;
+}
+
+/**
+ * gtk_css_parser_get_end_location:
  * @self: a #GtkCssParser
  * @out_location: (caller-allocates) Place to store the location
  *
- * Queries the current location of the parser.
+ * Queries the location of the current token.
+ *
+ * This function will return the location of the end of the
+ * current token. In the case a token has been consumed, but no
+ * new token has been queried yet via gtk_css_parser_peek_token()
+ * or gtk_css_parser_get_token(), the previous token's end location
+ * will be returned.
+ *
+ * This function may return the same location as
+ * gtk_css_parser_get_start_location() - in particular at the
+ * beginning and end of the document.
+ *
+ * Returns: the end location
  **/
-void
-gtk_css_parser_get_location (GtkCssParser   *self,
-                             GtkCssLocation *out_location)
+const GtkCssLocation *
+gtk_css_parser_get_end_location (GtkCssParser *self)
 {
-  *out_location = self->location;
+  return gtk_css_tokenizer_get_location (self->tokenizer);
+}
+
+/**
+ * gtk_css_parser_get_block_location:
+ * @self: a #GtkCssParser
+ *
+ * Queries the start location of the token that started the current
+ * block that is being parsed.
+ *
+ * If no block is currently parsed, the beginning of the document
+ * is returned.
+ *
+ * Returns: The start location of the current block
+ */
+const GtkCssLocation *
+gtk_css_parser_get_block_location (GtkCssParser *self)
+{
+  GtkCssParserBlock *block;
+
+  if (self->blocks->len == 0)
+    {
+      static const GtkCssLocation start_of_document = { 0, };
+      return &start_of_document;
+    }
+  
+  block = &g_array_index (self->blocks, GtkCssParserBlock, self->blocks->len - 1);
+  return &block->start_location;
 }
 
 static void
diff --git a/gtk/css/gtkcssparserprivate.h b/gtk/css/gtkcssparserprivate.h
index f39d0025a3..fa6b5757fb 100644
--- a/gtk/css/gtkcssparserprivate.h
+++ b/gtk/css/gtkcssparserprivate.h
@@ -65,8 +65,10 @@ void                    gtk_css_parser_unref                    (GtkCssParser
 GFile *                 gtk_css_parser_get_file                 (GtkCssParser                   *self);
 GFile *                 gtk_css_parser_resolve_url              (GtkCssParser                   *self,
                                                                  const char                     *url);
-void                    gtk_css_parser_get_location             (GtkCssParser                   *self,
-                                                                 GtkCssLocation                 
*out_location);
+
+const GtkCssLocation *  gtk_css_parser_get_start_location       (GtkCssParser                   *self);
+const GtkCssLocation *  gtk_css_parser_get_end_location         (GtkCssParser                   *self);
+const GtkCssLocation *  gtk_css_parser_get_block_location       (GtkCssParser                   *self);
 
 const GtkCssToken *     gtk_css_parser_peek_token               (GtkCssParser                   *self);
 const GtkCssToken *     gtk_css_parser_get_token                (GtkCssParser                   *self);
diff --git a/gtk/gtkcsssection.c b/gtk/gtkcsssection.c
index 6fae99d6bf..c581ca952f 100644
--- a/gtk/gtkcsssection.c
+++ b/gtk/gtkcsssection.c
@@ -51,7 +51,7 @@ gtk_css_section_new_for_parser (GtkCssSection     *parent,
   if (section->file)
     g_object_ref (section->file);
   section->parser = parser;
-  gtk_css_parser_get_location (section->parser, &section->start_location);
+  section->start_location = *gtk_css_parser_get_start_location (section->parser);
 
   return section;
 }
@@ -62,7 +62,7 @@ _gtk_css_section_end (GtkCssSection *section)
   gtk_internal_return_if_fail (section != NULL);
   gtk_internal_return_if_fail (section->parser != NULL);
 
-  gtk_css_parser_get_location (section->parser, &section->end_location);
+  section->end_location = *gtk_css_parser_get_end_location (section->parser);
   section->parser = NULL;
 }
 
@@ -181,7 +181,7 @@ gtk_css_section_get_end_location (const GtkCssSection *section)
   gtk_internal_return_val_if_fail (section != NULL, NULL);
 
   if (section->parser)
-    gtk_css_parser_get_location (section->parser, (GtkCssLocation *) &section->end_location);
+    return gtk_css_parser_get_end_location (section->parser);
 
   return &section->end_location;
 }


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