在Eclipse之外使用CDT解析器(如何制作项目?) [英] Using CDT parser out of Eclipse(How to make project?)
本文介绍了在Eclipse之外使用CDT解析器(如何制作项目?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除了Eclipse,我正在尝试使用CDT解析器来解析C ++源代码.
I am trying to parse C++ source by using CDT parser apart from eclipse.
要获取AST,我必须对IncludeFileContentProvider进行索引. 要制作索引,我需要制作项目.我认为这个项目意味着日食项目.
To get AST, I have to make, index, IncludeFileContentProvider. To make index, I need to make project. I think this project means eclipse project.
但是我在eclipse之外使用CDT解析器. 在这种情况下,如何制作项目.
But I am using CDT parser outside of eclipse. In this case how to make project.
推荐答案
下面是您想要的CDT解析器示例.
Here is an example of CDT parser as you want.
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);
}
}
结果: 找到一个声明:int a; 找到了一个声明:void test(){a ++;}
Results: Found a declaration: int a; Found a declaration: void test() {a++;}
这篇关于在Eclipse之外使用CDT解析器(如何制作项目?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文