Esempio n. 1
0
        internal override void WriteInternal(TextWriter writer, TinyToken parent, int indentLevel)
        {
            if (indentLevel != 0)
            {
                throw new InvalidOperationException();
            }

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

            case TinyTokenType.String:
                writer.WriteLine("\"" + TinyUtil.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());
            }
        }
Esempio n. 2
0
        public static T Value <T>(this TinyToken token, params object[] keys)
        {
            for (var i = 0; i < keys.Length - 1; i++)
            {
                token = token.Value <TinyToken>(keys[i]);
            }

            return(token.Value <T>(keys[keys.Length - 1]));
        }
Esempio n. 3
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. 4
0
 public static T Value <T>(this TinyToken token, object key1, object key2, object key3)
 => token.Value <TinyToken>(key1).Value <TinyToken>(key2).Value <T>(key3);
Esempio n. 5
0
 public static IEnumerable <T> Values <T>(this TinyToken token, object key = null)
 => token.Value <TinyArray>(key).Values <T>();
Esempio n. 6
0
 internal abstract void WriteInternal(TextWriter writer, TinyToken parent, int indentLevel);