blob: 453b10680857b6f998abfc24a00de58c14933ff3 (
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
68
69
70
71
72
73
|
/* NSC -- new Scala compiler
* Copyright 2005-2011 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 =>
/*
// A declaration introduces names and assigns them types.
// It can form part of a class definition (§5.1) or of a refinement in a compound type (§3.2.7).
// (Ed: aka abstract members.)
//
// ‘val’ ValDcl | ‘var’ VarDcl | ‘def’ FunDcl | ‘type’ {nl} TypeDcl
def decl(code: String): T
// A definition introduces names that denote terms or types.
// It can form part of an object or class definition or it can be local to a block.
// (Ed: aka concrete members.)
//
// ‘val’ PatDef | ‘var’ VarDef | ‘def’ FunDef | ‘type’ {nl} TypeDef |
// [‘case’] ‘class’ ClassDef | [‘case’] ‘object’ ObjectDef | ‘trait’ TraitDef
def defn(code: String): T
*/
// Expressions are composed of operators and operands.
def expr(code: String): T
// An import clause has the form import p.I where p is a stable identifier (§3.1) and I is an import expression.
def impt(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 decl(code: String) = try Some(self.decl(code)) catch handler
// def defn(code: String) = try Some(self.defn(code)) catch handler
def expr(code: String) = try Some(self.expr(code)) catch handler
def impt(code: String) = try Some(self.impt(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")
}
|