1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 10:46: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
*
* To change this generated comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
* GDB Type Parser.
* The code was lifted from: The C Programming Language
* B. W. Kernighan and D. Ritchie
*/
public class GDBTypeParser {
@ -35,10 +34,47 @@ public class GDBTypeParser {
String token;
String dataType;
String name;
String out;
GDBDerivedType gdbDerivedType;
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 final static int GENERIC = 0;
public final static int POINTER = 1;
@ -106,64 +142,22 @@ public class GDBTypeParser {
StringBuffer sb = new StringBuffer();
switch (getType()) {
case FUNCTION :
sb.append(" Function returning " + (hasChild() ? child.toString() : ""));
sb.append(" function returning " + (hasChild() ? child.toString() : ""));
break;
case ARRAY :
sb.append(" Array[" + dimension + "]" + " of " + (hasChild() ? child.toString() : ""));
sb.append(" array[" + dimension + "]" + " of " + (hasChild() ? child.toString() : ""));
break;
case REFERENCE :
sb.append(" Reference to " + (hasChild() ? child.toString() : ""));
sb.append(" reference to " + (hasChild() ? child.toString() : ""));
break;
case POINTER :
sb.append(" Pointer to " + (hasChild() ? child.toString() : ""));
sb.append(" pointer to " + (hasChild() ? child.toString() : ""));
break;
}
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() {
if (index >= line.length() || index < 0) {
return EOF;
@ -226,7 +220,6 @@ public class GDBTypeParser {
}
}
// method returns the next token
int getToken() {
token = "";
@ -297,11 +290,9 @@ public class GDBTypeParser {
}
dirdcl();
while (nstar-- > 0) {
out += " pointer to ";
prependChild(GDBType.POINTER);
}
while (namp-- > 0) {
out += " reference to ";
prependChild(GDBType.REFERENCE);
GDBDerivedType referenceType = new GDBDerivedType(genericType, GDBDerivedType.REFERENCE);
}
@ -330,68 +321,52 @@ public class GDBTypeParser {
return;
}
if (type == PARENS) {
out += " function returning ";
prependChild(GDBType.FUNCTION);
} else {
int len = 0;
if (token.length() > 0) {
try {
out += "" + " array[";
len = Integer.parseInt(token);
out += len + "]";
out += " of ";
} catch (NumberFormatException e) {
out += " array[0] of ";
}
} else {
out += " array of ";
}
prependChild(GDBType.ARRAY, len);
}
}
}
public static void main(String[] args) {
GDBTypeParser parser = new GDBTypeParser();
System.out.println("char **argv");
parser.parse("unsigned long long int **argv");
parser.verbose();
System.out.println(parser.getGDBType());
System.out.println("int (*daytab)[13]");
parser.parse("int (*daytab)[13]");
parser.verbose();
System.out.println(parser.getGDBType());
System.out.println("int *daytab[13]");
parser.parse("int *daytab[13]");
parser.verbose();
System.out.println(parser.getGDBType());
System.out.println("void *comp()");
parser.parse("void *comp()");
parser.verbose();
System.out.println(parser.getGDBType());
System.out.println("void (*comp)()");
parser.parse("void (*comp)()");
parser.verbose();
System.out.println(parser.getGDBType());
System.out.println("int (*func[15])()");
parser.parse("int (*func[15])()");
parser.verbose();
System.out.println(parser.getGDBType());
System.out.println("char (*(*x())[])()");
parser.parse("char (*(*x())[])()");
parser.verbose();
System.out.println(parser.getGDBType());
System.out.println("char (*(*x[3])())[5]");
parser.parse("char (*(*x[3])())[5]");
parser.verbose();
System.out.println(parser.getGDBType());
}
}