KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * VHDLMode.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: VHDLMode.java,v 1.1.1.1 2002/09/24 16:08:58 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 gnu.regexp.RE;
25 import gnu.regexp.UncheckedRE;
26 import java.awt.event.KeyEvent JavaDoc;
27
28 public final class VHDLMode extends AbstractMode implements Constants, Mode
29 {
30     private static final VHDLMode mode = new VHDLMode();
31
32     private VHDLMode()
33     {
34         super(VHDL_MODE, VHDL_MODE_NAME);
35         keywords = new Keywords(this);
36     }
37
38     public static VHDLMode getMode()
39     {
40         return mode;
41     }
42
43     public String JavaDoc getCommentStart()
44     {
45         return "-- ";
46     }
47
48     public Formatter getFormatter(Buffer buffer)
49     {
50         return new VHDLFormatter(buffer);
51     }
52
53     public boolean isKeyword(String JavaDoc s)
54     {
55         return keywords.isKeyword(s.toLowerCase());
56     }
57
58     protected void setKeyMapDefaults(KeyMap km)
59     {
60         km.mapKey(KeyEvent.VK_ENTER, 0, "newlineAndIndent");
61         km.mapKey(KeyEvent.VK_T, CTRL_MASK, "findTag");
62         km.mapKey(KeyEvent.VK_PERIOD, ALT_MASK, "findTagAtDot");
63         km.mapKey(KeyEvent.VK_F12, 0, "wrapComment");
64         // Duplicate mapping to support IBM 1.3 for Linux.
65
km.mapKey(0xffc9, 0, "wrapComment"); // F12
66
}
67
68     public boolean isTaggable()
69     {
70         return true;
71     }
72
73     public Tagger getTagger(SystemBuffer buffer)
74     {
75         return new VHDLTagger(buffer);
76     }
77
78     public boolean canIndent()
79     {
80         return true;
81     }
82
83     public boolean canIndentPaste()
84     {
85         return false;
86     }
87
88     private static final RE beginRE = new UncheckedRE("^begin\\s");
89     private static final RE thenRE = new UncheckedRE("\\s+then$");
90     private static final RE loopRE = new UncheckedRE("\\s+loop$");
91
92     public int getCorrectIndentation(Line line, Buffer buffer)
93     {
94         final int indentSize = buffer.getIndentSize();
95         final Line model = findModel(line);
96         if (model == null)
97             return 0;
98         final int modelIndent = buffer.getIndentation(model);
99         final String JavaDoc modelTrim = trimSyntacticWhitespace(model);
100
101         if (modelTrim.equals("begin") || beginRE.getMatch(modelTrim) != null) {
102             // Model starts with "begin".
103
return modelIndent + indentSize;
104         }
105         if (modelTrim.equals("then") || thenRE.getMatch(modelTrim) != null) {
106             // Model ends with "then".
107
return modelIndent + indentSize;
108         }
109         if (modelTrim.equals("loop") || loopRE.getMatch(modelTrim) != null) {
110             // Model ends with "loop".
111
return modelIndent + indentSize;
112         }
113
114         return modelIndent;
115     }
116
117     private Line findModel(Line line)
118     {
119         Line model = line.previous();
120         while (model != null && model.isBlank())
121             model = model.previous();
122         return model;
123     }
124
125     // Replaces syntactic whitespace (quotes and comments) with actual space
126
// characters and returns trimmed string.
127
private static String JavaDoc trimSyntacticWhitespace(Line line)
128     {
129         VHDLSyntaxIterator it = new VHDLSyntaxIterator(null);
130         return new String JavaDoc(it.hideSyntacticWhitespace(line.getText())).trim();
131     }
132
133     public boolean isIdentifierStart(char c)
134     {
135         return startChars.indexOf(c) >= 0;
136     }
137
138     public boolean isIdentifierPart(char c)
139     {
140         return partChars.indexOf(c) >= 0;
141     }
142
143     private static final String JavaDoc startChars =
144         "`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
145
146     private static final String JavaDoc partChars =
147         "`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
148 }
149
Popular Tags