public override IToken Perform(IToken token, TokenTreeList parameters, bool isFinal) { ListToken listToken = token as ListToken; if (listToken == null) throw new Exception($"Last token must be list for '{ID}'"); List<IToken> lastList = listToken.Tokens; int count = lastList.Count; IToken first = lastList[0]; if (first is ExpressionToken) return UnParsed(listToken); for (int i = 1; i < count - 1; i += 2) { IToken second = lastList[i]; if (second is ExpressionToken) return UnParsed(listToken); if (first.Text == second.Text) return lastList[i + 1]; } return count % 2 == 0 ? lastList[count - 1] : new NullToken(); }
public override IToken Perform(IToken dataToken, TokenTreeList parameters, bool isFinal) { string text = null; RegExToken.RegexType regexType = RegExToken.RegexType.Wildcard; ListToken listToken = dataToken as ListToken; if (listToken != null) { List<IToken> list = listToken.Tokens; if (list.Count < 1 || list.Count > 2) throw new Exception($"Must have 1 or 2 values for '{ID}': {listToken}"); text = list[0].Text; if (list.Count == 2) switch (list[1].Text) { case "R": regexType = RegExToken.RegexType.Regex; break; case "S": regexType = RegExToken.RegexType.Sql; break; case "W": regexType = RegExToken.RegexType.Wildcard; break; } } else { text = dataToken.Text; } return new RegExToken(text, regexType); }
/// <summary> /// Evaluate the function. /// </summary> /// <param name="parameters">The tokens that make up the function parameter list.</param> /// <param name="substitutions">The tokens that can be used for substitutions.</param> /// <param name="isFinal">Whether a result needs to be returned.</param> /// <returns></returns> public override IToken Perform(IToken parameters, TokenTreeList substitutions, bool isFinal) { ListToken listToken = parameters as ListToken; if (listToken == null) throw new Exception($"Token must be list for '{ID}'"); List<IToken> lastList = listToken.Tokens; int count = lastList.Count; IToken toFind = lastList[count - 1]; if (toFind is ExpressionToken) { if (isFinal) throw new Exception($"Could not find value for {toFind}"); return UnParsed(listToken); } for (int i = 0; i < count - 1; i++) { IToken token = lastList[i]; if (token is ExpressionToken) return UnParsed(listToken); ListToken list = token as ListToken; if (list != null && list.Tokens.Contains(toFind)) return new BoolTooken(true); if (token.Text == toFind.Text) return new BoolTooken(true); } return new BoolTooken(false); }
/// <summary> /// Constructor. /// </summary> /// <param name="list">The tokens that make up the parameters.</param> public TokenTreeParameters(TokenTreeList list) { foreach (TokenTree tree in list) { Add(tree); } }
public override IToken Evaluate(IToken first, IToken last, TokenTreeList parameters, bool isFinal) { if (first == null) throw new Exception($"Operation {Text} can not be unary."); IToken evaluated = last.Evaluate(parameters, isFinal); if (evaluated == null) throw new Exception($"Second element of Operation {Text} is not unique."); ListToken evaluatedList = evaluated as ListToken; if (evaluatedList != null) { ListToken list = new ListToken(); foreach (IToken item in evaluatedList.Tokens) list.Tokens.Add(Evaluate(first, item, parameters, isFinal)); return list; } IntToken intToken = evaluated as IntToken; if (intToken == null) { if (isFinal) throw new Exception($"Operation {Text} must have integer second element."); return new ExpressionToken(first, new IndexOperator(), last); } IToken tokenList = first.Evaluate(parameters, isFinal); ListToken listToken = tokenList as ListToken; int index = intToken.Value; return listToken == null ? (index == 0 && tokenList is ITypeToken ? tokenList : new ExpressionToken(first, new IndexOperator(), intToken)) : (listToken.Tokens.Count > index ? listToken.Tokens[index] : new NullToken()); }
/// <summary> /// Finds a property in the list. /// </summary> /// <param name="name">The name of the property.</param> /// <returns>List of properties matching the name.</returns> public IList<IProperty> Find(string name) { IList<IProperty> value; if (!_values.TryGetValue(name, out value)) { List<IProperty> values = new List<IProperty>(); IEnumerable<TokenTree> tokenTrees = _data.Where(child => child.Name == name); TokenTreeList parameters = new TokenTreeList(_parameters); foreach (TokenTree item in tokenTrees) { IToken evaluated = item.Value.Evaluate(parameters, false); ListToken list = evaluated as ListToken; if (list != null) { foreach (IToken token in list.Tokens) { values.Add(new TokenTreeProperty(new TokenTree(name, token))); } } else { values.Add(new TokenTreeProperty(new TokenTree(name, evaluated))); } } value = values; _values[name] = value; } return value; }
public TokenTree(string key, string value, TokenTreeList children = null) { Key = TokenGenerator.Parse(key).Simplify(); value = ReplaceValue(key, value); IToken tokenList = value == null ? new NullToken() : TokenGenerator.Parse(value).Simplify(); Value = tokenList ?? new StringToken(""); Children = children ?? new TokenTreeList(); }
public override IToken Evaluate(IToken first, IToken last, TokenTreeList parameters, bool isFinal) { IToken firstList = first.Evaluate(parameters, isFinal); IToken lastList = last.Evaluate(parameters, isFinal); if (firstList == null || lastList == null) throw new Exception($"Operation {Text} is a binary operation."); return AddToList(firstList, lastList); }
/// <summary> /// Evaluate the function. /// </summary> /// <param name="parameters">The tokens that make up the function parameter list.</param> /// <param name="substitutions">The tokens that can be used for substitutions.</param> /// <param name="isFinal">Whether a result needs to be returned.</param> /// <returns></returns> public override IToken Perform(IToken parameters, TokenTreeList substitutions, bool isFinal) { ListToken listToken = parameters as ListToken; if (listToken == null) throw new Exception($"Next token must be list for '{ID}'"); List<IToken> lastList = listToken.Tokens; int count = lastList.Count; IToken iterandKey = lastList[count - 2]; IToken iterandIndex = null; ListToken iterandList = iterandKey as ListToken; if (iterandList != null) { if (iterandList.Tokens.Count != 2) throw new Exception($"Can only have 1 or 2 iterators for '{ID}'"); iterandIndex = iterandList.Tokens[1]; iterandKey = iterandList.Tokens[0]; } IToken method = lastList[count - 1]; ListToken tokens = new ListToken(); for (int i = 0; i < count - 2; i++) { IToken token = lastList[i]; ListToken list = token as ListToken; if (list == null) { list = new ListToken(); list.Tokens.Add(token); } int index = 0; foreach (IToken item in list.Tokens) { TokenTree tree = new TokenTree(); tree.Children.Add(new TokenTree(iterandKey.Text, item)); if (iterandIndex != null) tree.Children.Add(new TokenTree(iterandIndex.Text, new IntToken(index))); IToken toCall = method.SubstituteParameters(tree); IToken parsed = toCall.Evaluate(substitutions, isFinal); if (parsed is ExpressionToken) { if (!isFinal) return UnParsed(listToken); } else if (!(parsed is NullToken)) { tokens.Add(parsed); } ++index; } } return tokens; }
public override TokenList Evaluate(IToken first, IToken last, TokenTreeList parameters) { TokenList firstList = first?.Evaluate(parameters); TokenList lastList = last?.Evaluate(parameters); if (firstList == null || lastList == null) throw new Exception($"Operation {Text} is a binary operation."); TokenList result = new TokenList(); result.AddRange(firstList); result.AddRange(lastList); return result; }
public override IToken Perform(IToken token, TokenTreeList parameters, bool isFinal) { ListToken list = token as ListToken; if (list == null) { list = new ListToken(); list.Tokens.Add(token); } ListToken result = new ListToken(); result.Tokens.AddRange(list.Tokens); result.Tokens.Reverse(); return result; }
public override TokenList Evaluate(IToken first, IToken last, TokenTreeList parameters) { if (first == null) throw new Exception($"Operation {Text} can not be unary."); TokenList lastList = last.Evaluate(parameters); if (lastList == null || lastList.Count != 1) throw new Exception($"Second element of Operation {Text} is not unique."); IntToken intToken = lastList[0] as IntToken; if (intToken == null) throw new Exception($"Operation {Text} must have integer second element."); return new TokenList(first.Evaluate(parameters)[intToken.Value]); }
public override IToken Perform(IToken parameterToken, TokenTreeList parameters, bool isFinal) { ListToken listToken = parameterToken as ListToken; if (listToken == null) throw new Exception($"Last token must be list for '{ID}'"); foreach (IToken token in listToken.Tokens) { if (token is ExpressionToken) return UnParsed(listToken); if (!token.Convert<bool>()) return new BoolTooken(false); } return new BoolTooken(true); }
public override IToken Perform(IToken dataToken, TokenTreeList parameters, bool isFinal) { ListToken listToken = dataToken as ListToken; if (listToken != null && listToken.Tokens.Count > 0) { IToken current = listToken.Tokens[0]; for (int i = 1; i < listToken.Tokens.Count; ++i) { IToken token = listToken.Tokens[i]; current = new ExpressionToken(current, new PlusOperator(), token).Evaluate(parameters, isFinal); } return current; } return dataToken; }
/// <summary> /// Evaluate the function. /// </summary> /// <param name="parameters">The tokens that make up the function parameter list.</param> /// <param name="substitutions">The tokens that can be used for substitutions.</param> /// <param name="isFinal">Whether a result needs to be returned.</param> /// <returns></returns> public override IToken Perform(IToken parameters, TokenTreeList substitutions, bool isFinal) { ListToken listToken = parameters as ListToken; if (listToken == null) return new ListToken {new ListToken {parameters, new IntToken(1)}}; Dictionary<string, int> found = new Dictionary<string, int>(); foreach (IToken child in listToken.Tokens) if (!AddToken(found, child)) return UnParsed(listToken); ListToken list = new ListToken(); foreach (var item in found) list.Add(new ListToken {new StringToken(item.Key), new IntToken(item.Value)}); return list; }
public override IToken Perform(IToken dataToken, TokenTreeList parameters, bool isFinal) { ListToken listToken = dataToken as ListToken; if (listToken != null) { ListToken returnList = new ListToken(); foreach (IToken token in listToken.Tokens) returnList.Add(Perform(token, parameters, isFinal)); return returnList; } ITypeToken typeToken = dataToken as ITypeToken; if (typeToken != null) return new DoubleToken(typeToken.Convert<double>()); throw new Exception($"Token must be list or convertible to double for {ID}"); }
/// <summary> /// Evaluate the function. /// </summary> /// <param name="parameters">The tokens that make up the function parameter list.</param> /// <param name="substitutions">The tokens that can be used for substitutions.</param> /// <param name="isFinal">Whether a result needs to be returned.</param> /// <returns></returns> public override IToken Perform(IToken parameters, TokenTreeList substitutions, bool isFinal) { ListToken listToken = parameters as ListToken; if (listToken == null) throw new Exception($"Last token must be list for '{ID}'"); List<IToken> lastList = listToken.Tokens; int count = lastList.Count; if (count < 3 || count > 5) throw new Exception($"Must have between 3 and 5 values for '{ID}': {listToken}"); IToken first = lastList[0].Evaluate(substitutions, isFinal); IToken second = lastList[1].Evaluate(substitutions, isFinal); if (first is ExpressionToken || second is ExpressionToken) return UnParsed(listToken); int comparison; if (first is RegExToken) { if (count > 4) throw new Exception($"Must have 3 or 4 values for '{ID}': {listToken}"); comparison = first.Contains(second.Text) ? 0 : 1; } else { comparison = (first is IntToken || first is DoubleToken) && (second is IntToken || second is DoubleToken) ? first.Convert<double>().CompareTo(second.Convert<double>()) : first.Text.CompareTo(second.Text); } IToken result; switch (count) { case 3: result = comparison == 0 ? lastList[2] : new NullToken(); break; case 4: result = comparison == 0 ? lastList[2] : lastList[3]; break; default: result = comparison == 1 ? lastList[2] : comparison == 0 ? lastList[3] : lastList[4]; break; } return result.Evaluate(substitutions, isFinal); }
public void TestMatching() { TokenTreeList children = new TokenTreeList { new TokenTree(new ListToken {new StringToken("C"), new StringToken("D")}, new StringToken("E")), new TokenTree(new StringToken("A"), new StringToken("B")), new TokenTree(new RegExToken(".*F", RegExToken.RegexType.Regex), new StringToken("G")), new TokenTree(new RegExToken("*H", RegExToken.RegexType.Wildcard), new StringToken("I")), new TokenTree(new RegExToken("%J", RegExToken.RegexType.Sql), new StringToken("K")) }; TokenTree toSearch = new TokenTree(children); Assert.AreEqual("B", toSearch["A"]); Assert.AreEqual("E", toSearch["C"]); Assert.AreEqual("E", toSearch["D"]); Assert.AreEqual("G", toSearch["IF"]); Assert.AreEqual("I", toSearch["EACH"]); Assert.AreEqual("K", toSearch["RAJ"]); }
/// <summary> /// Evaluate the function. /// </summary> /// <param name="parameters">The tokens that make up the function parameter list.</param> /// <param name="substitutions">The tokens that can be used for substitutions.</param> /// <param name="isFinal">Whether a result needs to be returned.</param> /// <returns></returns> public override IToken Perform(IToken parameters, TokenTreeList substitutions, bool isFinal) { string toSplit; int maxCount = -1; string[] splitOn = {" ", "\t"}; StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries; ListToken listToken = parameters as ListToken; if (listToken == null) { if (parameters is ExpressionToken) return UnParsed(parameters); toSplit = parameters.Text; } else { List<IToken> lastList = listToken.Tokens; int count = lastList.Count; if (count != 2 && count != 3) throw new Exception($"Must have 1, 2 or 3 values for '{ID}': {listToken}"); if (lastList[0] is ExpressionToken) return UnParsed(listToken); toSplit = lastList[0].Text; if (count > 1) { string text = lastList[1].Text; if (text != " ") { splitOn = new[] {text}; options = StringSplitOptions.None; } } if (count > 2) maxCount = lastList[2].Convert<int>(); } ListToken result = new ListToken(); string[] bits = maxCount <= 0 ? toSplit.Split(splitOn, options) : toSplit.Split(splitOn, maxCount, options); foreach (string bit in bits) result.Add(new StringToken(bit.Trim())); return result; }
private static IToken AddToken(IToken current, IToken separator, IToken toAdd, TokenTreeList parameters, bool isFinal) { ListToken currentList = toAdd as ListToken; if (currentList != null) { foreach (IToken tokenToAdd in currentList.Tokens) current = AddToken(current, separator, tokenToAdd, parameters, isFinal); } else if (current == null) { current = toAdd; } else { current = new ExpressionToken(current, new PlusOperator(), separator).Evaluate(parameters, isFinal); current = new ExpressionToken(current, new PlusOperator(), toAdd).Evaluate(parameters, isFinal); } return current; }
public override IToken Evaluate(IToken first, IToken last, TokenTreeList parameters, bool isFinal) { if (parameters == null) { if (isFinal) throw new Exception($"Operation {Text} must have parameters if final."); return new ExpressionToken(first, this, last); } if (first != null) throw new Exception($"Operation {Text} is unary."); if (last == null) throw new Exception($"Operation {Text} needs a variable."); IToken evaluated = last.Evaluate(parameters, isFinal); if (evaluated is ExpressionToken) return new ExpressionToken(null, this, evaluated); ListToken listToken = evaluated as ListToken; if (listToken != null && listToken.Tokens.Exists(x => x is ExpressionToken)) return new ExpressionToken(null, this, evaluated); string text = evaluated.Text; TokenTreeList found = parameters.FindMatches(text, true); ListToken result = new ListToken(); foreach (TokenTree tokenTree in found) { bool debug = bool.Parse(tokenTree["Debug"] ?? "False"); if (debug) LogControl?.SetLogging(true); IToken token = tokenTree.Value.Evaluate(parameters, isFinal); if (!(token is NullToken)) result.Add(token); if (debug) LogControl?.ResetLoggingToDefault(); } if (result.Tokens.Count == 0) return new ExpressionToken(null, this, evaluated); return result.Tokens.Count == 1 ? result.Tokens[0] : result; }
/// <summary> /// Evaluate the function. /// </summary> /// <param name="parameters">The tokens that make up the function parameter list.</param> /// <param name="substitutions">The tokens that can be used for substitutions.</param> /// <param name="isFinal">Whether a result needs to be returned.</param> /// <returns></returns> public override IToken Perform(IToken parameters, TokenTreeList substitutions, bool isFinal) { ListToken listToken = parameters as ListToken; if (listToken == null) throw new Exception($"Last token must be list for '{ID}'"); List<IToken> lastList = listToken.Tokens; int count = lastList.Count; if (count != 2 && count != 3) throw new Exception($"Must have 2 or 3 values for '{ID}': {listToken}"); IToken toCheck = lastList[0].Evaluate(substitutions, isFinal); if (toCheck is ExpressionToken) return UnParsed(listToken); BoolTooken query = toCheck as BoolTooken; if (query == null) throw new Exception($"First item must be boolean for '{ID}': {listToken}"); return (query.Value ? lastList[1] : (count == 3 ? lastList[2] : new NullToken())).Evaluate(substitutions, isFinal); }
/// <summary> /// Evaluate the function. /// </summary> /// <param name="parameters">The tokens that make up the function parameter list.</param> /// <param name="substitutions">The tokens that can be used for substitutions.</param> /// <param name="isFinal">Whether a result needs to be returned.</param> /// <returns></returns> public override IToken Perform(IToken parameters, TokenTreeList substitutions, bool isFinal) { ListToken list = parameters as ListToken; if (list == null) { list = new ListToken(); list.Tokens.Add(parameters); } List<IToken> parameterList = new List<IToken>(); foreach (IToken item in list) { IToken toAdd = item; if (toAdd is ExpressionToken && !isFinal) return UnParsed(parameters); parameterList.Add(toAdd); } ListToken result = new ListToken(); switch (parameterList.Count) { case 1: for (int i = 1; i <= parameterList[0].Convert<int>(); ++i) result.Tokens.Add(new IntToken(i)); break; case 2: for (int i = parameterList[0].Convert<int>(); i <= parameterList[1].Convert<int>(); ++i) result.Tokens.Add(new IntToken(i)); break; case 3: for (int i = parameterList[0].Convert<int>(); i <= parameterList[1].Convert<int>(); i += parameterList[2].Convert<int>()) result.Tokens.Add(new IntToken(i)); break; default: throw new Exception($"Must have between 1 and 3 values for '{ID}': {parameters}"); } return result; }
public virtual IToken Evaluate(IToken firstToken, IToken lastToken, TokenTreeList parameters, bool isFinal) { IToken first = firstToken?.Evaluate(parameters, isFinal); IToken last = lastToken?.Evaluate(parameters, isFinal); if (first == null) { if (CanBeUnary) { ITypeToken secondType = last?.Simplify() as ITypeToken; return secondType != null ? Evaluate(secondType) : new ExpressionToken(null, this, last); } throw new Exception($"Operation {Text} can not be unary."); } if (CanBeBinary) { ITypeToken firstType = first.Simplify() as ITypeToken; ITypeToken secondType = last?.Simplify() as ITypeToken; return firstType != null && secondType != null ? Evaluate(firstType, secondType) : new ExpressionToken(first, this, last); } throw new Exception($"Operation {Text} can not be binary."); }
public void TestElementType() { string data = @" Macro1: TypeM Macro2: TypeA Field1: TypeF Field2: Macro1: Parameter1 Field3: Macro2 "; TokenTree tokenTree = Parser.ParseString(data); TokenTreeList list = new TokenTreeList(tokenTree); TokenTree tree = tokenTree.FindFirst("Field1"); TokenTreeElement element = new TokenTreeElement(tree); Assert.AreEqual("TypeF", element.ElementType); tree = tokenTree.FindFirst("Field2"); element = new TokenTreeElement(tree, list); Assert.AreEqual("TypeM", element.ElementType); tree = tokenTree.FindFirst("Field3"); element = new TokenTreeElement(tree, list); Assert.AreEqual("TypeA", element.ElementType); }
public override IToken Perform(IToken parameterList, TokenTreeList parameters, bool isFinal) { ListToken listToken = parameterList as ListToken; if (listToken == null) throw new Exception($"Last token must be list for '{ID}'"); List<IToken> lastList = listToken.Tokens; int count = lastList.Count; if (count < 2) throw new Exception($"Must have at least 2 values for '{ID}': {listToken}"); IToken method = lastList[0]; if (isFinal) { TokenTree tree = new TokenTree(); for (int i = 1; i < lastList.Count; ++i) { IToken parameter = lastList[i]; tree.Children.Add(new TokenTree(i.ToString(), parameter)); } method = method.SubstituteParameters(tree); } IToken parsed = method.Evaluate(parameters, isFinal); if (parsed is ExpressionToken) { if (!isFinal) return UnParsed(listToken); } else { return parsed; } return new NullToken(); }
/// <summary> /// Evaluate the function. /// </summary> /// <param name="parameters">The tokens that make up the function parameter list.</param> /// <param name="substitutions">The tokens that can be used for substitutions.</param> /// <param name="isFinal">Whether a result needs to be returned.</param> /// <returns></returns> public override IToken Perform(IToken parameters, TokenTreeList substitutions, bool isFinal) { ListToken listToken = parameters as ListToken; int count = listToken?.Tokens.Count ?? 0; if (count > 0) { IToken separator = new StringToken(" "); if (count > 1) { separator = listToken.Tokens[count - 1]; --count; } IToken current = null; for (int i = 0; i < count; ++i) { IToken token = listToken.Tokens[i]; if (token is ExpressionToken) return UnParsed(parameters); current = AddToken(current, separator, token, substitutions, isFinal); } return current ?? new StringToken(""); } return parameters; }
public TokenTree(IToken key, IToken value) { Key = key; Value = value; Children = new TokenTreeList(); }
public TokenTree(string key, string value, TokenTreeList children = null) { Key = _tokenGenerator.Parse(key).Simplify()[0]; Value = _tokenGenerator.Parse(value)?.Simplify()[0] ?? new StringToken(""); Children = children ?? new TokenTreeList(); }
public TokenTree(TokenTreeList children = null) : this("", "", children) { }