Files
Autopilot-Data-ParserOLD/src/CHeaderParser.java

243 lines
4.1 KiB
Java
Raw Normal View History

/**
* Jakob A. Oberbuchner April, 24, 2025
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Stack;
//
/**
*
*/
public class CHeaderParser {
public static void main(String[] args) {
C_Type type1 = C_Type.valueOfTypeName("struct");
C_Type type2 = C_Type.valueOfTypeName("uint16_t");
C_Type type3 = C_Type.valueOfTypeName("float");
System.out.println(type1);
System.out.println(type2);
System.out.println(type3);
}
public static ArrayList<C_DataStructure> parseHeader(String file) {
ArrayList<C_DataStructure> data = new ArrayList<C_DataStructure>();
try {
BufferedReader reader = new BufferedReader( new FileReader(file));
Stack<C_DataStructure> structureStack = new Stack<C_DataStructure>();
Stack<ParseState> stateStack = new Stack<ParseState>();
String nextLine = null;
while((nextLine = reader.readLine()) != null) {
nextLine = nextLine.strip().replace("\t", "");
//typedef enum { bps125K, bps250K, bps500K, bps1M } CANbps_t;
//typedef struct config_s
//uint8_t NumPackets; // 1-16 (determines how many Broadcast[x] are displayed)
//} config_t;
String[] tokens = nextLine.split(" ", 2);
if (tokens.length == 0) {
continue; //nothing for us
}
for(int i = 0; i < tokens.length; i++) {
String current = tokens[i];
switch (stateStack.peek()) {
case Looking:
case null:
//General, looking for next thing
break;
case OpenBracket:
if (current == "{") {
stateStack.pop();
// stateStack.add(ParseState.Looking);
} else {
System.out.println("NOT open bracket BADDDD");
}
break;
case CloseBracket:
break;
case EnumVariables:
break;
case Name:
break;
case Type:
break;
}
}
switch (tokens[0]) {
case "{":
break;
case "}":
break;
default:
C_Type type = C_Type.valueOfTypeName(tokens[0].strip());
switch (type) {
case Typedef:
case Enum:
case Struct:
// structureStack.
break;
case null:
break;
default: //Any other type
}
if (type != null && tokens.length >= 2) {
String name = tokens[1];
}
}
}
reader.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
return data;
}//parseHeader
}
enum ParseState {
Looking, Type, Name, OpenBracket, CloseBracket, EnumVariables
}
class C_DataStructure {
private C_Type type;
private String name;
private String typedefName;
private ArrayList<C_DataStructure> subStructures;
public C_DataStructure( ) {
this.type = null;
this.name = null;
this.typedefName = null;
this.subStructures = new ArrayList<C_DataStructure>();
}
public C_Type getType() {
return type;
}
public void setType(C_Type type) {
this.type = type;
}
public String getName() {
return (typedefName != null) ? typedefName : name;
}
public void setName(String name) {
this.name = name;
}
public void setTypedefName(String name) {
this.typedefName = name;
}
public void addSubStructure(C_DataStructure subStructure) {
this.subStructures.add(subStructure);
}
}
enum C_Type {
Struct("struct"), Enum("enum"), Typedef("typedef"),
Int64("int64_t"), Int32("int32_t"), Int16("int16_t"), Int8("int8_t"),
UInt64("uint64_t"), UInt32("uint32_t"), UInt16("uint16_t"), UInt8("uint8_t"),
Float("float"), Double("double"); //Union
public final String typeName;
C_Type(String typeName) {
this.typeName = typeName;
}
public static C_Type valueOfTypeName(String label) {
for (C_Type e : values()) {
if (e.typeName.equals(label)) {
return e;
}
}
return null;
}
public String toString( ) {
return typeName;
}
}