Changeset 7d4f6ed for tools/prettyprinter/filter.cc
- Timestamp:
- Jun 29, 2017, 8:04:42 AM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 2b7afbd
- Parents:
- 9335ecc
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/prettyprinter/filter.cc
r9335ecc r7d4f6ed 1 // -*- Mode: C++ -*- 2 // 3 // Pretty Printer, Copyright (C) Peter A. Buhr 2002 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. 4 6 // 5 7 // filter.cc -- … … 8 10 // Created On : Tue Apr 9 22:33:44 2002 9 11 // Last Modified By : Peter A. Buhr 10 // Last Modified On : Tue Dec 17 11:25:50 2002 11 // Update Count : 60 12 // 13 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> 19 using namespace std; 14 20 #include "filter.h" 15 #include " yacc.tab.h"21 #include "parser.h" 16 22 17 23 … … 20 26 21 27 void freeTree( Token *tree ) { // postfix tree traversal 22 23 24 25 26 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; 27 33 } // freeTree 28 34 29 35 30 36 void Identity( Token *tree ) { // prefix tree traversal 31 32 33 34 35 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 ); 36 42 } // Identity 37 43 38 44 39 45 static void Parse_Tree1( Token *tree, int indent ) { // prefix tree traversal 40 41 42 cout << "\"" << tree->getText() << "\"";43 44 cout << tree->getText();45 46 47 48 << endl;49 50 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 ); 51 57 } // Parse_Tree1 52 58 53 59 void Parse_Tree( Token *tree ) { 54 55 60 if ( tree == NULL ) return; 61 Parse_Tree1( tree, 0 ); 56 62 } // Parse_Tree 57 63 58 64 59 65 void Nocode( Token *tree ) { // prefix tree traversal 60 static bool declprt = true; 61 if ( tree == NULL ) return; 62 63 if ( tree->isTerminal() ) { // terminals 64 cout << tree->getWS() << tree->getText(); 65 } else { // non-terminals 66 switch ( tree->getKind() ) { 67 case _RHS: { 68 int first = 0; // first RHS after ':' or '|' treated specially 69 int push = 0, pop = 0; 70 for ( Token *i = tree->down; i != 0; i = i->left ) { 71 switch ( i->getKind() ) { 72 case _ACTION: 73 cout << string( (push * 4 + pop * 3) / 7, '\t' ); 74 push = pop = 0; 75 cout << i->down->getComment(); // ignore code but print its comment, if any 76 break; 77 case _PREC: 78 Nocode( i->down ); // print verbatim 79 break; 80 case '|': // start of alternative and special case 81 first = 0; 82 case ';': // print with whitespace 83 cout << string( (push * 4 + pop * 3) / 7, '\t' ); 84 push = pop = 0; 85 cout << i->getWS() << i->getText(); 86 break; 87 default: 88 if ( i->getKind() == IDENTIFIER ) { 89 if ( i->getText() == "push" ) { 90 push += 1; 91 if ( first == 0 ) { // first RHS after ':' or '|' ? 92 cout << i->getComment(); // ignore rhs but print its comment, if any 93 } // if 94 break; 95 } else if ( i->getText() == "pop" ) { 96 pop += 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; 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(); 101 141 } // if 102 } // if 103 // If there is a comment or this is the first RHS after 104 // ':' or '|', then include the whitespace before the 105 // token. Otherwise, fold the token onto the same line 106 // separated with a blank. 107 string t1( i->getText() ); 108 if ( i->isComment() || first == 0 ) { 109 first = t1.length(); 110 cout << i->getWS() << t1; 111 } else { 112 if ( first + t1.length() <= 100 ) { // check for long lines during folding 113 first += t1.length(); 114 cout << " " << t1; 115 } else { 116 first = t1.length(); 117 cout << endl << "\t\t\t\t" << t1; 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 155 void 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 118 225 } // if 119 } // if 120 } // switch 121 } // for 122 break; 123 } 124 case _LITERALBLOCK: // ignore code but print its comment, if any 125 cout << tree->down->getComment(); 126 break; 127 case _DECLARATION: { // ignore certain declarations 128 int kind = tree->down->getKind(); // get kind of declaration 129 if ( kind != UNION && kind != TYPE ) { 130 declprt = true; 131 Nocode( tree->down ); // print verbatim 132 } else if ( declprt ) { // ignore declaration but print its comment, if any 133 declprt = false; 134 cout << tree->down->getComment(); 135 } // if 136 break; 137 } 138 case _USERSECTION_OPT: // ignore but add newline at the end 139 cout << endl; 140 break; 141 default: 142 if ( tree->down != NULL ) Nocode( tree->down ); 143 } // switch 144 } // if 145 if ( tree->left != NULL ) Nocode( tree->left ); 146 } // Nocode 147 148 149 void LaTeX( Token *tree ) { // prefix tree traversal 150 if ( tree == NULL ) return; 151 152 if ( tree->isTerminal() ) { // terminals 153 cout << tree->getWS() << tree->getText(); 154 if ( tree->getKind() == IDENTIFIER ) { 155 string id( tree->getText() ); 156 cout << "\\(\\index{" << id << "@\\protect\\LGbegin\\protect\\lgrinde\\)" << id << "\\(\\protect\\endlgrinde\\protect\\LGend{}}\\)"; 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 157 234 } // if 158 } else { // non-terminals 159 switch ( tree->getKind() ) { 160 case _RHS: { 161 int first = 0; // first RHS after ':' or '|' treated specially 162 int push = 0, pop = 0; 163 Token *prev = 0; 164 for ( Token *i = tree->down; i != 0; prev = i, i = i->left ) { 165 switch ( i->getKind() ) { 166 case _ACTION: 167 cout << i->down->getComment(); // ignore code but print its comment, if any 168 break; 169 case _PREC: 170 LaTeX( i->down ); // print verbatim 171 break; 172 case '|': // start of alternative and special case 173 first = 0; 174 push = pop = 0; 175 case ';': // print with whitespace 176 cout << i->getWS() << i->getText(); 177 break; 178 default: 179 if ( i->getKind() == IDENTIFIER ) { 180 if ( i->getText() == "push" ) { 181 push += 1; 182 break; 183 } else if ( i->getText() == "pop" ) { 184 pop += 1; 185 break; 186 } // if 187 } // if 188 // If there is a comment or this is the first RHS after 189 // ':' or '|', then include the whitespace before the 190 // token. Otherwise, fold the token onto the same line 191 // separated with a blank. 192 string t1( i->getText() ); 193 if ( i->isComment() || first == 0 ) { 194 first = t1.length(); 195 cout << i->getWS() << t1; 196 if ( i->getKind() == IDENTIFIER ) { 197 string id( tree->getText() ); 198 cout << "\\(\\index{" << id << "@\"\\verb=" << id << "=}\\)"; 199 } // if 200 } else { 201 if ( first + t1.length() <= 100 ) { // check for long lines during folding 202 first += t1.length(); 203 cout << " " << t1; 204 } else { 205 first = t1.length(); 206 cout << endl << "\t\t\t\t" << t1; 207 } // if 208 } // if 209 } // switch 210 } // for 211 break; 212 } 213 case _LITERALBLOCK: // ignore code but print its comment, if any 214 cout << tree->down->getComment(); 215 break; 216 case _DECLARATION: { // ignore certain declarations 217 int kind = tree->down->getKind(); // get kind of declaration 218 if ( kind != UNION && kind != TYPE ) { 219 LaTeX( tree->down ); // print verbatim 220 } // if 221 break; 222 } 223 case _USERSECTION_OPT: // ignore but add newline at the end 224 cout << endl; 225 break; 226 default: 227 if ( tree->down != NULL ) LaTeX( tree->down ); 228 } // switch 229 } // if 230 if ( tree->left != NULL ) LaTeX( tree->left ); 235 if ( tree->left != NULL ) LaTeX( tree->left ); 231 236 } // LaTeX 232 237 233 238 234 239 void HTML( Token *tree ) { // prefix tree traversal 235 240 cerr << "ERROR: html style not implemented" << endl; 236 241 } // HTML 237 242 238 239 243 // Local Variables: // 240 // compile-command: "gmake" // 244 // mode: c++ // 245 // tab-width: 4 // 246 // compile-command: "make install" // 241 247 // End: //
Note: See TracChangeset
for help on using the changeset viewer.