Permalink
Browse files

Added only() and except() methods to Env

  • Loading branch information...
1 parent 9b9e7fa commit 72bc16cabc78478659dae6d28736270cc8f76343 @davegurnell committed Jun 5, 2012
View
@@ -0,0 +1 @@
+sbt.version=0.11.2
@@ -7,7 +7,8 @@ trait Env {
def + (binding: (Id, Transform)): Env = CompoundEnv(SingleEnv(binding), this)
def ++ (that: Env): Env = CompoundEnv(that, this)
def prefix(prefix: Id): Env = PrefixEnv(prefix, this)
- def filter(names: List[Id]) = FilterEnv(names, this)
+ def only(ids: Id *) = OnlyEnv(ids.toList, this)
+ def except(ids: Id *) = ExceptEnv(ids.toList, this)
}
case class CompoundEnv(a: Env, b: Env) extends Env {
@@ -45,18 +46,26 @@ case class PrefixEnv(prefix: Id, inner: Env) extends Env {
}
}
-case class FilterEnv(allowed: List[Id], inner: Env) extends Env {
+case class OnlyEnv(allowed: List[Id], inner: Env) extends Env {
def get(id: Id): Option[Transform] = {
- if(allowed contains id) {
- inner.get(id)
- } else None
+ if(allowed contains id) inner.get(id) else None
}
def ids: Seq[Id] = {
inner.ids filter (allowed contains _)
}
}
+case class ExceptEnv(allowed: List[Id], inner: Env) extends Env {
+ def get(id: Id): Option[Transform] = {
+ if(allowed contains id) None else inner.get(id)
+ }
+
+ def ids: Seq[Id] = {
+ inner.ids filterNot (allowed contains _)
+ }
+}
+
case class MapEnv(val bindings: Map[Id, Transform]) extends Env {
def this(bindings: (Id, Transform) *) = {
this(Map(bindings : _*))
@@ -19,13 +19,13 @@ case class Template(val defn: Block, val globalEnv: Env) extends Transform with
def localEnv(callingEnv: Env, doc: Block): Env = {
val thisKwEnv = Env.empty + (Id("this") -> Expand((callingEnv, doc.body))._2)
- val argsEnv = Env.fromArgs(callingEnv, doc.args).filter(defnArgNames)
+ val argsEnv = Env.fromArgs(callingEnv, doc.args).only(defnArgNames : _*)
val bindEnv = {
def loop(env: Env, doc: Doc): Env = {
doc match {
case Block(Id("bind"), args, Range.Empty) =>
- env ++ Env.fromArgs(callingEnv, args).filter(defnArgNames)
+ env ++ Env.fromArgs(callingEnv, args).only(defnArgNames : _*)
case Block(Id("bind"), Arguments(UnitArgument(name) :: _), body) =>
env + (name -> Expand((callingEnv, body))._2)
@@ -37,7 +37,7 @@ case class Template(val defn: Block, val globalEnv: Env) extends Transform with
}
}
- loop(Env.empty, doc.body).filter(defnArgNames)
+ loop(Env.empty, doc.body).only(defnArgNames : _*)
}
val bindKwEnv =
@@ -17,20 +17,26 @@ class EnvSuite extends FunSuite {
expect(Some(titleTx))(env.get(titleId))
expect(Some(authorTx))(env.get(authorId))
expect(None)(env.get(undefinedId))
+
+ expect(Seq(titleId, authorId))(env.ids)
}
test("SingleEnv") {
val env = SingleEnv(titleId, titleTx)
expect(Some(titleTx))(env.get(titleId))
expect(None)(env.get(authorId))
expect(None)(env.get(undefinedId))
+
+ expect(Seq(titleId))(env.ids)
}
test("CompoundEnv") {
val env = CompoundEnv(SingleEnv(titleId, titleTx), SingleEnv(authorId, authorTx))
expect(Some(titleTx))(env.get(titleId))
expect(Some(authorTx))(env.get(authorId))
expect(None)(env.get(undefinedId))
+
+ expect(Seq(titleId, authorId))(env.ids)
}
test("PrefixEnv") {
@@ -49,10 +55,12 @@ class EnvSuite extends FunSuite {
expect(None)(env.get(titleId))
expect(None)(env.get(authorId))
expect(None)(env.get(undefinedId))
+
+ expect(Seq(titleId.prefix(Id("prefix:")), authorId.prefix(Id("prefix:"))))(env.ids)
}
- test("FilterEnv") {
- val env = FilterEnv(
+ test("OnlyEnv") {
+ val env = OnlyEnv(
List(titleId),
CompoundEnv(
SingleEnv(titleId, titleTx),
@@ -63,6 +71,24 @@ class EnvSuite extends FunSuite {
expect(Some(titleTx))(env.get(titleId))
expect(None)(env.get(authorId))
expect(None)(env.get(undefinedId))
+
+ expect(Seq(titleId))(env.ids)
+ }
+
+ test("ExceptEnv") {
+ val env = ExceptEnv(
+ List(titleId),
+ CompoundEnv(
+ SingleEnv(titleId, titleTx),
+ SingleEnv(authorId, authorTx)
+ )
+ )
+
+ expect(None)(env.get(titleId))
+ expect(Some(authorTx))(env.get(authorId))
+ expect(None)(env.get(undefinedId))
+
+ expect(Seq(authorId))(env.ids)
}
test("CustomEnv") {
@@ -79,5 +105,7 @@ class EnvSuite extends FunSuite {
expect(Some(Text("title")))(env.get(titleId).map(_.apply(Env.basic, Text(""))._2))
expect(None)(env.get(authorId).map(_.apply(Env.basic, Text(""))))
expect(None)(env.get(undefinedId))
+
+ expect(Seq(titleId))(env.ids)
}
}

0 comments on commit 72bc16c

Please sign in to comment.