Example #1
0
 private static void ValidateExampleStructure(CommandStructure parent, string[] example)
 {
     ParameterStructure[] usedParameters = new ParameterStructure[example.Length];
     for (int i = 0; i < example.Length; i++)
     {
         if (!parent.HasParameter(example[i]))
         {
             throw new ParameterDoesNotExistException(example, example[i]);
         }
         usedParameters[i] = parent.GetParameterByName(example[i]);
     }
     foreach (var usedParameter in usedParameters)
     {
         foreach (var otherParameter in usedParameters)
         {
             if (usedParameter == otherParameter)
             {
                 continue;
             }
             if (!usedParameter.Allows(otherParameter))
             {
                 throw new ParameterNotAllowedException(usedParameter, otherParameter);
             }
             if (!otherParameter.Allows(usedParameter))
             {
                 throw new ParameterNotAllowedException(otherParameter, usedParameter);
             }
         }
     }
 }
Example #2
0
        private static void ValidateCommandStructure(CommandStructure cmdStructure)
        {
            string[][] names = new string[cmdStructure.GetParameters().Count()][];
            int        i     = 0;

            foreach (var parameter in cmdStructure.GetParameters())
            {
                names[i] = parameter.Names;
                for (int j = 0; j < i; j++)
                {
                    foreach (var name in names[i])
                    {
                        if (names[j].Contains(name))
                        {
                            throw new NameAlreadyUsedException(name, parameter, cmdStructure[j]);
                        }
                    }
                }
                ValidateParameterStructure(parameter);
                i++;
            }
            foreach (var example in cmdStructure.GetExamples())
            {
                if (example != null)
                {
                    ValidateExampleStructure(cmdStructure, example);
                }
            }
        }
 public ParameterStructure(CommandStructure parent, string[] names)
 {
     Parent      = parent;
     Names       = names;
     HasShort    = false;
     Others      = null;
     IsExclusive = false;
 }
Example #4
0
        private static CommandStructure CollectCommandStructure(CommandFileReader reader, out int skip)
        {
            CommandStructure result = null;

            string line = reader.NextLine();

            if (Regex.IsMatch(line, @"\(" + RegexVariable + @"(\s*,\s*" + RegexVariable + @")*\)"))
            {
                string firstName = Regex.Match(line, @"\(" + RegexVariable + @"(\s*,\s*" + RegexVariable + @")*\)").Value
                                   .Trim('(', ')').Split(',')[0].Trim();
                result = new CommandStructure(firstName);
            }
            else
            {
                throw new PositionException(reader.GetLineObject(),
                                            new MalformedCodeException(@"\(" + RegexVariable + @"(\s*,\s*" + RegexVariable + @")*\)"));
            }
            while (!reader.IsDone)
            {
                line = reader.NextLine();
                if (Regex.IsMatch(line, RegexParameter))
                {
                    result.Add(CollectParameterStructure(reader.Copy(), result, out int _skip));
                    reader.Skip(_skip);
                    if (!reader.IsDone)
                    {
                        reader.Back();
                    }
                }
                else if (Regex.IsMatch(line, RegexExample))
                {
                    result.Add(CollectExampleStructure(reader.Copy(), out int _skip));
                    reader.Skip(_skip);
                    if (!reader.IsDone)
                    {
                        reader.Back();
                    }
                }
                else if (Regex.IsMatch(line, RegexCommandContents))
                {
                }
                else if (EndsBlock(line))
                {
                    break;
                }
                else
                {
                    throw new PositionException(reader.GetLineObject(),
                                                new MalformedCodeException(RegexParameter, RegexExample, RegexCommandContents));
                }
            }
            skip = reader.Position - reader.Start - 1;
            return(result);
        }
Example #5
0
        private static ParameterStructure CollectParameterStructure(CommandFileReader reader, CommandStructure parent, out int skip)
        {
            ParameterStructure result = null;

            string line = reader.NextLine();

            if (Regex.IsMatch(line, @"\(" + RegexVariable + @"(\s*,\s*" + RegexVariable + @")*\)"))
            {
                string[] names = Regex.Match(line, @"\(" + RegexVariable + @"(\s*,\s*" + RegexVariable + @")*\)").Value
                                 .Trim('(', ')').Split(',').Select(n => n.Trim()).ToArray();
                result = new ParameterStructure(parent, names);
            }
            else
            {
                throw new PositionException(reader.GetLineObject(),
                                            new MalformedCodeException(@"\(" + RegexVariable + @"(\s*,\s*" + RegexVariable + @")*\)"));
            }
            while (!reader.IsDone)
            {
                line = reader.NextLine();
                if (line.StartsWith("flags:"))
                {
                    string[] values = line.Substring(line.IndexOf(':') + 1).Trim().Split(',').Select(v => v.Trim()).ToArray();
                    foreach (var value in values)
                    {
                        result.FlagsPosition(reader.GetLineObject());
                        switch (value)
                        {
                        case "short":
                            result.SetShort();
                            break;

                        case "exclusive":
                            result.SetExclusive();
                            break;

                        case "meta":
                            //result.IsMeta = true;
                            throw new NotImplementedException();
                            break;

                        default:
                            throw new PositionException(reader.GetLineObject(),
                                                        new WrongValueException(value, new string[]
                            {
                                "short", "exclusive", "meta"
                            }));
                        }
                    }
                }
                else if (line.StartsWith("type:"))
                {
                    string value = line.Substring(line.IndexOf(':') + 1).Trim();
                    if (!Regex.IsMatch(line, RegexParameterType))
                    {
                        throw new PositionException(reader.GetLineObject(),
                                                    new WrongValueException(value, new string[]
                        {
                            "string", "file", "command", "bool", "int", "float"
                        }));
                    }
                }
                else if (Regex.IsMatch(line, RegexParameterOthers))
                {
                    if (result.IsExclusive)
                    {
                        throw new PositionException(reader.GetLineObject(),
                                                    new ExclusiveParameterException(result));
                    }
                    result.SetOther(line.Split(':')[1].Split(',').Select(o => o.Trim()).ToArray());
                }
                else if (EndsBlock(line))
                {
                    break;
                }
                else
                {
                    throw new PositionException(reader.GetLineObject(),
                                                new MalformedCodeException(RegexParameterFlags, RegexParameterOthers, RegexParameterType));
                }
            }

            skip = reader.Position - reader.Start - 1;
            return(result);
        }
Example #6
0
 internal void Add(CommandStructure commandStructure)
 {
     commands.Add(commandStructure);
 }