X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fgrammar.git;a=blobdiff_plain;f=rdp.c;h=7f0a3a13dee8239ef5570a47e31ae6629abd0143;hp=a6fca7c36229ab7e1ccbe8722f59b373546164a0;hb=HEAD;hpb=b634d3dac61fc6bb42bafd01eae3dbf6ae8e961d diff --git a/rdp.c b/rdp.c index a6fca7c..7f0a3a1 100644 --- a/rdp.c +++ b/rdp.c @@ -1,29 +1,31 @@ -#include "rdp.h" +/* + * Copyright (C) 2005 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + + +#include #include -rdp_t* rdp_new (nextcb cb, gpointer data) +struct _buffer { - - rdp_t* parser; - - parser = g_malloc (sizeof (rdp_t)); - parser->cb = cb; - parser->data = data; - - parser->rules = NULL; - - parser->buffer = g_list_append (NULL, NULL); - - return parser; - -} - -void rdp_delete (rdp_t* parser) -{ - - g_free (parser); - -} + symbol_t* symbol; + gpointer attrib; +}; gpointer leaf_new (gpointer data) { @@ -45,6 +47,29 @@ void tree_delete (gpointer tree) g_node_destroy (tree); } +rdp_t* rdp_new (nextcb cb, gpointer data, gint value, grammar_t* grammar) +{ + + rdp_t* parser; + + parser = g_malloc (sizeof (rdp_t)); + + parser->cb = cb; + parser->data = data; + parser->start = symbol_new (FALSE, value); + parser->grammar = grammar; + + parser->buffer = g_list_append (NULL, NULL); + + return parser; + +} + +void rdp_delete (rdp_t* rdp) +{ + g_free (rdp->start); +} + symbol_t* buffer_next (rdp_t* parser, gpointer* attrib) { @@ -91,44 +116,52 @@ gboolean rdp_step (rdp_t* parser, symbol_t* symbol, gpointer* attrib) return TRUE; } - for (l = g_list_first (parser->rules); l != NULL; l = g_list_next (l)) + l = grammar_get_rules (parser->grammar, symbol); + for (; l != NULL; l = g_list_next (l)) { rule_t* rule; + GList* m; rule = (rule_t*) l->data; - if (symbol == NULL || symbol_equal (symbol, rule->left)) - { - - GList* m; - *attrib = tree_new (rule); + *attrib = tree_new (symbol_copy (symbol)); - for (m = g_list_first (rule->right); m != NULL; m = g_list_next (m)) - { + m = grammar_get_rule (rule); - symbol_t* s; + /* + if (m == NULL) + { + tree_add (*attrib, leaf_new (g_string_new (""))); + return TRUE; + } + */ - s = (symbol_t*) m->data; + while (m != NULL) + { - if (!rdp_step (parser, s, &attr)) - { - parser->buffer = buffer; - break; - } + symbol_t* s; - tree_add (*attrib, attr); + s = (symbol_t*) m->data; + if (!rdp_step (parser, s, &attr)) + { + parser->buffer = buffer; + break; } - if (m == NULL) - return TRUE; - else - tree_delete (*attrib); + tree_add (*attrib, attr); + + m = g_list_next (m); } + if (m == NULL) + return TRUE; + else + tree_delete (*attrib); + } return FALSE; @@ -140,7 +173,7 @@ gpointer rdp_build (rdp_t* parser) gpointer attrib; - if (rdp_step (parser, NULL, &attrib)) + if (rdp_step (parser, parser->start, &attrib)) return attrib; return NULL;