Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed sample JSON parser to demand end of input after the first JSON …

…value is successfully parsed.
  • Loading branch information...
commit 8fccd462fefc78b3d93ac64aa1fe5dd82900fbd2 1 parent c33c63b
@plioi authored
View
12 src/Parsley.Test/IntegrationTests/Json/JsonGrammar.cs
@@ -9,6 +9,7 @@ namespace Parsley.IntegrationTests.Json
public class JsonGrammar : Grammar
{
public static readonly GrammarRule<object> Json = new GrammarRule<object>();
+ private static readonly GrammarRule<object> JsonValue = new GrammarRule<object>();
private static readonly GrammarRule<object> True = new GrammarRule<object>();
private static readonly GrammarRule<object> False = new GrammarRule<object>();
private static readonly GrammarRule<object> Null = new GrammarRule<object>();
@@ -38,21 +39,24 @@ static JsonGrammar()
select Unescape(quotation.Literal);
Array.Rule =
- from items in Between(Token("["), ZeroOrMore(Json, Token(",")), Token("]"))
+ from items in Between(Token("["), ZeroOrMore(JsonValue, Token(",")), Token("]"))
select items.ToArray();
Pair.Rule =
from key in Quotation
from colon in Token(":")
- from value in Json
+ from value in JsonValue
select new KeyValuePair<string, object>(key, value);
Dictionary.Rule =
from pairs in Between(Token("{"), ZeroOrMore(Pair, Token(",")), Token("}"))
select ToDictionary(pairs);
- Json.Rule =
- Choice(True, False, Null, Number, Quotation, Dictionary, Array);
+ JsonValue.Rule = Choice(True, False, Null, Number, Quotation, Dictionary, Array);
+
+ Json.Rule = from jsonValue in JsonValue
+ from end in EndOfInput
+ select jsonValue;
}
private static Parser<object> Constant(TokenKind kind, object constant)
View
10 src/Parsley.Test/IntegrationTests/Json/JsonGrammarTests.cs
@@ -114,5 +114,15 @@ public void ParsesComplexJsonValues()
window["transparent"].ShouldEqual(false);
});
}
+
+ [Fact]
+ public void RequiresEndOfInputAfterFirstValidJsonValue()
+ {
+ Json.FailsToParse(Tokenize("true $garbage$")).LeavingUnparsedTokens("$garbage$").WithMessage("(1, 6): end of input expected");
+ Json.FailsToParse(Tokenize("10.123E-11 $garbage$")).LeavingUnparsedTokens("$garbage$").WithMessage("(1, 13): end of input expected");
+ Json.FailsToParse(Tokenize("\"garbage\" $garbage$")).LeavingUnparsedTokens("$garbage$").WithMessage("(1, 11): end of input expected");
+ Json.FailsToParse(Tokenize("[0, 1, 2] $garbage$")).LeavingUnparsedTokens("$garbage$").WithMessage("(1, 11): end of input expected");
+ Json.FailsToParse(Tokenize("{\"zero\" : 0} $garbage$")).LeavingUnparsedTokens("$garbage$").WithMessage("(1, 14): end of input expected");
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.