• 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 #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 
11 class Token {
12  public:
13   enum Type {
14     INVALID,
15     INTEGER,    // 123
16     STRING,     // "blah"
17     TRUE_TOKEN,  // Not "TRUE" to avoid collisions with #define in windows.h.
18     FALSE_TOKEN,
19 
20     // Various operators.
21     EQUAL,
22     PLUS,
23     MINUS,
24     PLUS_EQUALS,
25     MINUS_EQUALS,
26     EQUAL_EQUAL,
27     NOT_EQUAL,
28     LESS_EQUAL,
29     GREATER_EQUAL,
30     LESS_THAN,
31     GREATER_THAN,
32     BOOLEAN_AND,
33     BOOLEAN_OR,
34     BANG,
35 
36     LEFT_PAREN,
37     RIGHT_PAREN,
38     LEFT_BRACKET,
39     RIGHT_BRACKET,
40     LEFT_BRACE,
41     RIGHT_BRACE,
42 
43     IF,
44     ELSE,
45     IDENTIFIER, // foo
46     COMMA,  // ,
47     COMMENT,    // #...\n
48 
49     UNCLASSIFIED_OPERATOR,  // TODO(scottmg): This shouldn't be necessary.
50 
51     NUM_TYPES
52   };
53 
54   Token();
55   Token(const Location& location, Type t, const base::StringPiece& v);
56 
type()57   Type type() const { return type_; }
value()58   const base::StringPiece& value() const { return value_; }
location()59   const Location& location() const { return location_; }
range()60   LocationRange range() const {
61     return LocationRange(location_,
62                          Location(location_.file(), location_.line_number(),
63                                   location_.char_offset() +
64                                       static_cast<int>(value_.size())));
65   }
66 
67   // Helper functions for comparing this token to something.
68   bool IsIdentifierEqualTo(const char* v) const;
69   bool IsStringEqualTo(const char* v) const;
70 
71   // For STRING tokens, returns the string value (no quotes at end, does
72   // unescaping).
73   std::string StringValue() const;
74 
75  private:
76   Type type_;
77   base::StringPiece value_;
78   Location location_;
79 };
80 
81 #endif  // TOOLS_GN_TOKEN_H_
82