• 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     DOT,
36 
37     LEFT_PAREN,
38     RIGHT_PAREN,
39     LEFT_BRACKET,
40     RIGHT_BRACKET,
41     LEFT_BRACE,
42     RIGHT_BRACE,
43 
44     IF,
45     ELSE,
46     IDENTIFIER, // foo
47     COMMA,  // ,
48     UNCLASSIFIED_COMMENT,  // #...\n, of unknown style (will be converted
49                            // to one below)
50     LINE_COMMENT,      // #...\n on a line alone.
51     SUFFIX_COMMENT,    // #...\n on a line following other code.
52     BLOCK_COMMENT,     // #...\n line comment, but free-standing.
53 
54     UNCLASSIFIED_OPERATOR,
55 
56     NUM_TYPES
57   };
58 
59   Token();
60   Token(const Location& location, Type t, const base::StringPiece& v);
61 
type()62   Type type() const { return type_; }
value()63   const base::StringPiece& value() const { return value_; }
location()64   const Location& location() const { return location_; }
range()65   LocationRange range() const {
66     return LocationRange(
67         location_,
68         Location(location_.file(),
69                  location_.line_number(),
70                  location_.char_offset() + static_cast<int>(value_.size()),
71                  location_.byte() + static_cast<int>(value_.size())));
72   }
73 
74   // Helper functions for comparing this token to something.
75   bool IsIdentifierEqualTo(const char* v) const;
76   bool IsStringEqualTo(const char* v) const;
77 
78  private:
79   Type type_;
80   base::StringPiece value_;
81   Location location_;
82 };
83 
84 #endif  // TOOLS_GN_TOKEN_H_
85