/// <summary> /// Parses the given input and creates an object with values matching the specified pattern. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="target">The target.</param> /// <param name="pattern">The pattern.</param> /// <param name="input">The input.</param> /// <returns></returns> public TokenResult <T> Parse <T>(T target, string pattern, string input) where T : class { var result = new TokenResult <T>(target); var tokens = GetTokens(pattern); var processed = new List <string>(); foreach (var line in input.ToLines()) { var count = result.Replacements.Count; if (!string.IsNullOrEmpty(line)) { result = ParseBlock(result, tokens, line, processed); processed.Add(line); } if (result.Replacements.Count == count) { foreach (var token in tokens) { if (token.IsList) { token.Replaced = true; } } } } return(result); }
private TokenResult <T> ParseBlock <T>(TokenResult <T> result, string pattern, string input) where T : class { // Extract all the tokens from the pattern var tokens = GetTokens(pattern); // Parse input return(ParseBlock(result, tokens, input, new string[0])); }
/// <summary> /// Creates a new instance of the <see cref="TokenizeResultBase"/> class. /// </summary> public TokenizeResultBase(Template template) { Exceptions = new List <Exception>(); Hints = new HintResult(); Tokens = new TokenResult(); Template = template; }
/// <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); }
/// <summary> /// Parses the given input and creates an object with values matching the specified pattern. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="target">The target.</param> /// <param name="pattern">The pattern.</param> /// <param name="input">The input.</param> /// <returns></returns> public TokenResult <T> ParseBlock <T>(T target, string pattern, string input) where T : class { var result = new TokenResult <T>(target); return(ParseBlock(result, pattern, input)); }