source: tools/prettyprinter/filter.cc@ 7d4f6ed

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 7d4f6ed was 7d4f6ed, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

remainder of pretty printer

  • Property mode set to 100644
File size: 7.6 KB
Line 
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 : Wed Jun 28 22:56:58 2017
13// Update Count : 72
14//
15
16#include <iostream>
17#include <string>
18#include <list>
19using namespace std;
20#include "filter.h"
21#include "parser.h"
22
23
24void (*filter)( Token *tree ) = 0;
25
26
27void 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
36void 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
45static 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
59void Parse_Tree( Token *tree ) {
60 if ( tree == NULL ) return;
61 Parse_Tree1( tree, 0 );
62} // Parse_Tree
63
64
65void 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
110 // ':' or '|', then include the whitespace before the
111 // token. Otherwise, fold the token onto the same line
112 // separated with a blank.
113 string t1( i->getText() );
114 if ( i->isComment() || first == 0 ) {
115 first = t1.length();
116 cout << i->getWS() << t1;
117 } else {
118 if ( first + t1.length() <= 100 ) { // check for long lines during folding
119 first += t1.length();
120 cout << " " << t1;
121 } else {
122 first = t1.length();
123 cout << endl << "\t\t\t\t" << t1;
124 } // if
125 } // if
126 } // switch
127 } // for
128 break;
129 }
130 case _LITERALBLOCK: // ignore code but print its comment, if any
131 cout << tree->down->getComment();
132 break;
133 case _DECLARATION: { // ignore certain declarations
134 int kind = tree->down->getKind(); // get kind of declaration
135 if ( kind != UNION && kind != TYPE ) {
136 declprt = true;
137 Nocode( tree->down ); // print verbatim
138 } else if ( declprt ) { // ignore declaration but print its comment, if any
139 declprt = false;
140 cout << tree->down->getComment();
141 } // if
142 break;
143 }
144 case _USERSECTION_OPT: // ignore but add newline at the end
145 cout << endl;
146 break;
147 default:
148 if ( tree->down != NULL ) Nocode( tree->down );
149 } // switch
150 } // if
151 if ( tree->left != NULL ) Nocode( tree->left );
152} // Nocode
153
154
155void LaTeX( Token *tree ) { // prefix tree traversal
156 if ( tree == NULL ) return;
157
158 if ( tree->isTerminal() ) { // terminals
159 cout << tree->getWS() << tree->getText();
160 if ( tree->getKind() == IDENTIFIER ) {
161 string id( tree->getText() );
162 cout << "\\(\\index{" << id << "@\\protect\\LGbegin\\protect\\lgrinde\\)" << id << "\\(\\protect\\endlgrinde\\protect\\LGend{}}\\)";
163 } // if
164 } else { // non-terminals
165 switch ( tree->getKind() ) {
166 case _RHS: {
167 int first = 0; // first RHS after ':' or '|' treated specially
168 int push = 0, pop = 0;
169 for ( Token *i = tree->down; i != 0; i = i->left ) {
170 switch ( i->getKind() ) {
171 case _ACTION:
172 cout << i->down->getComment(); // ignore code but print its comment, if any
173 break;
174 case _PREC:
175 LaTeX( i->down ); // print verbatim
176 break;
177 case '|': // start of alternative and special case
178 first = 0;
179 push = pop = 0;
180 case ';': // print with whitespace
181 cout << i->getWS() << i->getText();
182 break;
183 default:
184 if ( i->getKind() == IDENTIFIER ) {
185 if ( i->getText() == "push" ) {
186 push += 1;
187 break;
188 } else if ( i->getText() == "pop" ) {
189 pop += 1;
190 break;
191 } // if
192 } // if
193 // If there is a comment or this is the first RHS after
194 // ':' or '|', then include the whitespace before the
195 // token. Otherwise, fold the token onto the same line
196 // separated with a blank.
197 string t1( i->getText() );
198 if ( i->isComment() || first == 0 ) {
199 first = t1.length();
200 cout << i->getWS() << t1;
201 if ( i->getKind() == IDENTIFIER ) {
202 string id( tree->getText() );
203 cout << "\\(\\index{" << id << "@\"\\verb=" << id << "=}\\)";
204 } // if
205 } else {
206 if ( first + t1.length() <= 100 ) { // check for long lines during folding
207 first += t1.length();
208 cout << " " << t1;
209 } else {
210 first = t1.length();
211 cout << endl << "\t\t\t\t" << t1;
212 } // if
213 } // if
214 } // switch
215 } // for
216 break;
217 }
218 case _LITERALBLOCK: // ignore code but print its comment, if any
219 cout << tree->down->getComment();
220 break;
221 case _DECLARATION: { // ignore certain declarations
222 int kind = tree->down->getKind(); // get kind of declaration
223 if ( kind != UNION && kind != TYPE ) {
224 LaTeX( tree->down ); // print verbatim
225 } // if
226 break;
227 }
228 case _USERSECTION_OPT: // ignore but add newline at the end
229 cout << endl;
230 break;
231 default:
232 if ( tree->down != NULL ) LaTeX( tree->down );
233 } // switch
234 } // if
235 if ( tree->left != NULL ) LaTeX( tree->left );
236} // LaTeX
237
238
239void HTML( Token *tree ) { // prefix tree traversal
240 cerr << "ERROR: html style not implemented" << endl;
241} // HTML
242
243// Local Variables: //
244// mode: c++ //
245// tab-width: 4 //
246// compile-command: "make install" //
247// End: //
Note: See TracBrowser for help on using the repository browser.