mirror of
https://github.com/DMaroo/GhidRust.git
synced 2025-09-10 04:53:36 +02:00
Add CContext
to store context information while parsing
* For now, it is being used to denote whether to use a semicolon or not
This commit is contained in:
9
src/main/java/ghidrust/decompiler/parser/c/CContext.java
Normal file
9
src/main/java/ghidrust/decompiler/parser/c/CContext.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package ghidrust.decompiler.parser.c;
|
||||
|
||||
public class CContext {
|
||||
public boolean statement_end_sc;
|
||||
|
||||
public CContext() {
|
||||
statement_end_sc = true;
|
||||
}
|
||||
}
|
@@ -4,8 +4,7 @@ package ghidrust.decompiler.parser.c.gen;
|
||||
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
|
||||
public
|
||||
class ASTGhostStringToken extends SimpleNode {
|
||||
String str_val;
|
||||
|
||||
public String image;
|
||||
public ASTGhostStringToken(int id) {
|
||||
super(id);
|
||||
}
|
||||
@@ -21,13 +20,5 @@ class ASTGhostStringToken extends SimpleNode {
|
||||
return
|
||||
visitor.visit(this, data);
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return str_val;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.str_val = value;
|
||||
}
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=7d91f560265b12b4f437803bcd66b7ba (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=f69f4334f2315a848a78628a0a52e742 (do not edit this line) */
|
||||
|
@@ -4,6 +4,8 @@ package ghidrust.decompiler.parser.c.gen;
|
||||
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
|
||||
public
|
||||
class ASTIterationStatement extends SimpleNode {
|
||||
public int choice;
|
||||
|
||||
public ASTIterationStatement(int id) {
|
||||
super(id);
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ package ghidrust.decompiler.parser.c.gen;
|
||||
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
|
||||
public
|
||||
class ASTStringToken extends SimpleNode {
|
||||
String str_val;
|
||||
public String image;
|
||||
|
||||
public ASTStringToken(int id) {
|
||||
super(id);
|
||||
@@ -21,13 +21,5 @@ class ASTStringToken extends SimpleNode {
|
||||
return
|
||||
visitor.visit(this, data);
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return str_val;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.str_val = value;
|
||||
}
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=c289df07a5b51163b866d4bfab28fb00 (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=73f64d714d8c3f0f819df770be5c932d (do not edit this line) */
|
||||
|
@@ -4,7 +4,7 @@ package ghidrust.decompiler.parser.c.gen;
|
||||
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
|
||||
public
|
||||
class ASTTypeStringToken extends SimpleNode {
|
||||
String str_val;
|
||||
public String image;
|
||||
|
||||
public ASTTypeStringToken(int id) {
|
||||
super(id);
|
||||
@@ -21,13 +21,5 @@ class ASTTypeStringToken extends SimpleNode {
|
||||
return
|
||||
visitor.visit(this, data);
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return str_val;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.str_val = value;
|
||||
}
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=726f5c0cafc2cba1f5ca608dfd54d3e2 (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=b189beab6a69b3fbf2cd6d3dc65da871 (do not edit this line) */
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -6,6 +6,7 @@ import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import ghidrust.decompiler.parser.c.CVisitor;
|
||||
import ghidrust.decompiler.parser.c.CContext;
|
||||
|
||||
/** Token Manager. */
|
||||
@SuppressWarnings ("unused")
|
||||
|
@@ -21,6 +21,7 @@ import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import ghidrust.decompiler.parser.c.CVisitor;
|
||||
import ghidrust.decompiler.parser.c.CContext;
|
||||
|
||||
public class CParser/*@bgen(jjtree)*/implements CParserTreeConstants/*@egen*/ {/*@bgen(jjtree)*/
|
||||
protected JJTCParserState jjtree = new JJTCParserState();
|
||||
@@ -48,7 +49,7 @@ public class CParser/*@bgen(jjtree)*/implements CParserTreeConstants/*@egen*/ {/
|
||||
|
||||
public static String parse() throws ParseException {
|
||||
CParserVisitor visitor = new CVisitor();
|
||||
return (String) c_parser.FunctionDefinition().jjtAccept(visitor, null);
|
||||
return (String) c_parser.FunctionDefinition().jjtAccept(visitor, new CContext());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,7 +294,7 @@ void StorageClassSpecifier() :
|
||||
}
|
||||
/*@egen*/
|
||||
{
|
||||
jjtn000.setValue(t.image);
|
||||
jjtn000.image = t.image;
|
||||
}/*@bgen(jjtree)*/
|
||||
} finally {
|
||||
if (jjtc000) {
|
||||
@@ -322,7 +323,7 @@ void TypeSpecifier() :
|
||||
}
|
||||
/*@egen*/
|
||||
{
|
||||
jjtn000.setValue(t.image);
|
||||
jjtn000.image = t.image;
|
||||
}/*@bgen(jjtree)*/
|
||||
} finally {
|
||||
if (jjtc000) {
|
||||
@@ -350,7 +351,7 @@ void TypeQualifier() :
|
||||
}
|
||||
/*@egen*/
|
||||
{
|
||||
jjtn000.setValue(t.image);
|
||||
jjtn000.image = t.image;
|
||||
}/*@bgen(jjtree)*/
|
||||
} finally {
|
||||
if (jjtc000) {
|
||||
@@ -1065,17 +1066,28 @@ void SelectionStatement() : {/*@bgen(jjtree) SelectionStatement */
|
||||
/*@egen*/
|
||||
}
|
||||
|
||||
void IterationStatement() : {/*@bgen(jjtree) IterationStatement */
|
||||
ASTIterationStatement jjtn000 = new ASTIterationStatement(JJTITERATIONSTATEMENT);
|
||||
boolean jjtc000 = true;
|
||||
jjtree.openNodeScope(jjtn000);
|
||||
/*@egen*/}
|
||||
void IterationStatement() :
|
||||
{/*@bgen(jjtree) IterationStatement */
|
||||
ASTIterationStatement jjtn000 = new ASTIterationStatement(JJTITERATIONSTATEMENT);
|
||||
boolean jjtc000 = true;
|
||||
jjtree.openNodeScope(jjtn000);
|
||||
/*@egen*/
|
||||
int choice = 0;
|
||||
}
|
||||
{/*@bgen(jjtree) IterationStatement */
|
||||
try {
|
||||
/*@egen*/
|
||||
( <WHILE> "(" Expression() ")" Statement() |
|
||||
<DO> Statement() <WHILE> "(" Expression() ")" ";" |
|
||||
<FOR> "(" [ Expression() ] ";" [ Expression() ] ";" [ Expression() ] ")" Statement() )/*@bgen(jjtree)*/
|
||||
( <WHILE> "(" Expression() ")" Statement() { choice = 1; } |
|
||||
<DO> Statement() <WHILE> "(" Expression() ")" ";" { choice = 2; } |
|
||||
<FOR> "(" [ Expression() ] ";" [ Expression() ] ";" [ Expression() ] ")" Statement() { choice = 3; } )/*@bgen(jjtree)*/
|
||||
{
|
||||
jjtree.closeNodeScope(jjtn000, true);
|
||||
jjtc000 = false;
|
||||
}
|
||||
/*@egen*/
|
||||
{
|
||||
jjtn000.choice = choice;
|
||||
}/*@bgen(jjtree)*/
|
||||
} catch (Throwable jjte000) {
|
||||
if (jjtc000) {
|
||||
jjtree.clearNodeScope(jjtn000);
|
||||
@@ -1213,7 +1225,7 @@ void AssignmentOperator() :
|
||||
}
|
||||
/*@egen*/
|
||||
{
|
||||
jjtn000.setValue(t.image);
|
||||
jjtn000.image = t.image;
|
||||
}/*@bgen(jjtree)*/
|
||||
} finally {
|
||||
if (jjtc000) {
|
||||
@@ -1479,7 +1491,7 @@ void RelationalExpression() : {/*@bgen(jjtree) RelationalExpression */
|
||||
{/*@bgen(jjtree) RelationalExpression */
|
||||
try {
|
||||
/*@egen*/
|
||||
ShiftExpression() [ ( "<" | ">" | "<=" | ">=" ) RelationalExpression() ]/*@bgen(jjtree)*/
|
||||
ShiftExpression() [( "<" | ">" | "<=" | ">=" ) RelationalExpression()]/*@bgen(jjtree)*/
|
||||
} catch (Throwable jjte000) {
|
||||
if (jjtc000) {
|
||||
jjtree.clearNodeScope(jjtn000);
|
||||
@@ -1582,7 +1594,7 @@ void AdditionOperator() :
|
||||
}
|
||||
/*@egen*/
|
||||
{
|
||||
jjtn000.setValue(t.image);
|
||||
jjtn000.image = t.image;
|
||||
}/*@bgen(jjtree)*/
|
||||
} finally {
|
||||
if (jjtc000) {
|
||||
@@ -1826,22 +1838,22 @@ ASTStringToken Identifier() :
|
||||
Token r = null;
|
||||
ASTStringToken t = null;
|
||||
Token s = null;
|
||||
int choice;
|
||||
int choice = 0;
|
||||
}
|
||||
{/*@bgen(jjtree) StringToken */
|
||||
try {
|
||||
/*@egen*/
|
||||
(LOOKAHEAD(<IDENTIFIER>) r = <IDENTIFIER> { choice = 0; } | [ r = <IDENTIFIER> ] "<" t = Identifier() ">" [ s = <IDENTIFIER> ] { choice = 1; })/*@bgen(jjtree)*/
|
||||
(LOOKAHEAD(<IDENTIFIER>) r = <IDENTIFIER> { choice = 1; } | [ r = <IDENTIFIER> ] "<" t = Identifier() ">" [ s = <IDENTIFIER> ] { choice = 2; })/*@bgen(jjtree)*/
|
||||
{
|
||||
jjtree.closeNodeScope(jjtn000, true);
|
||||
jjtc000 = false;
|
||||
}
|
||||
/*@egen*/
|
||||
{
|
||||
if (choice == 1) {
|
||||
jjtn000.setValue((r != null ? r.image : "") + "<" + t.getValue() + ">" + (s != null ? s.image : ""));
|
||||
} else {
|
||||
jjtn000.setValue(r.image);
|
||||
if (choice == 2) {
|
||||
jjtn000.image = (r != null ? r.image : "") + "<" + t.image + ">" + (s != null ? s.image : "");
|
||||
} else if (choice == 1) {
|
||||
jjtn000.image = r.image;
|
||||
}
|
||||
|
||||
return jjtn000;
|
||||
@@ -1886,7 +1898,7 @@ void Constant() :
|
||||
}
|
||||
/*@egen*/
|
||||
{
|
||||
jjtn000.setValue(t.image);
|
||||
jjtn000.image = t.image;
|
||||
}/*@bgen(jjtree)*/
|
||||
} finally {
|
||||
if (jjtc000) {
|
||||
|
@@ -20,6 +20,7 @@ import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import ghidrust.decompiler.parser.c.CVisitor;
|
||||
import ghidrust.decompiler.parser.c.CContext;
|
||||
|
||||
public class CParser {
|
||||
private static CParser c_parser;
|
||||
@@ -44,7 +45,7 @@ public class CParser {
|
||||
|
||||
public static String parse() throws ParseException {
|
||||
CParserVisitor visitor = new CVisitor();
|
||||
return (String) c_parser.FunctionDefinition().jjtAccept(visitor, null);
|
||||
return (String) c_parser.FunctionDefinition().jjtAccept(visitor, new CContext());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,7 +170,7 @@ void StorageClassSpecifier() #GhostStringToken :
|
||||
{
|
||||
( t = <AUTO> | t = <REGISTER> | t = <STATIC> | t = <EXTERN> | t = <TYPEDEF> )
|
||||
{
|
||||
jjtThis.setValue(t.image);
|
||||
jjtThis.image = t.image;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,7 +182,7 @@ void TypeSpecifier() #TypeStringToken :
|
||||
( t = <VOID> | t = <CHAR> | t = <SHORT> | t = <INT> | t = <LONG> | t = <FLOAT> | t = <DOUBLE> | t = <SIGNED> |
|
||||
t = <UNSIGNED> | t = <BOOL_TYPE> | t = <CODE> | t = <UNDEFINED_TYPE> )
|
||||
{
|
||||
jjtThis.setValue(t.image);
|
||||
jjtThis.image = t.image;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,7 +193,7 @@ void TypeQualifier() #StringToken :
|
||||
{
|
||||
( t = <CONST> | t = <VOLATILE> )
|
||||
{
|
||||
jjtThis.setValue(t.image);
|
||||
jjtThis.image = t.image;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,11 +330,17 @@ void SelectionStatement() : {}
|
||||
<SWITCH> "(" Expression() ")" Statement() )
|
||||
}
|
||||
|
||||
void IterationStatement() : {}
|
||||
void IterationStatement() :
|
||||
{
|
||||
( <WHILE> "(" Expression() ")" Statement() |
|
||||
<DO> Statement() <WHILE> "(" Expression() ")" ";" |
|
||||
<FOR> "(" [ Expression() ] ";" [ Expression() ] ";" [ Expression() ] ")" Statement() )
|
||||
int choice = 0;
|
||||
}
|
||||
{
|
||||
( <WHILE> "(" Expression() ")" Statement() { choice = 1; } |
|
||||
<DO> Statement() <WHILE> "(" Expression() ")" ";" { choice = 2; } |
|
||||
<FOR> "(" [ Expression() ] ";" [ Expression() ] ";" [ Expression() ] ")" Statement() { choice = 3; } )
|
||||
{
|
||||
jjtThis.choice = choice;
|
||||
}
|
||||
}
|
||||
|
||||
void JumpStatement() : {}
|
||||
@@ -362,7 +369,7 @@ void AssignmentOperator() #StringToken :
|
||||
{
|
||||
( t = "=" | t = "*=" | t = "/=" | t = "%=" | t = "+=" | t = "-=" | t = "<<=" | t = ">>=" | t = "&=" | t = "^=" | t = "|=" )
|
||||
{
|
||||
jjtThis.setValue(t.image);
|
||||
jjtThis.image = t.image;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,7 +415,7 @@ void EqualityExpression() : {}
|
||||
|
||||
void RelationalExpression() : {}
|
||||
{
|
||||
ShiftExpression() [ ( "<" | ">" | "<=" | ">=" ) RelationalExpression() ]
|
||||
ShiftExpression() [( "<" | ">" | "<=" | ">=" ) RelationalExpression()]
|
||||
}
|
||||
|
||||
void ShiftExpression() : {}
|
||||
@@ -428,7 +435,7 @@ void AdditionOperator() #StringToken :
|
||||
{
|
||||
( t = "+" | t = "-" )
|
||||
{
|
||||
jjtThis.setValue(t.image);
|
||||
jjtThis.image = t.image;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -489,15 +496,15 @@ ASTStringToken Identifier() #StringToken :
|
||||
Token r = null;
|
||||
ASTStringToken t = null;
|
||||
Token s = null;
|
||||
int choice;
|
||||
int choice = 0;
|
||||
}
|
||||
{
|
||||
(LOOKAHEAD(<IDENTIFIER>) r = <IDENTIFIER> { choice = 0; } | [ r = <IDENTIFIER> ] "<" t = Identifier() ">" [ s = <IDENTIFIER> ] { choice = 1; })
|
||||
(LOOKAHEAD(<IDENTIFIER>) r = <IDENTIFIER> { choice = 1; } | [ r = <IDENTIFIER> ] "<" t = Identifier() ">" [ s = <IDENTIFIER> ] { choice = 2; })
|
||||
{
|
||||
if (choice == 1) {
|
||||
jjtThis.setValue((r != null ? r.image : "") + "<" + t.getValue() + ">" + (s != null ? s.image : ""));
|
||||
} else {
|
||||
jjtThis.setValue(r.image);
|
||||
if (choice == 2) {
|
||||
jjtThis.image = (r != null ? r.image : "") + "<" + t.image + ">" + (s != null ? s.image : "");
|
||||
} else if (choice == 1) {
|
||||
jjtThis.image = r.image;
|
||||
}
|
||||
|
||||
return jjtThis;
|
||||
@@ -511,6 +518,6 @@ void Constant() #StringToken :
|
||||
{
|
||||
(t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> | t = <CHARACTER_LITERAL> | t = <STRING_LITERAL> | t = <BOOLEAN>)
|
||||
{
|
||||
jjtThis.setValue(t.image);
|
||||
jjtThis.image = t.image;
|
||||
}
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@
|
||||
|
||||
cd c/gen
|
||||
|
||||
BACKUP_FILES="ASTPostfixExpression \
|
||||
BACKUP_FILES="ASTPostfixExpression ASTIterationStatement \
|
||||
$(ls -1 AST*Token.java | cut -d. -f1 | tr '\n' ' ')"
|
||||
|
||||
for file in $BACKUP_FILES; do
|
||||
|
Reference in New Issue
Block a user