public TokenProcessResult Process(IEnumerable <IToken> tokens, PartialNumberContent numberContent)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException("tokens");
            }
            if (numberContent == null)
            {
                throw new ArgumentNullException("numberContent");
            }

            var token = tokens.First();

            if (token == null)
            {
                throw new ArgumentException("Null reference encountered in tokens set");
            }

            if (token.Is <MemberAccessorOrDecimalPointToken>())
            {
                return(new TokenProcessResult(
                           numberContent.AddToken(token),
                           new IToken[0],
                           GotSomeDecimalNumberContent.Instance
                           ));
            }
            else if (token.IsMinusSignOperator())
            {
                return(new TokenProcessResult(
                           numberContent.AddToken(token),
                           new IToken[0],
                           GotMinusSignOfNumber.Instance
                           ));
            }
            else if (token is NumericValueToken)
            {
                return(new TokenProcessResult(
                           numberContent.AddToken(token),
                           new IToken[0],
                           GotSomeIntegerNumberContent.Instance
                           ));
            }

            return(Common.Reset(tokens, numberContent));
        }
        public TokenProcessResult Process(IEnumerable <IToken> tokens, PartialNumberContent numberContent)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException("tokens");
            }
            if (numberContent == null)
            {
                throw new ArgumentNullException("numberContent");
            }

            var token = tokens.First();

            if (token == null)
            {
                throw new ArgumentException("Null reference encountered in tokens set");
            }

            // The only continuation possibility for the number is if a decimal point is reached
            if (token.Is <MemberAccessorOrDecimalPointToken>())
            {
                return(new TokenProcessResult(
                           numberContent.AddToken(token),
                           new IToken[0],
                           GotSomeDecimalNumberContent.Instance
                           ));
            }

            // If we're not at a decimal point then the end of the number content must have been reached
            // - Try to extract the number content so far and express that as a new token
            // - Return a "processedTokens" set of this and the current token (we don't need to worry about trying to process
            //   that here since it's not valid for two number tokens to exist adjacently with nothing in between)
            var numbericValueToken = numberContent.TryToExpressNumericValueTokenFromCurrentTokens();

            if (numbericValueToken == null)
            {
                throw new Exception("numberContent should describe a number, null was returned from TryToExpressNumberFromTokens - invalid content");
            }
            return(new TokenProcessResult(
                       new PartialNumberContent(),
                       new[] { numbericValueToken, token },
                       Common.GetDefaultProcessor(tokens)
                       ));
        }
Esempio n. 3
0
        public TokenProcessResult Process(IEnumerable <IToken> tokens, PartialNumberContent numberContent)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException("tokens");
            }
            if (numberContent == null)
            {
                throw new ArgumentNullException("numberContent");
            }

            var token = tokens.First();

            if (token == null)
            {
                throw new ArgumentException("Null reference encountered in tokens set");
            }

            // At this point, the current token needs to be either a number or a decimal point. Otherwise it's not going to be
            // part of a valid numeric value.
            if (token is NumericValueToken)
            {
                return(new TokenProcessResult(
                           numberContent.AddToken(token),
                           new IToken[0],
                           GotSomeIntegerNumberContent.Instance
                           ));
            }
            else if (token.Is <MemberAccessorOrDecimalPointToken>())
            {
                return(new TokenProcessResult(
                           numberContent.AddToken(token),
                           new IToken[0],
                           GotSomeDecimalNumberContent.Instance
                           ));
            }
            return(Common.Reset(tokens, numberContent));
        }
Esempio n. 4
0
        /// <summary>
        /// If a processor has encountered a token that has invalidated the assumptions it was making that it would be able to rebuild a number
        /// token, then it can pass the current tokens and PartialNumberContent to this to get back a TokenProcessResult which will return the
        /// unprocessed token content and select an appropriate processor to restart with.
        /// </summary>
        public static TokenProcessResult Reset(IEnumerable <IToken> tokens, PartialNumberContent numberContent)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException("tokens");
            }
            if (numberContent == null)
            {
                throw new ArgumentNullException("numberContent");
            }

            var token = tokens.First();

            if (token == null)
            {
                throw new ArgumentException("Null reference encountered in tokens set");
            }

            return(new TokenProcessResult(
                       new PartialNumberContent(new IToken[0]),
                       numberContent.Tokens.Concat(new[] { token }),
                       GetDefaultProcessor(tokens)
                       ));
        }
