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_LOCATION_H_
6d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#define TOOLS_GN_LOCATION_H_
7d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
8d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <string>
9d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
10d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass InputFile;
11d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
12d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Represents a place in a source file. Used for error reporting.
13d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass Location {
14d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch public:
15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  Location();
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Location(const InputFile* file, int line_number, int char_offset, int byte);
17d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
18d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  const InputFile* file() const { return file_; }
19d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  int line_number() const { return line_number_; }
20d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  int char_offset() const { return char_offset_; }
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  int byte() const { return byte_; }
22d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
23d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool operator==(const Location& other) const;
24d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool operator!=(const Location& other) const;
25d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool operator<(const Location& other) const;
26d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
27d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Returns a string with the file, line, and (optionally) the character
28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // offset for this location. If this location is null, returns an empty
29d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // string.
30d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  std::string Describe(bool include_char_offset) const;
31d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
32d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch private:
33d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  const InputFile* file_;  // Null when unset.
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  int line_number_;        // -1 when unset. 1-based.
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  int char_offset_;        // -1 when unset. 1-based.
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  int byte_;               // Index into the buffer, 0-based.
37d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch};
38d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
39d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Represents a range in a source file. Used for error reporting.
40d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// The end is exclusive i.e. [begin, end)
41d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass LocationRange {
42d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch public:
43d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  LocationRange();
44d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  LocationRange(const Location& begin, const Location& end);
45d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
46d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  const Location& begin() const { return begin_; }
47d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  const Location& end() const { return end_; }
48d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
49d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  LocationRange Union(const LocationRange& other) const;
50d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
51d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch private:
52d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  Location begin_;
53d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  Location end_;
54d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch};
55d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
56d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#endif  // TOOLS_GN_LOCATION_H_
57