Voici un exemple d'analyseur CDT comme vous le souhaitez.
import java.util.HashMap;
import java.util.Map;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ILanguage;
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;
import org.eclipse.cdt.core.parser.IncludeFileContentProvider;
import org.eclipse.cdt.core.parser.ScannerInfo;
public class _CDTParser {
public static void main(String[] args) throws Exception {
String sourcecode = "int a; void test() {a++;}";
IASTTranslationUnit translationUnit = _CDTParser.getIASTTranslationUnit(sourcecode.toCharArray());
ASTVisitor visitor = new ASTVisitor() {
@Override
public int visit(IASTDeclaration declaration) {
// When CDT visit a declaration
System.out.println("Found a declaration: " + declaration.getRawSignature());
return PROCESS_CONTINUE;
}
};
// Enable CDT to visit declaration
visitor.shouldVisitDeclarations = true;
// Adapt visitor with source code unit
translationUnit.accept(visitor);
}
public static IASTTranslationUnit getIASTTranslationUnit(char[] code) throws Exception {
FileContent fc = FileContent.create("", code);
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 = ILanguage.OPTION_IS_SOURCE_UNIT;
IParserLogService log = new DefaultLogService();
return GPPLanguage.getDefault().getASTTranslationUnit(fc, si, ifcp, idx, options, log);
}
}
Résultats: une déclaration Trouvé: int a; J'ai trouvé une déclaration: void test() {a ++;}
Je ne connais pas grand-chose à l'écriture d'applications autonomes basées sur la plate-forme Eclipse, mais vous aurez probablement besoin de créer un espace de travail et d'utiliser l'espace de travail pour créer un projet. Quelques API pertinentes: 'ResourcesPlugin.getWorkspace()', 'IWorkspace.newProjectDescription()', 'IWorkspace.getRoot()', 'IWorkspaceRoot.getProject()', 'IProject.create()'. Une fois que vous avez un projet Eclipse ('IProject'), une étape supplémentaire est nécessaire pour créer un projet CDT (' ICProject') via 'CCorePlugin.getDefault(). CreateCDTProject()'. – HighCommander4
De plus, pour 'ResourcesPlugin.getWorkspace()' fonctionner, vous devrez peut-être faire quelque chose comme ceci: https://wiki.sei.cmu.edu/aadl/index.php/Creating_Headless_(Command_Line)_Plugins – HighCommander4
(Ce tutoriel peut être plus simple: http://www.developertesting.com/archives/month200508/20050823-HeadlessHelloWorldInEclipse.html) – HighCommander4