1 21 22 package org.armedbear.j; 23 24 import java.awt.event.KeyEvent ; 25 import java.util.HashSet ; 26 27 public class CMode extends JavaMode implements Constants, Mode 28 { 29 private static final String [] cConditionals = { 30 "if", 31 "else", 32 "do", 33 "while", 34 "for", 35 "switch" 36 }; 37 38 private static CMode mode; 39 40 private CMode() 41 { 42 super(C_MODE, C_MODE_NAME); 43 keywords = new Keywords(this); 44 conditionals = cConditionals; 45 } 46 47 protected CMode(int id, String displayName) 48 { 49 super(id, displayName); 50 } 51 52 public static Mode getMode() 56 { 57 if (mode == null) 58 mode = new CMode(); 59 return mode; 60 } 61 62 public String getCommentStart() 63 { 64 return "/*"; 65 } 66 67 public String getCommentEnd() 68 { 69 return "*/"; 70 } 71 72 public Formatter getFormatter(Buffer buffer) 73 { 74 return new CFormatter(buffer, LANGUAGE_C); 75 } 76 77 protected void setKeyMapDefaults(KeyMap km) 78 { 79 super.setKeyMapDefaults(km); 80 km.mapKey('#', "electricPound"); 81 km.mapKey(KeyEvent.VK_M, CTRL_MASK, "cppFindMatch"); 82 km.mapKey(KeyEvent.VK_F6, CTRL_MASK, "iList"); 83 } 84 85 public void populateModeMenu(Editor editor, Menu menu) 86 { 87 menu.add(editor, "Compile...", 'C', "compile"); 88 menu.add(editor, "Recompile", 'R', "recompile"); 89 boolean enabled = CompilationCommands.getCompilationBuffer() != null; 90 menu.addSeparator(); 91 menu.add(editor, "Next Error", 'N', "nextError", enabled); 92 menu.add(editor, "Previous Error", 'P', "previousError", enabled); 93 menu.add(editor, "Show Error Message", 'M', "showMessage", enabled); 94 } 95 96 public Tagger getTagger(SystemBuffer buffer) 97 { 98 return new CTagger(buffer); 99 } 100 101 public boolean hasQualifiedNames() 102 { 103 return false; 104 } 105 106 public boolean isQualifiedName(String s) 107 { 108 return false; 109 } 110 111 public int getCorrectIndentation(Line line, Buffer buffer) 112 { 113 if (line.trim().startsWith("#")) 114 return 0; 116 return super.getCorrectIndentation(line, buffer); 117 } 118 119 protected static String getPreprocessorToken(Line line) 120 { 121 String s = line.trim(); 122 if (s.length() == 0 || s.charAt(0) != '#') 123 return null; 124 final int limit = s.length(); 125 int i; 126 for (i = 1; i < limit; i++) { 127 char c = s.charAt(i); 128 if (c != ' ' && c != '\t') 129 break; 130 } 131 FastStringBuffer sb = new FastStringBuffer(); 132 for (; i < limit; i++) { 133 char c = s.charAt(i); 134 if (c >= 'a' && c <='z') 135 sb.append(c); 136 else 137 break; 138 } 139 return sb.toString(); 140 } 141 142 private static boolean matchBackwards = false; 145 146 public static Line findMatchPreprocessor(Line startLine) 147 { 148 final String patternIf = "if"; 149 final String patternElse = "el"; 150 final String patternEndif = "endif"; 151 String token = null; 152 String match = null; 153 boolean searchBackwards = false; 154 String s = getPreprocessorToken(startLine); 155 if (s == null) 156 return null; 157 if (s.startsWith(patternIf)) { 158 token = patternIf; 159 match = patternEndif; 160 matchBackwards = false; 161 } else if (s.startsWith(patternEndif)) { 162 token = patternEndif; 163 match = patternIf; 164 matchBackwards = true; 165 } else if (s.startsWith(patternElse)) { 166 if (matchBackwards) { 167 token = patternEndif; 168 match = patternIf; 169 } else { 170 token = patternIf; 171 match = patternEndif; 172 } 173 } else 174 return null; 175 int count = 1; 176 Line line = startLine; 177 while (true) { 178 if (matchBackwards) 179 line = line.previous(); 180 else 181 line = line.next(); 182 if (line == null) break; 183 s = getPreprocessorToken(line); 184 if (s != null) { 185 if (count == 1 && s.startsWith(patternElse)) 186 return line; 187 if (s.startsWith(token)) 188 ++count; 189 else if (s.startsWith(match)) 190 --count; 191 if (count == 0) 192 return line; 193 } 194 } 195 return null; 196 } 197 198 public boolean isIdentifierStart(char c) 199 { 200 if (c >= 'a' && c <= 'z') 201 return true; 202 if (c >='A' && c <= 'Z') 203 return true; 204 if (c == '_') 205 return true; 206 return false; 207 } 208 209 public boolean isIdentifierPart(char c) 210 { 211 if (c >= 'a' && c <= 'z') 212 return true; 213 if (c >='A' && c <= 'Z') 214 return true; 215 if (c >= '0' && c <= '9') 216 return true; 217 if (c == '_') 218 return true; 219 return false; 220 } 221 } 222 | Popular Tags |