Пример #1
0
        private static FileStructure CollectStructure(CommandFileReader reader)
        {
            FileStructure result = new FileStructure();

            string line = null;

            while (!reader.IsDone)
            {
                line = reader.NextLine();
                if (line.StartsWith("#"))
                {
                    continue;
                }
                if (Regex.IsMatch(line, RegexCommand))
                {
                    result.Add(CollectCommandStructure(reader.Copy(), out int skip));
                    reader.Skip(skip);
                    if (!reader.IsDone)
                    {
                        reader.Back();
                    }
                }
                else
                {
                    throw new PositionException(reader.GetLineObject(),
                                                new MalformedCodeException(RegexCommand));
                }
            }
            return(result);
        }
Пример #2
0
        private static void CheckVersion(CommandFileReader reader, int[] supportedVersions)
        {
            string          line        = null;
            CommandFileLine lastChecked = null;

            while (!reader.IsDone)
            {
                line = reader.NextLine();

                if (line.StartsWith("#ver"))
                {
                    if (lastChecked != null)
                    {
                        throw new MultipleDeclarationException(lastChecked, reader.GetLineObject());
                    }
                    lastChecked = reader.GetLineObject();
                    if (int.TryParse(line.Substring("#ver".Length), out int version))
                    {
                        if (!supportedVersions.Contains(version))
                        {
                            throw new WrongVersionException(supportedVersions, version);
                        }
                    }
                    else
                    {
                        throw new PositionException(lastChecked,
                                                    new MalformedCodeException(CreateVersionRegex(supportedVersions)));
                    }
                }
            }
        }
Пример #3
0
        public static void Validate(CommandFileReader reader, int[] supportedVersions)
        {
            reader.Reset();
            CheckVersion(reader, supportedVersions);
            reader.Reset();
            FileStructure structure = CollectStructure(reader);

            ValidateStructure(structure);
        }
Пример #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);
        }
Пример #5
0
 public CommandFileLine(string content, int line, CommandFileReader parent)
 {
     this.Content  = content;
     this.Line     = line;
     this.Parent   = parent;
     ContaintsCode = true;
     if (string.IsNullOrWhiteSpace(Sanitized))
     {
         ContaintsCode = false;
     }
 }
Пример #6
0
        public ConsoleCommand ParseCommand(CommandFileReader reader, CommandsFile commandsFile, Type targetClass, out int skip)
        {
            ConsoleCommand result = new ConsoleCommand();

            string line = reader.NextLine();

            if (Regex.IsMatch(line, RegexCommand))
            {
                result.Names = Regex.Match(line, @"\(" + CreateMulitpleRegex(RegexVariable) + @"\)").Value
                               .Trim('(', ')').Split(',').Select(s => s.Trim()).ToArray();
            }
            while (!reader.IsDone)
            {
                line = reader.NextLine();

                if (Regex.IsMatch(line, RegexParameter))
                {
                    result.Add(ParseParameter(reader.Copy(), commandsFile, out int _skip));
                    reader.Skip(_skip);
                    if (!reader.IsDone)
                    {
                        reader.Back();
                    }
                }
                else if (Regex.IsMatch(line, RegexExample))
                {
                    result.Add(ParseExample(reader.Copy(), commandsFile, out int _skip));
                    reader.Skip(_skip);
                    if (!reader.IsDone)
                    {
                        reader.Back();
                    }
                }
                else if (Regex.IsMatch(line, RegexCommandContents))
                {
                    string value = line.Substring(line.IndexOf(':') + 1).Trim();
                    result.SetCallback(targetClass.GetMethod(value));
                }
                else if (EndsBlock(line))
                {
                    break;
                }
                else
                {
                    throw new Exception();
                }
            }

            skip = reader.Position - reader.Start - 1;
            return(result);
        }
Пример #7
0
        private static string[] CollectExampleStructure(CommandFileReader reader, out int skip)
        {
            string[] result = null;

            string line = reader.NextLine();

            if (Regex.IsMatch(line, RegexExample))
            {
                string[] used = Regex.Match(line, @"\(" + CreateMulitpleRegex(RegexVariable) + @"\)").Value
                                .Trim('(', ')').Split(',').Select(s => s.Trim()).ToArray();
                if (used.Length > 0 && used[0] != string.Empty)
                {
                    result = used;
                }
            }
            else
            {
                throw new PositionException(reader.GetLineObject(),
                                            new MalformedCodeException(RegexExample));
            }
            while (!reader.IsDone)
            {
                line = reader.NextLine();

                if (Regex.IsMatch(line, RegexExampleExplanation))
                {
                }
                else if (Regex.IsMatch(line, RegexExampleLine))
                {
                }
                else if (EndsBlock(line))
                {
                    break;
                }
                else
                {
                    throw new PositionException(reader.GetLineObject(),
                                                new MalformedCodeException(RegexExampleExplanation, RegexExampleLine));
                }
            }
            skip = reader.Position - reader.Start - 1;

            return(result);
        }
