1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 18:56:02 +02:00

New work improving.

This commit is contained in:
Alain Magloire 2003-05-28 20:32:19 +00:00
parent c27d3e4175
commit c09a240262

View file

@ -1,16 +1,15 @@
package org.eclipse.cdt.debug.mi.core;
/* /*
* Created on May 26, 2003 *(c) Copyright QNX Software Systems Ltd. 2002.
* All Rights Reserved.
* *
* To change this generated comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/ */
package org.eclipse.cdt.debug.mi.core;
/** /**
* @author alain * GDB Type Parser.
* * The code was lifted from: The C Programming Language
* To change this generated comment go to * B. W. Kernighan and D. Ritchie
* Window>Preferences>Java>Code Generation>Code and Comments
*/ */
public class GDBTypeParser { public class GDBTypeParser {
@ -35,14 +34,51 @@ public class GDBTypeParser {
String token; String token;
String dataType; String dataType;
String name; String name;
String out;
GDBDerivedType gdbDerivedType; GDBDerivedType gdbDerivedType;
GDBType genericType; GDBType genericType;
public GDBType getGDBType() {
if (gdbDerivedType != null) {
return gdbDerivedType;
}
return genericType;
}
public String getVariableName() {
return name;
}
public GDBType parse(String s) {
// Sanity.
if (s == null) {
s = new String();
}
s = s.trim();
// Initialize.
line = s;
index = 0;
token = "";
dataType = "";
name = "";
gdbDerivedType = null;
// Fetch the datatype.
while (getToken() == NAME) {
dataType += " " + token;
}
genericType = new GDBType(dataType);
// Start the recursive parser.
dcl(tokenType);
return getGDBType();
}
public class GDBType { public class GDBType {
public final static int GENERIC = 0; public final static int GENERIC = 0;
public final static int POINTER = 1; public final static int POINTER = 1;
public final static int REFERENCE= 2; public final static int REFERENCE = 2;
public final static int ARRAY = 3; public final static int ARRAY = 3;
public final static int FUNCTION = 4; public final static int FUNCTION = 4;
@ -54,10 +90,10 @@ public class GDBTypeParser {
} }
public GDBType(int t) { public GDBType(int t) {
this ("", t); this("", t);
} }
GDBType (String n, int t) { GDBType(String n, int t) {
name = n; name = n;
type = t; type = t;
} }
@ -105,65 +141,23 @@ public class GDBTypeParser {
public String toString() { public String toString() {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
switch (getType()) { switch (getType()) {
case FUNCTION: case FUNCTION :
sb.append(" Function returning " + (hasChild() ? child.toString() : "")); sb.append(" function returning " + (hasChild() ? child.toString() : ""));
break; break;
case ARRAY: case ARRAY :
sb.append(" Array[" + dimension + "]" + " of " + (hasChild() ? child.toString() : "")); sb.append(" array[" + dimension + "]" + " of " + (hasChild() ? child.toString() : ""));
break; break;
case REFERENCE: case REFERENCE :
sb.append(" Reference to " + (hasChild() ? child.toString() : "")); sb.append(" reference to " + (hasChild() ? child.toString() : ""));
break; break;
case POINTER: case POINTER :
sb.append(" Pointer to " + (hasChild() ? child.toString() : "")); sb.append(" pointer to " + (hasChild() ? child.toString() : ""));
break; break;
} }
return sb.toString(); return sb.toString();
} }
} }
public GDBTypeParser() {
}
public GDBType getGDBType() {
if (gdbDerivedType != null) {
return gdbDerivedType;
}
return genericType;
}
public void verbose() {
System.out.println(name + " --> " + out + dataType);
}
public void parse(String s) {
// Sanity.
if (s == null) {
s = new String();
}
s = s.trim();
// Initialize.
line = s;
index = 0;
token = "";
dataType = "";
out = "";
name = "";
gdbDerivedType = null;
// Fetch the datatype.
while (getToken() == NAME) {
dataType += " " + token;
}
genericType = new GDBType(dataType);
// After getting the type move back
//ungetch();
dcl(tokenType);
}
int getch() { int getch() {
if (index >= line.length() || index < 0) { if (index >= line.length() || index < 0) {
return EOF; return EOF;
@ -211,11 +205,11 @@ public class GDBTypeParser {
// get to the last node in the list and add the new to it // get to the last node in the list and add the new to it
GDBType leaf = genericType; GDBType leaf = genericType;
GDBDerivedType node; GDBDerivedType node;
boolean keepGoing =true; boolean keepGoing = true;
for (node = gdbDerivedType; keepGoing;){ for (node = gdbDerivedType; keepGoing;) {
leaf = node.getChild(); leaf = node.getChild();
if (leaf instanceof GDBDerivedType) { if (leaf instanceof GDBDerivedType) {
node = (GDBDerivedType)leaf; node = (GDBDerivedType) leaf;
} else { } else {
keepGoing = false; keepGoing = false;
} }
@ -226,7 +220,6 @@ public class GDBTypeParser {
} }
} }
// method returns the next token // method returns the next token
int getToken() { int getToken() {
token = ""; token = "";
@ -238,7 +231,7 @@ public class GDBTypeParser {
c = getch(); c = getch();
} }
char character = (char)c; char character = (char) c;
if (c == '(') { if (c == '(') {
if ((c = getch()) == ')') { if ((c = getch()) == ')') {
@ -250,13 +243,13 @@ public class GDBTypeParser {
} }
} else if (c == '[') { } else if (c == '[') {
while ((c = getch()) != ']' && c != EOF) { while ((c = getch()) != ']' && c != EOF) {
token += (char)c; token += (char) c;
} }
tokenType = BRACKETS; tokenType = BRACKETS;
} else if (isCIdentifierStart(c)) { } else if (isCIdentifierStart(c)) {
token = "" + (char)c; token = "" + (char) c;
while (isCIdentifierPart((c = getch())) && c != EOF) { while (isCIdentifierPart((c = getch())) && c != EOF) {
token += (char)c; token += (char) c;
} }
ungetch(); ungetch();
tokenType = NAME; tokenType = NAME;
@ -297,11 +290,9 @@ public class GDBTypeParser {
} }
dirdcl(); dirdcl();
while (nstar-- > 0) { while (nstar-- > 0) {
out += " pointer to ";
prependChild(GDBType.POINTER); prependChild(GDBType.POINTER);
} }
while (namp-- > 0) { while (namp-- > 0) {
out += " reference to ";
prependChild(GDBType.REFERENCE); prependChild(GDBType.REFERENCE);
GDBDerivedType referenceType = new GDBDerivedType(genericType, GDBDerivedType.REFERENCE); GDBDerivedType referenceType = new GDBDerivedType(genericType, GDBDerivedType.REFERENCE);
} }
@ -330,68 +321,52 @@ public class GDBTypeParser {
return; return;
} }
if (type == PARENS) { if (type == PARENS) {
out += " function returning ";
prependChild(GDBType.FUNCTION); prependChild(GDBType.FUNCTION);
} else { } else {
int len = 0; int len = 0;
if (token.length() > 0) { if (token.length() > 0) {
try { try {
out += "" + " array[";
len = Integer.parseInt(token); len = Integer.parseInt(token);
out += len + "]";
out += " of ";
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
out += " array[0] of ";
} }
} else {
out += " array of ";
} }
prependChild(GDBType.ARRAY, len); prependChild(GDBType.ARRAY, len);
} }
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
GDBTypeParser parser = new GDBTypeParser(); GDBTypeParser parser = new GDBTypeParser();
System.out.println("char **argv"); System.out.println("char **argv");
parser.parse("unsigned long long int **argv"); parser.parse("unsigned long long int **argv");
parser.verbose();
System.out.println(parser.getGDBType()); System.out.println(parser.getGDBType());
System.out.println("int (*daytab)[13]"); System.out.println("int (*daytab)[13]");
parser.parse("int (*daytab)[13]"); parser.parse("int (*daytab)[13]");
parser.verbose();
System.out.println(parser.getGDBType()); System.out.println(parser.getGDBType());
System.out.println("int *daytab[13]"); System.out.println("int *daytab[13]");
parser.parse("int *daytab[13]"); parser.parse("int *daytab[13]");
parser.verbose();
System.out.println(parser.getGDBType()); System.out.println(parser.getGDBType());
System.out.println("void *comp()"); System.out.println("void *comp()");
parser.parse("void *comp()"); parser.parse("void *comp()");
parser.verbose();
System.out.println(parser.getGDBType()); System.out.println(parser.getGDBType());
System.out.println("void (*comp)()"); System.out.println("void (*comp)()");
parser.parse("void (*comp)()"); parser.parse("void (*comp)()");
parser.verbose();
System.out.println(parser.getGDBType()); System.out.println(parser.getGDBType());
System.out.println("int (*func[15])()"); System.out.println("int (*func[15])()");
parser.parse("int (*func[15])()"); parser.parse("int (*func[15])()");
parser.verbose();
System.out.println(parser.getGDBType()); System.out.println(parser.getGDBType());
System.out.println("char (*(*x())[])()"); System.out.println("char (*(*x())[])()");
parser.parse("char (*(*x())[])()"); parser.parse("char (*(*x())[])()");
parser.verbose();
System.out.println(parser.getGDBType()); System.out.println(parser.getGDBType());
System.out.println("char (*(*x[3])())[5]"); System.out.println("char (*(*x[3])())[5]");
parser.parse("char (*(*x[3])())[5]"); parser.parse("char (*(*x[3])())[5]");
parser.verbose();
System.out.println(parser.getGDBType()); System.out.println(parser.getGDBType());
} }
} }