public static TinyToken ToToken(object value) { if (value is TinyToken token) { return(token); } if (TinyValue.FindValueType(value) == TinyTokenType.Invalid) { if (value is IDictionary dictionary) { var o = new TinyObject(); foreach (var key in dictionary.Keys) { o.Add((string)key, ToToken(dictionary[key])); } return(o); } if (value is IEnumerable enumerable) { return(new TinyArray(enumerable)); } } return(new TinyValue(value)); }
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); }