token.h revision d3868032626d59662ff73b372b5d584c1d144c53
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" 17d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch OPERATOR, // =, +=, -=, +, -, ==, !=, <=, >=, <, > 18d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch IDENTIFIER, // foo 19d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch SCOPER, // (, ), [, ], {, } 20d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch SEPARATOR, // , 21d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch COMMENT // #...\n 22d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch }; 23d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 24d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Token(); 25d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Token(const Location& location, Type t, const base::StringPiece& v); 26d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 27d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Type type() const { return type_; } 28d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch const base::StringPiece& value() const { return value_; } 29d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch const Location& location() const { return location_; } 30d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch LocationRange range() const { 31d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch return LocationRange(location_, 32d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Location(location_.file(), location_.line_number(), 33d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch location_.char_offset() + value_.size())); 34d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch } 35d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 36d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch // Helper functions for comparing this token to something. 37d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch bool IsIdentifierEqualTo(const char* v) const; 38d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch bool IsOperatorEqualTo(const char* v) const; 39d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch bool IsScoperEqualTo(const char* v) const; 40d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch bool IsStringEqualTo(const char* v) const; 41d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 42d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch // For STRING tokens, returns the string value (no quotes at end, does 43d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch // unescaping). 44d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch std::string StringValue() const; 45d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 46d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch private: 47d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Type type_; 48d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch base::StringPiece value_; 49d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Location location_; 50d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch}; 51d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 52d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#endif // TOOLS_GN_TOKEN_H_ 53