summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/interpreter/CodeHandlers.scala
blob: a348478f93e4a31e32c69d264708b75af33748f2 (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
/* NSC -- new Scala compiler
 * Copyright 2005-2012 LAMP/EPFL
 * @author  Paul Phillips
 */

package scala.tools.nsc
package interpreter

import CodeHandlers.NoSuccess
import scala.util.control.ControlThrowable

/**
 *  The start of a simpler interface for utilizing the compiler with piecemeal
 *  code strings.  The "T" here could potentially be a Tree, a Type, a Symbol,
 *  a Boolean, or something even more exotic.
 */
trait CodeHandlers[T] {
  self =>

  // Expressions are composed of operators and operands.
  def expr(code: String): T

  // Statements occur as parts of blocks and templates.
  // A statement can be an import, a definition or an expression, or it can be empty.
  // Statements used in the template of a class definition can also be declarations.
  def stmt(code: String): T
  def stmts(code: String): Seq[T]

  object opt extends CodeHandlers[Option[T]] {
    val handler: PartialFunction[Throwable, Option[T]] = {
      case _: NoSuccess => None
    }
    val handlerSeq: PartialFunction[Throwable, Seq[Option[T]]] = {
      case _: NoSuccess => Nil
    }

    def expr(code: String)   = try Some(self.expr(code)) catch handler
    def stmt(code: String)   = try Some(self.stmt(code)) catch handler
    def stmts(code: String)  = try (self.stmts(code) map (x => Some(x))) catch handlerSeq
  }
}

object CodeHandlers {
  def incomplete() = throw CodeIncomplete
  def fail(msg: String) = throw new CodeException(msg)

  trait NoSuccess extends ControlThrowable
  class CodeException(msg: String) extends RuntimeException(msg) with NoSuccess { }
  object CodeIncomplete extends CodeException("CodeIncomplete")
}