[7d4f6ed] | 1 | // -*- Mode: C++ -*- |
---|
| 2 | // |
---|
| 3 | // Copyright (C) Peter A. Buhr 2017 |
---|
| 4 | // |
---|
| 5 | // Pretty Printer, Copyright (C) Richard C. Bilson and Rodolfo G. Esteves 2001 |
---|
| 6 | // |
---|
| 7 | // token.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Wed Jun 28 22:46:23 2017 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[c9383ee] | 12 | // Last Modified On : Thu Jun 29 09:33:49 2017 |
---|
| 13 | // Update Count : 9 |
---|
[7d4f6ed] | 14 | // |
---|
| 15 | |
---|
| 16 | #include <string> |
---|
| 17 | using namespace std; |
---|
[8c97ee7] | 18 | #include "ParserTypes.h" |
---|
| 19 | #include "parser.hh" |
---|
[7d4f6ed] | 20 | #include "filter.h" |
---|
| 21 | |
---|
[c9383ee] | 22 | Token::Token( const string & text, int kind ) : text(text), kind(kind) { |
---|
[7d4f6ed] | 23 | left = down = NULL; |
---|
| 24 | } // Token::Token |
---|
| 25 | |
---|
[c9383ee] | 26 | Token::Token( const string & text, list<string> & ws_list, int kind ) : text(text), kind(kind) { |
---|
[7d4f6ed] | 27 | // cerr << "Token3 : text \"" << text << "\""; |
---|
| 28 | // for ( list<string>::iterator i = ws_list.begin(); i != ws_list.end(); i ++ ) { |
---|
| 29 | // cerr << " WSt: \"" << *i << "\""; |
---|
| 30 | // } |
---|
| 31 | // cerr << endl; |
---|
| 32 | left = down = NULL; |
---|
| 33 | // O(1) transfer of the current lex whitespace list to the token's whitespace list, and clearing the lex whitespace |
---|
| 34 | // list. |
---|
| 35 | Token::ws_list.splice( Token::ws_list.end(), ws_list ); |
---|
| 36 | } // Token::Token |
---|
| 37 | |
---|
[c9383ee] | 38 | void Token::addLeftTail( Token * n ) { |
---|
| 39 | Token * p = this; |
---|
[7d4f6ed] | 40 | while ( p->left != 0 ) { |
---|
| 41 | p = p->left; |
---|
| 42 | } // while |
---|
| 43 | p->left = n; |
---|
| 44 | } // Token::addLeftTail |
---|
| 45 | |
---|
[c9383ee] | 46 | void Token::addDownLeftTail( Token * n ) { |
---|
[7d4f6ed] | 47 | if ( down == 0 ) { |
---|
| 48 | down = n; |
---|
| 49 | } else { |
---|
| 50 | down->addLeftTail( n ); |
---|
| 51 | } // if |
---|
| 52 | } // Token::addDownLeftTail |
---|
| 53 | |
---|
| 54 | bool Token::isTerminal() { |
---|
| 55 | return kind < END_TERMINALS; |
---|
| 56 | } // Token::isTerminal |
---|
| 57 | |
---|
| 58 | int Token::getKind() const { |
---|
| 59 | return kind; |
---|
| 60 | } // Token::getKind() |
---|
| 61 | |
---|
| 62 | string Token::getText() const { |
---|
| 63 | return text; |
---|
| 64 | } // Token::getText() |
---|
| 65 | |
---|
| 66 | string Token::getWS() { |
---|
| 67 | string ret; |
---|
| 68 | // concatenate whitespace and comment text |
---|
| 69 | for ( list<string>::iterator i = ws_list.begin(); i != ws_list.end(); i ++ ) { |
---|
| 70 | ret += *i; |
---|
| 71 | } // for |
---|
| 72 | return ret; |
---|
| 73 | } // Token::getWS |
---|
| 74 | |
---|
| 75 | bool Token::isComment() { |
---|
| 76 | return ws_list.size() > 1 || ( ws_list.size() == 1 && (*ws_list.begin())[0] == '/' ); |
---|
| 77 | } // Token::isComment |
---|
| 78 | |
---|
| 79 | string Token::getComment() { |
---|
| 80 | string ret; |
---|
| 81 | // concatenate whitespace and comment text up to the last comment |
---|
| 82 | if ( ws_list.size() > 1 ) { |
---|
| 83 | list<string>::iterator end = -- ws_list.end(); |
---|
| 84 | if ( (*end)[0] == '/' ) end ++; |
---|
| 85 | for ( list<string>::iterator i = ws_list.begin(); i != end; i ++ ) { |
---|
| 86 | ret += *i; |
---|
| 87 | } // for |
---|
| 88 | } else if ( ws_list.size() == 1 ) { |
---|
| 89 | if ( (*ws_list.begin())[0] == '/' ) ret = *ws_list.begin(); |
---|
| 90 | } // if |
---|
| 91 | return ret; |
---|
| 92 | } // Token::getComment |
---|
| 93 | |
---|
| 94 | // Local Variables: // |
---|
| 95 | // mode: c++ // |
---|
| 96 | // tab-width: 4 // |
---|
| 97 | // compile-command: "make install" // |
---|
| 98 | // End: // |
---|