예제 #1
0
        public static MatchesCondition Parse(List <Argument> arguments)
        {
            if (arguments.Count < 2)
            {
                return(null); // Not enough arguments
            }
            var condition = new MatchesCondition();

            int offset = condition.ParseNot(arguments);

            condition.LeftVariable = Variable.Parse(arguments[offset].GetAsText());
            if (condition.LeftVariable == null)
            {
                return(null);
            }

            condition.Matches = arguments[offset + 1].GetAsText();
            if (!MatchRegex.IsMatch(condition.Matches))
            {
                return(null); // Not a valid match syntax
            }
            condition.LastIndex = offset + 1;

            return(condition);
        }
예제 #2
0
        public Command.Command Filter(Command.Command command)
        {
            if (command.GetCommandName() != "if")
            {
                return(command);
            }

            // Remove the command name to make working with the arguments easier
            command.Arguments.RemoveAt(0);

            // Split if statement into individual conditions by seperating them at "&&"
            List <List <Argument> > ifConditionArguments = new List <List <Argument> > {
                new List <Argument>()
            };
            int pieceIndex = 0;

            foreach (Argument argument in command.Arguments)
            {
                string argText = argument.GetAsText();

                if (argText == "&&")
                {
                    ifConditionArguments.Add(new List <Argument>());
                    pieceIndex++;
                }
                else
                {
                    ifConditionArguments[pieceIndex].Add(new Argument(argText));
                }
            }

            List <Condition> conditions = new List <Condition>();

            foreach (var arguments in ifConditionArguments)
            {
                // Matches condition
                Condition condition = MatchesCondition.Parse(arguments);

                // Operation condition
                if (condition == null)
                {
                    condition = OperationCondition.Parse(arguments);
                }

                if (condition == null)
                {
                    throw new Exception("Invalid if statement condition");
                }
                else
                {
                    conditions.Add(condition);
                }
            }

            // Get all the left over arguments; these will make up the command that will be ran if the conditions are met
            int             startIndex = conditions.Last().LastIndex + 1;
            List <Argument> leftOver   = new List <Argument>(command.Arguments.GetRange(startIndex, command.Arguments.Count - startIndex));

            Command.Command successCommand = new Command.Command(leftOver);

            // Replace existing command with an if token
            command.Arguments.Clear();
            command.Arguments.Add(new Argument(new IfToken(conditions)));
            command.Arguments.AddRange(successCommand.Arguments);

            return(command);
        }