1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // filter.cc -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Tue Apr 9 22:33:44 2002 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Jun 29 08:56:46 2017 |
---|
13 | // Update Count : 73 |
---|
14 | // |
---|
15 | |
---|
16 | #include <iostream> |
---|
17 | #include <string> |
---|
18 | #include <list> |
---|
19 | using namespace std; |
---|
20 | #include "filter.h" |
---|
21 | #include "parser.hh" |
---|
22 | |
---|
23 | |
---|
24 | void (* filter)( Token * tree ) = 0; |
---|
25 | |
---|
26 | |
---|
27 | void freeTree( Token * tree ) { // postfix tree traversal |
---|
28 | if ( tree == NULL ) return; |
---|
29 | if ( tree->down != NULL ) freeTree( tree->down ); |
---|
30 | if ( tree->left != NULL ) freeTree( tree->left ); |
---|
31 | //cerr << "free token: \"" << tree->getText() << "\" : " << tree->getKind() << endl; |
---|
32 | delete tree; |
---|
33 | } // freeTree |
---|
34 | |
---|
35 | |
---|
36 | void Identity( Token * tree ) { // prefix tree traversal |
---|
37 | if ( tree == NULL ) return; |
---|
38 | // print only the terminals |
---|
39 | if ( tree->isTerminal() ) cout << tree->getWS() << tree->getText(); |
---|
40 | if ( tree->down != NULL ) Identity( tree->down ); |
---|
41 | if ( tree->left != NULL ) Identity( tree->left ); |
---|
42 | } // Identity |
---|
43 | |
---|
44 | |
---|
45 | static void Parse_Tree1( Token * tree, int indent ) { // prefix tree traversal |
---|
46 | cout << string( indent, ' ' ); |
---|
47 | if ( tree->isTerminal() ) { // terminals |
---|
48 | cout << "\"" << tree->getText() << "\""; |
---|
49 | } else { // non-terminals |
---|
50 | cout << tree->getText(); |
---|
51 | } // if |
---|
52 | cout << " : " << tree->getKind() |
---|
53 | //<< " \"" << tree->getWS() << " \"" |
---|
54 | << endl; |
---|
55 | if ( tree->down != NULL ) Parse_Tree1( tree->down, indent + 2 ); |
---|
56 | if ( tree->left != NULL ) Parse_Tree1( tree->left, indent ); |
---|
57 | } // Parse_Tree1 |
---|
58 | |
---|
59 | void Parse_Tree( Token * tree ) { |
---|
60 | if ( tree == NULL ) return; |
---|
61 | Parse_Tree1( tree, 0 ); |
---|
62 | } // Parse_Tree |
---|
63 | |
---|
64 | |
---|
65 | void Nocode( Token * tree ) { // prefix tree traversal |
---|
66 | static bool declprt = true; |
---|
67 | if ( tree == NULL ) return; |
---|
68 | |
---|
69 | if ( tree->isTerminal() ) { // terminals |
---|
70 | cout << tree->getWS() << tree->getText(); |
---|
71 | } else { // non-terminals |
---|
72 | switch ( tree->getKind() ) { |
---|
73 | case _RHS: { |
---|
74 | int first = 0; // first RHS after ':' or '|' treated specially |
---|
75 | int push = 0, pop = 0; |
---|
76 | for ( Token * i = tree->down; i != 0; i = i->left ) { |
---|
77 | switch ( i->getKind() ) { |
---|
78 | case _ACTION: |
---|
79 | cout << string( (push * 4 + pop * 3) / 7, '\t' ); |
---|
80 | push = pop = 0; |
---|
81 | cout << i->down->getComment(); // ignore code but print its comment, if any |
---|
82 | break; |
---|
83 | case _PREC: |
---|
84 | Nocode( i->down ); // print verbatim |
---|
85 | break; |
---|
86 | case '|': // start of alternative and special case |
---|
87 | first = 0; |
---|
88 | case ';': // print with whitespace |
---|
89 | cout << string( (push * 4 + pop * 3) / 7, '\t' ); |
---|
90 | push = pop = 0; |
---|
91 | cout << i->getWS() << i->getText(); |
---|
92 | break; |
---|
93 | default: |
---|
94 | if ( i->getKind() == IDENTIFIER ) { |
---|
95 | if ( i->getText() == "push" ) { |
---|
96 | push += 1; |
---|
97 | if ( first == 0 ) { // first RHS after ':' or '|' ? |
---|
98 | cout << i->getComment(); // ignore rhs but print its comment, if any |
---|
99 | } // if |
---|
100 | break; |
---|
101 | } else if ( i->getText() == "pop" ) { |
---|
102 | pop += 1; |
---|
103 | if ( first == 0 ) { // first RHS after ':' or '|' ? |
---|
104 | cout << i->getComment(); // ignore rhs but print its comment, if any |
---|
105 | } // if |
---|
106 | break; |
---|
107 | } // if |
---|
108 | } // if |
---|
109 | // If there is a comment or this is the first RHS after ':' or '|', then include the whitespace |
---|
110 | // before the token. Otherwise, fold the token onto the same line separated with a blank. |
---|
111 | string t1( i->getText() ); |
---|
112 | if ( i->isComment() || first == 0 ) { |
---|
113 | first = t1.length(); |
---|
114 | cout << i->getWS() << t1; |
---|
115 | } else { |
---|
116 | if ( first + t1.length() <= 100 ) { // check for long lines during folding |
---|
117 | first += t1.length(); |
---|
118 | cout << " " << t1; |
---|
119 | } else { |
---|
120 | first = t1.length(); |
---|
121 | cout << endl << "\t\t\t\t" << t1; |
---|
122 | } // if |
---|
123 | } // if |
---|
124 | } // switch |
---|
125 | } // for |
---|
126 | break; |
---|
127 | } |
---|
128 | case _LITERALBLOCK: // ignore code but print its comment, if any |
---|
129 | cout << tree->down->getComment(); |
---|
130 | break; |
---|
131 | case _DECLARATION: { // ignore certain declarations |
---|
132 | int kind = tree->down->getKind(); // get kind of declaration |
---|
133 | if ( kind != UNION && kind != TYPE ) { |
---|
134 | declprt = true; |
---|
135 | Nocode( tree->down ); // print verbatim |
---|
136 | } else if ( declprt ) { // ignore declaration but print its comment, if any |
---|
137 | declprt = false; |
---|
138 | cout << tree->down->getComment(); |
---|
139 | } // if |
---|
140 | break; |
---|
141 | } |
---|
142 | case _USERSECTION_OPT: // ignore but add newline at the end |
---|
143 | cout << endl; |
---|
144 | break; |
---|
145 | default: |
---|
146 | if ( tree->down != NULL ) Nocode( tree->down ); |
---|
147 | } // switch |
---|
148 | } // if |
---|
149 | if ( tree->left != NULL ) Nocode( tree->left ); |
---|
150 | } // Nocode |
---|
151 | |
---|
152 | |
---|
153 | void LaTeX( Token * tree ) { // prefix tree traversal |
---|
154 | if ( tree == NULL ) return; |
---|
155 | |
---|
156 | if ( tree->isTerminal() ) { // terminals |
---|
157 | cout << tree->getWS() << tree->getText(); |
---|
158 | if ( tree->getKind() == IDENTIFIER ) { |
---|
159 | string id( tree->getText() ); |
---|
160 | cout << "\\(\\index{" << id << "@\\protect\\LGbegin\\protect\\lgrinde\\)" << id << "\\(\\protect\\endlgrinde\\protect\\LGend{}}\\)"; |
---|
161 | } // if |
---|
162 | } else { // non-terminals |
---|
163 | switch ( tree->getKind() ) { |
---|
164 | case _RHS: { |
---|
165 | int first = 0; // first RHS after ':' or '|' treated specially |
---|
166 | int push = 0, pop = 0; |
---|
167 | for ( Token * i = tree->down; i != 0; i = i->left ) { |
---|
168 | switch ( i->getKind() ) { |
---|
169 | case _ACTION: |
---|
170 | cout << i->down->getComment(); // ignore code but print its comment, if any |
---|
171 | break; |
---|
172 | case _PREC: |
---|
173 | LaTeX( i->down ); // print verbatim |
---|
174 | break; |
---|
175 | case '|': // start of alternative and special case |
---|
176 | first = 0; |
---|
177 | push = pop = 0; |
---|
178 | case ';': // print with whitespace |
---|
179 | cout << i->getWS() << i->getText(); |
---|
180 | break; |
---|
181 | default: |
---|
182 | if ( i->getKind() == IDENTIFIER ) { |
---|
183 | if ( i->getText() == "push" ) { |
---|
184 | push += 1; |
---|
185 | break; |
---|
186 | } else if ( i->getText() == "pop" ) { |
---|
187 | pop += 1; |
---|
188 | break; |
---|
189 | } // if |
---|
190 | } // if |
---|
191 | // If there is a comment or this is the first RHS after ':' or '|', then include the whitespace |
---|
192 | // before the token. Otherwise, fold the token onto the same line separated with a blank. |
---|
193 | string t1( i->getText() ); |
---|
194 | if ( i->isComment() || first == 0 ) { |
---|
195 | first = t1.length(); |
---|
196 | cout << i->getWS() << t1; |
---|
197 | if ( i->getKind() == IDENTIFIER ) { |
---|
198 | string id( tree->getText() ); |
---|
199 | cout << "\\(\\index{" << id << "@\"\\verb=" << id << "=}\\)"; |
---|
200 | } // if |
---|
201 | } else { |
---|
202 | if ( first + t1.length() <= 100 ) { // check for long lines during folding |
---|
203 | first += t1.length(); |
---|
204 | cout << " " << t1; |
---|
205 | } else { |
---|
206 | first = t1.length(); |
---|
207 | cout << endl << "\t\t\t\t" << t1; |
---|
208 | } // if |
---|
209 | } // if |
---|
210 | } // switch |
---|
211 | } // for |
---|
212 | break; |
---|
213 | } |
---|
214 | case _LITERALBLOCK: // ignore code but print its comment, if any |
---|
215 | cout << tree->down->getComment(); |
---|
216 | break; |
---|
217 | case _DECLARATION: { // ignore certain declarations |
---|
218 | int kind = tree->down->getKind(); // get kind of declaration |
---|
219 | if ( kind != UNION && kind != TYPE ) { |
---|
220 | LaTeX( tree->down ); // print verbatim |
---|
221 | } // if |
---|
222 | break; |
---|
223 | } |
---|
224 | case _USERSECTION_OPT: // ignore but add newline at the end |
---|
225 | cout << endl; |
---|
226 | break; |
---|
227 | default: |
---|
228 | if ( tree->down != NULL ) LaTeX( tree->down ); |
---|
229 | } // switch |
---|
230 | } // if |
---|
231 | if ( tree->left != NULL ) LaTeX( tree->left ); |
---|
232 | } // LaTeX |
---|
233 | |
---|
234 | |
---|
235 | void HTML( Token * tree ) { // prefix tree traversal |
---|
236 | cerr << "ERROR: html style not implemented" << endl; |
---|
237 | } // HTML |
---|
238 | |
---|
239 | // Local Variables: // |
---|
240 | // mode: c++ // |
---|
241 | // tab-width: 4 // |
---|
242 | // compile-command: "make install" // |
---|
243 | // End: // |
---|