KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > CMode


1 /*
2  * CMode.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: CMode.java,v 1.3 2003/12/30 19:28:31 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.event.KeyEvent JavaDoc;
25 import java.util.HashSet JavaDoc;
26
27 public class CMode extends JavaMode implements Constants, Mode
28 {
29     private static final String JavaDoc[] 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 JavaDoc displayName)
48     {
49         super(id, displayName);
50     }
51
52     // Don't construct the singleton class instance until we actually need it,
53
// to avoid unnecessary overhead for CppMode which is derived from this
54
// class.
55
public static Mode getMode()
56     {
57         if (mode == null)
58             mode = new CMode();
59         return mode;
60     }
61
62     public String JavaDoc getCommentStart()
63     {
64         return "/*";
65     }
66
67     public String JavaDoc 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 JavaDoc s)
107     {
108         return false;
109     }
110
111     public int getCorrectIndentation(Line line, Buffer buffer)
112     {
113         if (line.trim().startsWith("#"))
114             return 0; // Preprocessor directive.
115

116         return super.getCorrectIndentation(line, buffer);
117     }
118
119     protected static String JavaDoc getPreprocessorToken(Line line)
120     {
121         String JavaDoc 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     // Used only by findMatchPreprocessor. This needs to persist between calls
143
// so we can handle #else/#elif correctly in successive calls.
144
private static boolean matchBackwards = false;
145
146     public static Line findMatchPreprocessor(Line startLine)
147     {
148         final String JavaDoc patternIf = "if";
149         final String JavaDoc patternElse = "el";
150         final String JavaDoc patternEndif = "endif";
151         String JavaDoc token = null;
152         String JavaDoc match = null;
153         boolean searchBackwards = false;
154         String JavaDoc 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