Home | History | Annotate | Download | only in gn
      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_ERR_H_
      6 #define TOOLS_GN_ERR_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "tools/gn/location.h"
     12 #include "tools/gn/token.h"
     13 
     14 class ParseNode;
     15 class Value;
     16 
     17 // Result of doing some operation. Check has_error() to see if an error
     18 // occurred.
     19 //
     20 // An error has a location and a message. Below that, is some optional help
     21 // text to go with the annotation of the location.
     22 //
     23 // An error can also have sub-errors which are additionally printed out
     24 // below. They can provide additional context.
     25 class Err {
     26  public:
     27   typedef std::vector<LocationRange> RangeList;
     28 
     29   // Indicates no error.
     30   Err();
     31 
     32   // Error at a single point.
     33   Err(const Location& location,
     34       const std::string& msg,
     35       const std::string& help = std::string());
     36 
     37   // Error at a given range.
     38   Err(const LocationRange& range,
     39       const std::string& msg,
     40       const std::string& help = std::string());
     41 
     42   // Error at a given token.
     43   Err(const Token& token,
     44       const std::string& msg,
     45       const std::string& help_text = std::string());
     46 
     47   // Error at a given node.
     48   Err(const ParseNode* node,
     49       const std::string& msg,
     50       const std::string& help_text = std::string());
     51 
     52   // Error at a given value.
     53   Err(const Value& value,
     54       const std::string msg,
     55       const std::string& help_text = std::string());
     56 
     57   ~Err();
     58 
     59   bool has_error() const { return has_error_; }
     60   const Location& location() const { return location_; }
     61   const std::string& message() const { return message_; }
     62   const std::string& help_text() const { return help_text_; }
     63 
     64   void AppendRange(const LocationRange& range) { ranges_.push_back(range); }
     65   const RangeList& ranges() const { return ranges_; }
     66 
     67   void AppendSubErr(const Err& err);
     68 
     69   void PrintToStdout() const;
     70 
     71  private:
     72   void InternalPrintToStdout(bool is_sub_err) const;
     73 
     74   bool has_error_;
     75   Location location_;
     76 
     77   std::vector<LocationRange> ranges_;
     78 
     79   std::string message_;
     80   std::string help_text_;
     81 
     82   std::vector<Err> sub_errs_;
     83 };
     84 
     85 #endif  // TOOLS_GN_ERR_H_
     86