Esempio n. 1
0
            public override void Parse(ParseContext <JsonTokenType> context)
            {
                switch (context.CurrentToken.Type)
                {
                case JsonTokenType.ObjectStart:
                case JsonTokenType.ArrayStart:
                case JsonTokenType.Word:
                case JsonTokenType.WordQuoted:

                    if (expectingSeparator)
                    {
                        throw new InvalidDataException("Unexpected token: " + context.CurrentToken);
                    }

                    context.PushParser(new AnyParser(r => result.Add(r)));

                    expectingSeparator = true;
                    return;

                case JsonTokenType.ValueSeparator:
                    expectingSeparator = false;
                    context.ConsumeToken();
                    return;

                case JsonTokenType.ArrayEnd:
                    context.ConsumeToken();
                    context.PopParser();
                    return;
                }

                throw new InvalidDataException("Unexpected token: " + context.CurrentToken);
            }
Esempio n. 2
0
        public TinyToken Parse(IEnumerable <Token <YamlTokenType> > tokens)
        {
            TinyToken result = null;

            var context = new ParseContext <YamlTokenType>(tokens, new AnyParser(r => result = r));

            while (context.CurrentToken != null)
            {
                switch (context.CurrentToken.Type)
                {
                case YamlTokenType.Indent:
                    context.Indent(context.CurrentToken.Value.Length / 2);
                    context.ConsumeToken();
                    continue;

                case YamlTokenType.EndLine:
                    context.NewLine();
                    context.ConsumeToken();
                    continue;
                }
                //Debug.Print($"  - {context.Parser.GetType().Name} ({context.ParserCount})");
                context.Parser.Parse(context);
            }
            context.End();

            return(result);
        }
Esempio n. 3
0
            public override void Parse(ParseContext <JsonTokenType> context)
            {
                switch (context.CurrentToken.Type)
                {
                case JsonTokenType.Property:
                case JsonTokenType.PropertyQuoted:

                    if (expectingSeparator)
                    {
                        throw new InvalidDataException("Unexpected token: " + context.LookaheadToken + ", after: " + context.CurrentToken);
                    }

                    var key = context.CurrentToken.Value;
                    if (context.CurrentToken.Type == JsonTokenType.PropertyQuoted)
                    {
                        key = JsonUtil.UnescapeString(key);
                    }

                    switch (context.LookaheadToken.Type)
                    {
                    case JsonTokenType.ObjectStart:
                    case JsonTokenType.ArrayStart:
                        context.PushParser(new AnyParser(r => result.Add(key, r)));
                        break;

                    case JsonTokenType.Word:
                    case JsonTokenType.WordQuoted:
                        context.PushParser(new ValueParser(r => result.Add(key, r)));
                        break;

                    default:
                        throw new InvalidDataException("Unexpected token: " + context.LookaheadToken + ", after: " + context.CurrentToken);
                    }

                    expectingSeparator = true;
                    context.ConsumeToken();
                    return;

                case JsonTokenType.ValueSeparator:
                    expectingSeparator = false;
                    context.ConsumeToken();
                    return;

                case JsonTokenType.ObjectEnd:
                    context.ConsumeToken();
                    context.PopParser();
                    return;
                }
                throw new InvalidDataException("Unexpected token: " + context.CurrentToken);
            }
Esempio n. 4
0
            public override void Parse(ParseContext context)
            {
                switch (context.LookaheadToken.TokenType)
                {
                case TinyTokenizer.TokenType.EndLine:
                    break;

                default:
                    throw new InvalidDataException("Unexpected token: " + context.LookaheadToken + ", after: " + context.CurrentToken);
                }

                switch (context.CurrentToken.TokenType)
                {
                case TinyTokenizer.TokenType.Word:
                {
                    var   value = context.CurrentToken.Value;
                    Match match;
                    if ((match = floatRegex.Match(value)).Success)
                    {
                        Callback(new TinyValue(value, TinyTokenType.Float));
                    }
                    else if ((match = integerRegex.Match(value)).Success)
                    {
                        Callback(new TinyValue(value, TinyTokenType.Integer));
                    }
                    else if ((match = boolRegex.Match(value)).Success)
                    {
                        Callback(new TinyValue(value == TinyValue.BooleanTrue));
                    }
                    else
                    {
                        Callback(new TinyValue(value));
                    }
                    context.ConsumeToken();
                    context.PopParser();
                }
                    return;

                case TinyTokenizer.TokenType.WordQuoted:
                {
                    var value = TinyUtil.UnescapeString(context.CurrentToken.Value);
                    Callback(new TinyValue(value));
                    context.ConsumeToken();
                    context.PopParser();
                }
                    return;
                }

                throw new InvalidDataException("Unexpected token: " + context.CurrentToken);
            }
