Skip to content

Commit

Permalink
Improve parsing of invalid preprocessor statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkyenus committed Oct 4, 2015
1 parent 66a5336 commit e519d33
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/glslplugin/annotation/impl/UnreachableAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import glslplugin.annotation.Annotator;
import glslplugin.lang.elements.GLSLElement;
import glslplugin.lang.elements.GLSLElementTypes;
import glslplugin.lang.elements.GLSLTokenTypes;
import glslplugin.lang.elements.statements.*;
import org.jetbrains.annotations.NotNull;

Expand All @@ -50,7 +51,7 @@ public void annotate(GLSLStatement expr, AnnotationHolder holder) {

PsiElement element = expr.getNextSibling();
while (element != null) {
if (element instanceof GLSLElement && element.getNode().getElementType() != GLSLElementTypes.PREPROCESSOR_DIRECTIVE) {
if (element instanceof GLSLElement && !GLSLTokenTypes.PREPROCESSOR_DIRECTIVES.contains(element.getNode().getElementType())) {
if (element instanceof GLSLLabelStatement) return;
PsiElement child = element.getFirstChild();

Expand All @@ -60,7 +61,7 @@ public void annotate(GLSLStatement expr, AnnotationHolder holder) {
}else{
do {
IElementType type = child.getNode().getElementType();
if(type != GLSLElementTypes.PREPROCESSOR_DIRECTIVE && type != TokenType.WHITE_SPACE){
if(!GLSLTokenTypes.PREPROCESSOR_DIRECTIVES.contains(type) && type != TokenType.WHITE_SPACE){
if (child instanceof GLSLLabelStatement) return;
Annotation annotation = holder.createWarningAnnotation(child, "Unreachable expression");
annotation.setTextAttributes(unreachableAttributes);
Expand Down
2 changes: 0 additions & 2 deletions src/glslplugin/lang/elements/GLSLElementTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.IFileElementType;
import glslplugin.lang.GLSLLanguage;
import glslplugin.lang.elements.expressions.GLSLLiteral;

public class GLSLElementTypes {
public static final IFileElementType FILE = new IFileElementType(Language.findInstance(GLSLLanguage.class));

public static final IElementType PREPROCESSOR_DIRECTIVE = new GLSLElementType("PREPROCESSOR_DIRECTIVE");
//Workaround before proper text redefinition of remapped tokens is implemented, see RedefinedTokenElementType below
//public static final IElementType REDEFINED_TOKEN = new GLSLElementType("REDEFINED_TOKEN");

Expand Down
7 changes: 5 additions & 2 deletions src/glslplugin/lang/elements/GLSLTokenTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ public class GLSLTokenTypes {
public static final IElementType PREPROCESSOR_CONCAT = new GLSLElementType("PREPROCESSOR_CONCAT");
public static final IElementType PREPROCESSOR_STRING = new GLSLElementType("PREPROCESSOR_STRING");

/** Not returned by lexer but used instead of PREPROCESSOR_[something] when the kind of directive is unknown */
public static final IElementType PREPROCESSOR_OTHER = new GLSLElementType("PREPROCESSOR_OTHER");

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

public static final TokenSet PREPROCESSOR_DIRECTIVES = TokenSet.create(
Expand All @@ -226,7 +229,8 @@ public class GLSLTokenTypes {
PREPROCESSOR_VERSION,
PREPROCESSOR_LINE,
PREPROCESSOR_DEFINED,
PREPROCESSOR_CONCAT);
PREPROCESSOR_CONCAT,
PREPROCESSOR_OTHER);

// Type specifiers
public static final TokenSet FLOAT_TYPE_SPECIFIER_NONARRAY = TokenSet.create(
Expand Down Expand Up @@ -353,7 +357,6 @@ public class GLSLTokenTypes {
QUALIFIER_TOKENS,
EXPRESSION_FIRST_SET
);
public static final TokenSet FUNCTION_IDENTIFIER_TOKENS = merge(TokenSet.create(IDENTIFIER), TYPE_SPECIFIER_NONARRAY_TOKENS);

public static TokenSet merge(TokenSet... sets) {
TokenSet tokenSet = TokenSet.create();
Expand Down
14 changes: 10 additions & 4 deletions src/glslplugin/lang/parser/GLSLParsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ protected final void parsePreprocessor() {
//false -> this is not a valid place for more preprocessor directives
//false -> don't substitute here (makes re"define"ing and "undef"ing impossible)

IElementType preprocessorType = b.getTokenType();
IElementType directiveType = b.getTokenType();

if(b.getTokenType() == PREPROCESSOR_DEFINE){
if(directiveType == PREPROCESSOR_DEFINE){
//Parse define
b.advanceLexer(false, false);//Get past DEFINE

Expand Down Expand Up @@ -120,7 +120,7 @@ protected final void parsePreprocessor() {
b.advanceLexer();
}
}
}else if(b.getTokenType() == PREPROCESSOR_UNDEF){
}else if(directiveType == PREPROCESSOR_UNDEF){
//Parse undefine
b.advanceLexer(false, false);//Get past UNDEF

Expand Down Expand Up @@ -155,7 +155,13 @@ protected final void parsePreprocessor() {
}
b.advanceLexer(false, false);//Get past PREPROCESSOR_END
//false -> don't check for PREPROCESSOR_BEGIN, we will handle that ourselves
preprocessor.done(preprocessorType);
if(directiveType == null || !PREPROCESSOR_DIRECTIVES.contains(directiveType)){
//Happens when typing new directive at the end of the file
//or when malformed directive is created (eg #foo)
preprocessor.done(PREPROCESSOR_OTHER);
}else{
preprocessor.done(directiveType);
}
b.advanceLexer_remapTokens(); //Remap explicitly after advancing without remapping, makes mess otherwise

if (b.getTokenType() == PREPROCESSOR_BEGIN) {
Expand Down

0 comments on commit e519d33

Please sign in to comment.