Exemple #1
0
        private PreParsedElement TryParseEnum(IList <string> lines, int index, out int end)
        {
            end = index + 1;
            var text  = lines[index].Trim();
            var parts = text.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length == 0)
            {
                return(null);
            }

            var name   = parts[0];
            var coment = string.Empty;

            if (parts.Length > 1)
            {
                coment = string.Join(" ", parts.Skip(2)).TrimStart(' ', '/', '<');
            }

            var field = new PreParsedElement
            {
                Name    = ToTitleCase(name),
                CppName = name,
                Comment = coment
            };

            return(field);
        }
Exemple #2
0
        private string GetCommentString(PreParsedElement element)
        {
            var com = string.Empty;

            if (!string.IsNullOrWhiteSpace(element.MainComment))
            {
                com = commClearRegex.Replace(element.MainComment, string.Empty);

                if (!string.IsNullOrWhiteSpace(element.Comment))
                {
                    com = $"{com} ({element.Comment})";
                }
            }
            else if (!string.IsNullOrWhiteSpace(element.Comment))
            {
                com = element.Comment;
            }

            if (!string.IsNullOrWhiteSpace(com))
            {
                com = com.Replace("\r\n", " ").Replace("  ", " ").Trim();
            }

            return(com);
        }
Exemple #3
0
        protected override PreParsedElement TryParseElement(PreParsedElement preParsedElement, string templateName, ObjectType objectType)
        {
            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);
        }
Exemple #4
0
        protected override PreParsedElement TryParseElement(PreParsedElement preParsedElement, string templateName, ObjectType objectType)
        {
            var test = preParsedElement.CppText;

            var field = new ParsedFunc();

            field.Type        = GetKnownTypeOrDefault($"{test}_return");
            field.Name        = _cashParser.ToTitleCase(test);
            field.CppName     = test;
            field.Params      = TryParseParams($"{test}_args args");
            field.Comment     = preParsedElement.Comment;
            field.MainComment = preParsedElement.MainComment;
            return(field);
        }
Exemple #5
0
        protected override PreParsedElement TryParseElement(PreParsedElement preParsedElement, string templateName, ObjectType objectType)
        {
            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);
        }
Exemple #6
0
        private void PrintParsedElements(StringBuilder sb, ParsedClass parsedClass, PreParsedElement parsedElement, int indentCount, string templateName)
        {
            var indent = new string(' ', indentCount);

            sb.AppendLine();

            Comment briefComment = null;
            var     parsedFunc   = parsedElement as ParsedFunc;

            if (parsedFunc != null)
            {
                briefComment = Comment.TryParseMainComment(parsedFunc.MainComment);
                if (briefComment != null && !briefComment.IsEmpty())
                {
                    parsedFunc.MainComment = Comment.RemoveBriefFromMainComment(parsedFunc.MainComment);
                }
            }

            //if (!string.IsNullOrEmpty(parsedElement.MainComment))
            //{
            //    sb.Append(indent);
            //    sb.AppendLine(parsedElement.MainComment);
            //    sb.AppendLine();
            //}

            var comment = parsedElement.Comment ?? string.Empty;

            //comment = comment.Replace("\\", $@"/// {Environment.NewLine}");

            sb.AppendLine($"{indent}/// <summary>");
            sb.AppendLine($"{indent}/// API name: {parsedElement.CppName}");
            if (!string.IsNullOrWhiteSpace(briefComment?.Brief))
            {
                sb.AppendLine($"{indent}/// {briefComment.Brief.Replace(Environment.NewLine, $"{Environment.NewLine}{indent}/// ")}");
                sb.AppendLine($"{indent}///");
            }
            if (parsedClass.ObjectType == ObjectType.Api && !string.IsNullOrWhiteSpace(parsedElement.CppName) && MethodDescriptions.ContainsKey(parsedElement.CppName))
            {
                var text = MethodDescriptions[parsedElement.CppName];
                if (!string.IsNullOrWhiteSpace(text) && (briefComment == null || !briefComment.IsBriefContainText(text)))
                {
                    sb.AppendLine($"{indent}/// *{text.Trim()}");
                }
            }
            sb.AppendLine($"{indent}/// {TypeCorrection(comment)}");
            sb.AppendLine($"{indent}/// </summary>");

            if (parsedFunc != null)
            {
                foreach (var itm in parsedFunc.Params)
                {
                    sb.Append($"{indent}/// <param name=\"{itm.Name}\">API type: {TypeCorrection(itm.CppType)}");

                    if (briefComment != null && briefComment.Params.ContainsKey(itm.CppName))
                    {
                        sb.Append(briefComment.Params[itm.CppName].Replace(Environment.NewLine, $"{Environment.NewLine}{indent}/// "));
                    }

                    sb.AppendLine("</param>");
                }

                if (parsedClass.ObjectType == ObjectType.Api)
                {
                    sb.AppendLine($"{indent}/// <param name=\"token\">Throws a <see cref=\"T:System.OperationCanceledException\" /> if this token has had cancellation requested.</param>");
                }
            }

            if (!string.IsNullOrWhiteSpace(briefComment?.Return) || !string.IsNullOrEmpty(parsedElement.Type?.CppName))
            {
                sb.Append($"{indent}/// <returns>");
                if (!string.IsNullOrEmpty(parsedElement.Type?.CppName))
                {
                    sb.Append($"API type: {TypeCorrection(parsedElement.Type.CppName)}");
                    if (!string.IsNullOrWhiteSpace(briefComment?.Return))
                    {
                        sb.Append(" ");
                    }
                }

                if (!string.IsNullOrWhiteSpace(briefComment?.Return))
                {
                    sb.Append($"{briefComment.Return.Replace(Environment.NewLine, $"{Environment.NewLine}{indent}/// ")}");
                }
                sb.AppendLine("</returns>");
            }


            if (parsedClass.ObjectType == ObjectType.Api)
            {
                sb.AppendLine($"{indent}/// <exception cref=\"T:System.OperationCanceledException\">The token has had cancellation requested.</exception>");
            }

            var type = GetTypeForPrint(parsedElement.Type, templateName);

            if (parsedFunc != null)
            {
                sb.AppendLine($"{indent}public JsonRpcResponse{(type.Equals("void", StringComparison.OrdinalIgnoreCase) ? string.Empty : $"<{type}>")} {parsedElement.Name}({string.Join(", ", parsedFunc.Params)}{(parsedClass.ObjectType == ObjectType.Api ? $"{(parsedFunc.Params.Any() ? ", " : string.Empty)}CancellationToken token" : string.Empty)})");
Exemple #7
0
 protected abstract PreParsedElement TryParseElement(PreParsedElement preParsedElement, string templateName, ObjectType objectType);
Exemple #8
0
        public virtual ParsedClass TryParseClass(SearchTask searchTask, IList <string> text, bool isApi)
        {
            if (!text.Any())
            {
                return(null);
            }

            int index;

            if (text.Count == 1)
            {
                var typedef = TryParseTypedefClass(text[0]);
                if (typedef != null)
                {
                    return(typedef);
                }
            }

            var result = new ParsedClass();

            if (isApi)
            {
                result.ObjectType = ObjectType.Api;
            }

            result.MainComment = TryParseComment(text, 0, out index);
            TryParseTemplate(result, text, index, out index);

            var headerSb = new StringBuilder();

            do
            {
                headerSb.Append(" " + text[index]);
            } while (!StartBodyRegex.IsMatch(text[index++]));

            var header = headerSb.ToString();
            var name   = TryParseClassName(header);

            result.CppName    = name;
            result.Name       = ToTitleCase(name);
            result.ObjectType = EnumRegex.IsMatch(header) ? ObjectType.Enum : result.ObjectType;

            var inherit = TryParseInherit(header).Trim();

            if (!string.IsNullOrEmpty(inherit))
            {
                result.CppInherit = inherit;
            }

            while (index < text.Count)
            {
                if (StartPrivateRegex.IsMatch(text[index]) || (index + 1 == text.Count && text[index].Trim().StartsWith("}")))
                {
                    break;
                }

                if (result.ObjectType == ObjectType.Api && GoToDeclareApiSection(text, index, out index))
                {
                    continue;
                }

                var comm = TryParseComment(text, index, out index);
                if (index == text.Count)
                {
                    break;
                }

                if (StartPrivateRegex.IsMatch(text[index]))
                {
                    break;
                }

                PreParsedElement field = null;
                switch (result.ObjectType)
                {
                case ObjectType.Enum:
                {
                    field = TryParseEnum(text, index, out index);
                    break;
                }

                case ObjectType.Class:
                {
                    field = TryParseElement(text, index, out index, isApi);
                    break;
                }

                case ObjectType.Api:
                {
                    field = TryParseMethod(text, index, out index);
                    break;
                }
                }

                if (field != null)
                {
                    field.MainComment = comm;
                    result.Fields.Add(field);
                }
            }

            return(result);
        }