Skip to content

Commit

Permalink
Add very basic support for flow attributes, closes #137
Browse files Browse the repository at this point in the history
Took 1 hour 7 minutes
  • Loading branch information
Jan Polák committed Apr 5, 2022
1 parent 9215f93 commit 5f39859
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 4 deletions.
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ intellij {
//version.set("IC-212.4746.92")
updateSinceUntilBuild.set(false)
instrumentCode.set(false)
//plugins.add("PsiViewer:203-SNAPSHOT")
}

tasks {
Expand All @@ -45,4 +46,8 @@ tasks {

// Because it is broken and crashes the build
buildSearchableOptions.get().enabled = false

runIde.configure {
jvmArgs!!.add("-Didea.ProcessCanceledException=disabled")
}
}
2 changes: 1 addition & 1 deletion src/main/java/glslplugin/GLSLHighlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class GLSLHighlighter extends SyntaxHighlighterBase {
{ TextAttributesKey.createTextAttributesKey("GLSL.IDENTIFIER.VARYING", GLSL_IDENTIFIER[0]) };
public static final TextAttributesKey[] GLSL_IDENTIFIER_ATTRIBUTE =
{ TextAttributesKey.createTextAttributesKey("GLSL.IDENTIFIER.ATTRIBUTE", GLSL_IDENTIFIER[0]) };
static final TextAttributesKey[] GLSL_PREPROCESSOR_DIRECTIVE =
public static final TextAttributesKey[] GLSL_PREPROCESSOR_DIRECTIVE =
{ TextAttributesKey.createTextAttributesKey("GLSL.PREPROCESSOR_DIRECTIVE", DefaultLanguageHighlighterColors.METADATA) };
static final TextAttributesKey[] GLSL_PREPROCESSOR_RAW =
{ TextAttributesKey.createTextAttributesKey("GLSL.PREPROCESSOR_RAW", HighlighterColors.TEXT) };
Expand Down
1 change: 1 addition & 0 deletions src/main/java/glslplugin/annotation/GLSLAnnotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private static void add(Annotator<?> annotator) {
add(new ConditionCheckAnnotation());
add(new MissingReturnAnnotation());
add(new DeclarationAssignmentTypeAnnotation());
add(new FlowAttributeHighlightAnnotation());
add(new ReservedKeywordAnnotation());
add(new ReservedIdentifierAnnotation());
add(new InitializerListEmptyAnnotation());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package glslplugin.annotation.impl;

import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.HighlightSeverity;
import glslplugin.GLSLHighlighter;
import glslplugin.annotation.Annotator;
import glslplugin.lang.elements.declarations.GLSLDeclarator;
import glslplugin.lang.elements.declarations.GLSLQualifier;
import glslplugin.lang.elements.expressions.GLSLIdentifierExpression;
import glslplugin.lang.elements.preprocessor.GLSLFlowAttribute;
import glslplugin.lang.elements.types.GLSLQualifiedType;
import org.jetbrains.annotations.NotNull;

/**
* Highlight-only annotation for variable references which are uniforms or varyings.
* Highlighting style is the same as normal variables by default, so for this to be visible, new style must be selected manually.
*/
public class FlowAttributeHighlightAnnotation extends Annotator<GLSLFlowAttribute> {

@Override
public void annotate(GLSLFlowAttribute expr, AnnotationHolder holder) {
holder.newSilentAnnotation(HighlightSeverity.INFORMATION).textAttributes(GLSLHighlighter.GLSL_PREPROCESSOR_DIRECTIVE[0]).create();
}

@NotNull
@Override
public Class<GLSLFlowAttribute> getElementType() {
return GLSLFlowAttribute.class;
}
}
3 changes: 3 additions & 0 deletions src/main/java/glslplugin/lang/elements/GLSLElementTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public class GLSLElementTypes {

public static final IElementType CONDITION = new GLSLElementType("CONDITION");

// https://github.com/KhronosGroup/GLSL/blob/master/extensions/ext/GL_EXT_control_flow_attributes.txt
public static final IElementType FLOW_ATTRIBUTE = new GLSLElementType("FLOW_ATTRIBUTE");

//Preprocessor dropins
public static final class RedefinedTokenElementType extends GLSLElementType {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public GLSLElement create(ASTNode node) {
if (type == GLSLElementTypes.DEFAULT_STATEMENT) return new GLSLDefaultStatement(node);

if (type == GLSLElementTypes.PRECISION_STATEMENT) return new GLSLPrecisionStatement(node);
if (type == GLSLElementTypes.FLOW_ATTRIBUTE) return new GLSLFlowAttribute(node);

// types and structs
if (type == GLSLElementTypes.QUALIFIER) return new GLSLQualifier(node);
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/glslplugin/lang/elements/GLSLTokenTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,17 @@ public class GLSLTokenTypes {
public static final IElementType COMMENT_LINE = new GLSLElementType("COMMENT_LINE");
public static final IElementType COMMENT_BLOCK = new GLSLElementType("COMMENT_BLOCK");

public static final IElementType LEFT_BRACE = new GLSLElementType("LEFT_BRACE");
public static final IElementType RIGHT_BRACE = new GLSLElementType("RIGHT_BRACE");
/** ( */
public static final IElementType LEFT_PAREN = new GLSLElementType("LEFT_PAREN");
/** ) */
public static final IElementType RIGHT_PAREN = new GLSLElementType("RIGHT_PAREN");
/** { */
public static final IElementType LEFT_BRACE = new GLSLElementType("LEFT_BRACE");
/** } */
public static final IElementType RIGHT_BRACE = new GLSLElementType("RIGHT_BRACE");
/** [ */
public static final IElementType LEFT_BRACKET = new GLSLElementType("LEFT_BRACKET");
/** ] */
public static final IElementType RIGHT_BRACKET = new GLSLElementType("RIGHT_BRACKET");

public static final IElementType EQUAL = new GLSLElementType("EQUAL");
Expand Down Expand Up @@ -368,7 +374,8 @@ public class GLSLTokenTypes {
LEFT_BRACE, // compound_statement
BREAK_JUMP_STATEMENT, CONTINUE_JUMP_STATEMENT, RETURN_JUMP_STATEMENT, CASE_KEYWORD, DEFAULT_KEYWORD,
DISCARD_JUMP_STATEMENT, IF_KEYWORD,
DO_KEYWORD, FOR_KEYWORD, WHILE_KEYWORD, SWITCH_KEYWORD // flow control
DO_KEYWORD, FOR_KEYWORD, WHILE_KEYWORD, SWITCH_KEYWORD, // flow control
LEFT_BRACKET // [[attribute]]
),
QUALIFIER_TOKENS,
EXPRESSION_FIRST_SET
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package glslplugin.lang.elements.preprocessor;

import com.intellij.lang.ASTNode;
import glslplugin.lang.elements.GLSLElementImpl;
import org.jetbrains.annotations.NotNull;

/**
* See https://github.com/KhronosGroup/GLSL/blob/master/extensions/ext/GL_EXT_control_flow_attributes.txt
*/
public class GLSLFlowAttribute extends GLSLElementImpl {

public GLSLFlowAttribute(@NotNull ASTNode astNode) {
super(astNode);
}

}
30 changes: 30 additions & 0 deletions src/main/java/glslplugin/lang/parser/GLSLParsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,43 @@ private void eatInvalidOperators() {
mark.drop();
}

// https://github.com/KhronosGroup/GLSL/blob/master/extensions/ext/GL_EXT_control_flow_attributes.txt
private void tryParseFlowAttributes() {
// These are currently not semantically processed at all
final IElementType type = b.getTokenType();
if (type != LEFT_BRACKET) {
return;
}
final PsiBuilder.Marker mark = b.mark();
b.advanceLexer();
if (b.getTokenType() != LEFT_BRACKET) {
mark.rollbackTo();
return;
}
b.advanceLexer();

// We are not very particular about this, just consume tokens until we get to ]]
//TODO: More precise parsing
while (b.getTokenType() != RIGHT_BRACKET && !b.eof()) {
b.advanceLexer();
}
b.advanceLexer();
if (b.getTokenType() != RIGHT_BRACKET) {
mark.rollbackTo();
return;
}
b.advanceLexer();
mark.done(FLOW_ATTRIBUTE);
}

private boolean parseSimpleStatement() {
// simple_statement: declaration_statement
// | expression_statement
// | selection_statement
// | switch_statement
// | iteration_statement
// | jump_statement
tryParseFlowAttributes();
eatInvalidOperators();

final IElementType type = b.getTokenType();
Expand Down

0 comments on commit 5f39859

Please sign in to comment.