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 <string_view>
9 
10 #include "gn/location.h"
11 
12 class Token {
13  public:
14   enum Type {
15     INVALID,
16     INTEGER,     // 123
17     STRING,      // "blah"
18     TRUE_TOKEN,  // Not "TRUE" to avoid collisions with #define in windows.h.
19     FALSE_TOKEN,
20 
21     // Various operators.
22     EQUAL,
23     PLUS,
24     MINUS,
25     PLUS_EQUALS,
26     MINUS_EQUALS,
27     EQUAL_EQUAL,
28     NOT_EQUAL,
29     LESS_EQUAL,
30     GREATER_EQUAL,
31     LESS_THAN,
32     GREATER_THAN,
33     BOOLEAN_AND,
34     BOOLEAN_OR,
35     BANG,
36     DOT,
37 
38     LEFT_PAREN,
39     RIGHT_PAREN,
40     LEFT_BRACKET,
41     RIGHT_BRACKET,
42     LEFT_BRACE,
43     RIGHT_BRACE,
44 
45     IF,
46     ELSE,
47     IDENTIFIER,            // foo
48     COMMA,                 // ,
49     UNCLASSIFIED_COMMENT,  // #...\n, of unknown style (will be converted
50                            // to one below)
51     LINE_COMMENT,          // #...\n on a line alone.
52     SUFFIX_COMMENT,        // #...\n on a line following other code.
53     BLOCK_COMMENT,         // #...\n line comment, but free-standing.
54 
55     UNCLASSIFIED_OPERATOR,
56 
57     NUM_TYPES
58   };
59 
60   Token();
61   Token(const Location& location, Type t, const std::string_view& v);
62 
type()63   Type type() const { return type_; }
value()64   const std::string_view& value() const { return value_; }
location()65   const Location& location() const { return location_; }
set_location(Location location)66   void set_location(Location location) { location_ = location; }
range()67   LocationRange range() const {
68     return LocationRange(
69         location_,
70         Location(location_.file(), location_.line_number(),
71                  location_.column_number() + static_cast<int>(value_.size()),
72                  location_.byte() + static_cast<int>(value_.size())));
73   }
74 
75   // Helper functions for comparing this token to something.
76   bool IsIdentifierEqualTo(const char* v) const;
77   bool IsStringEqualTo(const char* v) const;
78 
79  private:
80   Type type_;
81   std::string_view value_;
82   Location location_;
83 };
84 
85 #endif  // TOOLS_GN_TOKEN_H_
86