1 19 20 package org.netbeans.modules.lexer.gen.javacc; 21 22 import java.lang.reflect.Field ; 23 import java.lang.SecurityException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.Set ; 30 import java.util.List ; 31 import org.netbeans.modules.lexer.gen.MutableTokenId; 32 import org.netbeans.modules.lexer.gen.TokenTypes; 33 34 40 41 public class JavaCCTokenTypes extends TokenTypes { 42 43 private boolean defaultStateFieldFound; 44 45 private final Map name2sample = new HashMap (); 46 47 51 private int maxState; 52 53 public JavaCCTokenTypes(Class tokenTypesClass) { 54 super(tokenTypesClass); 55 } 56 57 protected void updateId(MutableTokenId id) { 58 super.updateId(id); 59 60 String sampleText = getSampleText(id.getTokenTypeName()); 61 if (sampleText != null) { 62 id.addSampleText(sampleText); 63 } 64 } 65 66 public String getSampleText(String tokenTypeName) { 67 inspect(); 68 69 return (String )name2sample.get(tokenTypeName); 70 } 71 72 public int getMaxState() { 73 return maxState; 74 } 75 76 protected boolean inspect() { 77 if (!super.inspect()) { 78 return false; 79 } 80 81 try { 82 Field tokenImageField = getTokenTypesClass().getDeclaredField("tokenImage"); 83 String [] tokenImage = (String [])tokenImageField.get(null); 84 85 for (int i = 0; i < tokenImage.length; i++) { 86 String tokenTypeName = getTokenTypeName(i); 87 if (tokenTypeName != null) { 88 String img = tokenImage[i]; 89 if (img != null && img.startsWith("\"")) { 90 img = img.substring(1, img.length() - 1); 91 name2sample.put(tokenTypeName, img); 92 } 93 } 94 } 95 96 } catch (NoSuchFieldException e) { 97 e.printStackTrace(); 98 } catch (SecurityException e) { 99 e.printStackTrace(); 100 } catch (IllegalAccessException e) { 101 e.printStackTrace(); 102 } 103 104 return true; } 106 107 protected boolean isAccepted(String tokenTypeName, int tokenTypeValue) { 108 if (!defaultStateFieldFound) { 109 if ("DEFAULT".equals(tokenTypeName)) { 110 defaultStateFieldFound = true; 111 } 112 113 } else { maxState = Math.max(maxState, tokenTypeValue); 115 } 116 117 return !defaultStateFieldFound; 118 } 119 120 } 121 122 | Popular Tags |