4 Commits
2 changed files with 113 additions and 24 deletions

No files matched your search

+112 -23
View File
@@ -6,6 +6,34 @@ using System.Text.RegularExpressions;
namespace CppTripleSlash
{
public static class StringExtensions
{
public static string SuperTrim(this string str)
{
var sb = new StringBuilder(str.Length);
var space = false;
for (var i = 0; i < str.Length; i++)
{
if (!space && str[i] == ' ')
{
space = true;
sb.Append(' ');
}
else
{
space = false;
sb.Append(str[i]);
}
}
return sb.ToString().Trim();
}
public static string ReplaceWord(this string str, string pattern, string replacement)
{
return Regex.Replace(str, "\\b" + pattern + "\\b", replacement);
}
}
class Function
{
class ParseException : Exception
@@ -22,53 +50,100 @@ namespace CppTripleSlash
{
return arg;
}
if (arg.Contains("=")) //default value
if (arg.Contains("=")) //default value => "int n = 42"
{
int eqIndex = arg.IndexOf('=');
return GetArgumentName(arg.Substring(0, eqIndex - 1));
return GetArgumentName(arg.Substring(0, eqIndex));
}
if (arg.Contains("(")) //function parameter
Match match = Regex.Match(arg, @"\( *\* *([a-zA-Z0-9_]+) *\)");
if (match.Success) //function parameter => "int (*name)(...)"
{
Match match = Regex.Match(arg, "\\( *\\* *([a-zA-Z0-9_]+) *\\)");
if (!match.Success)
{
throw new ParseException("FunctionParameterRegexFail");
}
return match.Groups[1].Value;
}
//char name[size]
var blockOpen = arg.IndexOf('[');
if (blockOpen != -1)
arg = arg.Substring(0, blockOpen);
//normal argument
arg = arg
.Replace('&', ' ')
.Replace('*', ' ')
.ReplaceWord("const", "")
.ReplaceWord("volatile", "")
.SuperTrim()
//http://en.cppreference.com/w/cpp/language/types
.ReplaceWord("unsigned long long int", "int")
.ReplaceWord("signed long long int", "int")
.ReplaceWord("unsigned long long", "int")
.ReplaceWord("unsigned short int", "int")
.ReplaceWord("unsigned long int", "int")
.ReplaceWord("signed long long", "int")
.ReplaceWord("signed short int", "int")
.ReplaceWord("signed long int", "int")
.ReplaceWord("unsigned short", "int")
.ReplaceWord("unsigned long", "int")
.ReplaceWord("long long int", "int")
.ReplaceWord("unsigned int", "int")
.ReplaceWord("signed short", "int")
.ReplaceWord("signed long", "int")
.ReplaceWord("long double", "int")
.ReplaceWord("signed int", "int")
.ReplaceWord("short int", "int")
.ReplaceWord("long long", "int")
.ReplaceWord("long int", "int")
.ReplaceWord("unsigned", "int")
.ReplaceWord("signed", "int")
.ReplaceWord("short", "int")
.ReplaceWord("long", "int");
int lastSpace = arg.LastIndexOf(' ');
if (lastSpace == -1)
{
throw new ParseException("NoSpaceInNormalArgument");
return "";
}
return arg.Substring(lastSpace + 1).Trim();
return arg.Substring(lastSpace + 1).SuperTrim();
}
private static IEnumerable<string> SplitArgs(string args)
{
List<string> result = new List<string>();
int depth = 0;
int parenDepth = 0;
int templateDepth = 0;
StringBuilder sb = new StringBuilder();
foreach (char ch in args)
{
switch (ch)
{
case '<':
templateDepth++;
break;
case '>':
templateDepth--;
if (templateDepth == 0)
{
sb.Append(' ');
}
break;
case '(':
depth++;
sb.Append(ch);
parenDepth++;
if (templateDepth == 0)
{
sb.Append(ch);
}
break;
case ')':
depth--;
sb.Append(ch);
parenDepth--;
if (templateDepth == 0)
{
sb.Append(ch);
}
break;
default:
if (depth == 0 && ch == ',')
if (parenDepth == 0 && templateDepth == 0 && ch == ',')
{
result.Add(sb.ToString());
sb.Clear();
}
else
else if (templateDepth == 0)
{
sb.Append(ch);
}
@@ -76,16 +151,18 @@ namespace CppTripleSlash
}
}
result.Add(sb.ToString());
return result.Where(s => !string.IsNullOrEmpty(s)).Select(s => s.Trim());
return result.Where(s => !string.IsNullOrEmpty(s)).Select(s => s.SuperTrim());
}
public void Parse(string decl)
{
ReturnType = null;
Arguments.Clear();
if (string.IsNullOrEmpty(decl))
{
throw new ParseException("NullOrEmpty");
}
decl = decl.Trim();
decl = decl.SuperTrim();
if (!decl.EndsWith(";"))
{
throw new ParseException("NotEndsWithSemicolon");
@@ -100,14 +177,26 @@ namespace CppTripleSlash
{
throw new ParseException("NoLastParen");
}
string nameReturnType = decl.Substring(0, firstParen).Trim();
string nameReturnType = decl.Substring(0, firstParen).SuperTrim();
int lastSpace = nameReturnType.LastIndexOf(' ');
string args = decl.Substring(firstParen + 1, lastParen - firstParen - 1).Trim();
string args = decl.Substring(firstParen + 1, lastParen - firstParen - 1).SuperTrim();
foreach (string arg in SplitArgs(args))
{
Arguments.Add(GetArgumentName(arg).Trim('*'));
Arguments.Add(GetArgumentName(arg).SuperTrim());
}
ReturnType = nameReturnType.Substring(0, lastSpace).Replace("static", "").Trim();
ReturnType = nameReturnType.Substring(0, lastSpace).Replace("static", "").SuperTrim();
}
public override string ToString()
{
var sb = new StringBuilder(ReturnType + "(");
for (var i = 0; i < Arguments.Count; i++)
{
if (i > 0)
sb.Append(',');
sb.Append($"\"{Arguments[i]}\"");
}
return sb.ToString() + ")";
}
}
}
+1 -1
View File
@@ -113,7 +113,7 @@
// Add the XML comments to the file
StringBuilder sb = new StringBuilder($"/ <summary>\r\n{prefix}\r\n{prefix}</summary>");
if(function.ReturnType.Length > 0)
if(!string.IsNullOrEmpty(function.ReturnType))
{
foreach (string argument in function.Arguments)
{