public void Or(IMatchSet b) { foreach (EntryId n in b) { Add(n); } }
/// <summary> /// Writes all matches to the console /// </summary> /// <param name="m">The match set</param> /// <param name="what">the original command, in case an error message is created</param> public void WriteLineResults(IMatchSet m, string what) { Console.ForegroundColor = ConsoleColor.Cyan; foreach (EntryId v in m) { string item = logEntries[v]; if (outCount++ < maxLinesToConsole) { Console.WriteLine(item); } if (uiLogOutput != null) { uiLogOutput.WriteLine(item); } } if (m.Count() > maxLinesToConsole) { WriteLineGreen("" + m.Count() + " results found for " + what); } if (m.Count() == 0) { WriteLineRed("no results found for " + what); } Console.ForegroundColor = ConsoleColor.White; }
/// <summary> /// Assignment operator from the command line /// </summary> /// <param name="tokens">parsed command line</param> /// <param name="errors">place to add error messages</param> /// <returns>the matches assigned to this variable</returns> public IMatchSet Assign(List <string> tokens, List <string> errors) { if (tokens.Count < 3 || tokens[1] != "=" || tokens[0][0] != '$') { if (errors != null) { errors.Add(string.Format("Assignement format error {0} ", string.Join(" ", tokens))); } return(new MatchSet()); } else { string key = tokens[0].ToLower();; IMatchSet matchSet = ParseTokens(tokens, errors, 2); if (errors != null) { variableDefinitions[key] = tokens; } variables[key] = matchSet; return(matchSet); } }
/// <summary> /// The Find command method. Evaluates an expression, and prints the resulting log entries to the console. /// </summary> /// <param name="commandTokens"></param> void Find(List <string> commandTokens) { IMatchSet m = logic.ParseTokens(commandTokens, errorMessages, 1); WriteLineResults(m, string.Join(" ", commandTokens)); }
/// <summary> /// Parses a list of tokens as a logical expression /// This is called recursively when needed for precedence or paranthesis /// </summary> /// <param name="tokens">list of parsed command tokens</param> /// <param name="errors">place to add error messages</param> /// <param name="start">end position in tokens, inclusive</param> /// <param name="end">end position in tokens, inclusive</param> /// <returns>matches for this subexpression</returns> public IMatchSet ParseTokens(List <string> tokens, List <string> errors, int start = 0, int end = 0) { IMatchSet matchSet = null; if (end == 0) { end = tokens.Count() - 1; } for (int i = start; i <= end; i++) { if (tokens[i] == "(") { int close = FindMatchingCloseParen(tokens, i, errors); if (close == 0) { return(nullMatches); } IMatchSet phrase = ParseTokens(tokens, errors, i + 1, close - 1); matchSet &= phrase; i = close; } else if (tokens[i] == "|") { int close = FindMatchingOrEnd(tokens, i, errors); IMatchSet phrase = ParseTokens(tokens, errors, i + 1, close - 1); matchSet |= phrase; i = close; } else if (tokens[i] == ",") { int close = FindMatchingCommaEnd(tokens, i, errors); IMatchSet phrase = ParseTokens(tokens, errors, i + 1, close - 1); matchSet |= phrase; i = close; } else if (tokens[i] == "&") { // & is implied, but it is legal too. } else if (tokens[i] == ")") { errors.Add(string.Format("unexpected ')' at position " + i)); return(matchSet); } else if (tokens[i].StartsWith("$")) { IMatchSet result; if (!variables.TryGetValue(tokens[i].ToLower(), out result)) { // if the variable has not been evaluated, look in the persistent variable declaration // and see if it is there. If it is, evaluate it now. List <string> variableCommand = variableDefinitions[tokens[i].ToLower()]; if (variableCommand == null) { errors.Add("Cannot recognize variable " + tokens[i].ToLower()); } else { result = Assign(variableCommand, null); } } else { matchSet &= result; } } else { IMatchSet match = index.Matches(tokens[i], errors); matchSet &= match; } } if (matchSet == null) { return(nullMatches); } return(matchSet); }