1d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Copyright (c) 2013 The Chromium Authors. All rights reserved. 2d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 3d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// found in the LICENSE file. 4d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 5d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#ifndef TOOLS_GN_TOKEN_H_ 6d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#define TOOLS_GN_TOKEN_H_ 7d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 8d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "base/strings/string_piece.h" 9d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/location.h" 10d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 11d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass Token { 12d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch public: 13d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch enum Type { 14d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch INVALID, 15d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch INTEGER, // 123 16d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch STRING, // "blah" 173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) TRUE_TOKEN, // Not "TRUE" to avoid collisions with #define in windows.h. 183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) FALSE_TOKEN, 193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Various operators. 213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) EQUAL, 223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) PLUS, 233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) MINUS, 243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) PLUS_EQUALS, 253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) MINUS_EQUALS, 263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) EQUAL_EQUAL, 273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) NOT_EQUAL, 283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) LESS_EQUAL, 293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) GREATER_EQUAL, 303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) LESS_THAN, 313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) GREATER_THAN, 323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) BOOLEAN_AND, 333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) BOOLEAN_OR, 343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) BANG, 353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) LEFT_PAREN, 373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) RIGHT_PAREN, 383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) LEFT_BRACKET, 393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) RIGHT_BRACKET, 403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) LEFT_BRACE, 413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) RIGHT_BRACE, 423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) IF, 443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) ELSE, 45d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch IDENTIFIER, // foo 463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) COMMA, // , 473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) COMMENT, // #...\n 483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) UNCLASSIFIED_OPERATOR, // TODO(scottmg): This shouldn't be necessary. 503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) NUM_TYPES 52d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch }; 53d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 54d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Token(); 55d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Token(const Location& location, Type t, const base::StringPiece& v); 56d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 57d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Type type() const { return type_; } 58d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch const base::StringPiece& value() const { return value_; } 59d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch const Location& location() const { return location_; } 60d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch LocationRange range() const { 61d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch return LocationRange(location_, 62d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Location(location_.file(), location_.line_number(), 633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) location_.char_offset() + 643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) static_cast<int>(value_.size()))); 65d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch } 66d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 67d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch // Helper functions for comparing this token to something. 68d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch bool IsIdentifierEqualTo(const char* v) const; 69d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch bool IsStringEqualTo(const char* v) const; 70d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 71d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch // For STRING tokens, returns the string value (no quotes at end, does 72d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch // unescaping). 73d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch std::string StringValue() const; 74d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 75d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch private: 76d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Type type_; 77d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch base::StringPiece value_; 78d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Location location_; 79d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch}; 80d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 81d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#endif // TOOLS_GN_TOKEN_H_ 82