aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/tests/gfsmRegexCompiler-v1.c
blob: 12495fa7517a3be16b2f619971e2298b6081c9d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <gfsmRegexCompiler.h>
#include <gfsmArith.h>
#include <gfsmUtils.h>

#define RETURN(rec,_rea) (rec)->rea=(_rea); return (_rea);

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_epsilon(gfsmRegexCompiler *rec)
{
  rec->rea.typ = gfsmREATEmpty;
  rec->rea.val.lab = 0;
  return rec->rea;
}

//--------------------------------------------------------------
gfsmAutomaton *gfsm_regex_automaton_new_fsm(gfsmRegexCompiler *rec)
{
  gfsmAutomaton *fsm = gfsm_automaton_new_full(gfsmAutomatonDefaultFlags,
					       rec->srtype,
					       gfsmAutomatonDefaultSize);
  fsm->flags.is_transducer = FALSE;
  return fsm;
}

//--------------------------------------------------------------
gfsmAutomaton *gfsm_regex_automaton_epsilon_fsm(gfsmRegexCompiler *rec)
{
  gfsmAutomaton *fsm = gfsm_regex_automaton_new_fsm(rec);
  fsm->root_id = gfsm_automaton_add_state(fsm);
  gfsm_automaton_set_final_state(fsm,fsm->root_id,TRUE);
  return fsm;
}

//--------------------------------------------------------------
gfsmAutomaton *gfsm_regex_automaton_label_fsm(gfsmRegexCompiler *rec, gfsmLabelVal lab)
{
  gfsmAutomaton *fsm = gfsm_regex_automaton_new_fsm(rec);
  gfsmStateId    labid;
  fsm->root_id = gfsm_automaton_add_state(fsm);
  labid        = gfsm_automaton_add_state(fsm);
  gfsm_automaton_add_arc(fsm, fsm->root_id, labid, lab, lab, fsm->sr->one);
  gfsm_automaton_set_final_state(fsm,labid,TRUE);
  return fsm;
}

//--------------------------------------------------------------
gfsmAutomaton *gfsm_regex_automaton_fsm(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea)
{
  switch (rea.typ) {
  case gfsmREATEmpty:
    return gfsm_regex_automaton_epsilon_fsm(rec);
    break;
  case gfsmREATLabel:
    return gfsm_regex_automaton_label_fsm(rec, rea.val.lab);
    break;
  case gfsmREATFull:
  default:
    return rea.val.fsm;
    break;
  }
}

