Skip to content

Commit

Permalink
Merge pull request #86 from szeiger/issue/69
Browse files Browse the repository at this point in the history
Detect duplicate fields in the parser
  • Loading branch information
szeiger authored Aug 24, 2020
2 parents a7e217b + 4902c1a commit 0eaea66
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 12 additions & 1 deletion sjsonnet/src/sjsonnet/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,18 @@ object Parser{

def objinside[_: P]: P[Expr.ObjBody] = P(
member.rep(sep = ",") ~ ",".? ~ (forspec ~ compspec).?
).map{
).flatMap { case t @ (exprs, _) =>
val seen = collection.mutable.Set.empty[String]
var overlap: String = null
exprs.foreach {
case Expr.Member.Field(_, Expr.FieldName.Fixed(n), _, _, _, _) =>
if(seen(n)) overlap = n
else seen.add(n)
case _ =>
}
if (overlap == null) Pass(t)
else Fail.opaque("no duplicate field: " + overlap)
}.map{
case (exprs, None) => Expr.ObjBody.MemberList(exprs)
case (exprs, Some(comps)) =>
val preLocals = exprs
Expand Down
5 changes: 5 additions & 0 deletions sjsonnet/test/src/sjsonnet/ParserTests.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package sjsonnet
import utest._
import Expr._
import fastparse.Parsed
object ParserTests extends TestSuite{
def parse(s: String) = fastparse.parse(s, Parser.document(_)).get.value._1
def parseErr(s: String) = fastparse.parse(s, Parser.document(_), verboseFailures = true).asInstanceOf[Parsed.Failure].msg
def tests = Tests{
test("hello") {
parse("true") ==> True(0)
Expand Down Expand Up @@ -42,5 +44,8 @@ object ParserTests extends TestSuite{
// parse("[1, 2]") ==> Arr(Seq(Num(1), Num(2)))
// parse("[1, [2, 3], 4]") ==> Arr(Seq(Num(1), Arr(Seq(Num(2), Num(3))), Num(4)))
}
test("duplicateFields") {
parseErr("{ a: 1, a: 2 }") ==> """Expected no duplicate field: a:1:14, found "}""""
}
}
}

0 comments on commit 0eaea66

Please sign in to comment.