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