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, std::string_view v); 62 63 static Token ClassifyAndMake(const Location& location, std::string_view v); 64 type()65 Type type() const { return type_; } value()66 std::string_view value() const { return value_; } location()67 const Location& location() const { return location_; } set_location(Location location)68 void set_location(Location location) { location_ = location; } range()69 LocationRange range() const { 70 return LocationRange( 71 location_, 72 Location(location_.file(), location_.line_number(), 73 location_.column_number() + static_cast<int>(value_.size()))); 74 } 75 76 // Helper functions for comparing this token to something. 77 bool IsIdentifierEqualTo(const char* v) const; 78 bool IsStringEqualTo(const char* v) const; 79 80 private: 81 Type type_; 82 std::string_view value_; 83 Location location_; 84 }; 85 86 #endif // TOOLS_GN_TOKEN_H_ 87