Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Detect duplicate fields in the parser #86

Merged
merged 1 commit into from
Aug 24, 2020
Merged
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
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 "}""""
}
}
}