• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SKSL_ERRORREPORTER
9 #define SKSL_ERRORREPORTER
10 
11 #include "src/sksl/SkSLPosition.h"
12 
13 namespace SkSL {
14 
15 /**
16  * Interface for the compiler to report errors.
17  */
18 class ErrorReporter {
19 public:
~ErrorReporter()20     virtual ~ErrorReporter() {}
21 
22     /** Reports an error message at the given character offset of the source text. */
23     virtual void error(int offset, String msg) = 0;
24 
error(int offset,const char * msg)25     void error(int offset, const char* msg) {
26         this->error(offset, String(msg));
27     }
28 
29     /** Returns the number of errors that have been reported. */
30     virtual int errorCount() = 0;
31 
32     /**
33      * Truncates the error list to the first `numErrors` reports. This allows us to backtrack and
34      * try another approach if a problem is encountered while speculatively parsing code.
35      */
36     virtual void setErrorCount(int numErrors) = 0;
37 };
38 
39 /**
40  * Error reporter for tests that need an SkSL context; aborts immediately if an error is reported.
41  */
42 class TestingOnly_AbortErrorReporter : public ErrorReporter {
43 public:
error(int offset,String msg)44     void error(int offset, String msg) override { SK_ABORT("%s", msg.c_str()); }
errorCount()45     int errorCount() override { return 0; }
setErrorCount(int)46     void setErrorCount(int) override {}
47 };
48 
49 }  // namespace SkSL
50 
51 #endif
52