diff --git a/src/main/grammar/RelAstGrammar.bnf b/src/main/grammar/RelAstGrammar.bnf index cf66ba0d2edb46dc0defec346c70c135ebdd801e..fa241e1c37e1ec6594a4a1a05d5773573f46a49f 100644 --- a/src/main/grammar/RelAstGrammar.bnf +++ b/src/main/grammar/RelAstGrammar.bnf @@ -23,7 +23,7 @@ type_decl ::= ABSTRACT? ID (COL ID)? (ASSIGN component*)? SCOL component ::= (SLASH actual_component SLASH) | actual_component -actual_component ::= (ID (COL ID)? STAR?) | (LBRACKET ID (COL ID)? RBRACKET) | (LT ID (COL (ID | java_type_use))? GT) +actual_component ::= (ID (COL ID)? STAR?) | (LBRACKET ID (COL ID)? RBRACKET) | (LT ID (COL (java_type_use))? GT) java_type_use ::= parameterized_java_type_use | simple_java_type_use diff --git a/src/main/java/org/jastadd/tooling/RelAstGrammarColorSettingsPage.java b/src/main/java/org/jastadd/tooling/RelAstGrammarColorSettingsPage.java new file mode 100644 index 0000000000000000000000000000000000000000..bb980d4b616ea414d7a8a808b78c3613ad212326 --- /dev/null +++ b/src/main/java/org/jastadd/tooling/RelAstGrammarColorSettingsPage.java @@ -0,0 +1,87 @@ +package org.jastadd.tooling; + + +import com.intellij.openapi.editor.colors.TextAttributesKey; +import com.intellij.openapi.fileTypes.SyntaxHighlighter; +import com.intellij.openapi.options.colors.AttributesDescriptor; +import com.intellij.openapi.options.colors.ColorDescriptor; +import com.intellij.openapi.options.colors.ColorSettingsPage; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.util.Map; + +public class RelAstGrammarColorSettingsPage implements ColorSettingsPage { + + private static final AttributesDescriptor[] DESCRIPTORS = new AttributesDescriptor[]{ + + new AttributesDescriptor("Keyword", RelAstGrammarSyntaxHighlighter.KEYWORD), + new AttributesDescriptor("Production Rule Symbol", RelAstGrammarSyntaxHighlighter.ASSIGN), + new AttributesDescriptor("Relation Direction", RelAstGrammarSyntaxHighlighter.RELATON_DIR), + new AttributesDescriptor("Superclass Symbol", RelAstGrammarSyntaxHighlighter.COL), + new AttributesDescriptor("Comma", RelAstGrammarSyntaxHighlighter.COMMA), + new AttributesDescriptor("Documentation Comment", RelAstGrammarSyntaxHighlighter.DOCCOMMENT), + new AttributesDescriptor("Namespace Qualifier Dot", RelAstGrammarSyntaxHighlighter.DOT), + new AttributesDescriptor("Identifier", RelAstGrammarSyntaxHighlighter.ID), + new AttributesDescriptor("Terminal Symbol Bracket", RelAstGrammarSyntaxHighlighter.ANGLE_BRACKET), + new AttributesDescriptor("Block Comment", RelAstGrammarSyntaxHighlighter.MULTILINECOMMENT), + new AttributesDescriptor("0..1 Multiplicity", RelAstGrammarSyntaxHighlighter.QUESTION_MARK), + new AttributesDescriptor("Optional Bracket", RelAstGrammarSyntaxHighlighter.BRACKET), + new AttributesDescriptor("Semicolon", RelAstGrammarSyntaxHighlighter.SCOL), + new AttributesDescriptor("Line Comment", RelAstGrammarSyntaxHighlighter.SINGLELINECOMMENT), + new AttributesDescriptor("NTA Bracket", RelAstGrammarSyntaxHighlighter.SLASH), + new AttributesDescriptor("Star Multiplicity", RelAstGrammarSyntaxHighlighter.STAR), + new AttributesDescriptor("Bad Value", RelAstGrammarSyntaxHighlighter.BAD_CHARACTER) + }; + + @Nullable + @Override + public Icon getIcon() { + return JastAddIcons.FILE; + } + + @NotNull + @Override + public SyntaxHighlighter getHighlighter() { + return new RelAstGrammarSyntaxHighlighter(); + } + + @NotNull + @Override + public String getDemoText() { + return "Program ::= GrammarFile* <NameList:java.util.List<String>>;\n" + + "abstract Grammar ::= Declaration*; /* good rule! */\n" + + "GrammarFile : Grammar ::= <FileName> ; // also good rule\n" + + "/** the best rule */\n" + + "abstract Declaration ::= Comment* /Comment/;\n" + + "TypeDecl:Declaration ::= <Name> <Abstract:boolean> Component* [Comment];\n" + + "rel TypeDecl.SuperType? <-> TypeDecl.SubType*;\n" + + "/* Bad character: */ #"; + } + + @Nullable + @Override + public Map<String, TextAttributesKey> getAdditionalHighlightingTagToDescriptorMap() { + return null; + } + + @NotNull + @Override + public AttributesDescriptor[] getAttributeDescriptors() { + return DESCRIPTORS; + } + + @NotNull + @Override + public ColorDescriptor[] getColorDescriptors() { + return ColorDescriptor.EMPTY_ARRAY; + } + + @NotNull + @Override + public String getDisplayName() { + return "RelAstGrammar"; + } + +} diff --git a/src/main/java/org/jastadd/tooling/RelAstGrammarSyntaxHighlighter.java b/src/main/java/org/jastadd/tooling/RelAstGrammarSyntaxHighlighter.java new file mode 100644 index 0000000000000000000000000000000000000000..fad48b51826737b77d6083c010314e5843b73de5 --- /dev/null +++ b/src/main/java/org/jastadd/tooling/RelAstGrammarSyntaxHighlighter.java @@ -0,0 +1,117 @@ +package org.jastadd.tooling; + + +import com.intellij.lexer.Lexer; +import com.intellij.openapi.editor.DefaultLanguageHighlighterColors; +import com.intellij.openapi.editor.HighlighterColors; +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.jastadd.tooling.lexer.RelAstGrammarFlexAdapter; +import org.jastadd.tooling.parser.RelAstGrammarTypes; +import org.jetbrains.annotations.NotNull; + +import static com.intellij.openapi.editor.colors.TextAttributesKey.createTextAttributesKey; + +public class RelAstGrammarSyntaxHighlighter extends SyntaxHighlighterBase { + + public static final TextAttributesKey KEYWORD = createTextAttributesKey("RELAST_KEYWORD", DefaultLanguageHighlighterColors.KEYWORD); + public static final TextAttributesKey ASSIGN = createTextAttributesKey("RELAST_ASSIGN", DefaultLanguageHighlighterColors.OPERATION_SIGN); + public static final TextAttributesKey RELATON_DIR = createTextAttributesKey("RELAST_RELATION_DIR", DefaultLanguageHighlighterColors.OPERATION_SIGN); + public static final TextAttributesKey COL = createTextAttributesKey("RELAST_COL", DefaultLanguageHighlighterColors.DOT); + public static final TextAttributesKey COMMA = createTextAttributesKey("RELAST_COMMA", DefaultLanguageHighlighterColors.COMMA); + public static final TextAttributesKey DOCCOMMENT = createTextAttributesKey("RELAST_DOCCOMMENT", DefaultLanguageHighlighterColors.DOC_COMMENT); + public static final TextAttributesKey DOT = createTextAttributesKey("RELAST_DOT", DefaultLanguageHighlighterColors.DOT); + public static final TextAttributesKey ID = createTextAttributesKey("RELAST_ID", DefaultLanguageHighlighterColors.IDENTIFIER); + public static final TextAttributesKey BRACKET = createTextAttributesKey("RELAST_BRACKET", DefaultLanguageHighlighterColors.BRACKETS); + public static final TextAttributesKey ANGLE_BRACKET = createTextAttributesKey("ANGLE_BRACKET", DefaultLanguageHighlighterColors.BRACKETS); + public static final TextAttributesKey MULTILINECOMMENT = createTextAttributesKey("RELAST_MULTILINECOMMENT", DefaultLanguageHighlighterColors.BLOCK_COMMENT); + public static final TextAttributesKey QUESTION_MARK = createTextAttributesKey("RELAST_QUESTION_MARK", DefaultLanguageHighlighterColors.OPERATION_SIGN); + public static final TextAttributesKey SCOL = createTextAttributesKey("RELAST_SCOL", DefaultLanguageHighlighterColors.SEMICOLON); + public static final TextAttributesKey SINGLELINECOMMENT = createTextAttributesKey("RELAST_SINGLELINECOMMENT", DefaultLanguageHighlighterColors.LINE_COMMENT); + public static final TextAttributesKey SLASH = createTextAttributesKey("RELAST_SLASH", DefaultLanguageHighlighterColors.BRACKETS); + public static final TextAttributesKey STAR = createTextAttributesKey("RELAST_STAR", DefaultLanguageHighlighterColors.OPERATION_SIGN); + public static final TextAttributesKey BAD_CHARACTER = createTextAttributesKey("SIMPLE_BAD_CHARACTER", HighlighterColors.BAD_CHARACTER); + + private static final TextAttributesKey[] KEYWORD_KEYS = new TextAttributesKey[]{KEYWORD}; + private static final TextAttributesKey[] ASSIGN_KEYS = new TextAttributesKey[]{ASSIGN}; + private static final TextAttributesKey[] RELATION_DIR_KEYS = new TextAttributesKey[]{RELATON_DIR}; + private static final TextAttributesKey[] COL_KEYS = new TextAttributesKey[]{COL}; + private static final TextAttributesKey[] COMMA_KEYS = new TextAttributesKey[]{COMMA}; + private static final TextAttributesKey[] DOCCOMMENT_KEYS = new TextAttributesKey[]{DOCCOMMENT}; + private static final TextAttributesKey[] DOT_KEYS = new TextAttributesKey[]{DOT}; + private static final TextAttributesKey[] ID_KEYS = new TextAttributesKey[]{ID}; + private static final TextAttributesKey[] ANGLE_BRACKET_KEYS = new TextAttributesKey[]{ANGLE_BRACKET}; + private static final TextAttributesKey[] MULTILINECOMMENT_KEYS = new TextAttributesKey[]{MULTILINECOMMENT}; + private static final TextAttributesKey[] QUESTION_MARK_KEYS = new TextAttributesKey[]{QUESTION_MARK}; + private static final TextAttributesKey[] BRACKET_KEYS = new TextAttributesKey[]{BRACKET}; + private static final TextAttributesKey[] SCOL_KEYS = new TextAttributesKey[]{SCOL}; + private static final TextAttributesKey[] SINGLELINECOMMENT_KEYS = new TextAttributesKey[]{SINGLELINECOMMENT}; + private static final TextAttributesKey[] SLASH_KEYS = new TextAttributesKey[]{SLASH}; + private static final TextAttributesKey[] STAR_KEYS = new TextAttributesKey[]{STAR}; + + private static final TextAttributesKey[] BAD_CHAR_KEYS = new TextAttributesKey[]{BAD_CHARACTER}; + private static final TextAttributesKey[] EMPTY_KEYS = new TextAttributesKey[0]; + + @NotNull + @Override + public Lexer getHighlightingLexer() { + return new RelAstGrammarFlexAdapter(); + } + + @NotNull + @Override + public TextAttributesKey[] getTokenHighlights(IElementType tokenType) { + + if (tokenType.equals(RelAstGrammarTypes.ABSTRACT)) { + return KEYWORD_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.ASSIGN)) { + return ASSIGN_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.BIDIRECTIONAL)) { + return RELATION_DIR_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.COL)) { + return COL_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.COMMA)) { + return COMMA_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.DOCCOMMENT)) { + return DOCCOMMENT_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.DOT)) { + return DOT_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.GT)) { + return ANGLE_BRACKET_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.ID)) { + return ID_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.LBRACKET)) { + return BRACKET_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.LEFT)) { + return RELATION_DIR_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.LT)) { + return ANGLE_BRACKET_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.MULTILINECOMMENT)) { + return MULTILINECOMMENT_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.QUESTION_MARK)) { + return QUESTION_MARK_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.RBRACKET)) { + return BRACKET_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.REL)) { + return KEYWORD_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.RIGHT)) { + return RELATION_DIR_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.SCOL)) { + return SCOL_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.SINGLELINECOMMENT)) { + return SINGLELINECOMMENT_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.SLASH)) { + return SLASH_KEYS; + } else if (tokenType.equals(RelAstGrammarTypes.STAR)) { + return STAR_KEYS; + } else if (tokenType.equals(TokenType.BAD_CHARACTER)) { + return BAD_CHAR_KEYS; + } else { + return EMPTY_KEYS; + } + + } + +} diff --git a/src/main/java/org/jastadd/tooling/RelAstGrammarSyntaxHighlighterFactory.java b/src/main/java/org/jastadd/tooling/RelAstGrammarSyntaxHighlighterFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..2860248c7929f051c586a822696ee67a50901f23 --- /dev/null +++ b/src/main/java/org/jastadd/tooling/RelAstGrammarSyntaxHighlighterFactory.java @@ -0,0 +1,17 @@ +package org.jastadd.tooling; + +import com.intellij.openapi.fileTypes.SyntaxHighlighter; +import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.NotNull; + +public class RelAstGrammarSyntaxHighlighterFactory extends SyntaxHighlighterFactory { + + @NotNull + @Override + public SyntaxHighlighter getSyntaxHighlighter(Project project, VirtualFile virtualFile) { + return new RelAstGrammarSyntaxHighlighter(); + } + +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index bb2ac1d0142e645d7aac7b571c7bcb57ab09d775..de4f7b65b0f8e19b96ddfc36937e8c736ae49a2b 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -17,6 +17,10 @@ fieldName="INSTANCE" language="JastAddGrammar" extensions="relast"/> <lang.parserDefinition language="JastAddGrammar" implementationClass="org.jastadd.tooling.parser.RelAstGrammarParserDefinition"/> + <lang.syntaxHighlighterFactory language="JastAddGrammar" + implementationClass="org.jastadd.tooling.RelAstGrammarSyntaxHighlighterFactory"/> + + <colorSettingsPage implementation="org.jastadd.tooling.RelAstGrammarColorSettingsPage"/> </extensions> <actions>