Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

make +: within an object comprehension functional #101

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sjsonnet/src/sjsonnet/Evaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class Evaluator(parseCache: collection.mutable.Map[String, fastparse.Parsed[(Exp
}
newSelf

case ObjBody.ObjComp(preLocals, key, value, postLocals, first, rest) =>
case ObjBody.ObjComp(preLocals, key, value, plus, postLocals, first, rest) =>
lazy val compScope: ValScope = scope.extend(
newSuper = None
)
Expand All @@ -487,7 +487,7 @@ class Evaluator(parseCache: collection.mutable.Map[String, fastparse.Parsed[(Exp

visitExpr(key)(s, implicitly) match {
case Val.Str(k) =>
builder += (k -> Val.Obj.Member(false, Visibility.Normal, (self: Val.Obj, sup: Option[Val.Obj], _, _) =>
builder += (k -> Val.Obj.Member(plus, Visibility.Normal, (self: Val.Obj, sup: Option[Val.Obj], _, _) =>
visitExpr(value)(
s.extend(
newBindings,
Expand Down
3 changes: 2 additions & 1 deletion sjsonnet/src/sjsonnet/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ object Expr{
case class ObjComp(preLocals: Seq[Member.BindStmt],
key: Expr,
value: Expr,
plus: Boolean,
postLocals: Seq[Member.BindStmt],
first: ForSpec,
rest: Seq[CompSpec]) extends ObjBody
}

}
}
4 changes: 2 additions & 2 deletions sjsonnet/src/sjsonnet/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ object Parser{
val preLocals = exprs
.takeWhile(_.isInstanceOf[Expr.Member.BindStmt])
.map(_.asInstanceOf[Expr.Member.BindStmt])
val Expr.Member.Field(offset, Expr.FieldName.Dyn(lhs), _, None, Visibility.Normal, rhs) =
val Expr.Member.Field(offset, Expr.FieldName.Dyn(lhs), plus, None, Visibility.Normal, rhs) =
exprs(preLocals.length)
val postLocals = exprs.drop(preLocals.length+1).takeWhile(_.isInstanceOf[Expr.Member.BindStmt])
.map(_.asInstanceOf[Expr.Member.BindStmt])
Expand All @@ -303,7 +303,7 @@ object Parser{
Fail.opaque(s"""no duplicate field: "${lhs.asInstanceOf[Expr.Str].value}" """)
case _ => // do nothing
}
Expr.ObjBody.ObjComp(preLocals, lhs, rhs, postLocals, comps._1, comps._2)
Expr.ObjBody.ObjComp(preLocals, lhs, rhs, plus, postLocals, comps._1, comps._2)
}

def member[_: P]: P[Expr.Member] = P( objlocal | "assert" ~~ assertStmt | field )
Expand Down
1 change: 1 addition & 0 deletions sjsonnet/test/src/sjsonnet/EvaluatorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ object EvaluatorTests extends TestSuite{

eval("{ ['foo']+: x for x in []}", false) ==> ujson.Obj()
eval("{ ['foo']+: x for x in [1]}", false) ==> ujson.Obj("foo" -> 1)
eval("{ ['foo']+: [x] for x in [1]} + { ['foo']+: [x] for x in [2]}", false) ==> ujson.Obj("foo" -> ujson.Arr(1,2))
}
test("givenNoDuplicateFieldsInListComprehension1_expectSuccess") {
eval("""{ ["bar"]: x for x in [-876.89]}""") ==> ujson.Obj("bar" -> -876.89)
Expand Down