/// <summary> /// Initializes a new instance of the <see cref="YamlDocument"/> class. /// </summary> internal YamlDocument(IParser parser) { var state = new DocumentLoadingState(); parser.Consume <DocumentStart>(); while (!parser.TryConsume <DocumentEnd>(out var _)) { Debug.Assert(RootNode == null); RootNode = YamlNode.ParseNode(parser, state); if (RootNode is YamlAliasNode) { throw new YamlException("A document cannot contain only an alias"); } } state.ResolveAliases(); // Throw should not happen unless the parser has a bug RootNode = RootNode ?? throw new ArgumentException("Atempted to parse an empty document"); }
public IExpression Parse(TextSpan token, IParser parser) { parser.ConsumeToken(SqlToken.Next); parser.ConsumeToken(SqlToken.From); var cursorName = parser.ConsumeTableName(); parser.ConsumeToken(SqlToken.Into); var variableNameList = new List <SqlCodeExpr>(); do { var variableName = parser.Consume(SqlToken.Variable); variableNameList.Add(variableName); } while (parser.MatchToken(SqlToken.Comma)); return(new FetchSqlCodeExpr { CursorName = cursorName, VariableNameList = variableNameList }); }
public object?ReadYaml(IParser parser, Type type) { type = Nullable.GetUnderlyingType(type) ?? throw new ArgumentException("Expected nullable enum type for ReadYaml"); if (parser.Accept <NodeEvent>(out var @event) && NodeIsNull(@event)) { parser.SkipThisAndNestedEvents(); return(null); } var scalar = parser.Consume <Scalar>(); try { return(Enum.Parse(type, scalar.Value, true)); } catch (Exception ex) { throw new YamlException($"Invalid value: \"{scalar.Value}\" for {type.Name}", ex); } }
bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func <IParser, Type, object?> nestedObjectDeserializer, out object?value) { if (!parser.TryConsume <MappingStart>(out var mapping)) { value = null; return(false); } value = objectFactory.Create(expectedType); while (!parser.TryConsume <MappingEnd>(out var _)) { var propertyName = parser.Consume <Scalar>(); var property = typeDescriptor.GetProperty(expectedType, null, propertyName.Value, ignoreUnmatched); if (property == null) { parser.SkipThisAndNestedEvents(); continue; } var propertyValue = nestedObjectDeserializer(parser, property.Type); if (propertyValue is IValuePromise propertyValuePromise) { var valueRef = value; propertyValuePromise.ValueAvailable += v => { var convertedValue = TypeConverter.ChangeType(v, property.Type); property.Write(valueRef, convertedValue); }; } else { var convertedValue = TypeConverter.ChangeType(propertyValue, property.Type); property.Write(value, convertedValue); } } return(true); }
public bool Deserialize(IParser reader, Type expectedType, Func <IParser, Type, object?> nestedObjectDeserializer, out object?value) { value = default; if (expectedType != typeof(VariableParameter) && expectedType != typeof(ExpressionParameter)) { return(false); } var argument = reader.Consume <Scalar>(); if (expectedType == typeof(VariableParameter)) { value = new VariableParameter(argument.Value); } else if (expectedType == typeof(ExpressionParameter)) { value = new ExpressionParameter(argument.Value); } else { return(false); } return(true); }
public object ReadYaml(IParser parser, Type type) { var currentAsScalar = parser.Current as Scalar; var currentAsSequence = parser.Current as SequenceStart; var currentAsMapping = parser.Current as MappingStart; if (currentAsScalar != null) { return(bool.Parse(parser.Consume <Scalar>().Value)); } else if (currentAsSequence != null) { return(YamlRead.Deserializer.Deserialize <List <UnittestSpec> >(parser)); } else if (currentAsMapping != null) { return(new List <UnittestSpec>() { YamlRead.Deserializer.Deserialize <UnittestSpec>(parser) }); } throw new NotImplementedException(); }
public object?ReadYaml(IParser parser, Type type) => (IntstrIntOrString)parser.Consume <Scalar>().Value;
public object ReadYaml(IParser parser, Type type) { string clientId = null, username = null, pwd = null, broker = null; int? port = null, retriesCount = null; TimeSpan?retriesInterval = null; if (!parser.TryConsume <MappingStart>(out _)) { throw new InvalidOperationException("Unexpected token received from the parser!"); } while (!parser.TryConsume <MappingEnd>(out _)) { var propertyName = parser.Consume <Scalar>(); switch (propertyName.Value) { case "client_id": clientId = parser.Consume <Scalar>().Value; break; case "user": username = parser.Consume <Scalar>().Value; break; case "password": pwd = parser.Consume <Scalar>().Value; break; case "broker": broker = parser.Consume <Scalar>().Value; break; case "port": port = int.TryParse(parser.Consume <Scalar>().Value, out var p) ? p : throw new InvalidOperationException("Unable to build MQTT configuration - invalid port given!"); break; case "retries_interval": retriesInterval = TimeSpan.TryParse(parser.Consume <Scalar>().Value, out var t) ? t : throw new InvalidOperationException("Unable to build MQTT configuration - invalid retries_interval given!"); break; case "retries_count": retriesCount = int.TryParse(parser.Consume <Scalar>().Value, out var c) ? c : throw new InvalidOperationException("Unable to build MQTT configuration - invalid retries_count given!"); break; } } if (string.IsNullOrEmpty(clientId)) { throw new InvalidOperationException("Unable to build MQTT configuration - client_id is not provided!"); } if (string.IsNullOrEmpty(username)) { throw new InvalidOperationException("Unable to build MQTT configuration - user is not provided!"); } if (string.IsNullOrEmpty(pwd)) { throw new InvalidOperationException("Unable to build MQTT configuration - password is not provided!"); } if (string.IsNullOrEmpty(broker)) { throw new InvalidOperationException("Unable to build MQTT configuration - broker is not provided!"); } var client = new MqttClientOptionsBuilder() .WithClientId(clientId) .WithTcpServer(broker, port) .WithCredentials(username, pwd) .Build(); if (retriesCount.HasValue && !retriesInterval.HasValue) { throw new InvalidOperationException("Unable to build MQTT configuration! - retries_interval provided without retries_count!"); } if (!retriesCount.HasValue && retriesInterval.HasValue) { throw new InvalidOperationException("Unable to build MQTT configuration! - retries_count provided without retries_interval!"); } if (retriesCount <= 0) { throw new InvalidOperationException("Unable to build MQTT configuration! - retries_count invalid retries_count given!"); } return(new MqttOptions(client, new RetryOptions(retriesInterval, (uint)(retriesCount ?? DefaultRetriesCount)))); }
internal static Ingredient Parse(IParser parser) { parser.Consume <MappingStart>(); string name = parser.Consume <Scalar>().Value; Ingredient ingredient = new Ingredient(name); bool HasMappingStart = parser.TryConsume <MappingStart>(out _); do { Scalar next = parser.Consume <Scalar>(); if (next.Value == "usda_num") { ingredient.USDA = parser.Consume <Scalar>().Value; } else if (next.Value == "amounts") { parser.Consume <SequenceStart>(); do { ingredient.Amounts.Add(Amount.Parse(parser)); }while (parser.TryConsume <SequenceEnd>(out _) == false); } else if (next.Value == "substitutions") { parser.Consume <SequenceStart>(); do { ingredient.Substitutions.Add(Ingredient.Parse(parser)); }while (parser.TryConsume <SequenceEnd>(out _) == false); } else if (next.Value == "notes") { parser.Consume <SequenceStart>(); do { ingredient.Notes.Add(parser.Consume <Scalar>().Value); }while (parser.TryConsume <SequenceEnd>(out _) == false); } else if (next.Value == "processing") { parser.Consume <SequenceStart>(); do { ingredient.Processing.Add(parser.Consume <Scalar>().Value); }while (parser.TryConsume <SequenceEnd>(out _) == false); } } while (parser.TryConsume <MappingEnd>(out _) == false); if (HasMappingStart) { parser.Consume <MappingEnd>(); } return(ingredient); }
/// <inheritdoc /> public object ReadYaml(IParser parser, Type type) => Double.TryParse(parser.Consume <Scalar>()?.Value, out var value) ? value as object : null;
public bool Deserialize(IParser parser, Type expectedType, Func <IParser, Type, object> nestedObjectDeserializer, out object value) { value = null; if (!typeof(ManagedGameObject).IsAssignableFrom(expectedType) || !parser.TryConsume(out MappingStart _)) { return(false); } bool findType = false; MappingEnd event2; Scalar scalar = parser.Consume <Scalar>(); if (scalar.Value == "MGO.Type") { string i = nestedObjectDeserializer(parser, typeof(string)) as string; expectedType = TypeByName(i); #if DEBUG debugLogger.Start("Deserialize"); debugLogger.WriteLine($"nestedObjectDeserializer : { i }"); debugLogger.WriteLine($"expectedType : { expectedType }"); debugLogger.End(); #endif if (expectedType == null) { findType = false; } else { value = objectFactory.Create(expectedType); findType = true; } } while (!parser.TryConsume(out event2)) { scalar = parser.Consume <Scalar>(); IPropertyDescriptor property = typeDescriptor.GetProperty(expectedType, null, scalar.Value, false); if (property == null || !findType) { parser.SkipThisAndNestedEvents(); continue; } object obj = nestedObjectDeserializer(parser, property.Type); IValuePromise valuePromise = obj as IValuePromise; if (valuePromise != null) { object valueRef = value; valuePromise.ValueAvailable += (Action <object>) delegate(object v) { object value3 = YamlDotNet.Serialization.Utilities. TypeConverter.ChangeType(v, property.Type); property.Write(valueRef, value3); }; } else { object value2 = YamlDotNet.Serialization.Utilities. TypeConverter.ChangeType(obj, property.Type); property.Write(value, value2); } } return(true); }
public object ReadYaml(IParser parser, Type type) { string s = parser.Consume <Scalar>().Value; return(new Secret(s.Sha256())); }
public object ReadYaml(IParser parser, Type type) { var value = parser.Consume <Scalar>().Value; return(new Guid(value)); }
public void Read(IParser parser, Type expectedType, ObjectDeserializer nestedObjectDeserializer) { if (!parser.TryConsume <MappingStart>(out var mapping)) { throw new SerializationException("Invalid varvalue"); } if (parser.TryConsume <MappingEnd>(out var _)) { throw new SerializationException("Invalid varvalue"); } var propertyName1 = parser.Consume <Scalar>().Value; if (propertyName1 != "type") { throw new SerializationException("Invalid varvalue: expected type"); } var propertyValue1 = (string)nestedObjectDeserializer(typeof(string)); if (parser.TryConsume <MappingEnd>(out var _)) { throw new SerializationException("Invalid varvalue"); } var propertyName2 = parser.Consume <Scalar>().Value; if (propertyName2 != "value") { throw new SerializationException("Invalid varvalue: expected value"); } // TODO: Add more type conversions! switch (propertyValue1) { case "string": { type = new TypeDefinition(); type.Name = "value"; type.Type = DataTypes.string_t; value = (string)nestedObjectDeserializer(typeof(string)); break; } case "double": { type = new TypeDefinition(); type.Name = "value"; type.Type = DataTypes.double_t; value = (double)nestedObjectDeserializer(typeof(double)); break; } case "int32": { type = new TypeDefinition(); type.Name = "value"; type.Type = DataTypes.int32_t; value = (int)nestedObjectDeserializer(typeof(int)); break; } case "uint32": { type = new TypeDefinition(); type.Name = "value"; type.Type = DataTypes.uint32_t; value = (uint)nestedObjectDeserializer(typeof(uint)); break; } case "double[]": { type = new TypeDefinition(); type.Name = "value"; type.Type = DataTypes.double_t; type.ArrayType = DataTypes_ArrayTypes.array; value = (double[])nestedObjectDeserializer(typeof(double[])); break; } case "int32[]": { type = new TypeDefinition(); type.Name = "value"; type.Type = DataTypes.int32_t; type.ArrayType = DataTypes_ArrayTypes.array; value = (int[])nestedObjectDeserializer(typeof(int[])); break; } case "uint32[]": { type = new TypeDefinition(); type.Name = "value"; type.Type = DataTypes.uint32_t; type.ArrayType = DataTypes_ArrayTypes.array; value = (uint[])nestedObjectDeserializer(typeof(uint[])); break; } default: throw new SerializationException($"Invalid varvalue: unknown type {propertyValue1}"); } if (!parser.TryConsume <MappingEnd>(out var _)) { throw new SerializationException("Invalid varvalue, extra fields found"); } }
public object ReadYaml(IParser parser, Type type) { return(new Version(parser.Consume <Scalar>().Value)); }
public object ReadYaml(IParser parser, Type type) { var scalar = parser.Consume <Scalar>().Value; return(ParseScalar(scalar, type)); }
public object?ReadYaml(IParser parser, Type type) { var value = parser.Consume <Scalar>().Value; return(_translate[value]); }
public object ReadYaml(IParser parser, Type type) { var parsedValue = parser.Consume <Scalar>().Value; return(new DateTimeOrDays(parsedValue)); }
public object ReadYaml(IParser parser, Type type) { var value = parser.Consume <Scalar>().Value; return(Type.GetType(value, throwOnError: true) !); // Will throw instead of returning null }
public object ReadYaml(IParser parser, Type type) { var value = parser.Consume <Scalar>().Value; return(TimeSpan.FromSeconds(double.Parse(value, CultureInfo.InvariantCulture))); }
public static (List <ASTError>, ASTType) Parse(IParser parser, List <ASTAnnotation> annotations, List <ASTDirective> directives) { ASTType result = new ASTType(); try { List <ASTError> errors = new List <ASTError>(); result.Annotations = annotations; result.Directives = directives; parser.Next(); result.Name = parser.Consume(TokenType.Identifier).Value; result.Parameters = parser .ConsumeWhile(TokenType.GenericParameter) .Select(v => v.Value) .ToList(); /* * If there is an 'extends' extension. */ var extends = parser.TryConsume(TokenType.KW_Extends); if (!(extends is null)) { result.Extensions = parser .ConsumeWhile(TokenType.Identifier) .Select(v => v.Value) .ToList(); } /* * If there is an '=' sign we know that there will be * Fields which we can parse... */ var equals = parser.TryConsume(TokenType.Equal); if (!(equals is null)) { List <ASTTypeField> fields = new List <ASTTypeField>(); while (parser.TryConsume(TokenType.ContextEnded) == null) { fields.Add(ASTTypeField.Parse(parser)); } result.Fields = fields; if (fields.Count == 0) { if (parser.Current.TokenType == TokenType.Paragraph) { var nextPhrase = parser.Current.Value; errors.Add(new ASTError($@" Missing type body. If you use an '=' sign you should have at least one field. It might be that you are missing an indentation: type {result.Name} = {nextPhrase} Example: type {result.Name} = {nextPhrase} ", parser.Current)); } else { errors.Add(new ASTError($@" Missing type body. If you use an '=' sign you should have at least one field.", parser.Current)); } } } return(errors, result); } catch (InvalidTokenException ex) { return(new List <ASTError> { new ASTError(ex.Message, parser.Current) }, result); } }
public object ReadYaml(IParser parser, Type type) { var env = new InternalEnvironmentVariables(); parser.Consume <MappingStart>(); do { parser.Accept <Scalar>(out var scalar); if (scalar != null) { if (scalar.Value == "global") { // discard "global" value itself parser.Consume <Scalar>(); // read global variables (common to all matrix items) parser.Consume <MappingStart>(); do { env.InternalCommonVariables.Add(_deserializer.Deserialize <InternalVariable>(parser)); } while (!parser.Accept <MappingEnd>(out _)); parser.Consume <MappingEnd>(); } else if (scalar.Value == "matrix") { // discard "matrix" value itself parser.Consume <Scalar>(); // discard SequenceStart parser.Consume <SequenceStart>(); do { var matrixItemVariables = new List <InternalVariable>(); parser.Consume <MappingStart>(); do { matrixItemVariables.Add(_deserializer.Deserialize <InternalVariable>(parser)); } while (!parser.Accept <MappingEnd>(out _)); parser.Consume <MappingEnd>(); env.InternalMatrix.Add(matrixItemVariables.AsReadOnly()); } while (!parser.Accept <SequenceEnd>(out _)); parser.Consume <SequenceEnd>(); } else { var variable = _deserializer.Deserialize <InternalVariable>(parser); env.InternalCommonVariables.Add(variable); } } } while (!parser.Accept <MappingEnd>(out _)); parser.Consume <MappingEnd>(); return(env); }