svn-gvsig-desktop / tags / v1_0_2_Build_905 / libraries / libIverUtiles / src / com / iver / utiles / console / jedit / CTokenMarker.java @ 10767
History | View | Annotate | Download (6.28 KB)
1 |
package com.iver.utiles.console.jedit; |
---|---|
2 |
/*
|
3 |
* CTokenMarker.java - C token marker
|
4 |
* Copyright (C) 1998, 1999 Slava Pestov
|
5 |
*
|
6 |
* You may use and modify this package for any purpose. Redistribution is
|
7 |
* permitted, in both source and binary form, provided that this notice
|
8 |
* remains intact in all source distributions of this package.
|
9 |
*/
|
10 |
|
11 |
import javax.swing.text.Segment; |
12 |
|
13 |
/**
|
14 |
* C token marker.
|
15 |
*
|
16 |
* @author Slava Pestov
|
17 |
* @version $Id$
|
18 |
*/
|
19 |
public class CTokenMarker extends TokenMarker |
20 |
{ |
21 |
public CTokenMarker()
|
22 |
{ |
23 |
this(true,getKeywords()); |
24 |
} |
25 |
|
26 |
public CTokenMarker(boolean cpp, KeywordMap keywords) |
27 |
{ |
28 |
this.cpp = cpp;
|
29 |
this.keywords = keywords;
|
30 |
} |
31 |
|
32 |
public byte markTokensImpl(byte token, Segment line, int lineIndex) |
33 |
{ |
34 |
char[] array = line.array; |
35 |
int offset = line.offset;
|
36 |
lastOffset = offset; |
37 |
lastKeyword = offset; |
38 |
int length = line.count + offset;
|
39 |
boolean backslash = false; |
40 |
|
41 |
loop: for(int i = offset; i < length; i++) |
42 |
{ |
43 |
int i1 = (i+1); |
44 |
|
45 |
char c = array[i];
|
46 |
if(c == '\\') |
47 |
{ |
48 |
backslash = !backslash; |
49 |
continue;
|
50 |
} |
51 |
|
52 |
switch(token)
|
53 |
{ |
54 |
case Token.NULL:
|
55 |
switch(c)
|
56 |
{ |
57 |
case '#': |
58 |
if(backslash)
|
59 |
backslash = false;
|
60 |
else if(cpp) |
61 |
{ |
62 |
if(doKeyword(line,i,c))
|
63 |
break;
|
64 |
addToken(i - lastOffset,token); |
65 |
addToken(length - i,Token.KEYWORD2); |
66 |
lastOffset = lastKeyword = length; |
67 |
break loop;
|
68 |
} |
69 |
break;
|
70 |
case '"': |
71 |
doKeyword(line,i,c); |
72 |
if(backslash)
|
73 |
backslash = false;
|
74 |
else
|
75 |
{ |
76 |
addToken(i - lastOffset,token); |
77 |
token = Token.LITERAL1; |
78 |
lastOffset = lastKeyword = i; |
79 |
} |
80 |
break;
|
81 |
case '\'': |
82 |
doKeyword(line,i,c); |
83 |
if(backslash)
|
84 |
backslash = false;
|
85 |
else
|
86 |
{ |
87 |
addToken(i - lastOffset,token); |
88 |
token = Token.LITERAL2; |
89 |
lastOffset = lastKeyword = i; |
90 |
} |
91 |
break;
|
92 |
case ':': |
93 |
if(lastKeyword == offset)
|
94 |
{ |
95 |
if(doKeyword(line,i,c))
|
96 |
break;
|
97 |
backslash = false;
|
98 |
addToken(i1 - lastOffset,Token.LABEL); |
99 |
lastOffset = lastKeyword = i1; |
100 |
} |
101 |
else if(doKeyword(line,i,c)) |
102 |
break;
|
103 |
break;
|
104 |
case '/': |
105 |
backslash = false;
|
106 |
doKeyword(line,i,c); |
107 |
if(length - i > 1) |
108 |
{ |
109 |
switch(array[i1])
|
110 |
{ |
111 |
case '*': |
112 |
addToken(i - lastOffset,token); |
113 |
lastOffset = lastKeyword = i; |
114 |
if(length - i > 2 && array[i+2] == '*') |
115 |
token = Token.COMMENT2; |
116 |
else
|
117 |
token = Token.COMMENT1; |
118 |
break;
|
119 |
case '/': |
120 |
addToken(i - lastOffset,token); |
121 |
addToken(length - i,Token.COMMENT1); |
122 |
lastOffset = lastKeyword = length; |
123 |
break loop;
|
124 |
} |
125 |
} |
126 |
break;
|
127 |
default:
|
128 |
backslash = false;
|
129 |
if(!Character.isLetterOrDigit(c) |
130 |
&& c != '_')
|
131 |
doKeyword(line,i,c); |
132 |
break;
|
133 |
} |
134 |
break;
|
135 |
case Token.COMMENT1:
|
136 |
case Token.COMMENT2:
|
137 |
backslash = false;
|
138 |
if(c == '*' && length - i > 1) |
139 |
{ |
140 |
if(array[i1] == '/') |
141 |
{ |
142 |
i++; |
143 |
addToken((i+1) - lastOffset,token);
|
144 |
token = Token.NULL; |
145 |
lastOffset = lastKeyword = i+1;
|
146 |
} |
147 |
} |
148 |
break;
|
149 |
case Token.LITERAL1:
|
150 |
if(backslash)
|
151 |
backslash = false;
|
152 |
else if(c == '"') |
153 |
{ |
154 |
addToken(i1 - lastOffset,token); |
155 |
token = Token.NULL; |
156 |
lastOffset = lastKeyword = i1; |
157 |
} |
158 |
break;
|
159 |
case Token.LITERAL2:
|
160 |
if(backslash)
|
161 |
backslash = false;
|
162 |
else if(c == '\'') |
163 |
{ |
164 |
addToken(i1 - lastOffset,Token.LITERAL1); |
165 |
token = Token.NULL; |
166 |
lastOffset = lastKeyword = i1; |
167 |
} |
168 |
break;
|
169 |
default:
|
170 |
throw new InternalError("Invalid state: " |
171 |
+ token); |
172 |
} |
173 |
} |
174 |
|
175 |
if(token == Token.NULL)
|
176 |
doKeyword(line,length,'\0');
|
177 |
|
178 |
switch(token)
|
179 |
{ |
180 |
case Token.LITERAL1:
|
181 |
case Token.LITERAL2:
|
182 |
addToken(length - lastOffset,Token.INVALID); |
183 |
token = Token.NULL; |
184 |
break;
|
185 |
case Token.KEYWORD2:
|
186 |
addToken(length - lastOffset,token); |
187 |
if(!backslash)
|
188 |
token = Token.NULL; |
189 |
default:
|
190 |
addToken(length - lastOffset,token); |
191 |
break;
|
192 |
} |
193 |
|
194 |
return token;
|
195 |
} |
196 |
|
197 |
public static KeywordMap getKeywords() |
198 |
{ |
199 |
if(cKeywords == null) |
200 |
{ |
201 |
cKeywords = new KeywordMap(false); |
202 |
cKeywords.add("char",Token.KEYWORD3);
|
203 |
cKeywords.add("double",Token.KEYWORD3);
|
204 |
cKeywords.add("enum",Token.KEYWORD3);
|
205 |
cKeywords.add("float",Token.KEYWORD3);
|
206 |
cKeywords.add("int",Token.KEYWORD3);
|
207 |
cKeywords.add("long",Token.KEYWORD3);
|
208 |
cKeywords.add("short",Token.KEYWORD3);
|
209 |
cKeywords.add("signed",Token.KEYWORD3);
|
210 |
cKeywords.add("struct",Token.KEYWORD3);
|
211 |
cKeywords.add("typedef",Token.KEYWORD3);
|
212 |
cKeywords.add("union",Token.KEYWORD3);
|
213 |
cKeywords.add("unsigned",Token.KEYWORD3);
|
214 |
cKeywords.add("void",Token.KEYWORD3);
|
215 |
cKeywords.add("auto",Token.KEYWORD1);
|
216 |
cKeywords.add("const",Token.KEYWORD1);
|
217 |
cKeywords.add("extern",Token.KEYWORD1);
|
218 |
cKeywords.add("register",Token.KEYWORD1);
|
219 |
cKeywords.add("static",Token.KEYWORD1);
|
220 |
cKeywords.add("volatile",Token.KEYWORD1);
|
221 |
cKeywords.add("break",Token.KEYWORD1);
|
222 |
cKeywords.add("case",Token.KEYWORD1);
|
223 |
cKeywords.add("continue",Token.KEYWORD1);
|
224 |
cKeywords.add("default",Token.KEYWORD1);
|
225 |
cKeywords.add("do",Token.KEYWORD1);
|
226 |
cKeywords.add("else",Token.KEYWORD1);
|
227 |
cKeywords.add("for",Token.KEYWORD1);
|
228 |
cKeywords.add("goto",Token.KEYWORD1);
|
229 |
cKeywords.add("if",Token.KEYWORD1);
|
230 |
cKeywords.add("return",Token.KEYWORD1);
|
231 |
cKeywords.add("sizeof",Token.KEYWORD1);
|
232 |
cKeywords.add("switch",Token.KEYWORD1);
|
233 |
cKeywords.add("while",Token.KEYWORD1);
|
234 |
cKeywords.add("asm",Token.KEYWORD2);
|
235 |
cKeywords.add("asmlinkage",Token.KEYWORD2);
|
236 |
cKeywords.add("far",Token.KEYWORD2);
|
237 |
cKeywords.add("huge",Token.KEYWORD2);
|
238 |
cKeywords.add("inline",Token.KEYWORD2);
|
239 |
cKeywords.add("near",Token.KEYWORD2);
|
240 |
cKeywords.add("pascal",Token.KEYWORD2);
|
241 |
cKeywords.add("true",Token.LITERAL2);
|
242 |
cKeywords.add("false",Token.LITERAL2);
|
243 |
cKeywords.add("NULL",Token.LITERAL2);
|
244 |
} |
245 |
return cKeywords;
|
246 |
} |
247 |
|
248 |
// private members
|
249 |
private static KeywordMap cKeywords; |
250 |
|
251 |
private boolean cpp; |
252 |
private KeywordMap keywords;
|
253 |
private int lastOffset; |
254 |
private int lastKeyword; |
255 |
|
256 |
private boolean doKeyword(Segment line, int i, char c) |
257 |
{ |
258 |
int i1 = i+1; |
259 |
|
260 |
int len = i - lastKeyword;
|
261 |
byte id = keywords.lookup(line,lastKeyword,len);
|
262 |
if(id != Token.NULL)
|
263 |
{ |
264 |
if(lastKeyword != lastOffset)
|
265 |
addToken(lastKeyword - lastOffset,Token.NULL); |
266 |
addToken(len,id); |
267 |
lastOffset = i; |
268 |
} |
269 |
lastKeyword = i1; |
270 |
return false; |
271 |
} |
272 |
} |