[fda8168] | 1 | // |
---|
[7d4f6ed] | 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. |
---|
[fda8168] | 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 |
---|
[c9383ee] | 12 | // Last Modified On : Thu Jun 29 08:56:46 2017 |
---|
| 13 | // Update Count : 73 |
---|
[fda8168] | 14 | // |
---|
| 15 | |
---|
[7d4f6ed] | 16 | #include <iostream> |
---|
| 17 | #include <string> |
---|
| 18 | #include <list> |
---|
| 19 | using namespace std; |
---|
[fda8168] | 20 | #include "filter.h" |
---|
[8c97ee7] | 21 | #include "parser.hh" |
---|
[fda8168] | 22 | |
---|
| 23 | |
---|
[c9383ee] | 24 | void (* filter)( Token * tree ) = 0; |
---|
[fda8168] | 25 | |
---|
| 26 | |
---|
[c9383ee] | 27 | void freeTree( Token * tree ) { // postfix tree traversal |
---|
[7d4f6ed] | 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; |
---|
[fda8168] | 33 | } // freeTree |
---|
| 34 | |
---|
| 35 | |
---|
[c9383ee] | 36 | void Identity( Token * tree ) { // prefix tree traversal |
---|
[7d4f6ed] | 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 ); |
---|
[fda8168] | 42 | } // Identity |
---|
| 43 | |
---|
| 44 | |
---|
[c9383ee] | 45 | static void Parse_Tree1( Token * tree, int indent ) { // prefix tree traversal |
---|
[7d4f6ed] | 46 | cout << string( indent, ' ' ); |
---|
[c9383ee] | 47 | if ( tree->isTerminal() ) { // terminals |
---|
[7d4f6ed] | 48 | cout << "\"" << tree->getText() << "\""; |
---|
[c9383ee] | 49 | } else { // non-terminals |
---|
[7d4f6ed] | 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 ); |
---|
[fda8168] | 57 | } // Parse_Tree1 |
---|
| 58 | |
---|
[c9383ee] | 59 | void Parse_Tree( Token * tree ) { |
---|
[7d4f6ed] | 60 | if ( tree == NULL ) return; |
---|
| 61 | Parse_Tree1( tree, 0 ); |
---|
[fda8168] | 62 | } // Parse_Tree |
---|
| 63 | |
---|
| 64 | |
---|
[c9383ee] | 65 | void Nocode( Token * tree ) { // prefix tree traversal |
---|
[7d4f6ed] | 66 | static bool declprt = true; |
---|
| 67 | if ( tree == NULL ) return; |
---|
| 68 | |
---|
[c9383ee] | 69 | if ( tree->isTerminal() ) { // terminals |
---|
[7d4f6ed] | 70 | cout << tree->getWS() << tree->getText(); |
---|
[c9383ee] | 71 | } else { // non-terminals |
---|
[7d4f6ed] | 72 | switch ( tree->getKind() ) { |
---|
| 73 | case _RHS: { |
---|
[c9383ee] | 74 | int first = 0; // first RHS after ':' or '|' treated specially |
---|
[7d4f6ed] | 75 | int push = 0, pop = 0; |
---|
[c9383ee] | 76 | for ( Token * i = tree->down; i != 0; i = i->left ) { |
---|
[7d4f6ed] | 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: |
---|
[c9383ee] | 84 | Nocode( i->down ); // print verbatim |
---|
[7d4f6ed] | 85 | break; |
---|
[c9383ee] | 86 | case '|': // start of alternative and special case |
---|
[7d4f6ed] | 87 | first = 0; |
---|
[c9383ee] | 88 | case ';': // print with whitespace |
---|
[7d4f6ed] | 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; |
---|
[c9383ee] | 97 | if ( first == 0 ) { // first RHS after ':' or '|' ? |
---|
[7d4f6ed] | 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; |
---|
[c9383ee] | 103 | if ( first == 0 ) { // first RHS after ':' or '|' ? |
---|
[7d4f6ed] | 104 | cout << i->getComment(); // ignore rhs but print its comment, if any |
---|
| 105 | } // if |
---|
| 106 | break; |
---|
| 107 | } // if |
---|
| 108 | } // if |
---|
[c9383ee] | 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. |
---|
[7d4f6ed] | 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 | } |
---|
[c9383ee] | 128 | case _LITERALBLOCK: // ignore code but print its comment, if any |
---|
[7d4f6ed] | 129 | cout << tree->down->getComment(); |
---|
| 130 | break; |
---|
[c9383ee] | 131 | case _DECLARATION: { // ignore certain declarations |
---|
| 132 | int kind = tree->down->getKind(); // get kind of declaration |
---|
[7d4f6ed] | 133 | if ( kind != UNION && kind != TYPE ) { |
---|
| 134 | declprt = true; |
---|
[c9383ee] | 135 | Nocode( tree->down ); // print verbatim |
---|
| 136 | } else if ( declprt ) { // ignore declaration but print its comment, if any |
---|
[7d4f6ed] | 137 | declprt = false; |
---|
| 138 | cout << tree->down->getComment(); |
---|
[fda8168] | 139 | } // if |
---|
[7d4f6ed] | 140 | break; |
---|
| 141 | } |
---|
[c9383ee] | 142 | case _USERSECTION_OPT: // ignore but add newline at the end |
---|
[7d4f6ed] | 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 ); |
---|
[fda8168] | 150 | } // Nocode |
---|
| 151 | |
---|
| 152 | |
---|
[c9383ee] | 153 | void LaTeX( Token * tree ) { // prefix tree traversal |
---|
[7d4f6ed] | 154 | if ( tree == NULL ) return; |
---|
| 155 | |
---|
[c9383ee] | 156 | if ( tree->isTerminal() ) { // terminals |
---|
[7d4f6ed] | 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 |
---|
[c9383ee] | 162 | } else { // non-terminals |
---|
[7d4f6ed] | 163 | switch ( tree->getKind() ) { |
---|
| 164 | case _RHS: { |
---|
[c9383ee] | 165 | int first = 0; // first RHS after ':' or '|' treated specially |
---|
[7d4f6ed] | 166 | int push = 0, pop = 0; |
---|
[c9383ee] | 167 | for ( Token * i = tree->down; i != 0; i = i->left ) { |
---|
[7d4f6ed] | 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: |
---|
[c9383ee] | 173 | LaTeX( i->down ); // print verbatim |
---|
[7d4f6ed] | 174 | break; |
---|
[c9383ee] | 175 | case '|': // start of alternative and special case |
---|
[7d4f6ed] | 176 | first = 0; |
---|
| 177 | push = pop = 0; |
---|
[c9383ee] | 178 | case ';': // print with whitespace |
---|
[7d4f6ed] | 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 |
---|
[c9383ee] | 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. |
---|
[7d4f6ed] | 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 | } |
---|
[c9383ee] | 214 | case _LITERALBLOCK: // ignore code but print its comment, if any |
---|
[7d4f6ed] | 215 | cout << tree->down->getComment(); |
---|
| 216 | break; |
---|
[c9383ee] | 217 | case _DECLARATION: { // ignore certain declarations |
---|
| 218 | int kind = tree->down->getKind(); // get kind of declaration |
---|
[7d4f6ed] | 219 | if ( kind != UNION && kind != TYPE ) { |
---|
[c9383ee] | 220 | LaTeX( tree->down ); // print verbatim |
---|
[fda8168] | 221 | } // if |
---|
[7d4f6ed] | 222 | break; |
---|
| 223 | } |
---|
[c9383ee] | 224 | case _USERSECTION_OPT: // ignore but add newline at the end |
---|
[7d4f6ed] | 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 ); |
---|
[fda8168] | 232 | } // LaTeX |
---|
| 233 | |
---|
| 234 | |
---|
[c9383ee] | 235 | void HTML( Token * tree ) { // prefix tree traversal |
---|
[7d4f6ed] | 236 | cerr << "ERROR: html style not implemented" << endl; |
---|
[fda8168] | 237 | } // HTML |
---|
| 238 | |
---|
| 239 | // Local Variables: // |
---|
[7d4f6ed] | 240 | // mode: c++ // |
---|
| 241 | // tab-width: 4 // |
---|
| 242 | // compile-command: "make install" // |
---|
[fda8168] | 243 | // End: // |
---|