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); }
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); }
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); }
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); }