Esempio n. 5
0
            public override void Parse(ParseContext <JsonTokenType> context)
            {
                switch (context.CurrentToken.Type)
                {
                case JsonTokenType.Word:
                {
                    var   value = context.CurrentToken.Value;
                    Match match;
                    if ((match = floatRegex.Match(value)).Success)
                    {
                        Callback(new TinyValue(value, TinyTokenType.Float));
                    }
                    else if ((match = integerRegex.Match(value)).Success)
                    {
                        Callback(new TinyValue(value, TinyTokenType.Integer));
                    }
                    else if ((match = boolRegex.Match(value)).Success)
                    {
                        Callback(new TinyValue(value == JsonFormat.BooleanTrue));
                    }
                    else
                    {
                        Callback(new TinyValue(value));
                    }
                    context.ConsumeToken();
                    context.PopParser();
                }
                    return;

                case JsonTokenType.WordQuoted:
                {
                    var value = JsonUtil.UnescapeString(context.CurrentToken.Value);
                    Callback(new TinyValue(value));
                    context.ConsumeToken();
                    context.PopParser();
                }
                    return;
                }

                throw new InvalidDataException("Unexpected token: " + context.CurrentToken);
            }
Esempio n. 6
0
            public override void Parse(ParseContext <JsonTokenType> context)
            {
                switch (context.CurrentToken.Type)
                {
                case JsonTokenType.ObjectStart:
                    context.ReplaceParser(new ObjectParser(Callback));
                    context.ConsumeToken();
                    return;

                case JsonTokenType.ArrayStart:
                    context.ReplaceParser(new ArrayParser(Callback));
                    context.ConsumeToken();
                    return;

                case JsonTokenType.Word:
                case JsonTokenType.WordQuoted:
                    context.ReplaceParser(new ValueParser(Callback));
                    return;
                }
                throw new InvalidDataException("Unexpected token: " + context.CurrentToken);
            }
Esempio n. 7
0
        public static TinyToken Parse(IEnumerable <TinyTokenizer.Token> tokens)
        {
            TinyToken result  = null;
            var       context = new ParseContext(tokens, r => result = r);

            TinyTokenizer.Token previousToken = null;
            while (context.CurrentToken != null)
            {
                if (context.CurrentToken != previousToken)
                {
                    //Debug.Print($"{context.CurrentToken}");
                    previousToken = context.CurrentToken;
                }

                switch (context.CurrentToken.TokenType)
                {
                case TinyTokenizer.TokenType.Indent:
                    context.Indent(context.CurrentToken.Value.Length / 2);
                    context.ConsumeToken();
                    continue;

                case TinyTokenizer.TokenType.EndLine:
                    context.NewLine();
                    context.ConsumeToken();
                    continue;
                }
                //Debug.Print($"  - {context.Parser.GetType().Name} ({context.ParserCount})");
                context.Parser.Parse(context);
            }

            while (context.Parser != null)
            {
                context.Parser.End();
                context.PopParser();
            }

            return(result);
        }
Esempio n. 8
0
            public override void Parse(ParseContext context)
            {
                if (CheckIndent(context))
                {
                    return;
                }

                switch (context.CurrentToken.TokenType)
                {
                case TinyTokenizer.TokenType.ArrayIndicator:
                    context.PushParser(new AnyParser(r => result.Add(r), result.Count == 0 ? VirtualIndent + 1 : 1));
                    context.ConsumeToken();
                    return;
                }

                throw new InvalidDataException("Unexpected token: " + context.CurrentToken);
            }
Esempio n. 9
0
            public override void Parse(ParseContext context)
            {
                if (CheckIndent(context))
                {
                    return;
                }

                switch (context.LookaheadToken.TokenType)
                {
                case TinyTokenizer.TokenType.ArrayIndicator:
                case TinyTokenizer.TokenType.Property:
                case TinyTokenizer.TokenType.PropertyQuoted:
                    throw new InvalidDataException("Unexpected token: " + context.LookaheadToken + ", after: " + context.CurrentToken);
                }

                switch (context.CurrentToken.TokenType)
                {
                case TinyTokenizer.TokenType.Property:
                case TinyTokenizer.TokenType.PropertyQuoted:

                    var key = context.CurrentToken.Value;
                    if (context.CurrentToken.TokenType == TinyTokenizer.TokenType.PropertyQuoted)
                    {
                        key = TinyUtil.UnescapeString(key);
                    }

                    switch (context.LookaheadToken.TokenType)
                    {
                    case TinyTokenizer.TokenType.Word:
                    case TinyTokenizer.TokenType.WordQuoted:
                        context.PushParser(new ValueParser(r => result.Add(key, r)));
                        break;

                    case TinyTokenizer.TokenType.EndLine:
                        context.PushParser(new EmptyProperyParser(r => result.Add(key, r), context.IndentLevel + 1));
                        break;

                    default:
                        throw new InvalidDataException("Unexpected token: " + context.LookaheadToken + ", after: " + context.CurrentToken);
                    }
                    context.ConsumeToken();
                    return;
                }
                throw new InvalidDataException("Unexpected token: " + context.CurrentToken);
            }