public static BaseGraphCommand Parse(string inputCommand) { List <BaseGraphTokenMatch> lineTokens = new List <BaseGraphTokenMatch>(); string startLine = inputCommand; BaseGraphToken firstToken = BaseGraphToken.Undefined; bool first = true; while (!String.IsNullOrEmpty(inputCommand)) { var match = MatchTokens(inputCommand, firstToken); if (match == null) { throw new InvalidOperationException("Invalid token at line \"" + inputCommand + "\""); } inputCommand = match.remainingText; if (first) { firstToken = match.token; } lineTokens.Add(match); first = false; } if (lineTokens.Count == 0) { throw new InvalidOperationException("Invalid empty command: " + inputCommand); } return(BuildCommand(lineTokens, startLine)); }
//returns all possible token matches static BaseGraphTokenMatch MatchTokens(string line, BaseGraphToken firstToken) { foreach (var tokenDef in GetTokenDefinitionsForStartToken(firstToken)) { BaseGraphTokenMatch ret = tokenDef.Match(line.Trim()); if (ret != null) { if (debug) { Debug.Log("Token " + ret.token + " maches value: |" + ret.value + "|, remainingText: |" + ret.remainingText + "|"); } return(ret); } } if (debug) { Debug.Log("No token maches found with line: " + line); } return(null); }
public BaseGraphTokenDefinition(BaseGraphToken graphToken, string regexString) { this.token = graphToken; this.regex = new Regex(regexString); }
static IEnumerable <BaseGraphTokenDefinition> GetTokenDefinitionsForStartToken(BaseGraphToken firstToken) { if (firstToken == BaseGraphToken.Undefined) { foreach (var tokenDef in tokenDefinitions) { yield return(tokenDef); } yield break; } var seq = BaseGraphValidCommandTokenSequence.validSequences.FirstOrDefault(s => s.requiredTokens.First() == firstToken); List <BaseGraphToken> tokens = new List <BaseGraphToken>(); foreach (var token in seq.requiredTokens) { tokens.Add(token); } if (seq.options != null) { foreach (var token in seq.options) { tokens.Add(token); } } foreach (var tokenDef in tokenDefinitions) { if (tokens.Contains(tokenDef.token)) { yield return(tokenDef); } } }