summaryrefslogtreecommitdiff
path: root/src/parse/tokentree.cpp
blob: 2d807f7d8f8cd62be5dd18bce6484e679ee2e80f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * MRustC - Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * parse/tokentree.cpp
 * - Token Tree (collection of tokens)
 */
#include "tokentree.hpp"
#include <common.hpp>

TokenTree TokenTree::clone() const
{
    if( m_subtrees.size() == 0 ) {
        return TokenTree(m_hygiene, m_tok.clone());
    }
    else {
        ::std::vector< TokenTree>   ents;
        ents.reserve( m_subtrees.size() );
        for(const auto& sub : m_subtrees)
            ents.push_back( sub.clone() );
        return TokenTree( m_hygiene, mv$(ents) );
    }
}

::std::ostream& operator<<(::std::ostream& os, const TokenTree& tt)
{
    if( tt.m_subtrees.size() == 0 )
    {
        switch(tt.m_tok.type())
        {
        case TOK_IDENT:
        case TOK_LIFETIME:
            os << "/*" << tt.m_hygiene << "*/";
            break;
        default:
            if( TOK_INTERPOLATED_IDENT <= tt.m_tok.type() && tt.m_tok.type() <= TOK_INTERPOLATED_ITEM ) {
                os << "/*int*/";
            }
            break;
        }
        return os << tt.m_tok.to_str();
    }
    else {
        os << "/*" << tt.m_hygiene << " TT*/";
        // NOTE: All TTs (except the outer tt on a macro invocation) include the grouping
        bool first = true;
        for(const auto& i : tt.m_subtrees) {
            if(!first)
                os << " ";
            os << i;
            first = false;
        }
        return os;
    }
}