private Func <Type, string, ParseStep.Result> _CreateParseContext() { var _stack = new HashSet <ParseStep>(); Func <Type, string, ParseStep.Result> _tryParse = null; _tryParse = (_type, _s) => { var _parseSteps = _GetParseSteps(_type); if (_parseSteps.Length == 0) { return(StringParseResult.UnsupportedType(_type, _s)); } foreach (var _parseStep in _parseSteps) { if (_stack.Add(_parseStep)) { return(_parseStep.TryParse(_type, _s, _CultureInfo, _tryParse)); } } return(StringParseResult.InvalidString(_type, _s)); }; return(_tryParse); }
public Result(object value, StringParseResult <object>?underlyingResult, bool?isTypeSupported, bool?isStringValid) { Value = value; UnderlyingResult = underlyingResult; IsTypeSupported = isTypeSupported; IsStringValid = isStringValid; }
private static StringParseResult <IReadOnlyList <Modifier> > AggregateAndBuild( CoreParserParameter parameter, StringParseResult <IReadOnlyList <IReadOnlyList <IIntermediateModifier> > > result) { var(successfullyParsed, remaining, intermediates) = result; var modifiers = intermediates.SelectMany(ms => AggregateAndBuild(parameter, ms)).ToList(); return(successfullyParsed, remaining, modifiers); }
public override StringParseResult <object> TryParse(Type type, string s) { if (type == typeof(object)) { return(StringParseResult.UnsupportedType(type, s)); } var _result = _CreateParseContext()(type, s); // Ensure relevant result return(_result.ToStringParseResult(type, s)); }
public override StringParseResult <object> TryParse(Type type, string s) { try { return(StringParseResult.Valid(type, s, Convert.ChangeType(s, type, _CultureInfo))); } catch (InvalidCastException) { return(StringParseResult.UnsupportedType(type, s)); } catch (FormatException) { return(StringParseResult.InvalidString(type, s)); } catch (OverflowException) { return(StringParseResult.InvalidString(type, s)); } }
public override StringParseResult <object> TryParse(Type type, string s) { bool _canParse; if (_TypeCache.TryGetValue(type, out _canParse)) { if (!_canParse) { return(StringParseResult.UnsupportedType(type, s)); } return(_UnderlyingStringParser.TryParse(type, s)); } var _result = _UnderlyingStringParser.TryParse(type, s); _TypeCache.TryAdd(type, _result.IsTypeSupported.GetValueOrDefault()); return(_result); }
public StringParseResult <object> ToStringParseResult(Type type, string s) { if (UnderlyingResult.HasValue) { return(UnderlyingResult.Value); } if (IsTypeSupported.GetValueOrDefault() && IsStringValid.GetValueOrDefault()) { return(StringParseResult.Valid(type, s, Value)); } if (!IsTypeSupported.GetValueOrDefault(true)) { return(StringParseResult.UnsupportedType(type, s)); } if (!IsStringValid.GetValueOrDefault(true)) { return(StringParseResult.InvalidString(type, s)); } throw new UnexpectedStringParseErrorException(); }
private static Result _FailParse(Type type, string s) { return(StringParseResult.UnsupportedType(type, s)); }
private static Mock <IStringParser <TResult> > MockParser <TResult>( string modifier, StringParseResult <TResult> result) => MockParser((modifier, result));