1 31 32 package org.antlr.works.syntax; 33 34 import org.antlr.works.ate.syntax.generic.ATESyntaxLexer; 35 import org.antlr.works.ate.syntax.generic.ATESyntaxParser; 36 import org.antlr.works.ate.syntax.language.ATELanguageSyntaxEngine; 37 import org.antlr.works.ate.syntax.misc.ATEToken; 38 import org.antlr.works.prefs.AWPrefs; 39 import org.antlr.works.syntax.element.*; 40 41 import javax.swing.text.AttributeSet ; 42 import javax.swing.text.SimpleAttributeSet ; 43 import javax.swing.text.StyleConstants ; 44 import java.awt.*; 45 import java.util.ArrayList ; 46 import java.util.List ; 47 48 public class GrammarSyntaxEngine extends ATELanguageSyntaxEngine { 49 50 public static final Color COLOR_PARSER = new Color(0.42f, 0, 0.42f); 51 public static final Color COLOR_LEXER = new Color(0, 0, 0.5f); 52 53 protected List <ElementRule> rules; 54 protected List <ElementGroup> groups; 55 protected List <ElementBlock> blocks; 56 protected List <ElementAction> actions; 57 protected List <ElementReference> references; 58 protected List <ATEToken> decls; 59 60 protected ElementGrammarName name; 61 62 protected SimpleAttributeSet parserRefAttr; 63 protected SimpleAttributeSet lexerRefAttr; 64 protected SimpleAttributeSet labelAttr; 65 protected SimpleAttributeSet actionRefAttr; 66 protected SimpleAttributeSet blockLabelAttr; 68 69 public GrammarSyntaxEngine() { 70 parserRefAttr = new SimpleAttributeSet (); 71 lexerRefAttr = new SimpleAttributeSet (); 72 labelAttr = new SimpleAttributeSet (); 73 actionRefAttr = new SimpleAttributeSet (); 74 blockLabelAttr = new SimpleAttributeSet (); 75 } 76 77 public void applyCommentAttribute(SimpleAttributeSet commentAttr) { 78 applyAttribute(commentAttr, AWPrefs.PREF_SYNTAX_COMMENT); 79 } 80 81 public void applyStringAttribute(SimpleAttributeSet stringAttr) { 82 applyAttribute(stringAttr, AWPrefs.PREF_SYNTAX_STRING); 83 } 84 85 public void applyKeywordAttribute(SimpleAttributeSet keywordAttr) { 86 applyAttribute(keywordAttr, AWPrefs.PREF_SYNTAX_KEYWORD); 87 } 88 89 public void applyAttribute(SimpleAttributeSet attr, String identifier) { 90 StyleConstants.setForeground(attr, AWPrefs.getSyntaxColor(identifier)); 91 StyleConstants.setBold(attr, AWPrefs.getSyntaxBold(identifier)); 92 StyleConstants.setItalic(attr, AWPrefs.getSyntaxItalic(identifier)); 93 } 94 95 public ATESyntaxLexer createLexer() { 96 return new GrammarSyntaxLexer(); 97 } 98 99 public ATESyntaxParser createParser() { 100 return new GrammarSyntaxParser(); 101 } 102 103 public void refreshColoring() { 104 super.refreshColoring(); 105 106 applyAttribute(parserRefAttr, AWPrefs.PREF_SYNTAX_PARSER); 107 applyAttribute(lexerRefAttr, AWPrefs.PREF_SYNTAX_LEXER); 108 applyAttribute(labelAttr, AWPrefs.PREF_SYNTAX_LABEL); 109 applyAttribute(actionRefAttr, AWPrefs.PREF_SYNTAX_REFS); 110 111 StyleConstants.setBold(blockLabelAttr, true); 112 } 113 114 public AttributeSet getAttributeForToken(ATEToken token) { 115 AttributeSet attr = super.getAttributeForToken(token); 116 switch(token.type) { 117 case GrammarSyntaxLexer.TOKEN_DECL: 118 case GrammarSyntaxLexer.TOKEN_REFERENCE: 119 if(((ElementToken)token).lexer) 120 attr = lexerRefAttr; 121 else 122 attr = parserRefAttr; 123 break; 124 125 case GrammarSyntaxLexer.TOKEN_LABEL: 126 attr = labelAttr; 127 break; 128 129 case GrammarSyntaxLexer.TOKEN_BLOCK_LIMIT: 130 case GrammarSyntaxLexer.TOKEN_BLOCK_LABEL: 131 attr = blockLabelAttr; 132 break; 133 134 case GrammarSyntaxLexer.TOKEN_INTERNAL_REF: 135 attr = actionRefAttr; 136 break; 137 } 138 return attr; 139 } 140 141 public static void setDelay(int delay) { 142 GrammarSyntaxEngine.delay = delay; 143 } 144 145 public synchronized List <ElementRule> getRules() { 146 return rules; 147 } 148 149 public synchronized List <ElementGroup> getGroups() { 150 return groups; 151 } 152 153 public synchronized List <ElementBlock> getBlocks() { 154 return blocks; 155 } 156 157 public synchronized List <ElementAction> getActions() { 158 return actions; 159 } 160 161 public synchronized List <ElementReference> getReferences() { 162 return references; 163 } 164 165 public synchronized List <ATEToken> getDecls() { 166 return decls; 167 } 168 169 public synchronized ElementGrammarName getName() { 170 return name; 171 } 172 173 public synchronized List <String > getDeclaredTokenNames() { 174 List <String > names = new ArrayList <String >(); 175 if(blocks != null) { 176 for(int index=0; index<blocks.size(); index++) { 177 ElementBlock block = blocks.get(index); 178 if(block.isTokenBlock) { 179 names.addAll(block.getDeclaredTokensAsString()); 180 } 181 } 182 } 183 return names; 184 } 185 186 public List <String > getPredefinedReferences() { 187 return GrammarSyntaxParser.predefinedReferences; 188 } 189 190 public synchronized String getTokenVocab() { 191 if(blocks == null) 192 return null; 193 194 for(int index=0; index<blocks.size(); index++) { 195 ElementBlock block = blocks.get(index); 196 if(block.isOptionsBlock) 197 return block.getTokenVocab(); 198 } 199 return null; 200 } 201 202 public synchronized List <String > getRuleNames() { 203 List <String > names = new ArrayList <String >(); 204 if(rules != null) { 205 for (int index=0; index<rules.size(); index++) { 206 ElementRule rule = rules.get(index); 207 names.add(rule.name); 208 } 209 } 210 return names; 211 } 212 213 public synchronized ElementRule getRuleAtIndex(int index) { 214 if(index < 0 || index >= rules.size()) 215 return null; 216 else 217 return rules.get(index); 218 } 219 220 223 224 protected synchronized void parserDidRun(ATESyntaxParser parser) { 225 GrammarSyntaxParser gp = (GrammarSyntaxParser)parser; 226 this.rules = new ArrayList <ElementRule>(gp.rules); 227 this.groups = new ArrayList <ElementGroup>(gp.groups); 228 this.blocks = new ArrayList <ElementBlock>(gp.blocks); 229 this.actions = new ArrayList <ElementAction>(gp.actions); 230 this.references = new ArrayList <ElementReference>(gp.references); 231 this.decls = new ArrayList <ATEToken>(gp.decls); 232 this.name = gp.name; 233 } 234 235 } 236 | Popular Tags |