Beispiel #1
0
        private bool HasMoreAmountCharacters(ParserContext context)
        {
            if (!context.Buffer.HasNext())
            {
                return(false);
            }

            if (NextCharacterIsValid(context))
            {
                return(true);
            }

            // When whitespace is next there are two cases:
            //
            //   1. Whitespace is being used to separate in the amount e.g. 1 1/2 grams, 1/4 - 1/3 cup
            //   2. Whitespace is being used to separate the next token e.g. 1/2 cup milk, 2 grams flour
            //
            // We handle these by looking ahead past the space to see if there are further characters valid
            // for an amount token - if not this is a single that the amount token is complete.
            if (context.Buffer.IsWhitespace())
            {
                // Offset by 1 to skip the whitepsace at the current position.
                var nextCharacters = context.Buffer.Peek(offset: 1, count: 1);

                if (!nextCharacters.Any())
                {
                    // When there's nothing after the whitespace then we definitely are done reading.
                    return(false);
                }

                var characterAfterWhitespace = nextCharacters.First();

                // There's something after the whitespace, check if it is a valid amount token character.
                return(ValidNextCharacterPredicates.Any(p => p(characterAfterWhitespace)));
            }

            return(false);
        }
Beispiel #2
0
 private bool NextCharacterIsValid(ParserContext context) =>
 ValidNextCharacterPredicates.Any(p => context.Buffer.Matches(p));