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#ifndef TOOLS_GN_TOKEN_H_
6#define TOOLS_GN_TOKEN_H_
7
8#include "base/strings/string_piece.h"
9#include "tools/gn/location.h"
10
11class Token {
12 public:
13  enum Type {
14    INVALID,
15    INTEGER,    // 123
16    STRING,     // "blah"
17    OPERATOR,   // =, +=, -=, +, -, ==, !=, <=, >=, <, >
18    IDENTIFIER, // foo
19    SCOPER,     // (, ), [, ], {, }
20    SEPARATOR,  // ,
21    COMMENT     // #...\n
22  };
23
24  Token();
25  Token(const Location& location, Type t, const base::StringPiece& v);
26
27  Type type() const { return type_; }
28  const base::StringPiece& value() const { return value_; }
29  const Location& location() const { return location_; }
30  LocationRange range() const {
31    return LocationRange(location_,
32                         Location(location_.file(), location_.line_number(),
33                                  location_.char_offset() + value_.size()));
34  }
35
36  // Helper functions for comparing this token to something.
37  bool IsIdentifierEqualTo(const char* v) const;
38  bool IsOperatorEqualTo(const char* v) const;
39  bool IsScoperEqualTo(const char* v) const;
40  bool IsStringEqualTo(const char* v) const;
41
42  // For STRING tokens, returns the string value (no quotes at end, does
43  // unescaping).
44  std::string StringValue() const;
45
46 private:
47  Type type_;
48  base::StringPiece value_;
49  Location location_;
50};
51
52#endif  // TOOLS_GN_TOKEN_H_
53