1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "tools/gn/token.h"
6
7#include "base/logging.h"
8
9namespace {
10
11std::string UnescapeString(const base::StringPiece& input) {
12  std::string result;
13  result.reserve(input.size());
14
15  for (size_t i = 0; i < input.size(); i++) {
16    if (input[i] == '\\') {
17      DCHECK(i < input.size() - 1);  // Last char shouldn't be a backslash or
18                                     // it would have escaped the terminator.
19      i++;  // Skip backslash, next char is a literal.
20    }
21    result.push_back(input[i]);
22  }
23  return result;
24}
25
26}  // namespace
27
28Token::Token() : type_(INVALID), value_() {
29}
30
31Token::Token(const Location& location,
32             Type t,
33             const base::StringPiece& v)
34    : type_(t),
35      value_(v),
36      location_(location) {
37}
38
39bool Token::IsIdentifierEqualTo(const char* v) const {
40  return type_ == IDENTIFIER && value_ == v;
41}
42
43bool Token::IsOperatorEqualTo(const char* v) const {
44  return type_ == OPERATOR && value_ == v;
45}
46
47bool Token::IsScoperEqualTo(const char* v) const {
48  return type_ == SCOPER && value_ == v;
49}
50
51bool Token::IsStringEqualTo(const char* v) const {
52  return type_ == STRING && value_ == v;
53}
54
55std::string Token::StringValue() const {
56  DCHECK(type() == STRING);
57
58  // Trim off the string terminators at the end.
59  return UnescapeString(value_.substr(1, value_.size() - 2));
60}
61