Ejemplo n.º 1
0
        private IGreenJsonSymbol CreateValue(int currentIndex)
        {
            int length             = currentIndex - SymbolStartIndex;
            IGreenJsonSymbol value = JsonValue.TryCreate(Json.AsSpan().Slice(SymbolStartIndex, length));

            if (value == null)
            {
                // Copy to a substring here, which is not necessary for JsonValue.TryCreate() anymore.
                Report(JsonParseErrors.UnrecognizedValue(Json.Substring(SymbolStartIndex, length), SymbolStartIndex, length));
                value = new GreenJsonUndefinedValueSyntax(length);
            }

            return(value);
        }
Ejemplo n.º 2
0
        private JsonSymbolType ShiftToNextForegroundToken()
        {
            // Skip background until encountering something meaningful.
            for (; ;)
            {
                IGreenJsonSymbol newToken = Tokens.MoveNext() ? Tokens.Current : EofSymbol.Value;
                CurrentLength += newToken.Length;
                JsonSymbolType symbolType = newToken.SymbolType;

                if (symbolType >= ForegroundThreshold)
                {
                    CurrentToken = newToken;
                    return(symbolType);
                }

                BackgroundBuilder.Add((GreenJsonBackgroundSyntax)newToken);
            }
        }
Ejemplo n.º 3
0
        public void ParseTreeTokensMatch(string json1, string json2)
        {
            // Sane structure as JsonTokenizerTests.Transition: first check two symbols, then all combinations of three.
            {
                string json           = json1 + json2;
                var    expectedTokens = JsonParser.TokenizeAll(json).Item1;
                Action <IJsonSymbol>[] tokenInspectors = expectedTokens.Select <IGreenJsonSymbol, Action <IJsonSymbol> >((IGreenJsonSymbol expectedGreen) => (IJsonSymbol red) =>
                {
                    IGreenJsonSymbol actualGreen = TerminalSymbolTester.Instance.Visit(red);
                    Assert.IsType(expectedGreen.GetType(), actualGreen);
                    Assert.Equal(expectedGreen.Length, actualGreen.Length);
                    Assert.Equal(expectedGreen.Length, red.Length);
                }).ToArray();

                Assert.Collection(
                    JsonParser.Parse(json).Syntax.TerminalSymbolsInRange(0, json.Length),
                    tokenInspectors);
            }

            // Here Assert.Collection is used so if such a test fails,
            // it gives the index of the third token that was tested.
            Assert.Collection(
                JsonTokenizerTests.JsonTestSymbols(),
                Enumerable.Repeat <Action <(string, Type)> >(x0 =>
            {
                string json        = x0.Item1 + json1 + json2;
                var expectedTokens = JsonParser.TokenizeAll(json).Item1;
                Action <IJsonSymbol>[] tokenInspectors = expectedTokens.Select <IGreenJsonSymbol, Action <IJsonSymbol> >(expectedGreen => symbol =>
                {
                    IGreenJsonSymbol actualGreen = TerminalSymbolTester.Instance.Visit(symbol);
                    Assert.IsType(expectedGreen.GetType(), actualGreen);
                    Assert.Equal(expectedGreen.Length, actualGreen.Length);
                    Assert.Equal(expectedGreen.Length, symbol.Length);
                }).ToArray();

                Assert.Collection(
                    JsonParser.Parse(json).Syntax.TerminalSymbolsInRange(0, json.Length),
                    tokenInspectors);
            }, JsonTokenizerTests.JsonTestSymbols().Count()).ToArray());
        }
Ejemplo n.º 4
0
 internal GreenJsonRootLevelValueDelimiterSyntax(IGreenJsonSymbol valueDelimiter)
 {
     ValueDelimiter = valueDelimiter;
     Debug.Assert(ValueDelimiter.SymbolType >= JsonParser.ValueDelimiterThreshold);
 }