svn-gvsig-desktop / tags / v1_1_Build_1007 / libraries / libIverUtiles / src / com / iver / utiles / console / jedit / PHPTokenMarker.java @ 12478
History | View | Annotate | Download (8.42 KB)
1 |
package com.iver.utiles.console.jedit; |
---|---|
2 |
/*
|
3 |
* PHPTokenMarker.java - Token marker for PHP
|
4 |
* Copyright (C) 1999 Clancy Malcolm
|
5 |
* Based on HTMLTokenMarker.java Copyright (C) 1998, 1999 Slava Pestov
|
6 |
*
|
7 |
* You may use and modify this package for any purpose. Redistribution is
|
8 |
* permitted, in both source and binary form, provided that this notice
|
9 |
* remains intact in all source distributions of this package.
|
10 |
*/
|
11 |
|
12 |
import javax.swing.text.Segment; |
13 |
|
14 |
/**
|
15 |
* PHP token marker.
|
16 |
*
|
17 |
* @author Clancy Malcolm
|
18 |
* @version $Id$
|
19 |
*/
|
20 |
public class PHPTokenMarker extends TokenMarker |
21 |
{ |
22 |
public static final byte SCRIPT = Token.INTERNAL_FIRST; |
23 |
|
24 |
public byte markTokensImpl(byte token, Segment line, int lineIndex) |
25 |
{ |
26 |
char[] array = line.array; |
27 |
int offset = line.offset;
|
28 |
lastOffset = offset; |
29 |
lastKeyword = offset; |
30 |
int length = line.count + offset;
|
31 |
boolean backslash = false; |
32 |
|
33 |
loop: for(int i = offset; i < length; i++) |
34 |
{ |
35 |
int i1 = (i+1); |
36 |
|
37 |
char c = array[i];
|
38 |
if(c == '\\') |
39 |
{ |
40 |
backslash = !backslash; |
41 |
continue;
|
42 |
} |
43 |
|
44 |
switch(token)
|
45 |
{ |
46 |
case Token.NULL: // HTML text |
47 |
backslash = false;
|
48 |
switch(c)
|
49 |
{ |
50 |
case '<': |
51 |
addToken(i - lastOffset,token); |
52 |
lastOffset = lastKeyword = i; |
53 |
if(SyntaxUtilities.regionMatches(false, |
54 |
line,i1,"!--"))
|
55 |
{ |
56 |
i += 3;
|
57 |
token = Token.COMMENT1; |
58 |
} |
59 |
else if(SyntaxUtilities.regionMatches( |
60 |
true,line,i1,"?php")) |
61 |
{ |
62 |
addToken(5,Token.LABEL);
|
63 |
lastOffset = lastKeyword = (i += 4) + 1; |
64 |
token = SCRIPT; |
65 |
} |
66 |
else if(SyntaxUtilities.regionMatches( |
67 |
true,line,i1,"?")) |
68 |
{ |
69 |
addToken(2,Token.LABEL);
|
70 |
lastOffset = lastKeyword = (i += 1) + 1; |
71 |
token = SCRIPT; |
72 |
} |
73 |
else if(SyntaxUtilities.regionMatches( |
74 |
true,line,i1,"script>")) |
75 |
{ |
76 |
addToken(8,Token.LABEL);
|
77 |
lastOffset = lastKeyword = (i += 7) + 1; |
78 |
token = SCRIPT; |
79 |
} |
80 |
else
|
81 |
{ |
82 |
token = Token.KEYWORD1; |
83 |
} |
84 |
break;
|
85 |
case '&': |
86 |
addToken(i - lastOffset,token); |
87 |
lastOffset = lastKeyword = i; |
88 |
token = Token.KEYWORD2; |
89 |
break;
|
90 |
} |
91 |
break;
|
92 |
case Token.KEYWORD1: // Inside a tag |
93 |
backslash = false;
|
94 |
if(c == '>') |
95 |
{ |
96 |
addToken(i1 - lastOffset,token); |
97 |
lastOffset = lastKeyword = i1; |
98 |
token = Token.NULL; |
99 |
} |
100 |
break;
|
101 |
case Token.KEYWORD2: // Inside an entity |
102 |
backslash = false;
|
103 |
if(c == ';') |
104 |
{ |
105 |
addToken(i1 - lastOffset,token); |
106 |
lastOffset = lastKeyword = i1; |
107 |
token = Token.NULL; |
108 |
break;
|
109 |
} |
110 |
break;
|
111 |
case Token.COMMENT1: // Inside a comment |
112 |
backslash = false;
|
113 |
if(SyntaxUtilities.regionMatches(false,line,i,"-->")) |
114 |
{ |
115 |
addToken(i + 3 - lastOffset,token);
|
116 |
i += 2;
|
117 |
lastOffset = lastKeyword = i + 1;
|
118 |
token = Token.NULL; |
119 |
} |
120 |
break;
|
121 |
case SCRIPT: // Inside a JavaScript or PHP |
122 |
switch(c)
|
123 |
{ |
124 |
case '<': |
125 |
backslash = false;
|
126 |
doKeyword(line,i,c); |
127 |
if(SyntaxUtilities.regionMatches(true, |
128 |
line,i1,"/script>"))
|
129 |
{ |
130 |
//Ending the script
|
131 |
addToken(i - lastOffset, |
132 |
Token.KEYWORD3); |
133 |
addToken(9,Token.LABEL);
|
134 |
lastOffset = lastKeyword = (i += 8) + 1; |
135 |
token = Token.NULL; |
136 |
} |
137 |
else
|
138 |
{ |
139 |
// < operator
|
140 |
addToken(i - lastOffset, |
141 |
Token.KEYWORD3); |
142 |
addToken(1,Token.OPERATOR);
|
143 |
lastOffset = lastKeyword = i1; |
144 |
} |
145 |
break;
|
146 |
case '?': |
147 |
backslash = false;
|
148 |
doKeyword(line, i, c); |
149 |
if (array[i1] == '>') |
150 |
{ |
151 |
//Ending the script
|
152 |
addToken(i - lastOffset, |
153 |
Token.KEYWORD3); |
154 |
addToken(2,Token.LABEL);
|
155 |
lastOffset = lastKeyword = (i += 1) + 1; |
156 |
token = Token.NULL; |
157 |
} |
158 |
else
|
159 |
{ |
160 |
//? operator
|
161 |
addToken(i - lastOffset, Token.KEYWORD3); |
162 |
addToken(1,Token.OPERATOR);
|
163 |
lastOffset = lastKeyword = i1; |
164 |
} |
165 |
break;
|
166 |
case '"': |
167 |
if(backslash)
|
168 |
backslash = false;
|
169 |
else
|
170 |
{ |
171 |
doKeyword(line,i,c); |
172 |
addToken(i - lastOffset,Token.KEYWORD3); |
173 |
lastOffset = lastKeyword = i; |
174 |
token = Token.LITERAL1; |
175 |
} |
176 |
break;
|
177 |
case '\'': |
178 |
if(backslash)
|
179 |
backslash = false;
|
180 |
else
|
181 |
{ |
182 |
doKeyword(line,i,c); |
183 |
addToken(i - lastOffset,Token.KEYWORD3); |
184 |
lastOffset = lastKeyword = i; |
185 |
token = Token.LITERAL2; |
186 |
} |
187 |
break;
|
188 |
case '#': |
189 |
doKeyword(line,i,c); |
190 |
addToken(i - lastOffset,Token.KEYWORD3); |
191 |
addToken(length - i,Token.COMMENT2); |
192 |
lastOffset = lastKeyword = length; |
193 |
break loop;
|
194 |
case '/': |
195 |
backslash = false;
|
196 |
doKeyword(line,i,c); |
197 |
if(length - i > 1) /*This is the same as if(length > i + 1) */ |
198 |
{ |
199 |
addToken(i - lastOffset,Token.KEYWORD3); |
200 |
lastOffset = lastKeyword = i; |
201 |
if(array[i1] == '/') |
202 |
{ |
203 |
addToken(length - i,Token.COMMENT2); |
204 |
lastOffset = lastKeyword = length; |
205 |
break loop;
|
206 |
} |
207 |
else if(array[i1] == '*') |
208 |
{ |
209 |
token = Token.COMMENT2; |
210 |
} |
211 |
else
|
212 |
{ |
213 |
// / operator
|
214 |
addToken(i - lastOffset, Token.KEYWORD3); |
215 |
addToken(1,Token.OPERATOR);
|
216 |
lastOffset = lastKeyword = i1; |
217 |
} |
218 |
} |
219 |
else
|
220 |
{ |
221 |
// / operator
|
222 |
addToken(i - lastOffset, Token.KEYWORD3); |
223 |
addToken(1,Token.OPERATOR);
|
224 |
lastOffset = lastKeyword = i1; |
225 |
} |
226 |
break;
|
227 |
default:
|
228 |
backslash = false;
|
229 |
if(!Character.isLetterOrDigit(c) |
230 |
&& c != '_' && c != '$') |
231 |
{ |
232 |
doKeyword(line,i,c); |
233 |
if (c != ' ') |
234 |
{ |
235 |
//assume non alphanumeric characters are operators
|
236 |
addToken(i - lastOffset, Token.KEYWORD3); |
237 |
addToken(1,Token.OPERATOR);
|
238 |
lastOffset = lastKeyword = i1; |
239 |
} |
240 |
} |
241 |
break;
|
242 |
} |
243 |
break;
|
244 |
case Token.LITERAL1: // Script "..." |
245 |
if(backslash)
|
246 |
backslash = false;
|
247 |
else if(c == '"') |
248 |
{ |
249 |
addToken(i1 - lastOffset,Token.LITERAL1); |
250 |
lastOffset = lastKeyword = i1; |
251 |
token = SCRIPT; |
252 |
} |
253 |
break;
|
254 |
case Token.LITERAL2: // Script '...' |
255 |
if(backslash)
|
256 |
backslash = false;
|
257 |
else if(c == '\'') |
258 |
{ |
259 |
addToken(i1 - lastOffset,Token.LITERAL1); |
260 |
lastOffset = lastKeyword = i1; |
261 |
token = SCRIPT; |
262 |
} |
263 |
break;
|
264 |
case Token.COMMENT2: // Inside a Script comment |
265 |
backslash = false;
|
266 |
if(c == '*' && length - i > 1 && array[i1] == '/') |
267 |
{ |
268 |
addToken(i + 2 - lastOffset,Token.COMMENT2);
|
269 |
i += 1;
|
270 |
lastOffset = lastKeyword = i + 1;
|
271 |
token = SCRIPT; |
272 |
} |
273 |
break;
|
274 |
default:
|
275 |
throw new InternalError("Invalid state: " |
276 |
+ token); |
277 |
} |
278 |
} |
279 |
|
280 |
switch(token)
|
281 |
{ |
282 |
case Token.LITERAL1:
|
283 |
addToken(length - lastOffset,Token.LITERAL1); |
284 |
break;
|
285 |
case Token.LITERAL2:
|
286 |
addToken(length - lastOffset,Token.LITERAL2); |
287 |
break;
|
288 |
case Token.KEYWORD2:
|
289 |
addToken(length - lastOffset,Token.INVALID); |
290 |
token = Token.NULL; |
291 |
break;
|
292 |
case SCRIPT:
|
293 |
doKeyword(line,length,'\0');
|
294 |
addToken(length - lastOffset,Token.KEYWORD3); |
295 |
break;
|
296 |
default:
|
297 |
addToken(length - lastOffset,token); |
298 |
break;
|
299 |
} |
300 |
|
301 |
return token;
|
302 |
} |
303 |
|
304 |
// private members
|
305 |
private static KeywordMap keywords; |
306 |
private int lastOffset; |
307 |
private int lastKeyword; |
308 |
|
309 |
static
|
310 |
{ |
311 |
keywords = new KeywordMap(false); |
312 |
keywords.add("function",Token.KEYWORD2);
|
313 |
keywords.add("class",Token.KEYWORD2);
|
314 |
keywords.add("var",Token.KEYWORD2);
|
315 |
keywords.add("require",Token.KEYWORD2);
|
316 |
keywords.add("include",Token.KEYWORD2);
|
317 |
keywords.add("else",Token.KEYWORD1);
|
318 |
keywords.add("elseif",Token.KEYWORD1);
|
319 |
keywords.add("do",Token.KEYWORD1);
|
320 |
keywords.add("for",Token.KEYWORD1);
|
321 |
keywords.add("if",Token.KEYWORD1);
|
322 |
keywords.add("endif",Token.KEYWORD1);
|
323 |
keywords.add("in",Token.KEYWORD1);
|
324 |
keywords.add("new",Token.KEYWORD1);
|
325 |
keywords.add("return",Token.KEYWORD1);
|
326 |
keywords.add("while",Token.KEYWORD1);
|
327 |
keywords.add("endwhile",Token.KEYWORD1);
|
328 |
keywords.add("with",Token.KEYWORD1);
|
329 |
keywords.add("break",Token.KEYWORD1);
|
330 |
keywords.add("switch",Token.KEYWORD1);
|
331 |
keywords.add("case",Token.KEYWORD1);
|
332 |
keywords.add("continue",Token.KEYWORD1);
|
333 |
keywords.add("default",Token.KEYWORD1);
|
334 |
keywords.add("echo",Token.KEYWORD1);
|
335 |
keywords.add("false",Token.KEYWORD1);
|
336 |
keywords.add("this",Token.KEYWORD1);
|
337 |
keywords.add("true",Token.KEYWORD1);
|
338 |
keywords.add("array",Token.KEYWORD1);
|
339 |
keywords.add("extends",Token.KEYWORD1);
|
340 |
} |
341 |
|
342 |
private boolean doKeyword(Segment line, int i, char c) |
343 |
{ |
344 |
int i1 = i+1; |
345 |
|
346 |
int len = i - lastKeyword;
|
347 |
byte id = keywords.lookup(line,lastKeyword,len);
|
348 |
if(id != Token.NULL)
|
349 |
{ |
350 |
if(lastKeyword != lastOffset)
|
351 |
addToken(lastKeyword - lastOffset,Token.KEYWORD3); |
352 |
addToken(len,id); |
353 |
lastOffset = i; |
354 |
} |
355 |
lastKeyword = i1; |
356 |
return false; |
357 |
} |
358 |
} |