//--------------------------------------------------------------
gfsmAutomaton *gfsm_regex_automaton_expand(gfsmRegexCompiler *rec, gfsmRegexAutomaton *rea)
{
  rea->val.fsm = gfsm_regex_automaton_fsm(rec,*rea);
  rea->typ     = gfsmREATFull;
  return rea->val.fsm;
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_label(gfsmRegexCompiler *rec, gfsmLabelVal lab)
{
  rec->rea.typ = gfsmREATLabel;
  rec->rea.val.lab = lab;
  return rec->rea;
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_concat(gfsmRegexCompiler *rec,
					       gfsmRegexAutomaton rea1,
					       gfsmRegexAutomaton rea2)
{
  switch (rea2.typ) {
  case gfsmREATEmpty:
    break;
  case gfsmREATLabel:
    gfsm_regex_automaton_append_lab(rec, gfsm_regex_automaton_expand(rec,&rea1), rea2.val.lab);
    break;
  case gfsmREATFull:
  default:
    gfsm_automaton_concat(gfsm_regex_automaton_expand(rec,&rea1), rea2.val.fsm);
    gfsm_automaton_free(rea2.val.fsm);
    break;
  }
  
  RETURN(rec,rea1);
}


//--------------------------------------------------------------
struct _gfsm_regex_append_lab_data {
  gfsmAutomaton     *fsm;
  gfsmLabelVal       lab;
  gfsmStateId        newid;
};

gboolean _gfsm_regex_append_lab_foreach_func(gfsmStateId qid, gpointer pw,
					     struct _gfsm_regex_append_lab_data *data)
{
  gfsm_automaton_get_state(data->fsm,qid)->is_final = FALSE;
  gfsm_automaton_add_arc(data->fsm, qid, data->newid, data->lab, data->lab, gfsm_ptr2weight(pw));
  return FALSE;
}

gfsmAutomaton *gfsm_regex_automaton_append_lab(gfsmRegexCompiler *rec, gfsmAutomaton *fsm, gfsmLabelVal lab)
{
  struct _gfsm_regex_append_lab_data data = { fsm, lab, gfsm_automaton_add_state(fsm) };
  gfsm_weightmap_foreach(fsm->finals,
			 (GTraverseFunc)_gfsm_regex_append_lab_foreach_func,
			 &data);
  gfsm_weightmap_clear(fsm->finals);
  gfsm_automaton_set_final_state(fsm, data.newid, TRUE);
  return fsm;
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_closure(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea, gboolean is_plus)
{
  gfsm_automaton_closure(gfsm_regex_automaton_expand(rec,&rea),is_plus);
  RETURN(rec,rea);
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_power(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea, guint32 n)
{
  gfsm_automaton_n_closure(gfsm_regex_automaton_expand(rec,&rea),n);
  RETURN(rec,rea);
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_project(gfsmRegexCompiler *rec,
						gfsmRegexAutomaton rea,
						gfsmLabelSide which)
{
  gfsm_automaton_project(gfsm_regex_automaton_expand(rec,&rea),which);
  RETURN(rec,rea);
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_optional(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea)
{
  gfsm_automaton_optional(gfsm_regex_automaton_expand(rec,&rea));
  RETURN(rec,rea);
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_complement(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea)
{
  gfsm_automaton_complement_full(gfsm_regex_automaton_expand(rec,&rea),rec->abet);
  RETURN(rec,rea);
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_union(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea1, gfsmRegexAutomaton rea2)
{
  gfsm_automaton_union(gfsm_regex_automaton_expand(rec,&rea1),gfsm_regex_automaton_expand(rec,&rea2));
  gfsm_automaton_free(gfsm_regex_automaton_expand(rec,&rea2));
  RETURN(rec,rea1);
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_intersect(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea1, gfsmRegexAutomaton rea2)
{
  gfsm_automaton_intersect(gfsm_regex_automaton_expand(rec,&rea1),gfsm_regex_automaton_expand(rec,&rea2));
  gfsm_automaton_free(gfsm_regex_automaton_expand(rec,&rea2));
  RETURN(rec,rea1);
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_product(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea1, gfsmRegexAutomaton rea2)
{
  _gfsm_automaton_product(gfsm_regex_automaton_expand(rec,&rea1),gfsm_regex_automaton_expand(rec,&rea2));
  gfsm_automaton_free(gfsm_regex_automaton_expand(rec,&rea2));
  RETURN(rec,rea1);
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_compose(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea1, gfsmRegexAutomaton rea2)
{
  gfsm_automaton_compose(gfsm_regex_automaton_expand(rec,&rea1),gfsm_regex_automaton_expand(rec,&rea2));
  gfsm_automaton_free(gfsm_regex_automaton_expand(rec,&rea2));
  RETURN(rec,rea1);
}

//--------------------------------------------------------------
gfsmRegexAutomaton gfsm_regex_automaton_difference(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea1, gfsmRegexAutomaton rea2)
{
  gfsm_automaton_difference(gfsm_regex_automaton_expand(rec,&rea1),gfsm_regex_automaton_expand(rec,&rea2));
  gfsm_automaton_free(gfsm_regex_automaton_expand(rec,&rea2));
  RETURN(rec,rea1);
}

//--------------------------------------------------------------
/** Weight */
gfsmRegexAutomaton gfsm_regex_automaton_weight(gfsmRegexCompiler *rec, gfsmRegexAutomaton rea, gfsmWeight w)
{
  gfsm_automaton_arith_final(gfsm_regex_automaton_expand(rec,&rea), gfsmAOSRTimes, w, FALSE);
  RETURN(rec,rea);
}