gplate/gplate

Removing all of the gscanner related stuff.
no_gscanner
2008-01-01, grim
e8b44cfead21
Parents 8ee36bd582e3
Children 5027775f3eef
Removing all of the gscanner related stuff.
Renamed process to tokenize
--- a/gplate/gplate-template.c Tue Jan 01 21:53:25 2008 -0600
+++ b/gplate/gplate-template.c Tue Jan 01 22:34:07 2008 -0600
@@ -32,39 +32,11 @@
/******************************************************************************
* Enums
*****************************************************************************/
-enum {
- GPLATE_TEMPLATE_SCOPE_NORMAL = 0,
- GPLATE_TEMPLATE_SCOPE_VAR,
- GPLATE_TEMPLATE_SCOPE_VAR_CLOSING,
- GPLATE_TEMPLATE_SCOPE_CODE,
- GPLATE_TEMPLATE_SCOPE_CODE_CLOSING,
- GPLATE_TEMPLATE_SCOPE_INCLUDE,
- GPLATE_TEMPLATE_SCOPE_IF,
- GPLATE_TEMPLATE_SCOPE_ELSE,
- GPLATE_TEMPLATE_SCOPE_FOR,
-};
-
-enum {
- GPLATE_TEMPLATE_KEYWORD_VAR_OPEN = G_TOKEN_LAST,
- GPLATE_TEMPLATE_KEYWORD_VAR_CLOSE,
- GPLATE_TEMPLATE_KEYWORD_CODE_OPEN,
- GPLATE_TEMPLATE_KEYWORD_CODE_CLOSE,
- GPLATE_TEMPLATE_KEYWORD_INCLUDE,
- GPLATE_TEMPLATE_KEYWORD_IF,
- GPLATE_TEMPLATE_KEYWORD_ELSE,
- GPLATE_TEMPLATE_KEYWORD_ENDIF,
- GPLATE_TEMPLATE_KEYWORD_FOR,
- GPLATE_TEMPLATE_KEYWORD_ENDFOR,
-};
/******************************************************************************
* Structs
*****************************************************************************/
typedef struct {
- GPlateVariable *dict;
-
- GScanner *scanner;
- GList *scopes;
} GPlateTemplatePrivate;
/******************************************************************************
@@ -73,287 +45,13 @@
static GObjectClass *parent_class = NULL;
/******************************************************************************
- * Helpers
+ * Template Stuff
*****************************************************************************/
-#define add_symbol(scanner, symbol, value) \
- g_scanner_scope_add_symbol(scanner, 0, symbol, GINT_TO_POINTER((value)));
-
static void
-gplate_template_add_symbols(GScanner *scanner) {
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_NORMAL, "{{",
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_VAR_OPEN));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_NORMAL, "}}",
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_VAR_CLOSE));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_NORMAL, "{%",
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_CODE_OPEN));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_NORMAL, "%}",
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_CODE_CLOSE));
-
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- "include",
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_INCLUDE));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- "if",
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_IF));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- "else",
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_ELSE));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- "endif",
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_ENDIF));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- "for",
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_FOR));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- "endfor",
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_ENDFOR));
-}
-
-static inline void
-gplate_template_push_scope(GPlateTemplate *tplate, guint scope_id) {
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
-
- g_scanner_set_scope(priv->scanner, scope_id);
-
- priv->scopes = g_list_prepend(priv->scopes, GINT_TO_POINTER(scope_id));
-}
-
-static inline guint
-gplate_template_get_scope(const GPlateTemplate *tplate) {
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
-
- return (priv->scopes) ? GPOINTER_TO_INT(priv->scopes->data) :
- GPLATE_TEMPLATE_SCOPE_NORMAL;
-}
-
-static inline void
-gplate_template_pop_scope(GPlateTemplate *tplate) {
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
-
- priv->scopes = g_list_delete_link(priv->scopes, priv->scopes);
-
- g_scanner_set_scope(priv->scanner, gplate_template_get_scope(tplate));
-}
-
-/******************************************************************************
- * Collection Stuff
- *****************************************************************************/
-static GPlateVariable *
-gplate_template_find_variable(const GPlateCollection *c, const gchar *n) {
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(c);
-
- return gplate_collection_find_variable(GPLATE_COLLECTION(priv->dict), n);
-}
-
-static gboolean
-gplate_template_add_variable(GPlateCollection *c, GPlateVariable *v) {
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(c);
-
- return gplate_collection_add_variable(GPLATE_COLLECTION(priv->dict), v);
-}
-
-static void
-gplate_template_collection_init(GPlateCollectionIface *iface) {
- iface->find_variable = gplate_template_find_variable;
- iface->add_variable = gplate_template_add_variable;
-}
-
-/******************************************************************************
- * Scanner Stuff
- *****************************************************************************/
-static const GScannerConfig scanner_config = {
- "",
- G_CSET_a_2_z "_{}%" G_CSET_A_2_Z,
- G_CSET_a_2_z "{}%._0123456789" G_CSET_A_2_Z G_CSET_LATINS G_CSET_LATINC,
- "",
- FALSE, /* case insentive symbols */
- FALSE, /* keep multi-line comments as tokens */
- FALSE, /* keep single-line comments as tokens */
- TRUE, /* scan multi-line comments */
- TRUE, /* scan identifier */
- TRUE, /* single char identifiers */
- FALSE, /* don't make NULL an identifier */
- TRUE, /* scan for symbols */
- FALSE, /* don't care about binary numbers */
- FALSE, /* don't care about octal numbers */
- FALSE, /* don't care about float numbers */
- FALSE, /* don't care about hex numbers */
- FALSE, /* don't care about hex numbers with a $ */
- FALSE, /* strings don't belong in single quotes */
- FALSE, /* we don't care about double quotes */
- FALSE, /* we don't care about bin/oct/hex so don't make 'em ints */
- FALSE, /* we don't care about float, so don't make ints floats */
- TRUE, /* don't make our identifiers strings */
- TRUE, /* make chars tokens */
- FALSE, /* mark symbols as G_TOKEN_SYMBOL */
- TRUE, /* Search all scopes for symbols */
-};
-
-/******************************************************************************
- * Template stuff
- *****************************************************************************/
-static const GScannerConfig *
-gplate_template_real_get_config(GPlateTemplate *tlate) {
- return &scanner_config;
-}
-
-static gchar *
-gplate_template_real_process(GPlateTemplate *tplate, const gchar *text,
- guint len, GError **error)
+gplate_template_real_tokenize(GPlateTemplate *tplate,
+ const gchar *text, guint len,
+ GError **error)
{
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
- GPlateCollection *collection = GPLATE_COLLECTION(tplate);
- GError *e = NULL;
- GString *output = NULL;
- gchar *ret = NULL;
-
- priv->scanner = g_scanner_new(gplate_template_get_config(tplate));
-
- gplate_template_add_symbols(priv->scanner);
-
- /* set our current scope */
- gplate_template_push_scope(tplate, GPLATE_TEMPLATE_SCOPE_NORMAL);
-
- g_scanner_input_text(priv->scanner, text, len);
-
- /* always expect at least 2k, glib will allocate this to 4k */
- output = g_string_sized_new(2048);
-
- while(g_scanner_peek_next_token(priv->scanner) != G_TOKEN_EOF) {
- GTokenType type = g_scanner_get_next_token(priv->scanner);
- GTokenValue value = g_scanner_cur_value(priv->scanner);
- guint scope = gplate_template_get_scope(tplate);
-
- switch(type) {
- case G_TOKEN_STRING:
- if(scope == GPLATE_TEMPLATE_SCOPE_VAR) {
- GPlateVariable *var = NULL;
- const gchar *val = NULL;
-
- var = gplate_collection_find_variable(collection,
- value.v_string);
-
- if(GPLATE_IS_VARIABLE(var))
- val = gplate_variable_get_value(var);
-
- g_string_append_printf(output, "%s", (val) ? val : "");
-
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_VAR_CLOSING);
- } else if(scope == GPLATE_TEMPLATE_SCOPE_VAR_CLOSING) {
-#warning need to add error output here
- } else if(scope == GPLATE_TEMPLATE_SCOPE_INCLUDE) {
- GPlateTemplate *iplate = gplate_template_new();
- gchar *include = NULL;
-
- /* remove the include scope */
- gplate_template_pop_scope(tplate);
-
- include = gplate_template_process_file(iplate,
- value.v_string,
- error);
-
- g_string_append_printf(output, "%s", include);
-
- g_free(include);
- g_object_unref(G_OBJECT(iplate));
-
- /* set our scope to expect a close */
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_CODE_CLOSING);
- } else {
- g_string_append_printf(output, "%s", value.v_string);
- }
-
- break;
- case G_TOKEN_SYMBOL:
- switch(GPOINTER_TO_INT(value.v_symbol)) {
- case GPLATE_TEMPLATE_KEYWORD_VAR_OPEN:
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_VAR);
- break;
- case GPLATE_TEMPLATE_KEYWORD_VAR_CLOSE:
- if(gplate_template_get_scope(tplate) !=
- GPLATE_TEMPLATE_SCOPE_VAR_CLOSING)
- {
- g_scanner_error(priv->scanner,
- "no variable found!");
- } else {
- gplate_template_pop_scope(tplate);
- }
-
- gplate_template_pop_scope(tplate);
- break;
- case GPLATE_TEMPLATE_KEYWORD_CODE_OPEN:
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_CODE);
- break;
- case GPLATE_TEMPLATE_KEYWORD_CODE_CLOSE:
- if(gplate_template_get_scope(tplate) !=
- GPLATE_TEMPLATE_SCOPE_CODE_CLOSING)
- {
- g_scanner_error(priv->scanner, "syntax error!");
- } else {
- printf("%p: code close\n", priv->scanner);
- gplate_template_pop_scope(tplate);
- }
-
- break;
- case GPLATE_TEMPLATE_KEYWORD_INCLUDE:
- if(gplate_template_get_scope(tplate) ==
- GPLATE_TEMPLATE_SCOPE_CODE)
- {
- printf("include keyword\n");
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_INCLUDE);
- }
-# warning we need to throw an error here
- break;
- case GPLATE_TEMPLATE_KEYWORD_IF:
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_IF);
- break;
- case GPLATE_TEMPLATE_KEYWORD_ELSE:
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_ELSE);
- break;
- case GPLATE_TEMPLATE_KEYWORD_FOR:
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_FOR);
- default:
- g_string_append_printf(output, "%d", value.v_symbol);
- break;
- }
-
- break;
- default:
- printf("%010p: unhandle type: %d\n", type);
- printf("%010p: scope is: %u\n", priv->scanner, gplate_template_get_scope(tplate));
- if(scope != GPLATE_TEMPLATE_SCOPE_NORMAL) {
- switch(type) {
- case ' ':
- case '\t':
- case '\r':
- case '\n':
- break;
- default:
- g_string_append_printf(output, "%c", type);
- break;
- }
- } else {
- g_string_append_printf(output, "%c", type);
- }
-
- break;
- }
- }
-
- g_scanner_destroy(priv->scanner);
-
- ret = output->str;
- g_string_free(output, FALSE);
-
- return ret;
}
/******************************************************************************
@@ -363,8 +61,6 @@
gplate_template_finalize(GObject *obj) {
GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(obj);
- g_object_unref(G_OBJECT(priv->dict));
-
G_OBJECT_CLASS(parent_class)->finalize(obj);
}
@@ -378,16 +74,7 @@
obj_class->finalize = gplate_template_finalize;
- klass->get_config = gplate_template_real_get_config;
- klass->process = gplate_template_real_process;
-}
-
-static void
-gplate_template_init(GTypeInstance *instance, gpointer klass) {
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(instance);
-
- priv->dict = gplate_dictionary_variable_new("dict");
- priv->scopes = NULL;
+ klass->tokenize = gplate_template_real_tokenize;
}
/******************************************************************************
@@ -407,21 +94,12 @@
NULL,
sizeof(GPlateTemplate),
0,
- gplate_template_init,
- };
-
- static const GInterfaceInfo collection_info = {
- (GInterfaceInitFunc)gplate_template_collection_init,
- NULL,
NULL,
};
type = g_type_register_static(G_TYPE_OBJECT,
"GPlateTemplate",
- &info, (GTypeFlags)0);
-
- g_type_add_interface_static(type, GPLATE_TYPE_COLLECTION,
- &collection_info);
+ &info, 0);
}
return type;
@@ -441,54 +119,35 @@
NULL);
}
-const GScannerConfig *
-gplate_template_get_config(GPlateTemplate *tplate) {
- GPlateTemplateClass *klass = NULL;
-
- g_return_val_if_fail(GPLATE_IS_TEMPLATE(tplate), NULL);
-
- klass = GPLATE_TEMPLATE_GET_CLASS(tplate);
-
- if(klass && klass->get_config)
- return klass->get_config(tplate);
-
- return NULL;
-}
-
-gchar *
+void
gplate_template_process(GPlateTemplate *tplate, const gchar *text, guint len,
GError **error)
{
GPlateTemplateClass *klass = NULL;
- g_return_val_if_fail(GPLATE_IS_TEMPLATE(tplate), NULL);
+ g_return_if_fail(GPLATE_IS_TEMPLATE(tplate));
klass = GPLATE_TEMPLATE_GET_CLASS(tplate);
- if(klass && klass->process)
- return klass->process(tplate, text, len, error);
-
- return NULL;
+ if(klass && klass->tokenize)
+ klass->tokenize(tplate, text, len, error);
}
-gchar *
+void
gplate_template_process_file(GPlateTemplate *tplate, const gchar *filename,
GError **error)
{
gchar *contents = NULL;
gsize len = 0;
- gchar *ret = NULL;
- g_return_val_if_fail(GPLATE_IS_TEMPLATE(tplate), NULL);
- g_return_val_if_fail(filename, NULL);
+ g_return_if_fail(GPLATE_IS_TEMPLATE(tplate));
+ g_return_if_fail(filename);
if(!g_file_get_contents(filename, &contents, &len, error))
- return NULL;
+ return;
- ret = gplate_template_process(tplate, contents, len, error);
+ gplate_template_tokenize(tplate, contents, len, error);
g_free(contents);
-
- return ret;
}
--- a/gplate/gplate-template.h Tue Jan 01 21:53:25 2008 -0600
+++ b/gplate/gplate-template.h Tue Jan 01 22:34:07 2008 -0600
@@ -22,8 +22,6 @@
#include <glib.h>
#include <glib-object.h>
-#include <gplate/gplate-variable.h>
-
#define GPLATE_TYPE_TEMPLATE (gplate_template_get_gtype())
#define GPLATE_TEMPLATE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GPLATE_TYPE_TEMPLATE, GPlateTemplate))
#define GPLATE_TEMPLATE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GPLATE_TYPE_TEMPLATE, GPlateTemplateClass))
@@ -46,9 +44,7 @@
struct _GPlateTemplateClass {
GObjectClass parent;
- const GScannerConfig *(*get_config)(GPlateTemplate *tplate);
-
- gchar *(*process)(GPlateTemplate *tplate, const gchar *text, guint len, GError **error);
+ void (*tokenize)(GPlateTemplate *tplate, const gchar *text, guint len, GError **error);
void (*_gplate_reserved1)(void);
void (*_gplate_reserved2)(void);
@@ -62,10 +58,8 @@
GPlateTemplate *gplate_template_new(void);
-const GScannerConfig *gplate_template_get_config(GPlateTemplate *tplate);
-
-gchar *gplate_template_process(GPlateTemplate *tplate, const gchar *text, guint len, GError **error);
-gchar *gplate_template_process_file(GPlateTemplate *tplate, const gchar *filename, GError **error);
+void gplate_template_tokenize(GPlateTemplate *tplate, const gchar *text, guint len, GError **error);
+void gplate_template_tokenize_file(GPlateTemplate *tplate, const gchar *filename, GError **error);
G_END_DECLS