| 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 |
/* Tests of Libmarpa methods on trivial grammar that is not merely nulling */ |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include "marpa.h" |
| 26 |
|
| 27 |
#include "marpa_m_test.h" |
| 28 |
|
| 29 |
static void |
| 30 |
warn (const char *s, Marpa_Grammar g) |
| 31 |
{ |
| 32 |
printf ("%s returned %d\n", s, marpa_g_error (g, NULL)); |
| 33 |
} |
| 34 |
|
| 35 |
static void |
| 36 |
fail (const char *s, Marpa_Grammar g) |
| 37 |
{ |
| 38 |
warn (s, g); |
| 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 |
/* Longest rule is <= 4 symbols */ |
| 51 |
Marpa_Symbol_ID rhs[4]; |
| 52 |
|
| 53 |
Marpa_Rule_ID R_top_1; |
| 54 |
Marpa_Rule_ID R_top_2; |
| 55 |
Marpa_Rule_ID R_C2_3; /* Highest rule id */ |
| 56 |
|
| 57 |
/* For (error) messages */ |
| 58 |
char msgbuf[80]; |
| 59 |
|
| 60 |
UNUSED static char * |
| 61 |
symbol_name (Marpa_Symbol_ID id) |
| 62 |
{ |
| 63 |
if (id == S_top) |
| 64 |
return (char *) "top"; |
| 65 |
if (id == S_A1) |
| 66 |
return (char *) "A1"; |
| 67 |
if (id == S_A2) |
| 68 |
return (char *) "A2"; |
| 69 |
if (id == S_B1) |
| 70 |
return (char *) "B1"; |
| 71 |
if (id == S_B2) |
| 72 |
return (char *) "B2"; |
| 73 |
if (id == S_C1) |
| 74 |
return (char *) "C1"; |
| 75 |
if (id == S_C2) |
| 76 |
return (char *) "C2"; |
| 77 |
sprintf (msgbuf, "no such symbol: %d", id); |
| 78 |
return msgbuf; |
| 79 |
} |
| 80 |
|
| 81 |
static Marpa_Grammar |
| 82 |
marpa_g_simple_new (Marpa_Config * config) |
| 83 |
{ |
| 84 |
Marpa_Grammar g; |
| 85 |
g = marpa_g_new (config); |
| 86 |
if (!g) { |
| 87 |
Marpa_Error_Code errcode = marpa_c_error (config, NULL); |
| 88 |
printf ("marpa_g_new returned %d", errcode); |
| 89 |
exit (1); |
| 90 |
} |
| 91 |
if ( marpa_g_force_valued( g) < 0) { |
| 92 |
fail ("marpa_g_force_valued", g); |
| 93 |
} |
| 94 |
if ((S_top = marpa_g_symbol_new (g)) < 0) { |
| 95 |
fail ("marpa_g_symbol_new", g); |
| 96 |
} |
| 97 |
if ((S_A1 = marpa_g_symbol_new (g)) < 0) { |
| 98 |
fail ("marpa_g_symbol_new", g); |
| 99 |
} |
| 100 |
if ((S_A2 = marpa_g_symbol_new (g)) < 0) { |
| 101 |
fail ("marpa_g_symbol_new", g); |
| 102 |
} |
| 103 |
if ((S_B1 = marpa_g_symbol_new (g)) < 0) { |
| 104 |
fail ("marpa_g_symbol_new", g); |
| 105 |
} |
| 106 |
if ((S_B2 = marpa_g_symbol_new (g)) < 0) { |
| 107 |
fail ("marpa_g_symbol_new", g); |
| 108 |
} |
| 109 |
if ((S_C1 = marpa_g_symbol_new (g)) < 0) { |
| 110 |
fail ("marpa_g_symbol_new", g); |
| 111 |
} |
| 112 |
if ((S_C2 = marpa_g_symbol_new (g)) < 0) { |
| 113 |
fail ("marpa_g_symbol_new", g); |
| 114 |
} |
| 115 |
|
| 116 |
/* |
| 117 |
* top ::= A1 |
| 118 |
* top ::= A2 |
| 119 |
* A1 ::= B1 |
| 120 |
* A2 ::= B2 |
| 121 |
* B1 ::= C1 |
| 122 |
* B2 ::= C2 |
| 123 |
*/ |
| 124 |
rhs[0] = S_A1; |
| 125 |
if ((R_top_1 = marpa_g_rule_new (g, S_top, rhs, 1)) < 0) { |
| 126 |
fail ("marpa_g_rule_new", g); |
| 127 |
} |
| 128 |
rhs[0] = S_A2; |
| 129 |
if ((R_top_2 = marpa_g_rule_new (g, S_top, rhs, 1)) < 0) { |
| 130 |
fail ("marpa_g_rule_new", g); |
| 131 |
} |
| 132 |
rhs[0] = S_B1; |
| 133 |
if (marpa_g_rule_new (g, S_A1, rhs, 1) < 0) { |
| 134 |
fail ("marpa_g_rule_new", g); |
| 135 |
} |
| 136 |
rhs[0] = S_B2; |
| 137 |
if (marpa_g_rule_new (g, S_A2, rhs, 1) < 0) { |
| 138 |
fail ("marpa_g_rule_new", g); |
| 139 |
} |
| 140 |
rhs[0] = S_C1; |
| 141 |
if (marpa_g_rule_new (g, S_B1, rhs, 1) < 0) { |
| 142 |
fail ("marpa_g_rule_new", g); |
| 143 |
} |
| 144 |
rhs[0] = S_C2; |
| 145 |
if (marpa_g_rule_new (g, S_B2, rhs, 1) < 0) { |
| 146 |
fail ("marpa_g_rule_new", g); |
| 147 |
} |
| 148 |
|
| 149 |
return g; |
| 150 |
} |
| 151 |
|
| 152 |
static Marpa_Error_Code |
| 153 |
marpa_g_simple_precompute (Marpa_Grammar g, Marpa_Symbol_ID S_start) |
| 154 |
{ |
| 155 |
Marpa_Error_Code rc; |
| 156 |
|
| 157 |
if (marpa_g_start_symbol_set (g, S_start) < 0) { |
| 158 |
fail ("marpa_g_start_symbol_set", g); |
| 159 |
} |
| 160 |
|
| 161 |
rc = marpa_g_precompute (g); |
| 162 |
if (rc < 0) |
| 163 |
fail ("marpa_g_precompute", g); |
| 164 |
|
| 165 |
return rc; |
| 166 |
} |
| 167 |
|
| 168 |
int |
| 169 |
main (int argc UNUSED, char *argv[] UNUSED) |
| 170 |
{ |
| 171 |
int rc; |
| 172 |
int flag; |
| 173 |
|
| 174 |
Marpa_Config marpa_configuration; |
| 175 |
Marpa_Grammar g; |
| 176 |
Marpa_Recognizer r; |
| 177 |
Marpa_Bocage b; |
| 178 |
Marpa_Order o; |
| 179 |
|
| 180 |
Marpa_Symbol_ID S_token = S_A2; |
| 181 |
|
| 182 |
API_test_data defaults; |
| 183 |
API_test_data this_test; |
| 184 |
|
| 185 |
plan (20); |
| 186 |
|
| 187 |
marpa_c_init (&marpa_configuration); |
| 188 |
g = marpa_g_simple_new (&marpa_configuration); |
| 189 |
|
| 190 |
defaults.g = g; |
| 191 |
defaults.expected_errcode = MARPA_ERR_NONE; |
| 192 |
defaults.msg = (char *) ""; |
| 193 |
defaults.rv_seen.long_rv = -86; |
| 194 |
|
| 195 |
this_test = defaults; |
| 196 |
|
| 197 |
marpa_g_simple_precompute (g, S_top); |
| 198 |
ok (1, "precomputation succeeded"); |
| 199 |
|
| 200 |
r = marpa_r_new (g); |
| 201 |
if (!r) |
| 202 |
fail ("marpa_r_new", g); |
| 203 |
|
| 204 |
this_test.msg = (char *) "before marpa_r_start_input()"; |
| 205 |
API_CODE_TEST3 (this_test, MARPA_ERR_RECCE_NOT_ACCEPTING_INPUT, |
| 206 |
marpa_r_alternative, r, S_token, 0, 0); |
| 207 |
|
| 208 |
rc = marpa_r_start_input (r); |
| 209 |
if (!rc) |
| 210 |
fail ("marpa_r_start_input", g); |
| 211 |
|
| 212 |
{ |
| 213 |
Marpa_Symbol_ID S_expected = S_A2; |
| 214 |
int value = 1; |
| 215 |
API_STD_TEST2 (this_test, value, MARPA_ERR_NONE, |
| 216 |
marpa_r_expected_symbol_event_set, r, S_expected, value); |
| 217 |
} |
| 218 |
|
| 219 |
/* recognizer reading methods on invalid and missing symbols */ |
| 220 |
|
| 221 |
this_test.msg = |
| 222 |
(char *) "invalid token symbol is checked before no-such"; |
| 223 |
API_CODE_TEST3 (this_test, MARPA_ERR_INVALID_SYMBOL_ID, |
| 224 |
marpa_r_alternative, r, S_invalid, 0, 0); |
| 225 |
this_test.msg = (char *) "no such token symbol"; |
| 226 |
API_CODE_TEST3 (this_test, MARPA_ERR_NO_SUCH_SYMBOL_ID, |
| 227 |
marpa_r_alternative, r, S_no_such, 0, 0); |
| 228 |
this_test.msg = |
| 229 |
(char *) marpa_m_error_message (MARPA_ERR_TOKEN_LENGTH_LE_ZERO); |
| 230 |
API_CODE_TEST3 (this_test, MARPA_ERR_TOKEN_LENGTH_LE_ZERO, |
| 231 |
marpa_r_alternative, r, S_token, 0, 0); |
| 232 |
|
| 233 |
|
| 234 |
API_STD_TEST0 (defaults, -2, MARPA_ERR_PARSE_EXHAUSTED, |
| 235 |
marpa_r_earleme_complete, r); |
| 236 |
|
| 237 |
/* re-create the recce and try some input */ |
| 238 |
marpa_r_unref(r); |
| 239 |
r = marpa_r_new (g); |
| 240 |
if (!r) |
| 241 |
fail ("marpa_r_new", g); |
| 242 |
|
| 243 |
rc = marpa_r_start_input (r); |
| 244 |
if (!rc) |
| 245 |
fail ("marpa_r_start_input", g); |
| 246 |
|
| 247 |
this_test = defaults; |
| 248 |
API_CODE_TEST3 (this_test, MARPA_ERR_NONE, |
| 249 |
marpa_r_alternative, r, S_C1, 1, 1); |
| 250 |
|
| 251 |
API_STD_TEST0 (defaults, 1, MARPA_ERR_NONE, |
| 252 |
marpa_r_earleme_complete, r); |
| 253 |
|
| 254 |
/* marpa_o_high_rank_only_* */ |
| 255 |
b = marpa_b_new (r, marpa_r_current_earleme (r)); |
| 256 |
if (!b) |
| 257 |
fail ("marpa_b_new", g); |
| 258 |
|
| 259 |
API_STD_TEST0 (defaults, 1, MARPA_ERR_NONE, |
| 260 |
marpa_b_ambiguity_metric, b); |
| 261 |
API_STD_TEST0 (defaults, 0, MARPA_ERR_NONE, marpa_b_is_null, b); |
| 262 |
|
| 263 |
o = marpa_o_new (b); |
| 264 |
ok (o != NULL, "marpa_o_new(): ordering at earleme 0"); |
| 265 |
|
| 266 |
flag = 1; |
| 267 |
API_STD_TEST1 (defaults, flag, MARPA_ERR_NONE, |
| 268 |
marpa_o_high_rank_only_set, o, flag); |
| 269 |
API_STD_TEST0 (defaults, flag, MARPA_ERR_NONE, |
| 270 |
marpa_o_high_rank_only, o); |
| 271 |
|
| 272 |
API_STD_TEST0 (defaults, 1, MARPA_ERR_NONE, |
| 273 |
marpa_o_ambiguity_metric, o); |
| 274 |
API_STD_TEST0 (defaults, 0, MARPA_ERR_NONE, marpa_o_is_null, o); |
| 275 |
|
| 276 |
API_STD_TEST1 (defaults, -2, MARPA_ERR_ORDER_FROZEN, |
| 277 |
marpa_o_high_rank_only_set, o, flag); |
| 278 |
API_STD_TEST0 (defaults, flag, MARPA_ERR_NONE, |
| 279 |
marpa_o_high_rank_only, o); |
| 280 |
|
| 281 |
|
| 282 |
/* Needed for ASan test */ |
| 283 |
marpa_o_unref(o); |
| 284 |
marpa_b_unref(b); |
| 285 |
marpa_r_unref(r); |
| 286 |
marpa_g_unref(g); |
| 287 |
|
| 288 |
return 0; |
| 289 |
} |