Exemplo n.º 1
0
        /// <summary>
        /// Parses the given input and creates an object with values matching the specified pattern.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="result">The result.</param>
        /// <param name="tokens">The tokens.</param>
        /// <param name="input">The input.</param>
        /// <param name="processed">The lines that have already been processed.</param>
        /// <returns></returns>
        private TokenResult <T> ParseBlock <T>(TokenResult <T> result, IEnumerable <Token> tokens, string input, IList <string> processed) where T : class
        {
            foreach (var token in tokens)
            {
                // Check for missing operatations
                if (OperatorFactory.HasMissingFunctions(token))
                {
                    throw new ArgumentException("Token has missing operators: " + token.Operation);
                }

                // Check token prerequisites
                if (!token.PrerequisiteSatisfied(processed))
                {
                    continue;
                }

                // Skip already replaced tokens
                if (token.Replaced)
                {
                    continue;
                }

                // Ignore tokens that aren't contained in the input
                if (!token.ContainedIn(input))
                {
                    continue;
                }

                // Extract token value from the input text
                object value = input
                               .SubstringAfterString(token.Prefix)
                               .SubstringBeforeString(token.Suffix)
                               .Trim();

                // Perform Validation
                if (!OperatorFactory.Validate(token, value.ToString()))
                {
                    continue;
                }

                // Perform token operation
                value = OperatorFactory.PerformOperation(token, value.ToString());

                // Use reflection to set the property on the object with the token value
                result.Value = SetValue(result.Value, token.Value, value);

                // Add the match to the result collection
                result.Replacements.Add(token);

                // Remove token so it's not replaced again
                if (!IsACollection(result.Value, token))
                {
                    token.Replaced = true;
                }
                else
                {
                    token.IsList = true;
                }

                break;
            }

            return(result);
        }