KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * VerilogMode.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: VerilogMode.java,v 1.1.1.1 2002/09/24 16:07:56 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
26 public final class VerilogMode extends AbstractMode implements Constants, Mode
27 {
28     private static final VerilogMode mode = new VerilogMode();
29
30     private VerilogMode()
31     {
32         super(VERILOG_MODE, VERILOG_MODE_NAME);
33         keywords = new Keywords(this);
34     }
35
36     public static VerilogMode getMode()
37     {
38         return mode;
39     }
40
41     public String JavaDoc getCommentStart()
42     {
43         return "// ";
44     }
45
46     public Formatter getFormatter(Buffer buffer)
47     {
48         return new VerilogFormatter(buffer);
49     }
50
51     protected void setKeyMapDefaults(KeyMap km)
52     {
53         km.mapKey(KeyEvent.VK_TAB, CTRL_MASK, "insertTab");
54         km.mapKey(KeyEvent.VK_TAB, 0, "tab");
55         km.mapKey(KeyEvent.VK_ENTER, 0, "newlineAndIndent");
56         km.mapKey(KeyEvent.VK_T, CTRL_MASK, "findTag");
57         km.mapKey(KeyEvent.VK_PERIOD, ALT_MASK, "findTagAtDot");
58         km.mapKey(KeyEvent.VK_F12, 0, "wrapComment");
59         // Duplicate mapping to support IBM 1.3 for Linux.
60
km.mapKey(0xffc9, 0, "wrapComment"); // F12
61
}
62
63     public boolean isTaggable()
64     {
65         return true;
66     }
67
68     public Tagger getTagger(SystemBuffer buffer)
69     {
70         return new VerilogTagger(buffer);
71     }
72
73     public boolean canIndent()
74     {
75         return true;
76     }
77
78     public int getCorrectIndentation(Line line, Buffer buffer)
79     {
80         final int indentSize = buffer.getIndentSize();
81         final Line model = findModel(line);
82         if (model == null)
83             return 0;
84         final String JavaDoc trim = line.getText().trim();
85         final int modelIndent = buffer.getIndentation(model);
86         final String JavaDoc modelTrim = model.getText().trim();
87         if (line.flags() == STATE_COMMENT) {
88             if (modelTrim.startsWith("/*") && trim.startsWith("*"))
89                 return modelIndent + 1;
90             else
91                 return modelIndent;
92         }
93         if (modelTrim.endsWith("("))
94             return modelIndent + indentSize;
95         final String JavaDoc modelIdentifier =
96             Utilities.getFirstIdentifier(modelTrim, this);
97         if (Utilities.isOneOf(modelIdentifier, alwaysIndentAfter))
98             return modelIndent + indentSize;
99         if (Utilities.isOneOf(modelIdentifier, maybeIndentAfter)) {
100             if (modelTrim.endsWith(";"))
101                 return modelIndent;
102             else
103                 return modelIndent + indentSize;
104         }
105         final String JavaDoc identifier = Utilities.getFirstIdentifier(trim, this);
106         if ("end".equals(identifier)) {
107             Line beginLine = findBeginLine(line);
108             if (beginLine != null)
109                 return buffer.getIndentation(beginLine);
110         }
111         return modelIndent;
112     }
113
114     private static Line findModel(Line line)
115     {
116         Line model = line.previous();
117         if (line.flags() == STATE_COMMENT) {
118             // Any non-blank line is an acceptable model.
119
while (model != null && model.isBlank())
120                 model = model.previous();
121         } else {
122             while (model != null && !isAcceptableModel(model))
123                 model = model.previous();
124         }
125         return model;
126     }
127
128     private static boolean isAcceptableModel(Line line)
129     {
130         if (line.isBlank())
131             return false;
132         if (line.flags() == STATE_COMMENT)
133             return false;
134
135         return true;
136     }
137
138     private Line findBeginLine(Line line)
139     {
140         int count = 1;
141         while (true) {
142             line = line.previous();
143             if (line == null)
144                 return null;
145             String JavaDoc identifier = Utilities.getFirstIdentifier(line.trim(), this);
146             if (identifier != null) {
147                 if (identifier.equals("begin")) {
148                     if (--count == 0)
149                         return line;
150                 } else if (identifier.equals("end"))
151                     ++count;
152             }
153         }
154     }
155
156     public boolean isIdentifierStart(char c)
157     {
158         return startChars.indexOf(c) >= 0;
159     }
160
161     public boolean isIdentifierPart(char c)
162     {
163         return partChars.indexOf(c) >= 0;
164     }
165
166     private static final String JavaDoc startChars =
167         "`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
168
169     private static final String JavaDoc partChars =
170         "`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$";
171
172     private static final String JavaDoc[] alwaysIndentAfter = {
173         "begin",
174         "case",
175         "casex",
176         "casez",
177         "fork",
178         "function",
179         "generate",
180         "module",
181         "primitive",
182         "specify",
183         "table",
184         "task"
185     };
186
187     private static final String JavaDoc[] maybeIndentAfter = {
188         "always",
189         "else",
190         "for",
191         "forever",
192         "if",
193         "initial",
194         "repeat",
195         "while"
196     };
197 }
198
Popular Tags