/TeXmacs-1.0.7.11-src/src/Data/Tree/tree_modify.cpp
C++ | 84 lines | 61 code | 8 blank | 15 comment | 22 complexity | 3b401f4dab545732f036ce0ea20e1d56 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception
1
2/******************************************************************************
3* MODULE : tree_modify.cpp
4* DESCRIPTION: high level tree modification subroutines
5* COPYRIGHT : (C) 2010 Joris van der Hoeven
6*******************************************************************************
7* This software falls under the GNU general public license version 3 or later.
8* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10******************************************************************************/
11
12#include "tree_modify.hpp"
13#include "drd_std.hpp"
14#include "path.hpp"
15
16extern tree the_et;
17
18/******************************************************************************
19* DRD-based correction of trees
20******************************************************************************/
21
22void
23correct_concat_node (tree& t, int done) {
24 //cout << "Correct " << t << ", " << done << "\n";
25 int i, n= N(t);
26 if (n == 0) {
27 assign (t, "");
28 return;
29 }
30 for (i=done; i<n; i++) {
31 if (t[i] == "") {
32 remove (t, i, 1);
33 correct_concat_node (t, i);
34 return;
35 }
36 if ((i<n-1) && is_atomic (t[i]) && is_atomic (t[i+1])) {
37 join (t, i);
38 correct_concat_node (t, i);
39 return;
40 }
41 if (is_concat (t[i])) {
42 insert_node (t, 0, CONCAT);
43 split (t, 0, i);
44 split (t, 1, 1);
45 remove_node (t[1], 0);
46 if (t[0] == tree (CONCAT)) remove (t, 0, 1);
47 else join (t, 0);
48 if (t[1] == tree (CONCAT)) remove (t, 1, 1);
49 else join (t, 0);
50 remove_node (t, 0);
51 correct_concat_node (t, max (i-1, 0));
52 return;
53 }
54 }
55}
56
57void
58correct_node (tree& t) {
59 // NOTE: this routine should only modify t and its descendants,
60 // but not any ancestors
61 if (is_compound (t)) {
62 if (the_drd->contains (as_string (L(t))) &&
63 !the_drd->correct_arity (L(t), N(t)))
64 assign (t, "");
65 if (is_concat (t))
66 correct_concat_node (t, 0);
67 }
68}
69
70void
71correct_downwards (tree& t) {
72 if (is_compound (t))
73 for (int i=0; i<N(t); i++)
74 correct_downwards (t[i]);
75 correct_node (t);
76}
77
78void
79correct_upwards (tree& t) {
80 correct_node (t);
81 path ip= obtain_ip (t);
82 if (ip_attached (ip) && !is_nil (ip))
83 correct_upwards (subtree (the_et, reverse (ip->next)));
84}