Пример #8
0
        public CommandsFile Parse(string file, Type targetClass)
        {
            CommandFileReader reader = new CommandFileReader(file);
            CommandsFile      result = new CommandsFile(5);

            try
            {
                Validator.Validate(reader, new int[] { 5 });
            }
            catch (Exception e)
            {
                Console.Write(e);
                GameConsole.Error(e);
                return(null);
            }
            reader.Reset();
            string line = null;

            while (!reader.IsDone)
            {
                line = reader.NextLine();
                if (line.StartsWith("#"))
                {
                    continue;
                }
                if (Regex.IsMatch(line, RegexCommand))
                {
                    result.Add(ParseCommand(reader.Copy(), result, targetClass, out int skip));
                    reader.Skip(skip);
                    if (!reader.IsDone)
                    {
                        reader.Back();
                    }
                }
                else
                {
                    throw new Exception();
                }
            }

            result.Load();
            return(result);
        }
Пример #9
0
        public CommandExample ParseExample(CommandFileReader reader, CommandsFile commandsFile, out int skip)
        {
            CommandExample result = new CommandExample();

            string line = reader.NextLine();

            if (Regex.IsMatch(line, RegexExample))
            {
                string[] used = Regex.Match(line, @"\(" + CreateMulitpleRegex(RegexVariable) + @"\)").Value
                                .Trim('(', ')').Split(',').Select(s => s.Trim()).ToArray();
                if (used.Length > 0 && used[0] != string.Empty)
                {
                    commandsFile.Add(result, used);
                }
            }
            while (!reader.IsDone)
            {
                line = reader.NextLine();

                if (Regex.IsMatch(line, RegexExampleExplanation))
                {
                    string value = line.Substring(line.IndexOf(':') + 1).Trim();
                    result.AddExplanation(value);
                }
                else if (Regex.IsMatch(line, RegexExampleLine))
                {
                    string value = line.Substring(line.IndexOf(':') + 1).Trim();
                    result.AddLine(value);
                }
                else if (EndsBlock(line))
                {
                    break;
                }
                else
                {
                    throw new Exception();
                }
            }
            skip = reader.Position - reader.Start - 1;
            return(result);
        }
Пример #10
0
        public Parameter ParseParameter(CommandFileReader reader, CommandsFile commandsFile, out int skip)
        {
            Parameter result = new Parameter();

            string line = reader.NextLine();

            if (Regex.IsMatch(line, RegexParameter))
            {
                result.Names = Regex.Match(line, @"\(" + CreateMulitpleRegex(RegexVariable) + @"\)").Value
                               .Trim('(', ')').Split(',').Select(s => s.Trim()).ToArray();
            }
            while (!reader.IsDone)
            {
                line = reader.NextLine();

                if (Regex.IsMatch(line, RegexParameterType))
                {
                    string value = line.Substring(line.IndexOf(':') + 1).Trim();
                    switch (value)
                    {
                    case "bool":
                        result.Type = ParameterType.Boolean;
                        break;

                    case "string":
                        result.Type = ParameterType.String;
                        break;

                    case "file":
                        result.Type = ParameterType.File;
                        break;

                    case "int":
                        result.Type = ParameterType.Integer;
                        break;

                    case "float":
                        result.Type = ParameterType.Float;
                        break;

                    case "command":
                        result.Type = ParameterType.Command;
                        break;

                    default:
                        throw new Exception("Can't happen because of Regex above.");
                    }
                }
                else if (Regex.IsMatch(line, RegexParameterFlags))
                {
                    string[] values = line.Substring(line.IndexOf(':') + 1).Trim().Split(',').Select(v => v.Trim()).ToArray();
                    foreach (var value in values)
                    {
                        switch (value)
                        {
                        case "short":
                            result.HasShort = true;
                            break;

                        case "exclusive":
                            result.SetOthers(null);
                            break;

                        case "meta":
                            result.IsMeta = true;
                            break;

                        default:
                            throw new NotImplementedException("No Flag with this name known: " + value);
                        }
                    }
                }
                else if (Regex.IsMatch(line, RegexParameterOthers))
                {
                    string[] others = line.Substring(line.IndexOf(':') + 1).Trim().Split(',').Select(o => o.Trim()).ToArray();
                    commandsFile.Add(result, others);
                }
                else if (EndsBlock(line))
                {
                    break;
                }
                else
                {
                    throw new Exception();
                }
            }

            skip = reader.Position - reader.Start - 1;
            return(result);
        }
Пример #11
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);
        }