Ejemplo n.º 1
0
            public override void Parse(ParseContext <YamlTokenType> context)
            {
                switch (context.LookaheadToken.Type)
                {
                case YamlTokenType.EndLine:
                    break;

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

                switch (context.CurrentToken.Type)
                {
                case YamlTokenType.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 == YamlFormat.BooleanTrue));
                    }
                    else
                    {
                        Callback(new TinyValue(value));
                    }
                    context.ConsumeToken();
                    context.PopParser();
                }
                    return;

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

                throw new InvalidDataException("Unexpected token: " + context.CurrentToken);
            }
Ejemplo n.º 2
0
            public override void Parse(ParseContext <YamlTokenType> context)
            {
                if (CheckIndent(context))
                {
                    return;
                }

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

                switch (context.CurrentToken.Type)
                {
                case YamlTokenType.Property:
                case YamlTokenType.PropertyQuoted:

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

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

                    case YamlTokenType.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);
            }
Ejemplo n.º 3
0
        private void writeObject(TextWriter writer, TinyObject obj, TinyToken parent, int indentLevel)
        {
            var parentIsArray = parent != null && parent.Type == TinyTokenType.Array;

            var first = true;

            foreach (var property in obj)
            {
                if (!first || !parentIsArray)
                {
                    writeIndent(writer, indentLevel);
                }

                var key = property.Key;
                if (key.Contains(" ") || key.Contains(":") || key.StartsWith("-"))
                {
                    key = "\"" + YamlUtil.EscapeString(key) + "\"";
                }

                var value = property.Value;
                if (value.IsEmpty)
                {
                    writer.WriteLine(key + ":");
                }
                else if (value.IsInline)
                {
                    writer.Write(key + ": ");
                    write(writer, value, obj, 0);
                }
                else
                {
                    writer.WriteLine(key + ":");
                    write(writer, value, obj, indentLevel + 1);
                }
                first = false;
            }
        }
Ejemplo n.º 4
0
        private void writeValue(TextWriter writer, TinyValue valueToken, TinyToken parent, int indentLevel)
        {
            if (indentLevel != 0)
            {
                throw new InvalidOperationException();
            }

            var type  = valueToken.Type;
            var value = valueToken.Value <object>();

            switch (type)
            {
            case TinyTokenType.Null:
                writer.WriteLine();
                break;

            case TinyTokenType.String:
                writer.WriteLine("\"" + YamlUtil.EscapeString((string)value) + "\"");
                break;

            case TinyTokenType.Integer:
                writer.WriteLine(value?.ToString());
                break;

            case TinyTokenType.Float:
                if (value is float floatFloat)
                {
                    writer.WriteLine(floatFloat.ToString(CultureInfo.InvariantCulture));
                }
                else if (value is double floatDouble)
                {
                    writer.WriteLine(floatDouble.ToString(CultureInfo.InvariantCulture));
                }
                else if (value is decimal floatDecimal)
                {
                    writer.WriteLine(floatDecimal.ToString(CultureInfo.InvariantCulture));
                }
                else if (value is string floatString)
                {
                    writer.WriteLine(floatString);
                }
                else
                {
                    throw new InvalidDataException(value?.ToString());
                }
                break;

            case TinyTokenType.Boolean:
                writer.WriteLine(((bool)value) ? BooleanTrue : BooleanFalse);
                break;

            case TinyTokenType.Array:
            case TinyTokenType.Object:
            case TinyTokenType.Invalid:
                // Should never happen :)
                throw new InvalidDataException(type.ToString());

            default:
                throw new NotImplementedException(type.ToString());
            }
        }