comparison anagram/guisupport/conflictdc.cpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, A System for Syntax Directed Programming
3 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
4 * See the file COPYING for license and usage terms.
5 *
6 * conflictdc.cpp - Conflict Display module
7 */
8
9 #include "cd.h"
10 #include "conflictdc.h"
11 #include "dc.h"
12 #include "conflicttrc.h"
13 #include "items.h"
14 #include "nckwtr.h"
15 #include "rule.h"
16 #include "stexpdc.h"
17 #include "stacks.h"
18 #include "token.h"
19 #include "ut.h"
20 #include "wm1.h"
21 #include "ws.h"
22
23 //#define INCLUDE_LOGGING
24 #include "log.h"
25
26
27 dc_ref build_rule_derivation(int sn, int fn) {
28 LOGSECTION("build_rule_derivation");
29
30 LOGV(sn) LCV(fn);
31 dc_ref new_window;
32 AgString foot = AgString::format("S%03d:R%03d",sn, fn);
33
34 return new derivation_dc(new_rule_derivation, "Rule Derivation",
35 foot, 1);
36 }
37
38 int conflict_table_dc::derive_rule_ok(unsigned ln) {
39 int sn, fn;
40 int tn, n;
41 return locate_conflict(ln, &sn, &tn, &fn, &n) != 0;
42 }
43
44 dc_ref conflict_table_dc::derive_rule(unsigned ln) {
45 LOGSECTION("conflict_table_dc::derive_rule");
46 int sn, fn;
47 int tn, n;
48
49 if (locate_conflict(ln, &sn, &tn, &fn, &n) == 0) {
50 return NULL;
51 }
52 analyze_conflict(sn, tn, fn, 0);
53 return build_rule_derivation(sn, fn);
54 }
55
56 dc_ref conflict_table_dc::derive_token(unsigned ln) {
57 LOGSECTION("conflict_table_dc::derive_token");
58 int sn, fn;
59 int tn, n;
60
61 if (locate_conflict(ln, &sn, &tn, &fn, &n) == 0) {
62 return NULL;
63 }
64 analyze_conflict(sn, tn, fn, 0);
65 return new_derive_token(sn, tn, fn);
66 }
67
68 int conflict_table_dc::expansion_chain_ok(unsigned ln) {
69 int sn, fn;
70 int tn, n;
71
72 screen_rule(ln, &sn, &tn, &fn, &n);
73 return n == 0;
74 }
75
76 dc_ref conflict_table_dc::expansion_chain(unsigned ln) {
77 int sn, fn;
78 int tn, n;
79
80 if (screen_rule(ln, &sn, &tn, &fn, &n) == 0) {
81 return NULL;
82 }
83 return expansion_chain_window(sn, fn, n);
84 }
85
86 static int conflict_tabs[] = {7,13,0};
87
88 void conflict_table_dc::init(void) {
89 LOGSECTION("conflict_table_dc::init");
90 unsigned i,n;
91 unsigned dw;
92 unsigned ns = sort_con();
93
94 tab_stops = conflict_tabs;
95
96 n = conflict_table->nt;
97 for (i = dw = 0; i < n; i++) {
98 int sn, tn, fn, fx;
99 unsigned length;
100
101 xtxf(conflict_table,i, &sn, &tn, &fn, &fx);
102 ics();
103 atkn(tn);
104 length = conflict_tabs[1] + rcs();
105 if (length > dw) {
106 dw = length;
107 }
108 ics();
109 append_item(fn, fx);
110 length = 2 + rcs();
111 if (length > dw) {
112 dw = length;
113 }
114 }
115
116 n = conflict_table->nt + ns;
117 des->d_size = cint(dw, n);
118 LOGV(dw) LCV(n);
119 }
120
121 int conflict_table_dc::locate_conflict(unsigned ln,
122 int *snp, int *tnp, int *fnp,
123 int *fxp) {
124 LOGSECTION("conflict_table_dc::locate_conflict");
125 int k;
126
127 for (k = 1; ln >= permutation[k] + k; k++);
128 k--;
129 if (ln == permutation[k] + k) {
130 ln++;
131 }
132 while (ln < permutation[k+1] + k+1) {
133 int kk = ln - k - 1;
134 xtxf(conflict_table, kk, snp, tnp, fnp, fxp);
135 if (*tnp == 0) {
136 return 0;
137 }
138 if (*fxp >= map_form_number[*fnp].length()) {
139 break;
140 }
141 ln++;
142 }
143 return 1;
144 }
145
146 dc_ref conflict_table_dc::new_derive_token (int sn, int tn, int fn) {
147 dc_ref window;
148 LOGSECTION("new_derive_token");
149 AgString foot = AgString::format("S%03d:R%03d:T%03d",sn, fn, tn);
150
151 return dc_ref(new derivation_dc(token_derivation, "Token Derivation",
152 foot, 0));
153 }
154
155 void conflict_table_dc::synchCursor(unsigned ln) const {
156 int k, sn, fn;
157 int tn, n;
158
159 for (k = 1; ln >= permutation[k] + k; k++);
160 k--;
161 if ((int) ln == permutation[k] + k) {
162 return;
163 }
164 k = ln - k - 1;
165 xtx(conflict_table, k, &sn, &tn, &fn, &n);
166 set_rule_line(fn);
167 }
168
169 dc_ref conflict_table_dc::problem_states(unsigned ln) {
170 int sn, fn;
171 int tn, n;
172
173 if (locate_conflict(ln, &sn, &tn, &fn, &n) == 0) {
174 return NULL;
175 }
176 return problem_states_window(sn, fn, map_form_number[fn].length(), tn);
177 }
178
179 int conflict_table_dc::reduction_states_ok(unsigned ln) {
180 LOGSECTION("conflict_table_dc::reduction_states_ok");
181 LOGV(ln);
182 int sn, fn;
183 int tn, n;
184
185 if (locate_conflict(ln, &sn, &tn, &fn, &n) == 0) {
186 return 0;
187 }
188 return reduction_states_window_ok(fn, map_form_number[fn].length());
189 }
190
191 dc_ref conflict_table_dc::reduction_states(unsigned ln) {
192 int sn, fn;
193 int tn, n;
194
195 if (locate_conflict(ln, &sn, &tn, &fn, &n) == 0) {
196 return NULL;
197 }
198 return reduction_states_window(sn, fn, map_form_number[fn].length());
199 }
200
201 dc_ref conflict_table_dc::rule_context(unsigned ln) {
202 int k;
203 int sn, tn, fn, fx;
204
205
206 for (k = 1; ln >= permutation[k] + k; k++);
207 k--;
208 if (ln == permutation[k] + k) {
209 ln++;
210 }
211 xtx(conflict_table, ln - k - 1, &sn, &tn, &fn, &fx);
212 return rule_context_window(fn);
213 }
214
215 int conflict_table_dc::screen_rule(unsigned ln,
216 int *snp, int *tnp, int *fnp,
217 int *fxp) {
218 int k;
219 int kk;
220
221 for (k = 1; ln >= permutation[k] + k; k++);
222 k--;
223 if (ln == permutation[k] + k) {
224 return 0;
225 }
226 kk = ln - k - 1;
227 xtx(conflict_table, kk, snp, tnp, fnp, fxp);
228 return *tnp != 0;
229 }
230
231 dc_ref conflict_table_dc::select_conflict(unsigned ln) {
232 int sn, tn, fn, n;
233 int k;
234
235 for (k = 1; ln >= permutation[k] + k; k++);
236 k--;
237 if (ln == permutation[k] + k) {
238 ln++;
239 }
240 while (ln < permutation[k+1] + k+1) {
241 int kk = ln - k - 1;
242 xtx(conflict_table, kk, &sn, &tn, &fn, &n);
243 if (tn == 0) {
244 return NULL;
245 }
246 if (n >= map_form_number[fn].length()) {
247 break;
248 }
249 ln++;
250 }
251 return build_conflict_trace(sn, tn, fn);
252 }
253
254 dc_ref conflict_table_dc::state_definition(unsigned ln) {
255 int sn, fn;
256 int tn, n;
257
258 if (locate_conflict(ln, &sn, &tn, &fn, &n) == 0) {
259 return NULL;
260 }
261 return conflict_state_window(sn);
262 }
263
264 dc_ref conflict_table_dc::state_expansion(unsigned ln) {
265 int sn, fn;
266 int tn, n;
267
268 if (locate_conflict(ln, &sn, &tn, &fn, &n) == 0) {
269 return NULL;
270 }
271 return state_expansion_window(sn);
272 }
273
274 dc_ref conflict_table_dc::usage(unsigned ln) {
275 int sn, fn;
276 int tn, n;
277
278 if (locate_conflict(ln, &sn, &tn, &fn, &n) == 0) {
279 return NULL;
280 }
281 return token_usage_window(tn);
282 }
283
284 unsigned conflict_table_dc::sort_con() {
285 int psn, ptn;
286 unsigned k;
287 unsigned ns;
288
289 ptn = psn = -1;
290 ns = 0;
291 for (k = 0; k < conflict_table->nt; k++) {
292 int sn, tn, fn, n;
293
294 xtxf(conflict_table, k, &sn, &tn, &fn, &n);
295 if (sn == psn && tn == ptn) {
296 continue;
297 }
298
299 ns++;
300 psn = sn;
301 ptn = tn;
302 }
303 permutation = AgArray<int>(ns+1);
304
305 psn = ptn = -1;
306 ns = 0;
307 for (k = 0; k < conflict_table->nt; k++) {
308 int sn, tn, fn, n;
309 xtxf(conflict_table, k, &sn, &tn, &fn, &n);
310 if (sn == psn && tn == ptn) {
311 continue;
312 }
313 permutation[ns++] = k;
314 psn = sn;
315 ptn = tn;
316 }
317 permutation[ns] = k;
318 return ns;
319 }
320
321 dc_ref conflict_table_dc::reduction_trace(unsigned ln) {
322 int sn, tn, fn, n;
323 int k;
324
325 for (k = 1; ln >= permutation[k] + k; k++);
326 k--;
327 if (ln == permutation[k] + k) {
328 ln++;
329 }
330 while (ln < permutation[k+1] + k+1) {
331 int kk = ln - k - 1;
332 xtx(conflict_table, kk, &sn, &tn, &fn, &n);
333 if (n >= map_form_number[fn].length()) {
334 break;
335 }
336 if (tn == 0) {
337 return NULL;
338 }
339 ln++;
340 }
341 return conflict_red_trace(sn, tn, fn);
342 }
343
344 derivation_dc::derivation_dc(tsd *td, const AgString head, const AgString foot,
345 int brkt)
346 : dc(head, foot)
347 {
348 LOGSECTION("derivation_dc::derivation_dc");
349 derivation = copy_tuple_set(td);
350 bracket = brkt;
351 des->c_loc_doc.y = td->nt - 1;
352 des->d_size.y = derivation->nt;
353 }
354
355 derivation_dc::~derivation_dc(void) {
356 delete_tsd(derivation);
357 }
358
359 void derivation_dc::getLine(unsigned tln) const {
360 int brkt;
361 int fn, fx;
362 int ln = derivation->nt - tln - 1;
363
364 LOGSECTION("derivation_dc::getLine");
365 LOGV(tln) LCV(ln) LCV(bracket);
366
367 //???if (ln < derivation->nt-1) xtxf(derivation, ln+1, &fn, &fx);
368 xtxf(derivation, ln, &fn, &fx);
369 if (ln == 0 && bracket) {
370 brkt = -1;
371 }
372 else {
373 brkt = fx - bracket;
374 }
375 /*
376 LOGV(fn) LCV(fx) LCV(brkt);
377 //int token = lstptr(map_form_number[fn],tokens)[brkt];
378 Token token = Rule(fn).token(brkt > 0 ? brkt : 0);
379 LOGV(token);
380 LOGV(brkt);
381 if (bracket && ln && token->zero_length_flag) {
382 int derivedRule;
383 int derivedRuleIndex;
384 xtxf(derivation, ln-1, &derivedRule, &derivedRuleIndex);
385 while (brkt >= 0 && token->zero_length_flag && !token.isExpansionRule(derivedRule)) {
386 //token = lstptr(map_form_number[fn],tokens)[--brkt];
387 token = Rule(fn).token(--brkt);
388 LOGV(token);
389 LOGV(brkt);
390 }
391 }
392 */
393 ics();
394 append_item_brkt(fn, brkt);
395 }
396
397 void derivation_dc::synchCursor(unsigned ln) const {
398 int fn, fx;
399
400 ln = derivation->nt - 1 - ln;
401 xtx(derivation, ln, &fn, &fx);
402 set_rule_line(fn);
403 }
404
405 unsigned derivation_dc::token(unsigned tln) {
406 int fn, fx;
407 int brkt;
408 unsigned ln;
409
410 ln = derivation->nt - tln - 1;
411 if (ln == 0 && bracket) {
412 return 0;
413 }
414 xtx(derivation, ln, &fn, &fx);
415 brkt = fx - bracket;
416 if (brkt < 0) {
417 return 0;
418 }
419 //return lstptr(map_form_number[fn], tokens)[brkt];
420 return Rule(fn).token(brkt);
421 }
422
423 int derivation_dc::expansion_rules_ok(unsigned ln) {
424 // int fn, fx;
425
426 ln = derivation->nt - 1 - ln;
427 if (ln == 0 && bracket) {
428 return 0;
429 }
430 return 1;
431 }
432
433 dc_ref derivation_dc::expansion_rules(unsigned ln) {
434 int fn, fx;
435
436 ln = derivation->nt - 1 - ln;
437 if (ln == 0 && bracket) {
438 return NULL;
439 }
440 xtx(derivation, ln, &fn, &fx);
441 return expand_specific_item(this, fn, fx-bracket);
442 }
443
444 int derivation_dc::productions_ok(unsigned ln) {
445 return productions_window_ok(token(ln));
446 }
447
448 dc_ref derivation_dc::productions(unsigned ln) {
449 return productions_window(token(ln));
450 }
451
452 dc_ref derivation_dc::rule_context(unsigned ln) {
453 int fn, fx;
454
455 ln = derivation->nt - 1 - ln;
456 xtx(derivation, ln, &fn, &fx);
457 return rule_context_window(fn);
458 }
459
460 int derivation_dc::set_elements_ok(unsigned ln) {
461 return token_set_window_ok(token(ln));
462 }
463
464 dc_ref derivation_dc::set_elements(unsigned ln) {
465 return token_set_window(token(ln));
466 }
467
468 int derivation_dc::usage_ok(unsigned ln) {
469 return token_usage_window_ok(token(ln));
470 }
471
472 dc_ref derivation_dc::usage(unsigned ln) {
473 return token_usage_window(token(ln));
474 }
475
476 void res_con_dc::getLine(unsigned ln) const {
477 LOGSECTION("res_con_dc::getLine");
478 int sn, tn, fn;
479 unsigned x, k, n;
480 int a, f;
481 int cs;
482
483 for (k = 1; ln >= permutation[k] + k; k++);
484 k--;
485 if (ln == permutation[k] + k) {
486 x = (ln - k);
487 xtxf(res_con, x, &sn, &tn, &fn, &n);
488 //ssprintf("S%03d: T%03d ", sn, tn);
489 LOGV(sn) LCV(tn);
490 ssprintf("S%03d:\tT%03d ", sn, tn);
491 atkn(tn);
492 LOGV(string_base);
493 return;
494 }
495 k = ln - k - 1;
496 xtxf(res_con, k, &sn, &tn, &fn, &n);
497 for (k = prr->nt; k--; ) {
498 int s, t;
499 xtxf(prr, k, &s, &a, &t, &f);
500 if (s != sn || t != tn) {
501 continue;
502 }
503 break;
504 }
505 k = map_form_number[fn].length();
506 if ((a && f == fn && n == k) || (a == 0 && n < k)) {
507 cs = '*';
508 }
509 else {
510 cs = ' ';
511 }
512 ssprintf("%c\t", cs);
513 append_item(fn, n);
514 }
515
516 void unres_con_dc::getLine(unsigned ln) const {
517 LOGSECTION("unres_con_dc::getLine");
518 LOGV(ln);
519 unsigned sn, tn, fn, n;
520 unsigned k, x;
521
522 for (k = 1; ln >= permutation[k] + k; k++);
523 k--;
524 if (ln == permutation[k] + k) {
525 x = (ln - k);
526 xtxf(unres_con, x, &sn, &tn, &fn, &n);
527 ssprintf("S%03d:\tT%03d ", sn, tn);
528 atkn(tn);
529 return;
530 }
531 k = ln - k - 1;
532 xtxf(unres_con, k, &sn, &tn, &fn, &n);
533 scs('\t');
534 append_item(fn, n);
535 }
536
537