|
| 1 | +package dotty.tools |
| 2 | +package dotc |
| 3 | +package reporting |
| 4 | + |
| 5 | +import scala.collection.mutable |
| 6 | +import util.SourcePosition |
| 7 | +import core.Contexts._ |
| 8 | +import Reporter._ |
| 9 | +import java.io.{ BufferedReader, IOException, PrintWriter } |
| 10 | +import scala.reflect.internal.util._ |
| 11 | +import diagnostic.{ Message, MessageContainer, NoExplanation } |
| 12 | +import diagnostic.messages._ |
| 13 | + |
| 14 | +/** |
| 15 | + * This class implements a Reporter that displays messages on a text |
| 16 | + * console. |
| 17 | + */ |
| 18 | +class ClassicReporter( |
| 19 | + reader: BufferedReader = Console.in, |
| 20 | + writer: PrintWriter = new PrintWriter(Console.err, true)) |
| 21 | + extends Reporter with UniqueMessagePositions with HideNonSensicalMessages { |
| 22 | + |
| 23 | + import MessageContainer._ |
| 24 | + |
| 25 | + /** maximal number of error messages to be printed */ |
| 26 | + protected def ErrorLimit = 100 |
| 27 | + |
| 28 | + def printPos(pos: SourcePosition): Unit = |
| 29 | + if (pos.exists) { |
| 30 | + printMessage(pos.lineContent.stripLineEnd) |
| 31 | + printMessage(" " * pos.column + "^") |
| 32 | + if (pos.outer.exists) { |
| 33 | + printMessage(s"\n... this location is in code that was inlined at ${pos.outer}:\n") |
| 34 | + printPos(pos.outer) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** Prints the message. */ |
| 39 | + def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() } |
| 40 | + |
| 41 | + /** Prints the message with the given position indication. */ |
| 42 | + def printMessageAndPos(msg: String, pos: SourcePosition)(implicit ctx: Context): Unit = { |
| 43 | + val posStr = if (pos.exists) s"$pos: " else "" |
| 44 | + printMessage(posStr + msg) |
| 45 | + printPos(pos) |
| 46 | + } |
| 47 | + |
| 48 | + override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = m match { |
| 49 | + case m: Error => |
| 50 | + printMessageAndPos(s"error: ${m.contained}", m.pos) |
| 51 | + if (ctx.settings.prompt.value) displayPrompt() |
| 52 | + case m: ConditionalWarning if !m.enablingOption.value => |
| 53 | + case m: FeatureWarning => |
| 54 | + printMessageAndPos(s"feature warning: ${m.contained.msg}", m.pos) |
| 55 | + case m: DeprecationWarning => |
| 56 | + printMessageAndPos(s"deprecation warning: ${m.contained.msg}", m.pos) |
| 57 | + case m: UncheckedWarning => |
| 58 | + printMessageAndPos(s"unchecked warning: ${m.contained.msg}", m.pos) |
| 59 | + case m: Info => |
| 60 | + printMessageAndPos(m.contained.msg, m.pos) |
| 61 | + case m: MigrationWarning => |
| 62 | + printMessageAndPos(s"migration warning: ${m.contained.msg}", m.pos) |
| 63 | + case m: Warning => |
| 64 | + printMessageAndPos(s"warning: ${m.contained.msg}", m.pos) |
| 65 | + case _ => |
| 66 | + printMessageAndPos(m.contained.msg, m.pos) |
| 67 | + } |
| 68 | + |
| 69 | + def displayPrompt(): Unit = { |
| 70 | + writer.print("\na)bort, s)tack, r)esume: ") |
| 71 | + writer.flush() |
| 72 | + if (reader != null) { |
| 73 | + val response = reader.read().asInstanceOf[Char].toLower |
| 74 | + if (response == 'a' || response == 's') { |
| 75 | + Thread.dumpStack() |
| 76 | + if (response == 'a') |
| 77 | + sys.exit(1) |
| 78 | + } |
| 79 | + writer.print("\n") |
| 80 | + writer.flush() |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + override def flush()(implicit ctx: Context): Unit = { writer.flush() } |
| 85 | +} |
0 commit comments