Debian Codegraph @

engine/read_only/tests/simple/trivial.c

raw
1 /*
2 * Copyright 2022 Jeffrey Kegler
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included
11 * in all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 * OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #include <sys/mman.h>
29 #include "marpa.h"
30
31 #include "tap/basic.h"
32
33 static void
34 fail (const char *s, Marpa_Grammar g)
35 {
36 const char *error_string;
37 Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
38 printf ("%s returned %d: %s", s, errcode, error_string);
39 exit (1);
40 }
41
42 Marpa_Symbol_ID S_top;
43 Marpa_Symbol_ID S_A1;
44 Marpa_Symbol_ID S_A2;
45 Marpa_Symbol_ID S_B1;
46 Marpa_Symbol_ID S_B2;
47 Marpa_Symbol_ID S_C1;
48 Marpa_Symbol_ID S_C2;
49
50 /* For fatal error messages */
51 char error_buffer[80];
52
53 static char *
54 symbol_name (Marpa_Symbol_ID id)
55 {
56 if (id == S_top)
57 return (char *) "top";
58 if (id == S_A1)
59 return (char *) "A1";
60 if (id == S_A2)
61 return (char *) "A2";
62 if (id == S_B1)
63 return (char *) "B1";
64 if (id == S_B2)
65 return (char *) "B2";
66 if (id == S_C1)
67 return (char *) "C1";
68 if (id == S_C2)
69 return (char *) "C2";
70 sprintf (error_buffer, "no such symbol: %d", id);
71 return error_buffer;
72 }
73
74 static int
75 is_nullable (Marpa_Symbol_ID id)
76 {
77 if (id == S_top)
78 return 1;
79 if (id == S_A1)
80 return 1;
81 if (id == S_A2)
82 return 1;
83 if (id == S_B1)
84 return 1;
85 if (id == S_B2)
86 return 1;
87 if (id == S_C1)
88 return 1;
89 if (id == S_C2)
90 return 1;
91 return 0;
92 }
93
94
95 int
96 main (int argc UNUSED, char *argv[] UNUSED)
97 {
98 int rc;
99 const char *error_string;
100
101 Marpa_Config marpa_configuration;
102
103 Marpa_Grammar g;
104 Marpa_Recognizer r;
105 /* Longest rule is <= 4 symbols */
106 Marpa_Symbol_ID rhs[4];
107
108 plan (20);
109
110 marpa_c_init (&marpa_configuration);
111 g = marpa_g_new (&marpa_configuration);
112 if (!g) {
113 Marpa_Error_Code errcode =
114 marpa_c_error (&marpa_configuration, &error_string);
115 printf ("marpa_g_new returned %d: %s", errcode, error_string);
116 exit (1);
117 }
118 if ( marpa_g_force_valued( g) < 0) {
119 fail ("marpa_g_force_valued", g);
120 }
121
122 if ((S_top = marpa_g_symbol_new (g)) < 0) {
123 fail ("marpa_g_symbol_new", g);
124 }
125 if ((marpa_g_symbol_is_nulled_event_set (g, S_top, 1)) < 0) {
126 fail ("marpa_g_symbol_is_nulled_event_set", g);
127 }
128 if ((S_A1 = marpa_g_symbol_new (g)) < 0) {
129 fail ("marpa_g_symbol_new", g);
130 }
131 if ((marpa_g_symbol_is_nulled_event_set (g, S_A1, 1)) < 0) {
132 fail ("marpa_g_symbol_is_nulled_event_set", g);
133 }
134 if ((S_A2 = marpa_g_symbol_new (g)) < 0) {
135 fail ("marpa_g_symbol_new", g);
136 }
137 if ((marpa_g_symbol_is_nulled_event_set (g, S_A2, 1)) < 0) {
138 fail ("marpa_g_symbol_is_nulled_event_set", g);
139 }
140 if ((S_B1 = marpa_g_symbol_new (g)) < 0) {
141 fail ("marpa_g_symbol_new", g);
142 }
143 if ((marpa_g_symbol_is_nulled_event_set (g, S_B1, 1)) < 0) {
144 fail ("marpa_g_symbol_is_nulled_event_set", g);
145 }
146 if ((S_B2 = marpa_g_symbol_new (g)) < 0) {
147 fail ("marpa_g_symbol_new", g);
148 }
149 if ((marpa_g_symbol_is_nulled_event_set (g, S_B2, 1)) < 0) {
150 fail ("marpa_g_symbol_is_nulled_event_set", g);
151 }
152 if ((S_C1 = marpa_g_symbol_new (g)) < 0) {
153 fail ("marpa_g_symbol_new", g);
154 }
155 if ((marpa_g_symbol_is_nulled_event_set (g, S_C1, 1)) < 0) {
156 fail ("marpa_g_symbol_is_nulled_event_set", g);
157 }
158 if ((S_C2 = marpa_g_symbol_new (g)) < 0) {
159 fail ("marpa_g_symbol_new", g);
160 }
161 if ((marpa_g_symbol_is_nulled_event_set (g, S_C2, 1)) < 0) {
162 fail ("marpa_g_symbol_is_nulled_event_set", g);
163 }
164
165 rhs[0] = S_A1;
166 if (marpa_g_rule_new (g, S_top, rhs, 1) < 0) {
167 fail ("marpa_g_rule_new", g);
168 }
169 rhs[0] = S_A2;
170 if (marpa_g_rule_new (g, S_top, rhs, 1) < 0) {
171 fail ("marpa_g_rule_new", g);
172 }
173 rhs[0] = S_B1;
174 if (marpa_g_rule_new (g, S_A1, rhs, 1) < 0) {
175 fail ("marpa_g_rule_new", g);
176 }
177 rhs[0] = S_B2;
178 if (marpa_g_rule_new (g, S_A2, rhs, 1) < 0) {
179 fail ("marpa_g_rule_new", g);
180 }
181 rhs[0] = S_C1;
182 if (marpa_g_rule_new (g, S_B1, rhs, 1) < 0) {
183 fail ("marpa_g_rule_new", g);
184 }
185 rhs[0] = S_C2;
186 if (marpa_g_rule_new (g, S_B2, rhs, 1) < 0) {
187 fail ("marpa_g_rule_new", g);
188 }
189 if (marpa_g_rule_new (g, S_C1, rhs, 0) < 0) {
190 fail ("marpa_g_rule_new", g);
191 }
192 if (marpa_g_rule_new (g, S_C2, rhs, 0) < 0) {
193 fail ("marpa_g_rule_new", g);
194 }
195
196 if (marpa_g_start_symbol_set (g, S_top) < 0) {
197 fail ("marpa_g_start_symbol_set", g);
198 }
199 if (marpa_g_precompute (g) < 0) {
200 marpa_g_error (g, &error_string);
201 puts (error_string);
202 exit (1);
203 }
204 ok (1, "precomputation succeeded");
205 r = marpa_r_new (g);
206 if (!r) {
207 marpa_g_error (g, &error_string);
208 puts (error_string);
209 exit (1);
210 }
211 rc = marpa_r_start_input (r);
212 if (!rc) {
213 marpa_g_error (g, &error_string);
214 puts (error_string);
215 exit (1);
216 }
217 ok ((marpa_r_is_exhausted (r)), "exhausted at earleme 0");
218
219 {
220 Marpa_Event event;
221 const int highest_symbol_id = marpa_g_highest_symbol_id (g);
222 int exhausted_events_triggered = 0;
223 int spurious_events_triggered = 0;
224 int symbol_nulled_events_triggered = 0;
225 int event_ix;
226 const int event_count = marpa_g_event_count (g);
227 struct sym_data
228 {
229 int expected;
230 int seen;
231 };
232
233 struct sym_data *nulled_symbols =
234 calloc ((size_t) (highest_symbol_id + 1),
235 sizeof (struct sym_data));
236 if (!nulled_symbols)
237 abort ();
238 nulled_symbols[S_top].expected = 1;
239 nulled_symbols[S_A1].expected = 1;
240 nulled_symbols[S_A2].expected = 1;
241 nulled_symbols[S_B1].expected = 1;
242 nulled_symbols[S_B2].expected = 1;
243 nulled_symbols[S_C1].expected = 1;
244 nulled_symbols[S_C2].expected = 1;
245 ok ((event_count == 8), "event count at earleme 0 is %ld",
246 (long) event_count);
247 for (event_ix = 0; event_ix < event_count; event_ix++) {
248 int event_type = marpa_g_event (g, &event, event_ix);
249 if (event_type == MARPA_EVENT_SYMBOL_NULLED) {
250 const Marpa_Symbol_ID event_symbol_id =
251 marpa_g_event_value (&event);
252 const char *event_symbol_name =
253 symbol_name (event_symbol_id);
254 if (is_nullable (event_symbol_id)) {
255 ok (1, "nulled event for nullable symbol %s",
256 event_symbol_name);
257 } else {
258 ok (0, "nulled event for non-nullable symbol %s",
259 event_symbol_name);
260 }
261 nulled_symbols[event_symbol_id].seen++;
262 if (!nulled_symbols[event_symbol_id].expected) {
263 diag ("Unexpected nulled symbol event for symbol %s",
264 event_symbol_name);
265 }
266 symbol_nulled_events_triggered++;
267 continue;
268 }
269 if (event_type == MARPA_EVENT_EXHAUSTED) {
270 exhausted_events_triggered++;
271 continue;
272 }
273 printf ("event type is %ld\n", (long) event_type);
274 spurious_events_triggered++;
275 }
276 is_int (0, spurious_events_triggered,
277 "spurious events triggered: %ld",
278 (long) spurious_events_triggered);
279 if (exhausted_events_triggered == 1) {
280 ok (1, "exhausted event triggered");
281 } else {
282 ok (0, "%ld exhausted events triggered",
283 (long) exhausted_events_triggered);
284 }
285 {
286 int i;
287 for (i = 0; i <= highest_symbol_id; i++) {
288 if (nulled_symbols[i].expected == 0
289 || nulled_symbols[i].seen == 0)
290 continue;
291 is_int (nulled_symbols[i].expected, nulled_symbols[i].seen,
292 "nulled events count for symbol %s", symbol_name (i));
293 }
294 }
295 is_int (7, symbol_nulled_events_triggered,
296 "symbol nulled events triggered = %ld",
297 (long) symbol_nulled_events_triggered);
298 free (nulled_symbols);
299 }
300
301 /* Needed for ASan test */
302 marpa_r_unref (r);
303 marpa_g_unref (g);
304
305 return 0;
306 }