source: tools/prettyprinter/token.cc @ 2b7afbd

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 2b7afbd was 7d4f6ed, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

remainder of pretty printer

  • Property mode set to 100644
File size: 2.5 KB
Line 
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
12// Last Modified On : Wed Jun 28 23:06:07 2017
13// Update Count     : 8
14//
15
16#include <string>
17using namespace std;
18#include "parser.hh"
19#include "parser.h"
20#include "filter.h"
21
22Token::Token( const string &text, int kind ) : text(text), kind(kind) {
23        left = down = NULL;
24} // Token::Token
25
26Token::Token( const string &text, list<string> &ws_list, int kind ) : text(text), kind(kind) {
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
38void Token::addLeftTail( Token *n ) {
39        Token *p = this;
40        while ( p->left != 0 ) {
41                p = p->left;
42        } // while
43        p->left = n;
44} // Token::addLeftTail
45
46void Token::addDownLeftTail( Token *n ) {
47        if ( down == 0 ) {
48                down = n;
49        } else {
50                down->addLeftTail( n );
51        } // if
52} // Token::addDownLeftTail
53
54bool Token::isTerminal() {
55        return kind < END_TERMINALS;
56} // Token::isTerminal
57
58int Token::getKind() const {
59        return kind;
60} // Token::getKind()
61
62string Token::getText() const {
63        return text;
64} // Token::getText()
65
66string 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
75bool Token::isComment() {
76        return ws_list.size() > 1 || ( ws_list.size() == 1 && (*ws_list.begin())[0] == '/' );
77} // Token::isComment
78
79string 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: //
Note: See TracBrowser for help on using the repository browser.