|
| 1 | +using Antlr4.Runtime; |
| 2 | +using Rubberduck.Parsing.PreProcessing; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Data; |
| 6 | +using System.Globalization; |
| 7 | + |
| 8 | +namespace Rubberduck.Inspections.Concrete.UnreachableCaseInspection |
| 9 | +{ |
| 10 | + public class ComparableDateValue : IValue, IComparable<ComparableDateValue> |
| 11 | + { |
| 12 | + private readonly DateValue _dateValue; |
| 13 | + private readonly int _hashCode; |
| 14 | + |
| 15 | + public ComparableDateValue(DateValue dateValue) |
| 16 | + { |
| 17 | + _dateValue = dateValue; |
| 18 | + _hashCode = dateValue.AsDecimal.GetHashCode(); |
| 19 | + } |
| 20 | + |
| 21 | + public Parsing.PreProcessing.ValueType ValueType => _dateValue.ValueType; |
| 22 | + |
| 23 | + public bool AsBool => _dateValue.AsBool; |
| 24 | + |
| 25 | + public byte AsByte => _dateValue.AsByte; |
| 26 | + |
| 27 | + public decimal AsDecimal => _dateValue.AsDecimal; |
| 28 | + |
| 29 | + public DateTime AsDate => _dateValue.AsDate; |
| 30 | + |
| 31 | + public string AsString => _dateValue.AsString; |
| 32 | + |
| 33 | + public IEnumerable<IToken> AsTokens => _dateValue.AsTokens; |
| 34 | + |
| 35 | + public int CompareTo(ComparableDateValue dateValue) |
| 36 | + => _dateValue.AsDecimal.CompareTo(dateValue._dateValue.AsDecimal); |
| 37 | + |
| 38 | + public override int GetHashCode() => _hashCode; |
| 39 | + |
| 40 | + public override bool Equals(object obj) |
| 41 | + { |
| 42 | + if (obj is ComparableDateValue decorator) |
| 43 | + { |
| 44 | + return decorator.CompareTo(this) == 0; |
| 45 | + } |
| 46 | + |
| 47 | + if (obj is DateValue dateValue) |
| 48 | + { |
| 49 | + return dateValue.AsDecimal == _dateValue.AsDecimal; |
| 50 | + } |
| 51 | + |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + public override string ToString() |
| 56 | + { |
| 57 | + return _dateValue.ToString(); |
| 58 | + } |
| 59 | + |
| 60 | + public string AsDateLiteral() |
| 61 | + { |
| 62 | + var asString = AsDate.ToString(CultureInfo.InvariantCulture); |
| 63 | + var prePostPend = "#"; |
| 64 | + var result = asString.StartsWith(prePostPend) ? asString : $"{prePostPend}{asString}"; |
| 65 | + result = result.EndsWith(prePostPend) ? result : $"{result}{prePostPend}"; |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + public static ComparableDateValue Parse(string valueText) |
| 70 | + { |
| 71 | + var literal = new DateLiteralExpression(new ConstantExpression(new StringValue(valueText))); |
| 72 | + return new ComparableDateValue((DateValue)literal.Evaluate()); |
| 73 | + } |
| 74 | + |
| 75 | + public static bool TryParse(string valueText, out ComparableDateValue value) |
| 76 | + { |
| 77 | + value = default; |
| 78 | + if (!(valueText.StartsWith("#") && valueText.EndsWith("#"))) |
| 79 | + { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + try |
| 84 | + { |
| 85 | + value = Parse(valueText); |
| 86 | + return true; |
| 87 | + } |
| 88 | + catch (SyntaxErrorException) |
| 89 | + { |
| 90 | + return false; |
| 91 | + } |
| 92 | + catch (ArgumentOutOfRangeException) |
| 93 | + { |
| 94 | + return false; |
| 95 | + } |
| 96 | + catch (InputMismatchException) |
| 97 | + { |
| 98 | + return false; |
| 99 | + } |
| 100 | + catch (Exception e) |
| 101 | + { |
| 102 | + //even though a SyntaxErrorException/InputMismatchException is thrown, |
| 103 | + //this catch-all block seems to be needed(?) |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments