public ParseResult <TemplateOptions> ParseTemplateOptions(Serializer serializer) { if (TemplateName == null) { return(ParseResult <TemplateOptions> .Success(null)); } if (!Template.IsValidTemplateName(TemplateName)) { return(ParseResult <TemplateOptions> .Failed($"Service {Name}: Unrecognised template {TemplateName}. Valid options are {string.Join(", ", Template.TemplateNames)}")); } if (RawTemplateOptions == null) { return(ParseResult <TemplateOptions> .Failed($"Service {Name}: No template options provided")); } var template = Template.GetTemplate(TemplateName); var serialisedOptions = serializer.Serialize(RawTemplateOptions); TemplateOptions = (TemplateOptions)serializer.Deserialize(serialisedOptions, template.OptionsType); var validationResult = template.Validate(TemplateOptions); return(new ParseResult <TemplateOptions>(TemplateOptions, validationResult)); }
public ParseResult <TTokenTypeName, TActionResult> Parse(IReadOnlyList <TokenMatch <TTokenTypeName> > inputTokens, int startIndex, bool mustConsumeTokens) { ParseResult <TTokenTypeName, TActionResult> lastResult = null; foreach (var subExpression in _choiceSubExpressions) { lastResult = subExpression.Parse(inputTokens, startIndex, mustConsumeTokens); if (lastResult.Succeed) { break; } } // lastResult cannot be null at this point - there will always be at least 2 choices to try if (!lastResult.Succeed) { return(ParseResult <TTokenTypeName, TActionResult> .Failed(startIndex)); } TActionResult actionResult = default; if (_matchAction != null && mustConsumeTokens) { actionResult = _matchAction(lastResult.MatchedTokens, new[] { lastResult.SemanticActionResult }, ToParserSpec.Value); } return(ParseResult <TTokenTypeName, TActionResult> .Succeeded(lastResult.NextParseStartIndex, lastResult.MatchedTokens, actionResult)); }
public ParseResult <TTokenTypeName, TActionResult> Parse(IReadOnlyList <TokenMatch <TTokenTypeName> > inputTokens, int startIndex, bool mustConsumeTokens) { var parseResult = _subExpression.Parse(inputTokens, startIndex, false); if (parseResult.Succeed) { return(ParseResult <TTokenTypeName, TActionResult> .Failed(startIndex)); } // Note: Predicates do not trigger semantic actions or consume tokens return(ParseResult <TTokenTypeName, TActionResult> .Succeeded(startIndex, new TokensMatch <TTokenTypeName>(inputTokens, new MatchRange(startIndex, 0)), default)); }
public static ParseResult <T?> ParseNullableNumeric <T>(this string?value, TryParseNumericFunc <T> tryParse, NumberStyles numberStyles, IFormatProvider provider) where T : struct { if (value == null) { return(ParseResult.Default <T?>()); } return(tryParse(value, numberStyles, provider, out T result) ? ParseResult.Success <T?>(result) : ParseResult.Failed <T?>()); }
public static ParseResult <T?> ParseNullable <T>(this string?value, TryParseFunc <T> tryParse) where T : struct { if (value == null) { return(ParseResult.Default <T?>()); } return(tryParse(value, out T result) ? ParseResult.Success <T?>(result) : ParseResult.Failed <T?>()); }
/// <inheritdoc /> public override ParseResult <T> Parse(string?source) { try { if (_parseFunc1 != null) { T value = _parseFunc1(source); return(ParseResult.Success(value)); } if (_parseFunc2 != null) { Option <T> optionalValue = _parseFunc2.Invoke(source); return(optionalValue.ToParseResult()); } if (_parseFunc3 != null) { ParseResult <T> parseResult = _parseFunc3.Invoke(source); return(parseResult); } } catch (Exception e) { return(ParseResult.Failed <T>(new Message( "Failed to parse value '{value}' to type '{type}'. Error: {error}", severity: MessageSeverity.Error, eventName: "ParserError", properties: new[] { new KeyValuePair <string, object>("value", source), new KeyValuePair <string, object>("type", typeof(T)), new KeyValuePair <string, object>("error", e.Message), }))); } return(ParseResult <T> .Failed); }
public static ParseResult <T> ParseNumeric <T>(this string?value, TryParseNumericFunc <T> tryParse, NumberStyles numberStyles, IFormatProvider provider) => tryParse(value, numberStyles, provider, out T result) ? ParseResult.Success(result) : ParseResult.Failed <T>();
public static ParseResult <T> Parse <T>(this string?value, TryParseFunc <T> tryParse) => tryParse(value, out T result) ? ParseResult.Success(result) : ParseResult.Failed <T>();