pending-compilation-error-handler.h 2.35 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_PENDING_COMPILATION_ERROR_HANDLER_H_
#define V8_PENDING_COMPILATION_ERROR_HANDLER_H_

#include "src/base/macros.h"
#include "src/globals.h"
10
#include "src/handles.h"
11
#include "src/messages.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

namespace v8 {
namespace internal {

class AstRawString;
class Isolate;
class Script;

// Helper class for handling pending compilation errors consistently in various
// compilation phases.
class PendingCompilationErrorHandler {
 public:
  PendingCompilationErrorHandler()
      : has_pending_error_(false),
        start_position_(-1),
        end_position_(-1),
28
        message_(MessageTemplate::kNone),
29 30 31 32 33
        arg_(nullptr),
        char_arg_(nullptr),
        error_type_(kSyntaxError) {}

  void ReportMessageAt(int start_position, int end_position,
34 35
                       MessageTemplate::Template message,
                       const char* arg = nullptr,
36 37 38 39 40 41 42 43 44 45 46 47
                       ParseErrorType error_type = kSyntaxError) {
    if (has_pending_error_) return;
    has_pending_error_ = true;
    start_position_ = start_position;
    end_position_ = end_position;
    message_ = message;
    char_arg_ = arg;
    arg_ = nullptr;
    error_type_ = error_type;
  }

  void ReportMessageAt(int start_position, int end_position,
48 49
                       MessageTemplate::Template message,
                       const AstRawString* arg,
50 51 52 53 54 55 56 57 58 59 60 61 62 63
                       ParseErrorType error_type = kSyntaxError) {
    if (has_pending_error_) return;
    has_pending_error_ = true;
    start_position_ = start_position;
    end_position_ = end_position;
    message_ = message;
    char_arg_ = nullptr;
    arg_ = arg;
    error_type_ = error_type;
  }

  bool has_pending_error() const { return has_pending_error_; }

  void ThrowPendingError(Isolate* isolate, Handle<Script> script);
64
  Handle<String> FormatMessage(Isolate* isolate);
65 66

 private:
67 68
  Handle<String> ArgumentString(Isolate* isolate);

69 70 71
  bool has_pending_error_;
  int start_position_;
  int end_position_;
72
  MessageTemplate::Template message_;
73 74 75 76 77 78 79 80 81 82
  const AstRawString* arg_;
  const char* char_arg_;
  ParseErrorType error_type_;

  DISALLOW_COPY_AND_ASSIGN(PendingCompilationErrorHandler);
};

}  // namespace internal
}  // namespace v8
#endif  // V8_PENDING_COMPILATION_ERROR_HANDLER_H_