private static Expression ParseAtomicExpression(string expressionsSequence) { string prettyExpressionsSequence = expressionsSequence.Trim(); string COMPARISON_MARK_REGEX = $@"([<>]|!=|==)"; string FLOAT_REGEX = @"-?[0-9]*(?:\.[0-9]*)?"; string STRING_COMPARISON_REGEX = $"^\\$(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*(?<cmp>{COMPARISON_MARK_REGEX})\\s*\".*\"$"; string FLOAT_COMPARISON_REGEX = $"^\\$(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*(?<cmp>{COMPARISON_MARK_REGEX})\\s*(?<arg>{FLOAT_REGEX})\\s*$"; string INTERVAL_REGEX = $"^\\$(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*in\\s*\\[(?<min>{FLOAT_REGEX})\\s*,(?<max>{FLOAT_REGEX})\\s*\\]$"; Expression expression = null; if (Regex.IsMatch(prettyExpressionsSequence, STRING_COMPARISON_REGEX)) { int firstQuotePosition = prettyExpressionsSequence.IndexOf("\""); int lastQuotePosition = prettyExpressionsSequence.LastIndexOf("\""); int start = firstQuotePosition + 1; int len = lastQuotePosition - start; string possibleString = prettyExpressionsSequence.Substring(start, len); if (BracketedConfigProcessor.AssertValidString(possibleString)) { Match m = Regex.Match(prettyExpressionsSequence, STRING_COMPARISON_REGEX); if (m.Length != 0) { string cmp = m.Groups["cmp"].Value; string comparisonType = null; switch (cmp) { case "<": comparisonType = "lt"; break; case ">": comparisonType = "gt"; break; case "!=": comparisonType = "ne"; break; case "==": comparisonType = "eq"; break; } if (comparisonType != null) { expression = new Comparison(m.Groups["var"].Value, possibleString, comparisonType); } } } } else if (Regex.IsMatch(prettyExpressionsSequence, FLOAT_COMPARISON_REGEX)) { Match m = Regex.Match(prettyExpressionsSequence, FLOAT_COMPARISON_REGEX); if (m.Length != 0) { string cmp = m.Groups["cmp"].Value; string comparison_type = null; switch (cmp) { case "<": comparison_type = "lt"; break; case ">": comparison_type = "gt"; break; case "!=": comparison_type = "ne"; break; case "==": comparison_type = "eq"; break; } if (comparison_type != null) { expression = new Comparison(m.Groups["var"].Value, m.Groups["arg"].Value, comparison_type); } } } else if (Regex.IsMatch(prettyExpressionsSequence, INTERVAL_REGEX)) { Match m = Regex.Match(prettyExpressionsSequence, INTERVAL_REGEX); if (m.Length != 0) { expression = new Interval(m.Groups["var"].Value, m.Groups["min"].Value, m.Groups["max"].Value); } } return(expression); }
private static Expression ParseAtomicExpression(string expressionsSequence) { string prettyExpressionsSequence = expressionsSequence.Trim(); string COMPARISON_MARK_REGEX = $@"([<>]|!=|==)"; string FLOAT_REGEX = @"-?[0-9]*(?:\.[0-9]*)?"; string STRING_COMPARISON_REGEX = $"^\\$(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*(?<cmp>{COMPARISON_MARK_REGEX})\\s*\".*\"$"; string FLOAT_COMPARISON_REGEX = $"^\\$(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*(?<cmp>{COMPARISON_MARK_REGEX})\\s*(?<arg>{FLOAT_REGEX})\\s*$"; string INTERVAL_REGEX = $"^\\$(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*in\\s*\\[(?<min>{FLOAT_REGEX})\\s*,(?<max>{FLOAT_REGEX})\\s*\\]$"; Expression expression = null; try { if (Regex.IsMatch(prettyExpressionsSequence, STRING_COMPARISON_REGEX)) { int firstQuotePosition = prettyExpressionsSequence.IndexOf("\""); int lastQuotePosition = prettyExpressionsSequence.LastIndexOf("\""); int start = firstQuotePosition + 1; int len = lastQuotePosition - start; string possibleString = prettyExpressionsSequence.Substring(start, len); BracketedConfigProcessor.AssertValidString(possibleString); Match m = Regex.Match(prettyExpressionsSequence, STRING_COMPARISON_REGEX); if (m.Length == 0) { throw new ExpressionParseException(); } string cmp = m.Groups["cmp"].Value; string comparison_type; if (cmp == "<") { comparison_type = "lt"; } else if (cmp == ">") { comparison_type = "gt"; } else if (cmp == "!=") { comparison_type = "ne"; } else if (cmp == "==") { comparison_type = "eq"; } else { throw new ExpressionParseException(); } expression = new Comparison(m.Groups["var"].Value, possibleString, comparison_type); //Console.WriteLine($"parsed {expressionsSequence} as comparison {m.Groups["var"].Value} {comparison_type} {possibleString}"); } else if (Regex.IsMatch(prettyExpressionsSequence, FLOAT_COMPARISON_REGEX)) { Match m = Regex.Match(prettyExpressionsSequence, FLOAT_COMPARISON_REGEX); if (m.Length == 0) { throw new ExpressionParseException(); } string cmp = m.Groups["cmp"].Value; string comparison_type; switch (cmp) { case "<": comparison_type = "lt"; break; case ">": comparison_type = "gt"; break; case "!=": comparison_type = "ne"; break; case "==": comparison_type = "eq"; break; default: throw new ExpressionParseException(); } expression = new Comparison(m.Groups["var"].Value, m.Groups["arg"].Value, comparison_type); //Console.WriteLine($"parsed {expressionsSequence} as comparison comparison {m.Groups["var"].Value} {comparison_type} {m.Groups["arg"].Value}"); } else if (Regex.IsMatch(prettyExpressionsSequence, INTERVAL_REGEX)) { Match m = Regex.Match(prettyExpressionsSequence, INTERVAL_REGEX); if (m.Length == 0) { throw new ActionParseException(); //TODO fix } expression = new Interval(m.Groups["var"].Value, m.Groups["min"].Value, m.Groups["max"].Value); //Console.WriteLine($"parsed {expressionsSequence} as interval check if {m.Groups["var"].Value} in [{m.Groups["min"].Value},{m.Groups["max"].Value}]"); } else { throw new ExpressionParseException(); } return(expression); } catch { throw new ExpressionParseException(); } }
private static Action ParseAtomicAction(string actionSequence) { string ASSIGNEMENT_STRING_REGEX = $"^(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*=\\s*\".*\"$"; string ASSIGNEMENT_REGEX = $"^(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*=\\s*(?<value>\\S+)$"; string CLEAR_REGEX = $"^clear\\s+\\$(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})$"; string SAY_REGEX = $"^say\\s+((?<probability>\\d*)\\s+)?\".*\"$"; string SAY_FAST_REGEX = $"^sayFast\\s+((?<probability>\\d*)\\s+)?\".*\"$"; string SHUT_UP_REGEX = $"^shutUp$"; string GPIO_REGEX = $"^GPIO\\s+((?<probability>\\d*)\\s+)?(?<signal>([10],)*[10])\\s+(?<time>\\d+)$"; string EXTERNAL_ACTION_NAME_REGEX_PATTERN = BracketedConfigProcessor.VARNAME_REGEX_PATTERN; string EXTERNAL_REGEX = $"^ext:(?<method>{EXTERNAL_ACTION_NAME_REGEX_PATTERN})\\s+\".*\"$"; string PLAY_DELAY_REGEX = $"^play\\s+((?<probability>\\d*)\\s+)?\".*\"(\\s+(?<time>\\d+))?$"; string STAY_ACTIVE_REGEX = $"^stayActive$"; string COMPARE_ANSWERS_REGEX = $"^compareAnswers\\s+(?<goodAnswer>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s+(?<realAnswer>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})$"; string QUIZ_REGEX = $"^\\s*quiz\\s+(\\\"(?<filename>.*)\\\"\\s*)((?<randomOrder>randomOrder)\\s*)?((?<length>\\d+\\:\\d+))?\\s*$"; Action action = null; string prettyActionSequence = actionSequence.Trim(); int probability; try { if (Regex.IsMatch(prettyActionSequence, ASSIGNEMENT_STRING_REGEX)) { int firstQuotePosition = prettyActionSequence.IndexOf("\""); int lastQuotePosition = prettyActionSequence.LastIndexOf("\""); int start = firstQuotePosition + 1; int len = lastQuotePosition - start; string possibleString = prettyActionSequence.Substring(start, len); Match m = Regex.Match(prettyActionSequence, ASSIGNEMENT_STRING_REGEX); if (BracketedConfigProcessor.AssertValidString(possibleString)) { if (m.Length != 0) { action = new Assign(m.Groups["var"].Value, possibleString); } } } else if (Regex.IsMatch(prettyActionSequence, ASSIGNEMENT_REGEX)) { Match m = Regex.Match(prettyActionSequence, ASSIGNEMENT_REGEX); if (m.Length != 0) { action = new Assign(m.Groups["var"].Value, m.Groups["value"].Value); } } else if (Regex.IsMatch(prettyActionSequence, CLEAR_REGEX)) { Match m = Regex.Match(prettyActionSequence, CLEAR_REGEX); if (m.Length != 0) { action = new Clear(m.Groups["var"].Value); } } else if (Regex.IsMatch(prettyActionSequence, SAY_REGEX)) { int firstQuotePosition = prettyActionSequence.IndexOf("\""); int lastQuotePosition = prettyActionSequence.LastIndexOf("\""); int start = firstQuotePosition + 1; int len = lastQuotePosition - start; string possibleString = prettyActionSequence.Substring(start, len); Match m = Regex.Match(prettyActionSequence, SAY_REGEX); if (m.Length != 0 && m.Groups["probability"].Value.Length != 0) { probability = Int32.Parse(m.Groups["probability"].Value); } else { probability = 100; } if (BracketedConfigProcessor.AssertValidString(possibleString)) { action = new Say(possibleString, probability); } } else if (Regex.IsMatch(prettyActionSequence, SAY_FAST_REGEX)) { int firstQuotePosition = prettyActionSequence.IndexOf("\""); int lastQuotePosition = prettyActionSequence.LastIndexOf("\""); int start = firstQuotePosition + 1; int len = lastQuotePosition - start; string possibleString = prettyActionSequence.Substring(start, len); Match m = Regex.Match(prettyActionSequence, SAY_REGEX); if (m.Length != 0 && m.Groups["probability"].Value.Length != 0) { probability = Int32.Parse(m.Groups["probability"].Value); } else { probability = 100; } if (BracketedConfigProcessor.AssertValidString(possibleString)) { action = new SayFast(possibleString, probability); } } else if (Regex.IsMatch(prettyActionSequence, SHUT_UP_REGEX)) { action = new ShutUp(); } else if (Regex.IsMatch(prettyActionSequence, STAY_ACTIVE_REGEX)) { action = new StayActive(); } else if (Regex.IsMatch(prettyActionSequence, COMPARE_ANSWERS_REGEX)) { Match m = Regex.Match(prettyActionSequence, COMPARE_ANSWERS_REGEX); if (m.Length != 0) { action = new CompareAnswers(m.Groups["goodAnswer"].Value, m.Groups["realAnswer"].Value); } } else if (Regex.IsMatch(prettyActionSequence, PLAY_DELAY_REGEX)) { int firstQuotePosition = prettyActionSequence.IndexOf("\""); int lastQuotePosition = prettyActionSequence.LastIndexOf("\""); int start = firstQuotePosition + 1; int len = lastQuotePosition - start; string possibleString = prettyActionSequence.Substring(start, len); Match m = Regex.Match(prettyActionSequence, PLAY_DELAY_REGEX); if (m.Length != 0 && m.Groups["probability"].Value.Length != 0) { probability = Int32.Parse(m.Groups["probability"].Value); } else { probability = 100; } Match m2 = Regex.Match(prettyActionSequence, PLAY_DELAY_REGEX); if (m2.Length != 0 && m2.Groups["time"].Value.Length != 0) { var time = Int32.Parse(m.Groups["time"].Value); if (BracketedConfigProcessor.AssertValidString(possibleString)) { action = new Play(possibleString, probability, time); } } else { if (BracketedConfigProcessor.AssertValidString(possibleString)) { action = new Play(possibleString, probability); } } } else if (Regex.IsMatch(prettyActionSequence, QUIZ_REGEX)) { int firstQuotePosition = prettyActionSequence.IndexOf("\""); int lastQuotePosition = prettyActionSequence.LastIndexOf("\""); int start = firstQuotePosition + 1; int len = lastQuotePosition - start; string possibleString = prettyActionSequence.Substring(start, len); if (BracketedConfigProcessor.AssertValidString(possibleString)) { action = new Quiz(possibleString); Match m = Regex.Match(prettyActionSequence, QUIZ_REGEX); ((Quiz)action).randomOrdered = m.Length != 0 && m.Groups["randomOrder"].Value.Length != 0; // Match m = Regex.Match(prettyActionSequence, QUIZ_REGEX); if (m.Length != 0 && m.Groups["length"].Value.Length != 0) { var lengths = m.Groups["length"].Value.Split(':'); ((Quiz)action).lengthLowerBound = int.Parse(lengths[0]); ((Quiz)action).lengthUpperBound = int.Parse(lengths[1]); } } } else if (Regex.IsMatch(prettyActionSequence, GPIO_REGEX)) { Match m = Regex.Match(prettyActionSequence, GPIO_REGEX); if (m.Length != 0 && m.Groups["probability"].Value.Length != 0) { probability = Int32.Parse(m.Groups["probability"].Value); } else { probability = 100; } if (m.Length != 0) { action = new GPIO(m.Groups["signal"].Value.Split(',', ' ') .Select(Int32.Parse).ToList(), Int32.Parse(m.Groups["time"].Value), probability); } } else if (Regex.IsMatch(prettyActionSequence, EXTERNAL_REGEX)) { int firstQuotePosition = prettyActionSequence.IndexOf("\""); int lastQuotePosition = prettyActionSequence.LastIndexOf("\""); int start = firstQuotePosition + 1; int len = lastQuotePosition - start; string possibleString = prettyActionSequence.Substring(start, len); if (BracketedConfigProcessor.AssertValidString(possibleString)) { Match m = Regex.Match(prettyActionSequence, EXTERNAL_REGEX); if (m.Length != 0) { action = new Extension(m.Groups["method"].Value, possibleString); } } } } catch { action = null; } return(action); }
private static Action ParseAtomicAction(string actionSequence) { string ASSIGNEMENT_STRING_REGEX = $"^(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*=\\s*\\\"(?<value>\\S+)\\\"$"; string ASSIGNEMENT_REGEX = $"^(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*=\\s*(?<value>\\S+)$"; string CLEAR_REGEX = $"^clear\\s+\\$(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})$"; string SAY_REGEX = $"^say\\s+\".*\"$"; string GPIO_REGEX = $"^GPIO\\s+(?<signal>([10],)*[10])\\s+(?<time>\\d+)$"; string EXTERNAL_ACTION_NAME_REGEX_PATTERN = BracketedConfigProcessor.VARNAME_REGEX_PATTERN; string EXTERNAL_REGEX = $"^ext:(?<method>{EXTERNAL_ACTION_NAME_REGEX_PATTERN})\\s+\".*\"$"; Action action = null; string prettyActionSequence = actionSequence.Trim(); try { if (Regex.IsMatch(prettyActionSequence, ASSIGNEMENT_STRING_REGEX)) { Match m = Regex.Match(prettyActionSequence, ASSIGNEMENT_STRING_REGEX); if (m.Length == 0) { throw new ActionParseException(); } action = new Assign(m.Groups["var"].Value, m.Groups["value"].Value); //Console.WriteLine($"parsed {actionSequence} as assignement {m.Groups["var"].Value}:={m.Groups["value"].Value}"); } else if (Regex.IsMatch(prettyActionSequence, ASSIGNEMENT_REGEX)) { Match m = Regex.Match(prettyActionSequence, ASSIGNEMENT_REGEX); if (m.Length == 0) { throw new ActionParseException(); } action = new Assign(m.Groups["var"].Value, m.Groups["value"].Value); //Console.WriteLine($"parsed {actionSequence} as assignement {m.Groups["var"].Value}:={m.Groups["value"].Value}"); } else if (Regex.IsMatch(prettyActionSequence, CLEAR_REGEX)) { Match m = Regex.Match(prettyActionSequence, CLEAR_REGEX); if (m.Length == 0) { throw new ActionParseException(); } action = new Clear(m.Groups["var"].Value); //Console.WriteLine($"parsed {actionSequence} as clear {m.Groups["var"].Value}"); } else if (Regex.IsMatch(prettyActionSequence, SAY_REGEX)) { int firstQuotePosition = prettyActionSequence.IndexOf("\""); int lastQuotePosition = prettyActionSequence.LastIndexOf("\""); int start = firstQuotePosition + 1; int len = lastQuotePosition - start; string possibleString = prettyActionSequence.Substring(start, len); BracketedConfigProcessor.AssertValidString(possibleString); action = new Say(possibleString); //Console.WriteLine($"parsed {actionSequence} as say {possibleString}"); } else if (Regex.IsMatch(prettyActionSequence, GPIO_REGEX)) { Match m = Regex.Match(prettyActionSequence, GPIO_REGEX); if (m.Length == 0) { throw new ActionParseException(); } action = new GPIO(m.Groups["signal"].Value.Split(',', ' ').Select(Int32.Parse).ToList(), Int32.Parse(m.Groups["time"].Value)); } else if (Regex.IsMatch(prettyActionSequence, EXTERNAL_REGEX)) { int firstQuotePosition = prettyActionSequence.IndexOf("\""); int lastQuotePosition = prettyActionSequence.LastIndexOf("\""); int start = firstQuotePosition + 1; int len = lastQuotePosition - start; string possibleString = prettyActionSequence.Substring(start, len); BracketedConfigProcessor.AssertValidString(possibleString); Match m = Regex.Match(prettyActionSequence, EXTERNAL_REGEX); if (m.Length == 0) { throw new ActionParseException(); } action = new Extension(m.Groups["method"].Value, possibleString); //Console.WriteLine($"parsed {actionSequence} as external {m.Groups["method"].Value} {possibleString}"); } else { throw new ActionParseException(); } if (action == null) { throw new ActionParseException(); } return(action); } catch { throw new ActionParseException(); } }