public List <SearchTask> Execute(IList <SearchTask> searchTask, string storeResultDir) { var allTasks = new List <SearchTask>(searchTask); var groups = searchTask.GroupBy(i => i.SearchDir); foreach (var group in groups) { var tasks = group.ToList(); somethingChanged: BaseConverter.Founded.Clear(); List <SearchTask> allTasksPart; var projName = DirNameRegex.Match(tasks.First().SearchDir).Value; projName = CashParser.ToTitleCase(projName); var classes = RecursiveSearch(tasks, storeResultDir, true, out allTasksPart, projName); GenerateTestFileForApi(classes, tasks, projName); if (TaskHelper.AddTask(allTasks, allTasksPart)) { tasks = allTasksPart; goto somethingChanged; } } return(allTasks); }
public string PrintParsedClass(ParsedClass parsedClass, string projName, string absPathToFile, string searchDir) { var sb = new StringBuilder(); PrinNamespace(sb, parsedClass, projName); AddClassName(sb, parsedClass, absPathToFile, 4); string templateName = null; if (parsedClass.IsTemplate) { templateName = CashParser.Unpack(parsedClass.Template, 1); } var doc = string.Empty; if (parsedClass.ObjectType == ObjectType.Api && !string.IsNullOrEmpty(searchDir)) { doc = File.ReadAllText(searchDir + absPathToFile); } foreach (var t in parsedClass.Fields) { if (parsedClass.ObjectType == ObjectType.Api && !string.IsNullOrEmpty(doc) && !doc.Contains($"({t.CppName})")) { continue; } PrintParsedElements(sb, parsedClass, t, 8, templateName); } CloseTag(sb, 4); CloseTag(sb, 0); return(sb.ToString()); }
public ParsedClass Parse(string text, bool isApi) { var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.None); var preParseClass = CashParser.TryParseClass(lines, isApi); ExtendPreParsedClass(preParseClass); return(preParseClass); }
protected override PreParsedElement TryParseElement(PreParsedElement preParsedElement, string templateName) { var test = preParsedElement.CppText; if (!FuncRegex.IsMatch(test))// || VoidFuncRegex.IsMatch(test)) { return(null); } var cppTypeMatch = FuncTypeRegex.Match(test); if (!cppTypeMatch.Success) { return(null); } var nameMatch = FuncNameRegex.Match(test); if (!nameMatch.Success) { return(null); } var paramsMatch = ParamsRegex.Match(test); if (!paramsMatch.Success) { return(null); } try { var field = new ParsedFunc(); field.Type = GetKnownTypeOrDefault(cppTypeMatch.Value, templateName); field.Name = CashParser.ToTitleCase(nameMatch.Value); field.CppName = nameMatch.Value; field.Params = TryParseParams(paramsMatch.Value); field.Comment = preParsedElement.Comment; field.MainComment = preParsedElement.MainComment; return(field); } catch (Exception e) { } var error = new ParsedFunc(); error.Comment = "Parsing error: { preParsedElement.CppText}"; return(error); }
private ParsedType GetKnownCompositType(string type) { if (type.StartsWith("map<")) //TODO: research is needed { return new ParsedType { CppName = type, Name = "object" } } ; var unpacked = CashParser.Unpack(type, 1); ParsedType parsedType; if (type.StartsWith("array<")) //TODO: research is needed { var countPart = unpacked.LastIndexOf(','); if (countPart > 0) { unpacked = unpacked.Remove(countPart); parsedType = GetKnownTypeOrDefault(unpacked); parsedType.IsArray = true; return(parsedType); } } if (IsArray(type)) { parsedType = GetKnownTypeOrDefault(unpacked); parsedType.IsArray = true; return(parsedType); } var chArray = CashParser.SplitParams(unpacked); var tmpl = type.Remove(type.IndexOf('<')); parsedType = tmpl.Equals("pair") ? new ParsedType { Name = "KeyValuePair" } : GetKnownTypeOrDefault(tmpl); parsedType.IsTemplate = true; foreach (var item in chArray) { var ch = GetKnownTypeOrDefault(item); parsedType.Container.Add(ch); } return(parsedType); }
private void AddTypeToTask(string type) { if (!Founded.ContainsKey(type)) { Founded.Add(type, CashParser.ToTitleCase(type)); if (!UnknownTypes.Any(i => i.SearchLine.Equals(type) && string.IsNullOrEmpty(i.SearchDir))) { var task = new SearchTask { Converter = KnownConverter.StructConverter, SearchLine = type, }; UnknownTypes.Add(task); } } }
public ParsedClass FindAndParse(SearchTask searchTask, string projName, string[] extensions, bool isApi) { try { if (string.IsNullOrWhiteSpace(searchTask.SearchLine)) { return(null); } var classes = CashParser.FindAndParse(searchTask, projName, extensions, isApi); foreach (var parsedClass in classes) { ExtendPreParsedClass(parsedClass); foreach (var newTask in UnknownTypes) { if (string.IsNullOrEmpty(newTask.SearchDir)) { newTask.SearchDir = searchTask.SearchDir; } } if (!parsedClass.Fields.Any() && parsedClass.Inherit.Count == 1 && (parsedClass.Inherit[0].IsArray || _knownTypes.ContainsKey(parsedClass.Inherit[0].CppName))) { if (Founded.ContainsKey(parsedClass.CppName)) { Founded[parsedClass.CppName] = parsedClass.Inherit[0].Name; } else { Founded.Add(parsedClass.CppName, parsedClass.Inherit[0].Name); } UnknownTypes.Add(new SearchTask { SearchLine = parsedClass.CppName, Converter = KnownConverter.None }); } return(parsedClass); } } catch (Exception e) { throw new Exception($"{searchTask.SearchDir} | {searchTask.SearchLine}", e); } return(null); }
private void ExtendPreParsedClass(ParsedClass parsedClass) { if (parsedClass == null) { return; } if (!string.IsNullOrEmpty(parsedClass.CppName)) { parsedClass.Name = CashParser.ToTitleCase(parsedClass.CppName); } if (!string.IsNullOrEmpty(parsedClass.CppInherit)) { parsedClass.Inherit = new List <ParsedType> { GetKnownTypeOrDefault(parsedClass.CppInherit) } } ; if (parsedClass.CppConstructorParams != null && parsedClass.CppConstructorParams.Any()) { foreach (var itm in parsedClass.CppConstructorParams) { parsedClass.ConstructorParams.Add(TryParseParam(itm)); } } if (parsedClass.ObjectType != ObjectType.Enum) { string templateName = null; if (parsedClass.IsTemplate) { templateName = CashParser.Unpack(parsedClass.Template, 1); } for (var i = 0; i < parsedClass.Fields.Count; i++) { var preParsedElement = parsedClass.Fields[i]; parsedClass.Fields[i] = TryParseElement(preParsedElement, templateName); } } parsedClass.Fields.RemoveAll(i => i == null); }
protected List <ParsedParams> TryParseParams(string parameters) { parameters = ParamNormRegex.Replace(parameters, string.Empty); parameters = parameters.Trim(); if (string.IsNullOrEmpty(parameters)) { return(new List <ParsedParams>()); } var typeMatches = ParamTypeRegex.Matches(parameters); var nameMatches = ParamNameRegex.Matches(parameters); if (typeMatches.Count != nameMatches.Count) { throw new InvalidCastException(); } var rez = new List <ParsedParams>(); for (var i = 0; i < typeMatches.Count; i++) { var defaultValue = string.Empty; var name = nameMatches[i].Value; var eqv = name.IndexOf("=", StringComparison.Ordinal); if (eqv > -1) { defaultValue = name.Substring(eqv + 1).Trim(); name = name.Remove(eqv).Trim(); } var param = new ParsedParams() { CppName = name, Name = CashParser.ToTitleCase(name, false), Default = defaultValue, CppType = typeMatches[i].Value, Type = GetKnownTypeOrDefault(typeMatches[i].Value) }; rez.Add(param); } return(rez); }
public string PrintParsedClass(ParsedClass parsedClass, string projName, string absPathToFile) { var sb = new StringBuilder(); PrinNamespace(sb, parsedClass, projName); AddClassName(sb, parsedClass, absPathToFile, 4); string templateName = null; if (parsedClass.IsTemplate) { templateName = CashParser.Unpack(parsedClass.Template, 1); } foreach (var t in parsedClass.Fields) { PrintParsedElements(sb, parsedClass, t, 8, templateName); } CloseTag(sb, 4); CloseTag(sb, 0); return(sb.ToString()); }
private ParsedParams TryParseParam(string parameters) { parameters = ParamNormRegex.Replace(parameters, string.Empty); parameters = parameters.Trim(); if (string.IsNullOrEmpty(parameters)) { return(new ParsedParams()); } var typeMatche = ParamTypeRegex.Match(parameters); var nameMatche = ParamNameRegex.Match(parameters); if (!typeMatche.Success || !nameMatche.Success) { throw new InvalidCastException(); } var defaultValue = string.Empty; var name = nameMatche.Value; var eqv = name.IndexOf("=", StringComparison.Ordinal); if (eqv > -1) { defaultValue = name.Substring(eqv + 1).Trim(); name = name.Remove(eqv).Trim(); } var rez = new ParsedParams() { CppName = name, Name = CashParser.ToTitleCase(name, false), Default = defaultValue, CppType = typeMatche.Value, Type = GetKnownTypeOrDefault(typeMatche.Value) }; return(rez); }
protected ParsedType GetKnownTypeOrDefault(string type, string templateName = null) { type = type.Trim(); if (NamespacePref.IsMatch(type)) { type = NamespacePref.Replace(type, string.Empty); } if (type.StartsWith("optional<")) { type = CashParser.Unpack(type, 1); } if (type.StartsWith("static ")) { type = type.Remove(0, 7); } if (type.StartsWith("const ")) { type = type.Remove(0, 6); } if (type.EndsWith("<>")) { type = type.Remove(type.Length - 2); } if (type.IndexOf('<') > -1) { return(GetKnownCompositType(type)); } if (_knownTypes.ContainsKey(type)) { return new ParsedType { CppName = type, Name = _knownTypes[type] } } ; if (!string.IsNullOrEmpty(templateName) && type.Equals(templateName, StringComparison.OrdinalIgnoreCase)) { return new ParsedType { CppName = type, Name = templateName } } ; if (Founded.ContainsKey(type)) { return(new ParsedType { CppName = type, Name = Founded[type] }); } if (!NotNameChar.IsMatch(type)) { AddTypeToTask(type); return(new ParsedType { CppName = type, Name = CashParser.ToTitleCase(type) }); } return(new ParsedType { CppName = type, Name = "object" }); }
protected override PreParsedElement TryParseElement(PreParsedElement preParsedElement, string templateName) { var test = preParsedElement.CppText; if (string.IsNullOrEmpty(test)) { return(null); } if (test.StartsWith("static ")) { test = test.Remove(0, 7); } if (test.StartsWith("const ")) { test = test.Remove(0, 6); } if (!_fieldRules.IsMatch(test)) { return(null); } var parts = test.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 2) { return(null); } var cppType = parts[0]; if (_notTypeChar.IsMatch(cppType)) { return(null); } var name = parts[1].Trim(' ', ';'); if (NotNameChar.IsMatch(name)) { return(null); } var coment = string.Empty; if (parts.Length > 2) { coment = string.Join(" ", parts.Skip(2)).TrimStart(' ', '/', '<'); } var field = new PreParsedElement { Type = GetKnownTypeOrDefault(cppType, templateName), Name = CashParser.ToTitleCase(name), CppName = name, Comment = coment, MainComment = preParsedElement.MainComment }; return(field); }