Search in sources :

Example 1 with InputLexerSource

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);
    }
}
Also used : Path(java.nio.file.Path) InputLexerSource(org.anarres.cpp.InputLexerSource) CppException(mrmathami.cia.cpp.CppException) StringReader(java.io.StringReader) Preprocessor(org.anarres.cpp.Preprocessor) IOException(java.io.IOException) LexerException(org.anarres.cpp.LexerException) Nonnull(mrmathami.annotations.Nonnull)

Aggregations

IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Path (java.nio.file.Path)1 Nonnull (mrmathami.annotations.Nonnull)1 CppException (mrmathami.cia.cpp.CppException)1 InputLexerSource (org.anarres.cpp.InputLexerSource)1 LexerException (org.anarres.cpp.LexerException)1 Preprocessor (org.anarres.cpp.Preprocessor)1