use of org.anarres.cpp.InputLexerSource in project Cpp4CIA by thanhminhmr.
the class PreprocessorBuilder method build.
@Nonnull
public static char[] build(@Nonnull Path projectRootPath, @Nonnull List<Path> projectFiles, @Nonnull List<Path> includePaths, boolean isReadable) throws CppException {
try {
final Preprocessor preprocessor = new Preprocessor(EMPTY_PREPROCESSOR_LISTENER);
preprocessor.addFeatures(FEATURE_LIST);
preprocessor.setSystemIncludePath(includePaths);
final StringBuilder builder = new StringBuilder();
for (final Path sourceFile : includeList(projectFiles, includePaths)) {
builder.append("#include \"").append(projectRootPath.relativize(sourceFile)).append("\"\n");
}
final Path virtualFile = projectRootPath.resolve(UUID.randomUUID() + ".virtual_file");
preprocessor.addInput(new InputLexerSource(new StringReader(builder.toString()), virtualFile));
// =====
final StringBuilder fileContent = new StringBuilder();
if (isReadable) {
readablePreprocessor(preprocessor, fileContent);
} else {
fastPreprocessor(preprocessor, fileContent);
}
// =====
char[] content = new char[fileContent.length()];
fileContent.getChars(0, content.length, content, 0);
return content;
} catch (IOException | LexerException e) {
throw new CppException("Cannot preprocess the source code!", e);
}
}
Aggregations