Bad characters highlighted. TODO: Error reporting

This commit is contained in:
Andrey Breslav
2011-01-03 15:48:29 +03:00
parent e2fca30f22
commit 728cbdf10f
2 changed files with 26 additions and 15 deletions
@@ -8,6 +8,7 @@ import com.intellij.openapi.editor.HighlighterColors;
import com.intellij.openapi.editor.SyntaxHighlighterColors;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase;
import com.intellij.psi.TokenType;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lexer.JetLexer;
@@ -20,7 +21,7 @@ public class JetHighlighter extends SyntaxHighlighterBase {
private static final Map<IElementType, TextAttributesKey> keys1;
private static final Map<IElementType, TextAttributesKey> keys2;
static final TextAttributesKey JET_KEYWORD = TextAttributesKey.createTextAttributesKey(
private static final TextAttributesKey JET_KEYWORD = TextAttributesKey.createTextAttributesKey(
"JET.KEYWORD",
SyntaxHighlighterColors.KEYWORD.getDefaultAttributes()
);
@@ -30,31 +31,26 @@ public class JetHighlighter extends SyntaxHighlighterBase {
SyntaxHighlighterColors.KEYWORD.getDefaultAttributes()
);
static final TextAttributesKey JET_NUMBER = TextAttributesKey.createTextAttributesKey(
private static final TextAttributesKey JET_NUMBER = TextAttributesKey.createTextAttributesKey(
"JET.NUMBER",
SyntaxHighlighterColors.NUMBER.getDefaultAttributes()
);
static final TextAttributesKey JET_STRING = TextAttributesKey.createTextAttributesKey(
private static final TextAttributesKey JET_STRING = TextAttributesKey.createTextAttributesKey(
"JET.STRING",
SyntaxHighlighterColors.STRING.getDefaultAttributes()
);
static final TextAttributesKey JET_COMMENT = TextAttributesKey.createTextAttributesKey(
private static final TextAttributesKey JET_COMMENT = TextAttributesKey.createTextAttributesKey(
"JET.COMMENT",
SyntaxHighlighterColors.LINE_COMMENT.getDefaultAttributes()
);
static final TextAttributesKey JET_BAD_CHARACTER = TextAttributesKey.createTextAttributesKey(
private static final TextAttributesKey JET_BAD_CHARACTER = TextAttributesKey.createTextAttributesKey(
"JET.BADCHARACTER",
HighlighterColors.BAD_CHARACTER.getDefaultAttributes()
);
@NotNull
public Lexer getHighlightingLexer() {
return new JetLexer();
@@ -83,5 +79,7 @@ public class JetHighlighter extends SyntaxHighlighterBase {
keys1.put(JetTokens.EOL_COMMENT, JET_COMMENT);
keys1.put(JetTokens.BLOCK_COMMENT, JET_COMMENT);
keys1.put(JetTokens.DOC_COMMENT, JET_COMMENT);
keys1.put(TokenType.BAD_CHARACTER, JET_BAD_CHARACTER);
}
}
@@ -6,6 +6,12 @@ import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.jet.lexer.JetKeywordToken;
import org.jetbrains.jet.lexer.JetToken;
import org.jetbrains.jet.lexer.JetTokens;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.jetbrains.jet.lexer.JetTokens.*;
@@ -13,6 +19,15 @@ import static org.jetbrains.jet.lexer.JetTokens.*;
* @author abreslav
*/
/*package*/ abstract class AbstractJetParsing {
private static final Set<String> SOFT_KEYWORD_TEXTS = new HashSet<String>();
static {
for (IElementType type : JetTokens.SOFT_KEYWORDS.getTypes()) {
JetKeywordToken keywordToken = (JetKeywordToken) type;
assert keywordToken.isSoft();
SOFT_KEYWORD_TEXTS.add(keywordToken.getValue());
}
}
protected final SemanticWhitespaceAwarePsiBuilder myBuilder;
public AbstractJetParsing(SemanticWhitespaceAwarePsiBuilder builder) {
@@ -77,14 +92,12 @@ import static org.jetbrains.jet.lexer.JetTokens.*;
}
protected void advance() {
// TODO: how to report errors on bad characters? (Other than highlighting)
myBuilder.advanceLexer();
}
protected IElementType tt() {
IElementType tokenType = myBuilder.getTokenType();
// TODO: review
if (tokenType == TokenType.BAD_CHARACTER) errorAndAdvance("Bad character");
return tokenType;
return myBuilder.getTokenType();
}
protected boolean at(final IElementType expectation) {
@@ -124,7 +137,7 @@ import static org.jetbrains.jet.lexer.JetTokens.*;
if (token == SEMICOLON) return true;
if (myBuilder.eolInLastWhitespace()) return true;
}
if (token == IDENTIFIER) {
if (token == IDENTIFIER && SOFT_KEYWORD_TEXTS.contains(myBuilder.getTokenText())) {
// TODO : this loop seems to be a bad solution
for (IElementType type : set.getTypes()) {
if (type instanceof JetKeywordToken) {