• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 namespace {
10 
UnescapeString(const base::StringPiece & input)11 std::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 
Token()28 Token::Token() : type_(INVALID), value_() {
29 }
30 
Token(const Location & location,Type t,const base::StringPiece & v)31 Token::Token(const Location& location,
32              Type t,
33              const base::StringPiece& v)
34     : type_(t),
35       value_(v),
36       location_(location) {
37 }
38 
IsIdentifierEqualTo(const char * v) const39 bool Token::IsIdentifierEqualTo(const char* v) const {
40   return type_ == IDENTIFIER && value_ == v;
41 }
42 
IsStringEqualTo(const char * v) const43 bool Token::IsStringEqualTo(const char* v) const {
44   return type_ == STRING && value_ == v;
45 }
46 
StringValue() const47 std::string Token::StringValue() const {
48   DCHECK(type() == STRING);
49 
50   // Trim off the string terminators at the end.
51   return UnescapeString(value_.substr(1, value_.size() - 2));
52 }
53