Esempio n. 5
0
        public TokenProcessResult Process(IEnumerable <IToken> tokens, PartialNumberContent numberContent)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException("tokens");
            }
            if (numberContent == null)
            {
                throw new ArgumentNullException("numberContent");
            }

            var token = tokens.First();

            if (token == null)
            {
                throw new ArgumentException("Null reference encountered in tokens set");
            }

            // If we've already got tokens describing a decimal number and we encounter another decimal point then things are going badly
            // (this can't represent a valid number)
            if (token.Is <MemberAccessorOrDecimalPointToken>())
            {
                throw new Exception("Encountered a MemberAccessorOrDecimalPointToken while part way through processing a decimal value - invalid content");
            }

            // If we hit a numeric token, though, then things ARE going well and we can conclude the number search and incorporate the current token
            if (token is NumericValueToken)
            {
                var numbericValueToken = numberContent.AddToken(token).TryToExpressNumericValueTokenFromCurrentTokens();
                if (numbericValueToken == null)
                {
                    throw new Exception("numberContent should describe a number, null was returned from TryToExpressNumberFromTokens - invalid content");
                }
                return(new TokenProcessResult(
                           new PartialNumberContent(),
                           new[] { numbericValueToken },
                           Common.GetDefaultProcessor(tokens)
                           ));
            }
            else
            {
                // If we hit any other token then hopefully things have gone well and we extracted a number, but not have processed the current
                // token (we don't have to try to process it as number content since it's not valid for two number tokens to exist adjacently)
                var numbericValueToken = numberContent.TryToExpressNumericValueTokenFromCurrentTokens();
                if (numbericValueToken == null)
                {
                    if ((numberContent.Tokens.Count() == 1) && numberContent.Tokens.Single().Is <MemberAccessorOrDecimalPointToken>())
                    {
                        // If we've hit what appears to be a decimal point, but it's the current token does not result in a successful numeric
                        // parse, then presumably it's actually a property or method access within a "WITH" construct. As such, don't try to
                        // treat it as numeric content - return the tokens as "processed" (meaning there is no NumberRebuilder processing
                        // to be done to them).
                        return(new TokenProcessResult(
                                   new PartialNumberContent(),
                                   new[] { numberContent.Tokens.Single(), token },
                                   Common.GetDefaultProcessor(tokens)
                                   ));
                    }
                    else
                    {
                        throw new Exception("numberContent should describe a number, null was returned from TryToExpressNumberFromTokens - invalid content");
                    }
                }
                return(new TokenProcessResult(
                           new PartialNumberContent(),
                           new[] { numbericValueToken, token },
                           Common.GetDefaultProcessor(tokens)
                           ));
            }
        }
        public TokenProcessResult Process(IEnumerable <IToken> tokens, PartialNumberContent numberContent)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException("tokens");
            }
            if (numberContent == null)
            {
                throw new ArgumentNullException("numberContent");
            }

            var token = tokens.First();

            if (token == null)
            {
                throw new ArgumentException("Null reference encountered in tokens set");
            }

            if (numberContent.Tokens.Any())
            {
                throw new Exception("The numberContent reference should be empty when using the NoNumberContentYet processor");
            }

            // If this token is a NumericValueToken then this is the start of numeric content (since we don't currently have any number
            // content at all). At this point we only know that it's the start of an integer value, the GotSomeIntegerNumberContent
            // state will deal with any decimal points that are encountered (switching to the GotSomeDecimalNumberContent state).
            if (token.Is <NumericValueToken>())
            {
                return(new TokenProcessResult(
                           new PartialNumberContent(new[] { token }),
                           new IToken[0],
                           GotSomeIntegerNumberContent.Instance
                           ));
            }

            // If this is a MemberAccessorOrDecimalPointToken then it could be a member accessor (eg. "a.Name") or it could be the
            // start of a zero-less decimal number (eg. "fnc .1") - if the next token is a NumericValueToken then it is the latter
            // case and we need to switch to the GotSomeDecimalNumberContent state.
            if (token.Is <MemberAccessorOrDecimalPointToken>())
            {
                var nextTokens = tokens.Skip(1);
                if (nextTokens.Any())
                {
                    var nextToken = nextTokens.First();
                    if (nextToken == null)
                    {
                        throw new ArgumentException("Null reference encountered in tokens set");
                    }
                    if (nextToken.Is <NumericValueToken>())
                    {
                        return(new TokenProcessResult(
                                   new PartialNumberContent(new[] { token }),
                                   new IToken[0],
                                   GotSomeDecimalNumberContent.Instance
                                   ));
                    }
                }
            }

            return(new TokenProcessResult(
                       numberContent,
                       new[] { token },
                       Common.GetDefaultProcessor(tokens)
                       ));
        }