summaryrefslogtreecommitdiff
path: root/src/grammar.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/grammar.c')
-rw-r--r--src/grammar.c55
1 files changed, 47 insertions, 8 deletions
diff --git a/src/grammar.c b/src/grammar.c
index afb9353..5ed0b96 100644
--- a/src/grammar.c
+++ b/src/grammar.c
@@ -46,12 +46,44 @@ rg_nth(CCR_MOD(Rule_group *) rg, NUM n)
struct PT_DATA_s {
dfa *dfap;
- /* NULL-terminated strings */
- char *user_name;
- char *raw_name;
+ str *user_name;
+ str *raw_name;
};
-typedef struct PT_DATA_s PTD;
+PTD *
+new_ptd(str *user_name, str *raw_name, dfa *dfap)
+{
+ PTD *result = NULL;
+ SAFE_MALLOC(PTD, result, 1, return NULL;);
+
+ result->dfap = dfap;
+ result->user_name = user_name;
+ result->raw_name = raw_name;
+
+ return result;
+}
+
+void
+destroy_ptd(PTD *p, int flag)
+{
+ destroy_dfa(p->dfap);
+ destroy_str(p->user_name, flag);
+ destroy_str(p->raw_name, flag);
+
+ free(p);
+}
+
+static void
+destroy_ptd_free_all(void *e)
+{
+ destroy_ptd((PTD *)e, 1);
+}
+
+static void
+destroy_ptd_no_free(void *e)
+{
+ destroy_ptd((PTD *)e, 0);
+}
/* TODO: Add some statistic counts to assist the hash tables. For
example, store the total number of terminals as an integer; then
@@ -72,8 +104,8 @@ struct Grammar_s {
To print them, first encode them into normal strings. */
List *names;
- /* an array of predicates. */
- PTD *predicates;
+ /* a list of predicates, of type PTD. */
+ List *predicates;
};
static void
@@ -186,10 +218,11 @@ new_grammar()
/* We classify the rules into different rule groups. */
BOOL
-build_grammar(Grammar *g, const List *rules, CC_MOD(List *) names)
+build_grammar(Grammar *g, const List *rules,
+ CC_MOD(List *) names, CC_MOD(List *) predicates)
{
g->names = (List *) names;
- g->predicates = NULL;
+ g->predicates = predicates;
NUM len = list_length(names);
NUM rule_len = list_length(rules);
@@ -486,6 +519,12 @@ destroy_grammar(void *grammar, int flag)
destroy_list(g->names, 0);
+ if (g->predicates) {
+ if (flag) map_list(g->predicates, destroy_ptd_free_all);
+ else map_list(g->predicates, destroy_ptd_no_free);
+ destroy_list(g->predicates, 0);
+ }
+
free(grammar);
}