Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse C #2

Closed
wants to merge 15 commits into from
Prev Previous commit
Next Next commit
update
  • Loading branch information
MichaelCampos-eng committed Nov 20, 2021
commit 5395e9e33855a0dcb532ee98217475df2053ba53
114 changes: 82 additions & 32 deletions src/parsingCopy/src/com/company/_CDTParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.company;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -12,64 +13,76 @@
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IncludeFileContentProvider;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTranslationUnit;
import java.util.Iterator;

public class _CDTParser {
String sourceCode;
IASTTranslationUnit translationUnit;
String result;
HashMap<Integer, String> ids;
HashMap<Integer, String> value;
int index1;
int index2;

public _CDTParser(String Code) throws Exception {
this.sourceCode = Code;
this.result = "";
this.ids = new HashMap<>();
this.value = new HashMap<>();
index1 = 0;
index2 = 0;
this.translationUnit = getIASTTranslationUnit(this.sourceCode.toCharArray());
HashMap<String, String> repo;


HashMap<String, Integer> mapping;

IASTPreprocessorIncludeStatement[] includes = translationUnit.getIncludeDirectives(); //wtf does this do?
for (IASTPreprocessorIncludeStatement include : includes) {
System.out.println("include - " + include.getName());
}
public _CDTParser(String Code) throws Exception {
this.sourceCode = Code;

this.mapping = new HashMap<>();

this.repo = new HashMap<>();
this.translationUnit = getIASTTranslationUnit(this.sourceCode.toCharArray());
//visitSetUp();
}

//ok
/**
//Sets up what needs to be visited
public void visitSetUp() throws Exception{
ASTVisitor visitor = makeNewASTVisitor();

visitor.shouldVisitNames = true;
visitor.shouldVisitInitializers = true;

visitor.shouldVisitExpressions = true;
printTree(translationUnit, 1, this);
this.translationUnit.accept(visitor);
} */


//iterate over mapping and find the ID given the name's location
public String iterate(int location) {
Iterator iter = mapping.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
if (mapping.get(key) == location) {
return key;
}
}
return "";
}

public String getID(String name) {
int location = mapping.get(name) + name.length() + 3;
String ID = iterate(location);
return ID;
}

/**
//Identifies the IDs and its values
public ASTVisitor makeNewASTVisitor() {
return new ASTVisitor() {
public int visit(IASTInitializer initializer) {
String rawSignature = initializer.getRawSignature();
public int visit(IASTName a) {
String rawSignature = a.getRawSignature();
ids.put(index1, rawSignature);
result += rawSignature + " ";
index1 += 1;
return 3;
}
public int visit(IASTName name) {
String rawSignature = name.getRawSignature();
public int visit(IASTExpression a) {
String rawSignature = a.getRawSignature();
value.put(index2, rawSignature);
result += rawSignature + " ";
index2 += 1;
return 3;
}
};
}
} */


//Setup
//Initializes the setup
public static IASTTranslationUnit getIASTTranslationUnit(char[] code) throws Exception {
FileContent fc = FileContent.create("TestFile", code);
Map<String, String> macroDefinitions = new HashMap();
Expand All @@ -79,8 +92,45 @@ public static IASTTranslationUnit getIASTTranslationUnit(char[] code) throws Exc
IIndex idx = null;
int options = 8;
IParserLogService log = new DefaultLogService();

return GPPLanguage.getDefault().getASTTranslationUnit(fc, si, ifcp, (IIndex)idx, options, log);
}

//Visual Representation of Tree
private static void printTree(IASTNode node, int index, _CDTParser current) {
IASTNode[] children = node.getChildren();

boolean printContents = true;


if ((node instanceof CPPASTTranslationUnit)) {
printContents = false;
}

String offset = "";
int loc = 0;
try {
int location = node.getFileLocation().getNodeOffset();
int nodeLength = node.getFileLocation().getNodeLength();
offset = node.getSyntax() != null ? " (offset: " + location + "," + nodeLength + ")" : "";
printContents = node.getFileLocation().getNodeLength() < 30;
loc = location;
} catch (ExpansionOverlapsBoundaryException e) {
e.printStackTrace();
} catch (UnsupportedOperationException e) {
offset = "UnsupportedOperationException";
}

System.out.println(String.format(new StringBuilder("%1$").append(index * 2).append("s").toString(),
new Object[] { "-" }) + node.getClass().getSimpleName() + offset + " -> " +
(printContents ? node.getRawSignature().replaceAll("\n", " \\ ")
: node.getRawSignature().subSequence(0, 5)));

current.mapping.put("" + (printContents ? node.getRawSignature().replaceAll("\n", " \\ ")
: node.getRawSignature().subSequence(0, 5)), loc);

for (IASTNode iastNode : children) {
printTree(iastNode, index + 1, current);
}
}
}

