private static bool SyntaxEquals(TokenLiteral Left, TokenLiteral Right) { if (Left._Tag != Right._Tag) { return(false); } if (Left.OnNullValue) { return(true); } else if (Left.OnBooleanValue) { return(Left.BooleanValue == Right.BooleanValue); } else if (Left.OnNumberValue) { return(Left.NumberValue == Right.NumberValue); } else if (Left.OnStringValue) { return(Left.StringValue == Right.StringValue); } else { throw new InvalidOperationException(); } }
public static void DoTest() { Assert(SyntaxEquals(TokenParse(@"""123abc\u0020\t\"""""), SyntaxRule.CreateLiteral(TokenLiteral.CreateStringValue("123abc \t\"")))); Assert(SyntaxEquals(TokenParse(@"0"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(0)))); Assert(SyntaxEquals(TokenParse(@"-1000"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(-1000)))); Assert(SyntaxEquals(TokenParse(@"0.0"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(0)))); Assert(SyntaxEquals(TokenParse(@"123.456"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(123.456)))); Assert(SyntaxEquals(TokenParse(@"-10.1"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(-10.1)))); Assert(SyntaxEquals(TokenParse(@"100.001e10"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(100.001e10)))); Assert(SyntaxEquals(TokenParse(@"-100.00e+10"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(-100.00e+10)))); Assert(SyntaxEquals(TokenParse(@"100.001e-10"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(100.001e-10)))); Assert(SyntaxEquals(TokenParse(@"1.23456789123456789123456789e10"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(1.23456789123456789123456789e10)))); Assert(SyntaxEquals(TokenParse(@"1.79769e+308"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(1.79769e+308)))); Assert(SyntaxEquals(TokenParse(@"-1.79769e+308"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(-1.79769e+308)))); Assert(SyntaxEquals(TokenParse(@"2.22507e-308"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(2.22507e-308)))); Assert(SyntaxEquals(TokenParse(@"-2.22507e-308"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNumberValue(-2.22507e-308)))); Assert(SyntaxEquals(TokenParse(@"null"), SyntaxRule.CreateLiteral(TokenLiteral.CreateNullValue()))); Assert(SyntaxEquals(TokenParse(@"false"), SyntaxRule.CreateLiteral(TokenLiteral.CreateBooleanValue(false)))); Assert(SyntaxEquals(TokenParse(@"true"), SyntaxRule.CreateLiteral(TokenLiteral.CreateBooleanValue(true)))); Assert(SyntaxEquals(TokenParse(@"{"), SyntaxRule.CreateLeftBrace())); Assert(SyntaxEquals(TokenParse(@"}"), SyntaxRule.CreateRightBrace())); Assert(SyntaxEquals(TokenParse(@"["), SyntaxRule.CreateLeftBracket())); Assert(SyntaxEquals(TokenParse(@"]"), SyntaxRule.CreateRightBracket())); Assert(SyntaxEquals(TokenParse(@":"), SyntaxRule.CreateColon())); Assert(SyntaxEquals(TokenParse(@","), SyntaxRule.CreateComma())); Assert(SyntaxEquals(TokenParse(" \t\r\n"), SyntaxRule.CreateWhitespace())); Assert(SyntaxEquals(SyntaxParse("null"), SyntaxValue.CreateLiteral(TokenLiteral.CreateNullValue()))); Assert(SyntaxEquals(SyntaxParse("false"), SyntaxValue.CreateLiteral(TokenLiteral.CreateBooleanValue(false)))); Assert(SyntaxEquals(SyntaxParse("true"), SyntaxValue.CreateLiteral(TokenLiteral.CreateBooleanValue(true)))); Assert(SyntaxEquals(SyntaxParse("123"), SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(123)))); Assert(SyntaxEquals(SyntaxParse("\"123\""), SyntaxValue.CreateLiteral(TokenLiteral.CreateStringValue("123")))); Assert(SyntaxEquals(SyntaxParse(" null "), SyntaxValue.CreateLiteral(TokenLiteral.CreateNullValue()))); Assert(SyntaxEquals(SyntaxParse(" false "), SyntaxValue.CreateLiteral(TokenLiteral.CreateBooleanValue(false)))); Assert(SyntaxEquals(SyntaxParse(" true "), SyntaxValue.CreateLiteral(TokenLiteral.CreateBooleanValue(true)))); Assert(SyntaxEquals(SyntaxParse(" 123 "), SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(123)))); Assert(SyntaxEquals(SyntaxParse(" \"123\" "), SyntaxValue.CreateLiteral(TokenLiteral.CreateStringValue("123")))); Assert(SyntaxEquals(SyntaxParse("{}"), SyntaxValue.CreateObject(new SyntaxObject { Members = Optional <SyntaxMembers> .Empty }))); Assert(SyntaxEquals(SyntaxParse("[]"), SyntaxValue.CreateArray(new SyntaxArray { Elements = Optional <SyntaxElements> .Empty }))); Assert(SyntaxEquals(SyntaxParse("{ }"), SyntaxValue.CreateObject(new SyntaxObject { Members = Optional <SyntaxMembers> .Empty }))); Assert(SyntaxEquals(SyntaxParse("[ ]"), SyntaxValue.CreateArray(new SyntaxArray { Elements = Optional <SyntaxElements> .Empty }))); Assert(SyntaxEquals(SyntaxParse(" { }"), SyntaxValue.CreateObject(new SyntaxObject { Members = Optional <SyntaxMembers> .Empty }))); Assert(SyntaxEquals(SyntaxParse(" [ ]"), SyntaxValue.CreateArray(new SyntaxArray { Elements = Optional <SyntaxElements> .Empty }))); Assert(SyntaxEquals(SyntaxParse(" { } "), SyntaxValue.CreateObject(new SyntaxObject { Members = Optional <SyntaxMembers> .Empty }))); Assert(SyntaxEquals(SyntaxParse(" [ ] "), SyntaxValue.CreateArray(new SyntaxArray { Elements = Optional <SyntaxElements> .Empty }))); Assert(SyntaxEquals(SyntaxParse("{\"key\":1}"), SyntaxValue.CreateObject(new SyntaxObject { Members = SyntaxMembers.CreateSingle(new Tuple <TokenLiteral, SyntaxValue>(TokenLiteral.CreateStringValue("key"), SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(1)))) }))); Assert(SyntaxEquals(SyntaxParse("[1]"), SyntaxValue.CreateArray(new SyntaxArray { Elements = SyntaxElements.CreateSingle(SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(1))) }))); Assert(SyntaxEquals(SyntaxParse("{ \"key\" : 1 }"), SyntaxValue.CreateObject(new SyntaxObject { Members = SyntaxMembers.CreateSingle(new Tuple <TokenLiteral, SyntaxValue>(TokenLiteral.CreateStringValue("key"), SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(1)))) }))); Assert(SyntaxEquals(SyntaxParse("[ 1 ]"), SyntaxValue.CreateArray(new SyntaxArray { Elements = SyntaxElements.CreateSingle(SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(1))) }))); Assert(SyntaxEquals(SyntaxParse("{\"key\":1,\"value\":2}"), SyntaxValue.CreateObject(new SyntaxObject { Members = SyntaxMembers.CreateMultiple(new Tuple <SyntaxMembers, TokenLiteral, SyntaxValue>(SyntaxMembers.CreateSingle(new Tuple <TokenLiteral, SyntaxValue>(TokenLiteral.CreateStringValue("key"), SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(1)))), TokenLiteral.CreateStringValue("value"), SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(2)))) }))); Assert(SyntaxEquals(SyntaxParse("[1,2]"), SyntaxValue.CreateArray(new SyntaxArray { Elements = SyntaxElements.CreateMultiple(new Tuple <SyntaxElements, SyntaxValue>(SyntaxElements.CreateSingle(SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(1))), SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(2)))) }))); Assert(SyntaxEquals(SyntaxParse("{ \"key\" : 1, \"value\" : 2 }"), SyntaxValue.CreateObject(new SyntaxObject { Members = SyntaxMembers.CreateMultiple(new Tuple <SyntaxMembers, TokenLiteral, SyntaxValue>(SyntaxMembers.CreateSingle(new Tuple <TokenLiteral, SyntaxValue>(TokenLiteral.CreateStringValue("key"), SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(1)))), TokenLiteral.CreateStringValue("value"), SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(2)))) }))); Assert(SyntaxEquals(SyntaxParse("[ 1, 2 ]"), SyntaxValue.CreateArray(new SyntaxArray { Elements = SyntaxElements.CreateMultiple(new Tuple <SyntaxElements, SyntaxValue>(SyntaxElements.CreateSingle(SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(1))), SyntaxValue.CreateLiteral(TokenLiteral.CreateNumberValue(2)))) }))); }