summaryrefslogtreecommitdiffstats
path: root/Source/ThirdParty/ANGLE/src/compiler/preprocessor/new/Token.cpp
blob: a2e759c6919bc1fd20784fca65a0ad98c8ab22a2 (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
//
// Copyright (c) 2011 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

#include "Token.h"

#include "token_type.h"

static const int kLocationLineSize = 16;  // in bits.
static const int kLocationLineMask = (1 << kLocationLineSize) - 1;

namespace pp
{

Token::Location Token::encodeLocation(int line, int file)
{
    return (file << kLocationLineSize) | (line & kLocationLineMask);
}

void Token::decodeLocation(Location loc, int* line, int* file)
{
    if (file) *file = loc >> kLocationLineSize;
    if (line) *line = loc & kLocationLineMask;
}

Token::Token(Location location, int type, std::string* value)
    : mLocation(location),
      mType(type),
      mValue(value)
{
}

Token::~Token() {
    delete mValue;
}

std::ostream& operator<<(std::ostream& out, const Token& token)
{
    switch (token.type())
    {
      case INT_CONSTANT:
      case FLOAT_CONSTANT:
      case IDENTIFIER:
        out << *(token.value());
        break;
      default:
        out << static_cast<char>(token.type());
        break;
    }
    return out;
}
}  // namespace pp