Search in sources :

Example 1 with LexerException

use of org.anarres.cpp.LexerException 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)

Example 2 with LexerException

use of org.anarres.cpp.LexerException in project Iris by IrisShaders.

the class PropertiesPreprocessor method preprocessSource.

// Derived from ShaderProcessor.glslPreprocessSource, which is derived from GlShader from Canvas, licenced under LGPL
public static String preprocessSource(String source, ShaderPackOptions shaderPackOptions) {
    if (source.contains(PropertyCollectingListener.PROPERTY_MARKER)) {
        throw new RuntimeException("Some shader author is trying to exploit internal Iris implementation details, stop!");
    }
    List<String> booleanValues = getBooleanValues(shaderPackOptions);
    Map<String, String> stringValues = getStringValues(shaderPackOptions);
    @SuppressWarnings("resource") final Preprocessor pp = new Preprocessor();
    try {
        for (String value : booleanValues) {
            pp.addMacro(value);
        }
        pp.addMacro("MC_VERSION", StandardMacros.getMcVersion());
    } catch (LexerException e) {
        e.printStackTrace();
    }
    stringValues.forEach((name, value) -> {
        try {
            pp.addMacro(name, value);
        } catch (LexerException e) {
            e.printStackTrace();
        }
    });
    return process(pp, source);
}
Also used : Preprocessor(org.anarres.cpp.Preprocessor) LexerException(org.anarres.cpp.LexerException)

Example 3 with LexerException

use of org.anarres.cpp.LexerException in project Iris by IrisShaders.

the class JcppProcessor method glslPreprocessSource.

// Derived from GlShader from Canvas, licenced under LGPL
public static String glslPreprocessSource(String source, Iterable<StringPair> environmentDefines) {
    if (source.contains(GlslCollectingListener.VERSION_MARKER) || source.contains(GlslCollectingListener.EXTENSION_MARKER)) {
        throw new RuntimeException("Some shader author is trying to exploit internal Iris implementation details, stop!");
    }
    // Note: This is an absolutely awful hack. But JCPP's lack of extensibility leaves me with no choice...
    // We should write our own preprocessor at some point to avoid this.
    // 
    // Why are we doing this awful hack instead of just using the preprocessor like a normal person? Because it lets
    // us only hoist #extension directives if they're actually used. This is needed for shader packs written on
    // lenient drivers that allow #extension directives to be placed anywhere to work on strict drivers like Mesa
    // that require #extension directives to occur at the top.
    // 
    // TODO: This allows #version to not appear as the first non-comment non-whitespace thing in the file.
    // That's not the behavior we want. If you're reading this, don't rely on this behavior.
    source = source.replace("#version", GlslCollectingListener.VERSION_MARKER);
    source = source.replace("#extension", GlslCollectingListener.EXTENSION_MARKER);
    GlslCollectingListener listener = new GlslCollectingListener();
    @SuppressWarnings("resource") final Preprocessor pp = new Preprocessor();
    // in errors...
    try {
        for (StringPair envDefine : environmentDefines) {
            pp.addMacro(envDefine.getKey(), envDefine.getValue());
        }
    } catch (LexerException e) {
        throw new RuntimeException("Unexpected LexerException processing macros", e);
    }
    pp.setListener(listener);
    pp.addInput(new StringLexerSource(source, true));
    pp.addFeature(Feature.KEEPCOMMENTS);
    final StringBuilder builder = new StringBuilder();
    try {
        for (; ; ) {
            final Token tok = pp.token();
            if (tok == null)
                break;
            if (tok.getType() == Token.EOF)
                break;
            builder.append(tok.getText());
        }
    } catch (final Exception e) {
        throw new RuntimeException("GLSL source pre-processing failed", e);
    }
    builder.append("\n");
    source = listener.collectLines() + builder;
    return source;
}
Also used : StringLexerSource(org.anarres.cpp.StringLexerSource) StringPair(net.coderbot.iris.shaderpack.StringPair) Token(org.anarres.cpp.Token) Preprocessor(org.anarres.cpp.Preprocessor) LexerException(org.anarres.cpp.LexerException) LexerException(org.anarres.cpp.LexerException)

Example 4 with LexerException

use of org.anarres.cpp.LexerException in project Iris by IrisShaders.

the class PropertiesPreprocessor method preprocessSource.

// Derived from ShaderProcessor.glslPreprocessSource, which is derived from GlShader from Canvas, licenced under LGPL
public static String preprocessSource(String source, ShaderPackOptions shaderPackOptions, Iterable<StringPair> environmentDefines) {
    if (source.contains(PropertyCollectingListener.PROPERTY_MARKER)) {
        throw new RuntimeException("Some shader author is trying to exploit internal Iris implementation details, stop!");
    }
    List<String> booleanValues = getBooleanValues(shaderPackOptions);
    Map<String, String> stringValues = getStringValues(shaderPackOptions);
    try (Preprocessor pp = new Preprocessor()) {
        for (String value : booleanValues) {
            pp.addMacro(value);
        }
        for (StringPair envDefine : environmentDefines) {
            pp.addMacro(envDefine.getKey(), envDefine.getValue());
        }
        stringValues.forEach((name, value) -> {
            try {
                pp.addMacro(name, value);
            } catch (LexerException e) {
                e.printStackTrace();
            }
        });
        return process(pp, source);
    } catch (IOException e) {
        throw new RuntimeException("Unexpected IOException while processing macros", e);
    } catch (LexerException e) {
        throw new RuntimeException("Unexpected LexerException processing macros", e);
    }
}
Also used : StringPair(net.coderbot.iris.shaderpack.StringPair) Preprocessor(org.anarres.cpp.Preprocessor) IOException(java.io.IOException) LexerException(org.anarres.cpp.LexerException)

Aggregations

LexerException (org.anarres.cpp.LexerException)4 Preprocessor (org.anarres.cpp.Preprocessor)4 IOException (java.io.IOException)2 StringPair (net.coderbot.iris.shaderpack.StringPair)2 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 StringLexerSource (org.anarres.cpp.StringLexerSource)1 Token (org.anarres.cpp.Token)1