summaryrefslogtreecommitdiffstats
path: root/tools/gn/token.h
blob: 25f176bba7f1939891dc443bcfec2c58d04bbacf (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef TOOLS_GN_TOKEN_H_
#define TOOLS_GN_TOKEN_H_

#include "base/strings/string_piece.h"
#include "tools/gn/location.h"

class Token {
 public:
  enum Type {
    INVALID,
    INTEGER,    // 123
    STRING,     // "blah"
    TRUE_TOKEN,  // Not "TRUE" to avoid collisions with #define in windows.h.
    FALSE_TOKEN,

    // Various operators.
    EQUAL,
    PLUS,
    MINUS,
    PLUS_EQUALS,
    MINUS_EQUALS,
    EQUAL_EQUAL,
    NOT_EQUAL,
    LESS_EQUAL,
    GREATER_EQUAL,
    LESS_THAN,
    GREATER_THAN,
    BOOLEAN_AND,
    BOOLEAN_OR,
    BANG,

    LEFT_PAREN,
    RIGHT_PAREN,
    LEFT_BRACKET,
    RIGHT_BRACKET,
    LEFT_BRACE,
    RIGHT_BRACE,

    IF,
    ELSE,
    IDENTIFIER, // foo
    COMMA,  // ,
    COMMENT,    // #...\n

    UNCLASSIFIED_OPERATOR,  // TODO(scottmg): This shouldn't be necessary.

    NUM_TYPES
  };

  Token();
  Token(const Location& location, Type t, const base::StringPiece& v);

  Type type() const { return type_; }
  const base::StringPiece& value() const { return value_; }
  const Location& location() const { return location_; }
  LocationRange range() const {
    return LocationRange(location_,
                         Location(location_.file(), location_.line_number(),
                                  location_.char_offset() +
                                      static_cast<int>(value_.size())));
  }

  // Helper functions for comparing this token to something.
  bool IsIdentifierEqualTo(const char* v) const;
  bool IsStringEqualTo(const char* v) const;

  // For STRING tokens, returns the string value (no quotes at end, does
  // unescaping).
  std::string StringValue() const;

 private:
  Type type_;
  base::StringPiece value_;
  Location location_;
};

#endif  // TOOLS_GN_TOKEN_H_