-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from TauMC/caching
Add caching
- Loading branch information
Showing
6 changed files
with
804 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.taumc.glsl; | ||
|
||
import org.taumc.glsl.grammar.GLSLParser; | ||
import org.taumc.glsl.grammar.GLSLParserBaseListener; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public class InjectorPoint extends GLSLParserBaseListener { | ||
|
||
private final Transformer transformer; | ||
private final boolean function; | ||
|
||
public InjectorPoint(Transformer transformer, boolean function) { | ||
this.transformer = transformer; | ||
this.function = function; | ||
} | ||
|
||
@Override | ||
public void enterFunction_definition(GLSLParser.Function_definitionContext ctx) { | ||
if (ctx.getParent() instanceof GLSLParser.External_declarationContext list) { | ||
transformer.function = list; | ||
transformer.variable = list; | ||
} | ||
} | ||
|
||
@Override | ||
public void enterStorage_qualifier(GLSLParser.Storage_qualifierContext ctx) { | ||
if (function) { | ||
return; | ||
} | ||
var parent = ctx.getParent(); | ||
while (!(parent instanceof GLSLParser.External_declarationContext list)) { | ||
if (parent.getParent() == null) { | ||
return; | ||
} | ||
parent = parent.getParent(); | ||
} | ||
transformer.variable = list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.taumc.glsl; | ||
|
||
import com.github.bsideup.jabel.Desugar; | ||
import org.antlr.v4.runtime.BailErrorStrategy; | ||
import org.antlr.v4.runtime.BufferedTokenStream; | ||
import org.antlr.v4.runtime.CharStreams; | ||
import org.antlr.v4.runtime.CommonTokenStream; | ||
import org.antlr.v4.runtime.ConsoleErrorListener; | ||
import org.antlr.v4.runtime.DefaultErrorStrategy; | ||
import org.antlr.v4.runtime.Parser; | ||
import org.antlr.v4.runtime.atn.PredictionMode; | ||
import org.taumc.glsl.grammar.GLSLLexer; | ||
import org.taumc.glsl.grammar.GLSLParser; | ||
import org.taumc.glsl.grammar.GLSLPreParser; | ||
|
||
import java.util.function.Function; | ||
|
||
public class ShaderParser { | ||
@Desugar | ||
public record ParsedShader(GLSLPreParser.Translation_unitContext pre, GLSLParser.Translation_unitContext full) {} | ||
|
||
private static void configureNoError(Parser parser) { | ||
parser.setErrorHandler(new BailErrorStrategy()); | ||
parser.removeErrorListeners(); | ||
parser.getInterpreter().setPredictionMode(PredictionMode.SLL); | ||
} | ||
|
||
private static void configureError(Parser parser) { | ||
parser.setErrorHandler(new DefaultErrorStrategy()); | ||
parser.addErrorListener(ConsoleErrorListener.INSTANCE); | ||
parser.getInterpreter().setPredictionMode(PredictionMode.LL); | ||
} | ||
|
||
private static ParsedShader tryParse(GLSLLexer lexer, GLSLPreParser preParser, GLSLParser parser) { | ||
var pre = preParser.translation_unit(); | ||
lexer.reset(); | ||
parser.reset(); | ||
var full = parser.translation_unit(); | ||
return new ParsedShader(pre, full); | ||
} | ||
|
||
public static ParsedShader parseShader(String shader) { | ||
GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(shader)); | ||
GLSLPreParser preParser = new GLSLPreParser(new BufferedTokenStream(lexer)); | ||
GLSLParser parser = new GLSLParser(new CommonTokenStream(lexer)); | ||
preParser.setBuildParseTree(true); | ||
parser.setBuildParseTree(true); | ||
configureNoError(parser); | ||
configureNoError(preParser); | ||
try { | ||
return tryParse(lexer, preParser, parser); | ||
} catch (Exception e) { | ||
lexer.reset(); | ||
parser.reset(); | ||
preParser.reset(); | ||
configureError(parser); | ||
configureError(preParser); | ||
return tryParse(lexer, preParser, parser); | ||
} | ||
} | ||
|
||
public static <T> T parseSnippet(String codeSnippet, Function<GLSLParser, T> parseLogic) { | ||
GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(codeSnippet)); | ||
GLSLParser parser = new GLSLParser(new CommonTokenStream(lexer)); | ||
parser.setBuildParseTree(true); | ||
configureNoError(parser); | ||
try { | ||
return parseLogic.apply(parser); | ||
} catch (Exception e) { | ||
lexer.reset(); | ||
parser.reset(); | ||
configureError(parser); | ||
return parseLogic.apply(parser); | ||
} | ||
} | ||
} |
Oops, something went wrong.