84 changes: 65 additions & 19 deletions src/parsingCopy/src/com/company/_CDTParserTest.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,85 @@
package com.company;


import org.junit.Assert;
import org.junit.jupiter.api.Test;


class _CDTParserTest {
@Test
public void Comb1() throws Exception {
String code = "int a =21; int b =31; void test() {a++;} ";
_CDTParser comb1 = new _CDTParser(code);
System.out.println(comb1.ids.get(1));
System.out.println(comb1.value.get(1));
Assert.assertEquals(code, comb1.result);

public void testIdNValue() throws Exception {
String code = "int a = 21; int b = 31;";
_CDTParser test = new _CDTParser(code);
Assert.assertEquals("a", test.ids.get(0));
Assert.assertEquals("21", test.value.get(0));
}

@Test
public void stringsTest() throws Exception{
String code ="String hello; String bruh; String bruvs ";
_CDTParser stringsTest = new _CDTParser(code);
Assert.assertEquals(code, stringsTest.result);
public void testRepo() throws Exception {
String code = "int a = 21; int b = 31;";
_CDTParser test = new _CDTParser(code);
Assert.assertEquals("21", test.repo.get("a"));
Assert.assertEquals("31", test.repo.get("b"));
}

@Test
public void zephyrTest() throws Exception{
public void testRepo2() throws Exception{
String code = "struct MPPTData {\n" +
" uint16_t arrayVoltage_10mV;\n" +
" uint16_t arrayCurrent_mA;\n" +
" uint16_t arrayCurrent_mAm;\n" +
" uint16_t batteryVoltage_10mV;\n" +
" uint16_t temperature_10mC;\n" +
"}";
_CDTParser stringsTest = new _CDTParser(code);
Assert.assertEquals(code, stringsTest.result);
_CDTParser test = new _CDTParser(code);
Assert.assertEquals(true, test.repo.containsKey("arrayVoltage_10mV"));
Assert.assertEquals(true, test.repo.containsKey("arrayCurrent_mAm"));
Assert.assertEquals(true, test.repo.containsKey("batteryVoltage_10mV"));
Assert.assertEquals(true, test.repo.containsKey("temperature_10mC"));
}

//Test getting information

@Test
public void testMapping() throws Exception {
String code = "const uint32_t CAN_PETALS_POS = 0x42\n" +
"const uint32_t CAN_WHEELS_POS = 0x43\n" +
"const uint32_t CAN_TIRES_POS = 0x44\n" +
"struct PetalsPos {\n" +
" uint8_t accelPos;\n" +
" uint8_t brakePos;\n" +
"}";
_CDTParser test = new _CDTParser(code);

String name1 = "CAN_PETALS_POS";
String id1 = "0x42";
String name2 = "CAN_WHEELS_POS";
String id2 = "0x43";
String name3 = "CAN_TIRES_POS";
String id3 = "0x44";

Assert.assertEquals(15, (int) test.mapping.get(name1));
Assert.assertEquals(52, (int) test.mapping.get(name2));
Assert.assertEquals(89, (int) test.mapping.get(name3));

int locationCANPETALPOS = test.mapping.get(name1) + name1.length() + 3;
int locationNANWHEELSPOS = test.mapping.get(name2) + name2.length() + 3;
int locationCANTIRESPOS = test.mapping.get(name3) + name3.length() + 3;

Assert.assertEquals(locationCANPETALPOS, (int) test.mapping.get(id1));
Assert.assertEquals(locationNANWHEELSPOS, (int) test.mapping.get(id2));
Assert.assertEquals(locationCANTIRESPOS, (int) test.mapping.get(id3));

}

@Test
public void testRepo3() throws Exception {
String code = "const uint32_t CAN_PETALS_POS = 0x42\n" +
"const uint32_t CAN_WHEELS_POS = 0x43\n" +
"const uint32_t CAN_TIRES_POS = 0x44\n" +
"const uint32_t CAN_Bre_POS = 0x44\n" +
"struct PetalsPos {\n" +
" uint8_t accelPos;\n" +
" uint8_t brakePos;\n" +
"}";
_CDTParser test = new _CDTParser(code);
String payload1 = "0x01";
String payload2 = "0x02";
System.out.println(test.value.get(1));
}
}
27 changes: 14 additions & 13 deletions src/parsingLibrary/src/main/java/ParserExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
/* */ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
/* */ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel;
/* */ import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
/* */ import org.eclipse.cdt.core.parser.DefaultLogService;
/* */ import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.parser.DefaultLogService;
/* */ import org.eclipse.cdt.core.parser.FileContent;
/* */ import org.eclipse.cdt.core.parser.IParserLogService;
/* */ import org.eclipse.cdt.core.parser.IScannerInfo;
Expand All @@ -34,17 +35,15 @@
/* */ public static void main(String[] args)
/* */ throws Exception
/* */ {
/* 40 */ FileContent fileContent = FileContent.createForExternalFileLocation("/media/Dados/Codigos/Java/Projetos/OpenDevice/opendevice-hardware-libraries/arduino/OpenDevice/DeviceConnection.h");
/* */
/* 45 */ Map definedSymbols = new HashMap();
/* 46 */ String[] includePaths = new String[0];
/* 47 */ IScannerInfo info = new ScannerInfo(definedSymbols, includePaths);
/* 48 */ IParserLogService log = new DefaultLogService();
/* */
/* 50 */ IncludeFileContentProvider emptyIncludes = IncludeFileContentProvider.getEmptyFilesProvider();
/* */
/* 52 */ int opts = 8;
/* 53 */ IASTTranslationUnit translationUnit = GPPLanguage.getDefault().getASTTranslationUnit(fileContent, info, emptyIncludes, null, opts, log);
/* 40 */ FileContent fc = FileContent.create("TestFile", "int a = 21;".toCharArray());
Map<String, String> macroDefinitions = new HashMap();
String[] includeSearchPaths = new String[0];
IScannerInfo si = new ScannerInfo(macroDefinitions, includeSearchPaths);
IncludeFileContentProvider ifcp = IncludeFileContentProvider.getEmptyFilesProvider();
IIndex idx = null;
int options = 8;
IParserLogService log = new DefaultLogService();
/* 53 */ IASTTranslationUnit translationUnit = GPPLanguage.getDefault().getASTTranslationUnit(fc, si, ifcp, (IIndex)idx, options, log);;
/* */
/* 56 */ IASTPreprocessorIncludeStatement[] includes = translationUnit.getIncludeDirectives();
/* 57 */ for (IASTPreprocessorIncludeStatement include : includes) {
Expand Down Expand Up @@ -168,7 +167,9 @@
/* 181 */ offset = "UnsupportedOperationException";
/* */ }
/* */
/* 184 */ System.out.println(String.format(new StringBuilder("%1$").append(index * 2).append("s").toString(), new Object[] { "-" }) + node.getClass().getSimpleName() + offset + " -> " + (printContents ? node.getRawSignature().replaceAll("\n", " \\ ") : node.getRawSignature().subSequence(0, 5)));
/* 184 */ System.out.println(String.format(new StringBuilder("%1$").append(index * 2).append("s").toString(),
new Object[] { "-" }) + node.getClass().getSimpleName() + offset + " -> " +
(printContents ? node.getRawSignature().replaceAll("\n", " \\ ") : node.getRawSignature().subSequence(0, 5)));
/* */
/* 186 */ for (IASTNode iastNode : children)
/* 187 */ printTree(iastNode, index + 1);
Expand Down