Debian Codegraph @

engine/read_only/tests/simple/rule1.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 <stdio.h>
23 #include "marpa.h"
24
25 #include "tap/basic.h"
26
27 static void
28 err (const char *s, Marpa_Grammar g)
29 {
30 Marpa_Error_Code errcode = marpa_g_error (g, NULL);
31 printf ("%s: Error %d\n\n", s, errcode);
32 marpa_g_error_clear (g);
33 }
34
35 static void
36 code_fail (const char *s, Marpa_Grammar g)
37 {
38 err (s, g);
39 exit (1);
40 }
41
42 int
43 main (int argc UNUSED, char *argv[] UNUSED)
44 {
45
46 Marpa_Config marpa_configuration;
47 Marpa_Grammar g;
48
49 Marpa_Symbol_ID S_lhs, S_rhs;
50 Marpa_Symbol_ID rhs[1];
51
52 Marpa_Rule_ID R_new;
53
54 int rc;
55
56 plan (4);
57
58 marpa_c_init (&marpa_configuration);
59 g = marpa_g_new (&marpa_configuration);
60 if (!g) {
61 Marpa_Error_Code errcode =
62 marpa_c_error (&marpa_configuration, NULL);
63 printf ("marpa_g_new: error %d", errcode);
64 exit (1);
65 }
66 if (marpa_g_force_valued( g) < 0) {
67 code_fail ("marpa_g_symbol_new", g);
68 }
69
70 /* Symbols */
71 if ((S_lhs = marpa_g_symbol_new (g)) < 0) {
72 code_fail ("marpa_g_symbol_new", g);
73 }
74 if ((S_rhs = marpa_g_symbol_new (g)) < 0) {
75 code_fail ("marpa_g_symbol_new", g);
76 }
77
78 /* Rule */
79 rhs[0] = S_rhs;
80 if ((R_new = marpa_g_rule_new (g, S_lhs, rhs, 1)) < 0) {
81 code_fail ("marpa_g_rule_new", g);
82 }
83 ok ((R_new == 0), "marpa_g_rule_new returns 0");
84
85 /* Precompute */
86 rc = marpa_g_start_symbol_set (g, S_lhs);
87 if (rc < 0) {
88 code_fail ("marpa_g_start_symbol_set", g);
89 }
90
91 rc = marpa_g_precompute (g);
92 ok ((rc == 0), "marpa_g_precompute returned 0");
93 if (rc < 0) {
94 code_fail ("marpa_g_sequence_separator", g);
95 }
96
97 rc = marpa_g_is_precomputed (g);
98 ok ((rc == 1), "marpa_g_is_precomputed returned 1");
99 if (rc < 0) {
100 code_fail ("marpa_g_is_precomputed", g);
101 }
102
103 /* Rule accessors */
104 rc = marpa_g_rule_lhs (g, R_new);
105 ok ((rc == 0), "marpa_g_rule_lhs(%d) returned 0", R_new);
106 if (rc < 0) {
107 code_fail ("marpa_g_rule_lhs", g);
108 }
109
110 marpa_g_unref(g);
111
112 return 0;
113 }