private static void DisplayToken(IToken token) { var score = TokenWeightResolver.Invoke(token); var tokenInfo = $"{token.GetType().Name}"; var valueInfo = "[value = {0}]"; var scoreInfo = $"[score = {score}]"; valueInfo = token switch { LiteralToken literalToken => string.Format(valueInfo, $"'{literalToken.Value}'"), LiteralAmountToken literalAmountToken => string.Format(valueInfo, literalAmountToken.Amount), FractionalAmountToken fractionalAmountToken => string.Format(valueInfo, GetRepresentation(fractionalAmountToken)), RangeAmountToken rangeAmountToken => string.Format(valueInfo, GetRepresentation(rangeAmountToken)), UnitToken unitToken => string.Format(valueInfo, unitToken.Unit), FormToken formToken => string.Format(valueInfo, formToken.Form), IngredientToken ingredientToken => string.Format(valueInfo, ingredientToken.Ingredient), _ => string.Empty }; var output = $"-> {tokenInfo}\n\t{valueInfo}\n\t{scoreInfo}"; Console.WriteLine(output); }
/// <summary> /// Visits a <see cref="FractionalAmountToken"/>. /// </summary> /// <param name="token">A <see cref="FractionalAmountToken"/> instance.</param> public void Visit(FractionalAmountToken token) { _parseResult.Details.Amount = token.WholeNumber != null ? $"{token.WholeNumber.Amount} {token.Numerator.Amount}/{token.Denominator.Amount}" : $"{token.Numerator.Amount}/{token.Denominator.Amount}";; }
/// <summary> /// Initializes a new instance of the <see cref="RangeAmountToken"/> class. /// </summary> /// <param name="lowerBound"> /// A <see cref="FractionalAmountToken"/> instance representing the lower bound of the range. /// </param> /// <param name="upperBound"> /// A <see cref="FractionalAmountToken"/> instance representing the upper bound of the range. /// </param> internal RangeAmountToken(FractionalAmountToken lowerBound, FractionalAmountToken upperBound) { LowerBound = lowerBound; UpperBound = upperBound; }
private bool Fraction(InputBuffer buffer, out FractionalAmountToken fraction) => ExecuteRules(GetFractionRules, buffer, out fraction);
private static string GetRepresentation(FractionalAmountToken fraction) => fraction.WholeNumber != null ? $"{fraction.WholeNumber.Amount} {fraction.Numerator.Amount}/{fraction.Denominator.Amount}" : $"{fraction.Numerator.Amount}/{fraction.Denominator.Amount}";