private bool Literal(InputBuffer buffer, out LiteralAmountToken literal)
        {
            var seenDecimalPoint = false;
            var digits           = new StringBuilder();

            // TODO: Would be good for this to use rules like the other parts
            // of the grammar, but for now let's keep it simple.
            while (buffer.HasNext() && (buffer.IsDigit() || buffer.Peek() == '.'))
            {
                var c = buffer.Next();

                if (c == '.' && seenDecimalPoint)
                {
                    // We only accept a single decimal point.
                    literal = null;

                    return(false);
                }

                seenDecimalPoint = c == '.';

                digits.Append(c);
            }

            if (!decimal.TryParse(digits.ToString(), out var amount))
            {
                literal = null;

                return(false);
            }

            literal = AmountToken.Literal(amount);

            return(true);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FractionalAmountToken"/> class.
 /// </summary>
 /// <param name="wholeNumber">The whole number component of the fraction, if any.</param>
 /// <param name="numerator">The numerator component of the fraction.</param>
 /// <param name="denominator">The denominator component of the fraction.</param>
 internal FractionalAmountToken(
     LiteralAmountToken wholeNumber,
     LiteralAmountToken numerator,
     LiteralAmountToken denominator)
 {
     WholeNumber = wholeNumber;
     Numerator   = numerator;
     Denominator = denominator;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Visits a <see cref="LiteralAmountToken"/>.
 /// </summary>
 /// <param name="token">A <see cref="LiteralAmountToken"/> instance.</param>
 public void Visit(LiteralAmountToken token)
 {
     _parseResult.Details.Amount = token.Amount.ToString();
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RangeAmountToken"/> class.
 /// </summary>
 /// <param name="lowerBound">
 /// A <see cref="LiteralAmountToken"/> instance representing the lower bound of the range.
 /// </param>
 /// <param name="upperBound">
 /// A <see cref="LiteralAmountToken"/> instance representing the upper bound of the range.
 /// </param>
 internal RangeAmountToken(LiteralAmountToken lowerBound, LiteralAmountToken upperBound)
 {
     LowerBound = lowerBound;
     UpperBound = upperBound;
 }