Permalink
Browse files

Improve error message when string does not have ending quote mark

  • Loading branch information...
1 parent fe833d8 commit 5d843c9d89ddaa019457e051447471fcfa6eb20c Joni Freeman committed Jun 15, 2011
@@ -113,15 +113,19 @@ object JsonParser {
}
s.toString
}
-
+
+ buf.eofIsFailure = true
buf.mark
var c = buf.next
while (c != '"') {
if (c == '\\') {
- return unquote0(buf, buf.substring)
+ val s = unquote0(buf, buf.substring)
+ buf.eofIsFailure = false
+ return s
}
c = buf.next
}
+ buf.eofIsFailure = false
buf.substring
}
@@ -238,13 +242,13 @@ object JsonParser {
fail("expected string end")
}
- def parseString: String = {
+ def parseString: String =
try {
unquote(buf)
} catch {
+ case p: ParseException => throw p
case _ => fail("unexpected string end")
}
- }
def parseValue(first: Char) = {
var wasInt = true
@@ -335,6 +339,7 @@ object JsonParser {
var offset = 0
var curMark = -1
var curMarkSegment = -1
+ var eofIsFailure = false
private[this] var segments: List[Segment] = List(Segments.apply())
private[this] var segment: Array[Char] = segments.head.seg
private[this] var cur = 0 // Pointer which points current parsing location
@@ -344,8 +349,9 @@ object JsonParser {
def back = cur = cur-1
def next: Char = {
- if (cur == offset && read < 0) EOF
- else {
+ if (cur == offset && read < 0) {
+ if (eofIsFailure) throw new ParseException("unexpected eof", null) else EOF
+ } else {
val c = segment(cur)
cur += 1
c
@@ -380,7 +386,7 @@ object JsonParser {
}
}
- def near = new String(segment, (cur-20) max 0, 40 min (offset - cur + 20))
+ def near = new String(segment, (cur-20) max 0, 40 min (offset - cur + 20) max 0)
def release = segments.foreach(Segments.release)
@@ -31,11 +31,24 @@ object ParserBugs extends Specification {
allCatch.opt(parse(""" [ 22.250738585072012e-309 ] """)) mustEqual None
}
- "Does not allow colon at start of array" in {
+ "Does not allow colon at start of array (1039)" in {
parseOpt("""[:"foo", "bar"]""") mustEqual None
}
- "Does not allow colon instead of comma in array" in {
+ "Does not allow colon instead of comma in array (1039)" in {
parseOpt("""["foo" : "bar"]""") mustEqual None
}
+
+ "Solo quote mark should fail cleanly (not StringIndexOutOfBoundsException) (1041)" in {
+ JsonParser.parse("\"", discardParser) must throwA(new Exception()).like {
+ case e: JsonParser.ParseException => e.getMessage.startsWith("unexpected eof")
+ }
+ }
+
+ private val discardParser = (p : JsonParser.Parser) => {
+ var token: JsonParser.Token = null
+ do {
+ token = p.nextToken
+ } while (token != JsonParser.End)
+ }
}

0 comments on commit 5d843c9

Please sign in to comment.