Skip to content

Instantly share code, notes, and snippets.

@AbigailBuccaneer
Created July 12, 2015 18:48
Show Gist options
  • Save AbigailBuccaneer/081e03ac7767a6a7f1f8 to your computer and use it in GitHub Desktop.
Save AbigailBuccaneer/081e03ac7767a6a7f1f8 to your computer and use it in GitHub Desktop.
PreprocessingLexer.java
package glslplugin.lang.scanner;
import com.intellij.lang.ForeignLeafType;
import com.intellij.lang.TokenWrapper;
import com.intellij.lexer.Lexer;
import com.intellij.lexer.LookAheadLexer;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.containers.ImmutableUserMap;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static glslplugin.lang.elements.GLSLTokenTypes.IDENTIFIER;
/**
* Created by abigail on 12/07/15.
*/
public class PreprocessingLexer extends LookAheadLexer {
public PreprocessingLexer(Lexer delegate) {
super(delegate);
}
Map<String, List<IElementType>> definitions;
@Override
public void start(CharSequence buffer, int startOffset, int endOffset, int initialState) {
super.start(buffer, startOffset, endOffset, initialState);
definitions = new HashMap<>();
definitions.put("hello", Arrays.asList(new ForeignLeafType(IDENTIFIER, "salut")));
}
@Override
public void advance() {
if (definitions.containsKey(getTokenText())) {
definitions.get(getTokenText()).forEach(this::addToken);
}
super.advance();
}
@NotNull
@Override
public CharSequence getTokenSequence() {
if (getTokenType() instanceof TokenWrapper) {
return ((TokenWrapper) getTokenType()).getValue();
}
return super.getTokenSequence();
}
@NotNull
@Override
public PreprocessingLexerPosition getCurrentPosition() {
return new PreprocessingLexerPosition(this, ImmutableUserMap.EMPTY);
}
@Override
protected void restore(LookAheadLexerPosition position) {
super.restore(position);
assert position instanceof PreprocessingLexerPosition;
definitions = ((PreprocessingLexerPosition) position).definitions;
}
protected static class PreprocessingLexerPosition extends LookAheadLexerPosition {
final Map<String, List<IElementType>> definitions;
public PreprocessingLexerPosition(final PreprocessingLexer lexer, ImmutableUserMap map) {
super(lexer, map);
definitions = new HashMap<>(lexer.definitions);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment