--- 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 @@
/******************************************************************************
*****************************************************************************/
- 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,
- 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,
/******************************************************************************
*****************************************************************************/
/******************************************************************************
@@ -73,287 +45,13 @@
static GObjectClass *parent_class = NULL;
/******************************************************************************
*****************************************************************************/
-#define add_symbol(scanner, symbol, value) \
- g_scanner_scope_add_symbol(scanner, 0, symbol, GINT_TO_POINTER((value)));
-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,
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_INCLUDE));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_IF));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_ELSE));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_ENDIF));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_FOR));
- g_scanner_scope_add_symbol(scanner, GPLATE_TEMPLATE_SCOPE_CODE,
- GINT_TO_POINTER(GPLATE_TEMPLATE_KEYWORD_ENDFOR));
-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));
-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;
-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));
-/******************************************************************************
- *****************************************************************************/
-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);
-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);
-gplate_template_collection_init(GPlateCollectionIface *iface) {
- iface->find_variable = gplate_template_find_variable;
- iface->add_variable = gplate_template_add_variable;
-/******************************************************************************
- *****************************************************************************/
-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 */
-/******************************************************************************
- *****************************************************************************/
-static const GScannerConfig *
-gplate_template_real_get_config(GPlateTemplate *tlate) {
- return &scanner_config;
-gplate_template_real_process(GPlateTemplate *tplate, const gchar *text,
- guint len, GError **error)
+gplate_template_real_tokenize(GPlateTemplate *tplate, + const gchar *text, guint len, - GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
- GPlateCollection *collection = GPLATE_COLLECTION(tplate);
- GString *output = 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);
- if(scope == GPLATE_TEMPLATE_SCOPE_VAR) {
- GPlateVariable *var = NULL;
- const gchar *val = NULL;
- var = gplate_collection_find_variable(collection,
- 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();
- /* remove the include scope */
- gplate_template_pop_scope(tplate);
- include = gplate_template_process_file(iplate,
- g_string_append_printf(output, "%s", include);
- g_object_unref(G_OBJECT(iplate));
- /* set our scope to expect a close */
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_CODE_CLOSING);
- g_string_append_printf(output, "%s", value.v_string);
- switch(GPOINTER_TO_INT(value.v_symbol)) {
- case GPLATE_TEMPLATE_KEYWORD_VAR_OPEN:
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_VAR);
- case GPLATE_TEMPLATE_KEYWORD_VAR_CLOSE:
- if(gplate_template_get_scope(tplate) !=
- GPLATE_TEMPLATE_SCOPE_VAR_CLOSING)
- g_scanner_error(priv->scanner,
- gplate_template_pop_scope(tplate);
- gplate_template_pop_scope(tplate);
- case GPLATE_TEMPLATE_KEYWORD_CODE_OPEN:
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_CODE);
- case GPLATE_TEMPLATE_KEYWORD_CODE_CLOSE:
- if(gplate_template_get_scope(tplate) !=
- GPLATE_TEMPLATE_SCOPE_CODE_CLOSING)
- g_scanner_error(priv->scanner, "syntax error!");
- printf("%p: code close\n", priv->scanner);
- gplate_template_pop_scope(tplate);
- 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
- case GPLATE_TEMPLATE_KEYWORD_IF:
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_IF);
- case GPLATE_TEMPLATE_KEYWORD_ELSE:
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_ELSE);
- case GPLATE_TEMPLATE_KEYWORD_FOR:
- gplate_template_push_scope(tplate,
- GPLATE_TEMPLATE_SCOPE_FOR);
- g_string_append_printf(output, "%d", value.v_symbol);
- 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) {
- g_string_append_printf(output, "%c", type);
- g_string_append_printf(output, "%c", type);
- g_scanner_destroy(priv->scanner);
- g_string_free(output, FALSE);
/******************************************************************************
@@ -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;
-gplate_template_init(GTypeInstance *instance, gpointer klass) {
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(instance);
- priv->dict = gplate_dictionary_variable_new("dict");
+ klass->tokenize = gplate_template_real_tokenize; /******************************************************************************
@@ -407,21 +94,12 @@
- static const GInterfaceInfo collection_info = {
- (GInterfaceInitFunc)gplate_template_collection_init,
type = g_type_register_static(G_TYPE_OBJECT,
- g_type_add_interface_static(type, GPLATE_TYPE_COLLECTION,
@@ -441,54 +119,35 @@
-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);
gplate_template_process(GPlateTemplate *tplate, const gchar *text, guint len,
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);
+ if(klass && klass->tokenize) + klass->tokenize(tplate, text, len, error);
gplate_template_process_file(GPlateTemplate *tplate, const gchar *filename,
- 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))
- ret = gplate_template_process(tplate, contents, len, error);
+ gplate_template_tokenize(tplate, contents, len, error);
--- 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 <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 {
- 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);