svn-gvsig-desktop / trunk / libraries / libIverUtiles / src / com / iver / utiles / console / jedit / PythonTokenMarker.java @ 4890
History | View | Annotate | Download (5.38 KB)
1 |
package com.iver.utiles.console.jedit; |
---|---|
2 |
/*
|
3 |
* PythonTokenMarker.java - Python token marker
|
4 |
* Copyright (C) 1999 Jonathan Revusky
|
5 |
* 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 |
* Python token marker.
|
16 |
*
|
17 |
* @author Jonathan Revusky
|
18 |
* @version $Id$
|
19 |
*/
|
20 |
public class PythonTokenMarker extends TokenMarker |
21 |
{ |
22 |
private static final byte TRIPLEQUOTE1 = Token.INTERNAL_FIRST; |
23 |
private static final byte TRIPLEQUOTE2 = Token.INTERNAL_LAST; |
24 |
|
25 |
public PythonTokenMarker()
|
26 |
{ |
27 |
this.keywords = getKeywords();
|
28 |
} |
29 |
|
30 |
public byte markTokensImpl(byte token, Segment line, int lineIndex) |
31 |
{ |
32 |
char[] array = line.array; |
33 |
int offset = line.offset;
|
34 |
lastOffset = offset; |
35 |
lastKeyword = offset; |
36 |
int length = line.count + offset;
|
37 |
boolean backslash = false; |
38 |
|
39 |
loop: for(int i = offset; i < length; i++) |
40 |
{ |
41 |
int i1 = (i+1); |
42 |
|
43 |
char c = array[i];
|
44 |
if(c == '\\') |
45 |
{ |
46 |
backslash = !backslash; |
47 |
continue;
|
48 |
} |
49 |
|
50 |
switch(token)
|
51 |
{ |
52 |
case Token.NULL:
|
53 |
switch(c)
|
54 |
{ |
55 |
case '#': |
56 |
if(backslash)
|
57 |
backslash = false;
|
58 |
else
|
59 |
{ |
60 |
doKeyword(line,i,c); |
61 |
addToken(i - lastOffset,token); |
62 |
addToken(length - i,Token.COMMENT1); |
63 |
lastOffset = lastKeyword = length; |
64 |
break loop;
|
65 |
} |
66 |
break;
|
67 |
case '"': |
68 |
doKeyword(line,i,c); |
69 |
if(backslash)
|
70 |
backslash = false;
|
71 |
else
|
72 |
{ |
73 |
addToken(i - lastOffset,token); |
74 |
if(SyntaxUtilities.regionMatches(false, |
75 |
line,i1,"\"\""))
|
76 |
{ |
77 |
token = TRIPLEQUOTE1; |
78 |
} |
79 |
else
|
80 |
{ |
81 |
token = Token.LITERAL1; |
82 |
} |
83 |
lastOffset = lastKeyword = i; |
84 |
} |
85 |
break;
|
86 |
case '\'': |
87 |
doKeyword(line,i,c); |
88 |
if(backslash)
|
89 |
backslash = false;
|
90 |
else
|
91 |
{ |
92 |
addToken(i - lastOffset,token); |
93 |
if(SyntaxUtilities.regionMatches(false, |
94 |
line,i1,"''"))
|
95 |
{ |
96 |
token = TRIPLEQUOTE2; |
97 |
} |
98 |
else
|
99 |
{ |
100 |
token = Token.LITERAL2; |
101 |
} |
102 |
lastOffset = lastKeyword = i; |
103 |
} |
104 |
break;
|
105 |
default:
|
106 |
backslash = false;
|
107 |
if(!Character.isLetterOrDigit(c) |
108 |
&& c != '_')
|
109 |
doKeyword(line,i,c); |
110 |
break;
|
111 |
} |
112 |
break;
|
113 |
case Token.LITERAL1:
|
114 |
if(backslash)
|
115 |
backslash = false;
|
116 |
else if(c == '"') |
117 |
{ |
118 |
addToken(i1 - lastOffset,token); |
119 |
token = Token.NULL; |
120 |
lastOffset = lastKeyword = i1; |
121 |
} |
122 |
break;
|
123 |
case Token.LITERAL2:
|
124 |
if(backslash)
|
125 |
backslash = false;
|
126 |
else if(c == '\'') |
127 |
{ |
128 |
addToken(i1 - lastOffset,Token.LITERAL1); |
129 |
token = Token.NULL; |
130 |
lastOffset = lastKeyword = i1; |
131 |
} |
132 |
break;
|
133 |
case TRIPLEQUOTE1:
|
134 |
if(backslash)
|
135 |
backslash = false;
|
136 |
else if(SyntaxUtilities.regionMatches(false, |
137 |
line,i,"\"\"\""))
|
138 |
{ |
139 |
addToken((i+=4) - lastOffset,
|
140 |
Token.LITERAL1); |
141 |
token = Token.NULL; |
142 |
lastOffset = lastKeyword = i; |
143 |
} |
144 |
break;
|
145 |
case TRIPLEQUOTE2:
|
146 |
if(backslash)
|
147 |
backslash = false;
|
148 |
else if(SyntaxUtilities.regionMatches(false, |
149 |
line,i,"'''"))
|
150 |
{ |
151 |
addToken((i+=4) - lastOffset,
|
152 |
Token.LITERAL1); |
153 |
token = Token.NULL; |
154 |
lastOffset = lastKeyword = i; |
155 |
} |
156 |
break;
|
157 |
default:
|
158 |
throw new InternalError("Invalid state: " |
159 |
+ token); |
160 |
} |
161 |
} |
162 |
|
163 |
switch(token)
|
164 |
{ |
165 |
case TRIPLEQUOTE1:
|
166 |
case TRIPLEQUOTE2:
|
167 |
addToken(length - lastOffset,Token.LITERAL1); |
168 |
break;
|
169 |
case Token.NULL:
|
170 |
doKeyword(line,length,'\0');
|
171 |
default:
|
172 |
addToken(length - lastOffset,token); |
173 |
break;
|
174 |
} |
175 |
|
176 |
return token;
|
177 |
} |
178 |
|
179 |
public static KeywordMap getKeywords() |
180 |
{ |
181 |
if (pyKeywords == null) |
182 |
{ |
183 |
pyKeywords = new KeywordMap(false); |
184 |
pyKeywords.add("and",Token.KEYWORD3);
|
185 |
pyKeywords.add("not",Token.KEYWORD3);
|
186 |
pyKeywords.add("or",Token.KEYWORD3);
|
187 |
pyKeywords.add("if",Token.KEYWORD1);
|
188 |
pyKeywords.add("for",Token.KEYWORD1);
|
189 |
pyKeywords.add("assert",Token.KEYWORD1);
|
190 |
pyKeywords.add("break",Token.KEYWORD1);
|
191 |
pyKeywords.add("continue",Token.KEYWORD1);
|
192 |
pyKeywords.add("elif",Token.KEYWORD1);
|
193 |
pyKeywords.add("else",Token.KEYWORD1);
|
194 |
pyKeywords.add("except",Token.KEYWORD1);
|
195 |
pyKeywords.add("exec",Token.KEYWORD1);
|
196 |
pyKeywords.add("finally",Token.KEYWORD1);
|
197 |
pyKeywords.add("raise",Token.KEYWORD1);
|
198 |
pyKeywords.add("return",Token.KEYWORD1);
|
199 |
pyKeywords.add("try",Token.KEYWORD1);
|
200 |
pyKeywords.add("while",Token.KEYWORD1);
|
201 |
pyKeywords.add("def",Token.KEYWORD2);
|
202 |
pyKeywords.add("class",Token.KEYWORD2);
|
203 |
pyKeywords.add("del",Token.KEYWORD2);
|
204 |
pyKeywords.add("from",Token.KEYWORD2);
|
205 |
pyKeywords.add("global",Token.KEYWORD2);
|
206 |
pyKeywords.add("import",Token.KEYWORD2);
|
207 |
pyKeywords.add("in",Token.KEYWORD2);
|
208 |
pyKeywords.add("is",Token.KEYWORD2);
|
209 |
pyKeywords.add("lambda",Token.KEYWORD2);
|
210 |
pyKeywords.add("pass",Token.KEYWORD2);
|
211 |
pyKeywords.add("print",Token.KEYWORD2);
|
212 |
} |
213 |
return pyKeywords;
|
214 |
} |
215 |
|
216 |
// private members
|
217 |
private static KeywordMap pyKeywords; |
218 |
|
219 |
private KeywordMap keywords;
|
220 |
private int lastOffset; |
221 |
private int lastKeyword; |
222 |
|
223 |
private boolean doKeyword(Segment line, int i, char c) |
224 |
{ |
225 |
int i1 = i+1; |
226 |
|
227 |
int len = i - lastKeyword;
|
228 |
byte id = keywords.lookup(line,lastKeyword,len);
|
229 |
if(id != Token.NULL)
|
230 |
{ |
231 |
if(lastKeyword != lastOffset)
|
232 |
addToken(lastKeyword - lastOffset,Token.NULL); |
233 |
addToken(len,id); |
234 |
lastOffset = i; |
235 |
} |
236 |
lastKeyword = i1; |
237 |
return false; |
238 |
} |
239 |
} |