Beispiel #1
0
        public static bool TryScan(string line, out Command scanned)
        {
            scanned = null;
            line    = line.Trim().TrimEnd(';');
            if (Regex.IsMatch(line, RegExWholeLine(AssignmentCommand.RegEx)))
            {
                if (AssignmentCommand.TryScan(CodeReader.FromText(line), out AssignmentCommand cmd))
                {
                    scanned = cmd;
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(EnumCommand.RegEx)) ||
                Regex.IsMatch(line, RegExWholeLine(GetPropertyCommand.RegEx)))
            {
                string firstPart  = line.Split('.')[0];
                string secondPart = line.Split('.')[1];
                Type   enumType   = Assembly.GetAssembly(typeof(NoneType)).GetTypes().FirstOrDefault(t => t.Name == firstPart);
                if (enumType != null)
                {
                    scanned = new EnumCommand(enumType, secondPart);
                    return(true);
                }
                else
                {
                    scanned = new GetPropertyCommand(firstPart, secondPart);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(SetPropertyCommand.RegEx)))
            {
                string obj      = line.Split('.')[0];
                string property = line.Split('.', ':')[1];
                string value    = line.Substring(line.IndexOf(property) + property.Length + 1);
                if (TryScan(value, out Command cmd))
                {
                    scanned = new SetPropertyCommand(obj, property, cmd);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(ConstructorCommand.RegEx)))
            {
                string type = line.Split(' ', '(')[1];
                if (TryScan(line.Substring(line.IndexOf(type) + type.Length), out Command cmd))
                {
                    var t = Assembly.GetAssembly(typeof(NoneType)).GetTypes().FirstOrDefault(ty => ty.Name == type);
                    scanned = new ConstructorCommand(t, (ParameterCommand)cmd);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(ParameterCommand.RegEx)))
            {
                List <Command> parameters = new List <Command>();
                foreach (var parameter in line.RemoveBrackets().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (TryScan(parameter.Trim(), out Command cmd))
                    {
                        parameters.Add(cmd);
                    }
                    else
                    {
                        throw new Exception("Some Error occured while passing parameters.");
                    }
                }
                scanned = new ParameterCommand(parameters.ToArray());
                return(true);
            }
            if (Regex.IsMatch(line, Command.RegExWholeLine(OperationalCommand.Regex)))
            {
                for (int i = 0; i < line.Length; i++)
                {
                    switch (line[i])
                    {
                    case '+':
                    {
                        if (TryScan(line.Remove(i), out Command left) && TryScan(line.Substring(i + 1), out Command right))
                        {
                            scanned = new OperationalCommand(Operation.Plus, left, right);
                            return(true);
                        }
                        break;
                    }

                    case '-':
                    {
                        if (TryScan(line.Remove(i), out Command left) && TryScan(line.Substring(i + 1), out Command right))
                        {
                            scanned = new OperationalCommand(Operation.Minus, left, right);
                            return(true);
                        }
                        break;
                    }

                    case '*':
                    {
                        if (TryScan(line.Remove(i), out Command left) && TryScan(line.Substring(i + 1), out Command right))
                        {
                            scanned = new OperationalCommand(Operation.Multiply, left, right);
                            return(true);
                        }
                        break;
                    }

                    case '/':
                    {
                        if (TryScan(line.Remove(i), out Command left) && TryScan(line.Substring(i + 1), out Command right))
                        {
                            scanned = new OperationalCommand(Operation.Divide, left, right);
                            return(true);
                        }
                        break;
                    }

                    case '%':
                    {
                        if (TryScan(line.Remove(i), out Command left) && TryScan(line.Substring(i + 1), out Command right))
                        {
                            scanned = new OperationalCommand(Operation.Modulo, left, right);
                            return(true);
                        }
                        break;
                    }

                    case '^':
                    {
                        if (TryScan(line.Remove(i), out Command left) && TryScan(line.Substring(i + 1), out Command right))
                        {
                            scanned = new OperationalCommand(Operation.Pow, left, right);
                            return(true);
                        }
                        break;
                    }

                    default:
                        break;
                    }
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(FunctionCallCommand.RegEx)))
            {
                string obj        = line.Split('.')[0];
                string method     = line.Split('.', '(')[1];
                string parameters = line.Substring(line.IndexOf(method) + method.Length);
                if (Command.TryScan(parameters, out Command cmd))
                {
                    scanned = new FunctionCallCommand(obj, method, (ParameterCommand)cmd);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(StyleCommand.RegEx)))
            {
                if (StyleValue.TryScan(line, out StyleValue styleValue))
                {
                    scanned = new StyleCommand(styleValue);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(StaticFunctionCallCommand.RegEx)))
            {
                if (StaticFunctionCallCommand.TryScan(line, out StaticFunctionCallCommand cmd))
                {
                    scanned = cmd;
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(ValueCommand.RegEx)))
            {
                scanned = new ValueCommand(line);
                return(true);
            }
            if (Regex.IsMatch(line, RegExWholeLine(CreateArrayCommand.RegEx)))
            {
                line = line.Trim('[', ']');
                if (int.TryParse(line, out int length))
                {
                    scanned = new CreateArrayCommand(length);
                    return(true);
                }
            }
            if (Regex.IsMatch(line, RegExWholeLine(RegExHelper.Variable)))
            {
                scanned = new VariableCommand(line);
                return(true);
            }
            return(false);
        }