Add a separate formatter

* This would allow for custom formatting
    * Keeps the CVisitor simple
This commit is contained in:
Dhruv Maroo
2023-04-26 08:40:45 +05:30
parent d317d6ff3c
commit f0f3d0d747
2 changed files with 61 additions and 1 deletions

View File

@@ -1,9 +1,10 @@
package ghidrust.decompiler.parser;
import ghidrust.decompiler.parser.c.gen.CParser;
import ghidrust.decompiler.parser.c.CFormatter;
public class Run {
public static void main(String[] args) {
System.out.println(CParser.transpile(System.in));
System.out.println(CFormatter.format(CParser.transpile(System.in)));
}
}

View File

@@ -0,0 +1,59 @@
package ghidrust.decompiler.parser.c;
public class CFormatter {
static int indent_level;
static String indent(int level) {
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < level; i++) {
sb.append("\t");
}
return sb.toString();
}
public CFormatter(int initial_indent) {
indent_level = 0;
}
public static String format(String code) {
StringBuffer pretty = new StringBuffer("");
int str_len = code.length();
pretty.append(indent(indent_level));
boolean disable = false;
for (int i = 0; i < str_len; i++) {
if (code.charAt(i) == '{') {
pretty.append("{\n");
indent_level++;
pretty.append(indent(indent_level));
} else if (code.charAt(i) == '}') {
indent_level--;
if (code.charAt(i - 1) != ';') {
pretty.append("\n");
pretty.append(indent(indent_level));
} else {
pretty.deleteCharAt(pretty.length() - 1);
}
pretty.append("}");
if (!(i + 1 < str_len && code.charAt(i + 1) == ' ')) {
pretty.append("\n");
pretty.append(indent(indent_level));
}
} else if (code.charAt(i) == ';') {
pretty.append(";\n");
pretty.append(indent(indent_level));
} else if (code.charAt(i) == '@') {
/* special character to denote no action for next char */
i++;
pretty.append(code.charAt(i));
} else {
pretty.append(code.charAt(i));
}
}
return pretty.toString();
}
}