Skip to content

Commit

Permalink
Round some weird corner-cases of preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkyenus committed Oct 3, 2015
1 parent 5ede414 commit 683f9c6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package glslplugin.lang.elements.preprocessor;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import glslplugin.lang.elements.GLSLElementImpl;
import glslplugin.lang.elements.GLSLTokenTypes;
import glslplugin.lang.elements.reference.GLSLMacroReference;
import org.jetbrains.annotations.NotNull;

Expand All @@ -22,7 +24,13 @@ public GLSLMacroReference getReference() {
@Override
@NotNull
public String getName() {
return getText();
//Explicitly asking for IDENTIFIER works around corner-case
// when two replaced tokens are right next to each other and second becomes child of first

//It shouldn't happen anymore though
final PsiElement name = findChildByType(GLSLTokenTypes.IDENTIFIER);
if(name == null)return getText();
return name.getText();
}

}
5 changes: 3 additions & 2 deletions src/glslplugin/lang/parser/GLSLParsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected final void parsePreprocessor() {
// potentially parsing a preprocessor directive inside this one.
PsiBuilder.Marker preprocessor = b.mark();
b.advanceLexer(false, false); //Get past the PREPROCESSOR_BEGIN ("#")
//advanceLexer(false,false)
//advanceLexer(false,false) explanation:
//false -> this is not a valid place for more preprocessor directives
//false -> don't substitute here (makes re"define"ing and "undef"ing impossible)

Expand Down Expand Up @@ -145,9 +145,10 @@ protected final void parsePreprocessor() {
b.advanceLexer();
}
}
b.advanceLexer(false, true);//Get past PREPROCESSOR_END
b.advanceLexer(false, false);//Get past PREPROCESSOR_END
//false -> don't check for PREPROCESSOR_BEGIN, we will handle that ourselves
preprocessor.done(preprocessorType);
b.advanceLexer_remapTokens(); //Remap explicitly after advancing without remapping, makes mess otherwise

if (b.getTokenType() == PREPROCESSOR_BEGIN) {
parsePreprocessor();
Expand Down
22 changes: 21 additions & 1 deletion src/glslplugin/lang/parser/GLSLParsingBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,32 @@ public void advanceLexer(boolean checkForPreprocessor, boolean remapTokens){
}
}

if (remapTokens && definitions.get(getTokenText()) != null) {
if (remapTokens) {
advanceLexer_remapTokens();
}
}

public void advanceLexer_remapTokens(){
if (definitions.get(getTokenText()) != null) {
Marker macro = mark();
remapCurrentTokenAdvanceLexer_redefineTokens = false;
remapCurrentToken(definitions.get(getTokenText()));
remapCurrentTokenAdvanceLexer_redefineTokens = true;
macro.done(REDEFINED_TOKEN);
advanceLexer_remapTokens();
}
}

//Behold, the longest boolean on this hemisphere
//Used in advanceLexer_remapTokens to not remap immediately after advancing in remapCurrentToken
//That prevents two redefined tokens merging together (second becomes child of first)
//I know that it sounds complicated, but you will have to trust me.
private boolean remapCurrentTokenAdvanceLexer_redefineTokens = true;

@Override
protected void remapCurrentTokenAdvanceLexer() {
advanceLexer(false, remapCurrentTokenAdvanceLexer_redefineTokens);
}
}

//Utility code
Expand Down
21 changes: 20 additions & 1 deletion src/glslplugin/lang/parser/MultiRemapPsiBuilderAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ public void remapCurrentToken(IElementType type) {
remapCurrentToken(Collections.singletonList(type));
}

public void remapCurrentToken(Collection<IElementType> types) {
protected void remapCurrentTokenAdvanceLexer(){
advanceLexer();
}

public void remapCurrentToken(Collection<IElementType> types) {
remapCurrentTokenAdvanceLexer();
waitingTokens.addAll(0, types);
}

Expand All @@ -76,6 +80,19 @@ public Marker mark() {
return new DelegateMarker(super.mark());
}

@Override
public IElementType lookAhead(int steps) {
final Marker lookaheadMark = mark();
try{
for (int i = 0; i < steps; i++) {
advanceLexer();
}
return getTokenType();
}finally {
lookaheadMark.rollbackTo();
}
}

protected class DelegateMarker extends com.intellij.lang.impl.DelegateMarker {
protected ArrayList<IElementType> rollbackWaitingTokens = new ArrayList<IElementType>();

Expand All @@ -98,5 +115,7 @@ public void rollbackTo() {
super.rollbackTo();
waitingTokens = rollbackWaitingTokens;
}


}
}

0 comments on commit 683f9c6

Please sign in to comment.