summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/reporters/Reporter.scala
blob: 54690ba03b8b28b3857f69bc5645163e014d2b12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* NSC -- new Scala compiler
 * Copyright 2002-2007 LAMP/EPFL
 * @author Martin Odersky
 */
// $Id$

package scala.tools.nsc.reporters

import scala.tools.nsc.util.Position

/**
 * This interface provides methods to issue information, warning and
 * error messages.
 */
abstract class Reporter {
  abstract class Severity(val code: Int)
  object INFO    extends Severity(0)
  object WARNING extends Severity(1)
  object ERROR   extends Severity(2)

  def reset: Unit = {
    errors   = 0
    warnings = 0
    cancelled = false
  }

  def count(severity: Severity): Int = severity match {
    case ERROR   => errors
    case WARNING => warnings
    case INFO    => 0
  }

  def incr(severity: Severity): Unit = severity match {
    case ERROR   => errors   = errors   + 1
    case WARNING => warnings = warnings + 1
    case INFO    => {}
  }

  var errors   : Int = 0
  var warnings : Int = 0
  var cancelled: boolean = false

  def hasErrors: boolean = errors != 0 || cancelled

  protected def info0(pos: Position, msg: String, severity: Severity, force: Boolean): Unit

  def    info(pos: Position, msg: String, force: Boolean): Unit = info0(pos, msg, INFO   , force)
  def warning(pos: Position, msg: String                ): Unit = info0(pos, msg, WARNING, false)
  def   error(pos: Position, msg: String                ): Unit = info0(pos, msg,   ERROR, false)

  /** An error that could possibly be fixed if the unit were longer.
   *  This is used, for example, when the interpreter tries
   *  to distinguish fatal errors from those that are due to
   *  needing more lines of input from the user.
   */
  var incompleteInputError: (Position, String) => Unit = error

  def withIncompleteHandler[T](handler: (Position, String) => Unit)(thunk: => T) = {
    val savedHandler = incompleteInputError
    try {
      incompleteInputError = handler
      thunk
    } finally {
      incompleteInputError = savedHandler
    }
  }
}