2 * Copyright (C) 2005 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 gpointer leaf_new (gpointer data)
32 return g_node_new (data);
35 gpointer tree_new (rule_t* rule)
37 return g_node_new (rule);
40 gpointer tree_add (gpointer tree, gpointer data)
42 return g_node_append (tree, data);
45 void tree_delete (gpointer tree)
47 g_node_destroy (tree);
50 rdp_t* rdp_new (nextcb cb, gpointer data, gint value, grammar_t* grammar)
55 parser = g_malloc (sizeof (rdp_t));
59 parser->start = symbol_new (FALSE, value);
60 parser->grammar = grammar;
62 parser->buffer = g_list_append (NULL, NULL);
68 void rdp_delete (rdp_t* rdp)
73 symbol_t* buffer_next (rdp_t* parser, gpointer* attrib)
78 if (parser->buffer->next == NULL)
80 buffer = g_malloc (sizeof (buffer_t));
81 buffer->symbol = g_malloc (sizeof (symbol_t));
82 buffer->symbol->terminal = TRUE;
83 buffer->symbol->value = parser->cb (parser->data, &(buffer->attrib));
84 g_list_append (parser->buffer, buffer);
87 parser->buffer = g_list_next (parser->buffer);
88 buffer = (buffer_t*) parser->buffer->data;
91 *attrib = buffer->attrib;
93 return buffer->symbol;
97 gboolean rdp_step (rdp_t* parser, symbol_t* symbol, gpointer* attrib)
104 buffer = parser->buffer;
106 if (symbol != NULL && symbol->terminal)
109 s = buffer_next (parser, &attr);
110 if (!symbol_equal (symbol, s))
112 parser->buffer = buffer;
115 *attrib = leaf_new (attr);
119 l = grammar_get_rules (parser->grammar, symbol);
120 for (; l != NULL; l = g_list_next (l))
126 rule = (rule_t*) l->data;
129 *attrib = tree_new (symbol_copy (symbol));
131 m = grammar_get_rule (rule);
136 tree_add (*attrib, leaf_new (g_string_new ("")));
146 s = (symbol_t*) m->data;
148 if (!rdp_step (parser, s, &attr))
150 parser->buffer = buffer;
154 tree_add (*attrib, attr);
163 tree_delete (*attrib);
171 gpointer rdp_build (rdp_t* parser)
176 if (rdp_step (parser, parser->start, &attrib))