svn-gvsig-desktop / tags / v1_0_2_Build_897 / libraries / libIverUtiles / src / com / iver / utiles / console / jedit / SQLTokenMarker.java @ 10444
History | View | Annotate | Download (4.41 KB)
1 |
package com.iver.utiles.console.jedit; |
---|---|
2 |
/*
|
3 |
* SQLTokenMarker.java - Generic SQL token marker
|
4 |
* Copyright (C) 1999 mike dillon
|
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 |
* SQL token marker.
|
15 |
*
|
16 |
* @author mike dillon
|
17 |
* @version $Id$
|
18 |
*/
|
19 |
public class SQLTokenMarker extends TokenMarker |
20 |
{ |
21 |
private int offset, lastOffset, lastKeyword, length; |
22 |
|
23 |
// public members
|
24 |
public SQLTokenMarker(KeywordMap k)
|
25 |
{ |
26 |
this(k, false); |
27 |
} |
28 |
|
29 |
public SQLTokenMarker(KeywordMap k, boolean tsql) |
30 |
{ |
31 |
keywords = k; |
32 |
isTSQL = tsql; |
33 |
} |
34 |
|
35 |
public byte markTokensImpl(byte token, Segment line, int lineIndex) |
36 |
{ |
37 |
offset = lastOffset = lastKeyword = line.offset; |
38 |
length = line.count + offset; |
39 |
|
40 |
loop: |
41 |
for(int i = offset; i < length; i++) |
42 |
{ |
43 |
switch(line.array[i])
|
44 |
{ |
45 |
case '*': |
46 |
if(token == Token.COMMENT1 && length - i >= 1 && line.array[i+1] == '/') |
47 |
{ |
48 |
token = Token.NULL; |
49 |
i++; |
50 |
addToken((i + 1) - lastOffset,Token.COMMENT1);
|
51 |
lastOffset = i + 1;
|
52 |
} |
53 |
else if (token == Token.NULL) |
54 |
{ |
55 |
searchBack(line, i); |
56 |
addToken(1,Token.OPERATOR);
|
57 |
lastOffset = i + 1;
|
58 |
} |
59 |
break;
|
60 |
case '[': |
61 |
if(token == Token.NULL)
|
62 |
{ |
63 |
searchBack(line, i); |
64 |
token = Token.LITERAL1; |
65 |
literalChar = '[';
|
66 |
lastOffset = i; |
67 |
} |
68 |
break;
|
69 |
case ']': |
70 |
if(token == Token.LITERAL1 && literalChar == '[') |
71 |
{ |
72 |
token = Token.NULL; |
73 |
literalChar = 0;
|
74 |
addToken((i + 1) - lastOffset,Token.LITERAL1);
|
75 |
lastOffset = i + 1;
|
76 |
} |
77 |
break;
|
78 |
case '.': case ',': case '(': case ')': |
79 |
if (token == Token.NULL) {
|
80 |
searchBack(line, i); |
81 |
addToken(1, Token.NULL);
|
82 |
lastOffset = i + 1;
|
83 |
} |
84 |
break;
|
85 |
case '+': case '%': case '&': case '|': case '^': |
86 |
case '~': case '<': case '>': case '=': |
87 |
if (token == Token.NULL) {
|
88 |
searchBack(line, i); |
89 |
addToken(1,Token.OPERATOR);
|
90 |
lastOffset = i + 1;
|
91 |
} |
92 |
break;
|
93 |
case ' ': case '\t': |
94 |
if (token == Token.NULL) {
|
95 |
searchBack(line, i, false);
|
96 |
} |
97 |
break;
|
98 |
case ':': |
99 |
if(token == Token.NULL)
|
100 |
{ |
101 |
addToken((i+1) - lastOffset,Token.LABEL);
|
102 |
lastOffset = i + 1;
|
103 |
} |
104 |
break;
|
105 |
case '/': |
106 |
if(token == Token.NULL)
|
107 |
{ |
108 |
if (length - i >= 2 && line.array[i + 1] == '*') |
109 |
{ |
110 |
searchBack(line, i); |
111 |
token = Token.COMMENT1; |
112 |
lastOffset = i; |
113 |
i++; |
114 |
} |
115 |
else
|
116 |
{ |
117 |
searchBack(line, i); |
118 |
addToken(1,Token.OPERATOR);
|
119 |
lastOffset = i + 1;
|
120 |
} |
121 |
} |
122 |
break;
|
123 |
case '-': |
124 |
if(token == Token.NULL)
|
125 |
{ |
126 |
if (length - i >= 2 && line.array[i+1] == '-') |
127 |
{ |
128 |
searchBack(line, i); |
129 |
addToken(length - i,Token.COMMENT1); |
130 |
lastOffset = length; |
131 |
break loop;
|
132 |
} |
133 |
else
|
134 |
{ |
135 |
searchBack(line, i); |
136 |
addToken(1,Token.OPERATOR);
|
137 |
lastOffset = i + 1;
|
138 |
} |
139 |
} |
140 |
break;
|
141 |
case '!': |
142 |
if(isTSQL && token == Token.NULL && length - i >= 2 && |
143 |
(line.array[i+1] == '=' || line.array[i+1] == '<' || line.array[i+1] == '>')) |
144 |
{ |
145 |
searchBack(line, i); |
146 |
addToken(1,Token.OPERATOR);
|
147 |
lastOffset = i + 1;
|
148 |
} |
149 |
break;
|
150 |
case '"': case '\'': |
151 |
if(token == Token.NULL)
|
152 |
{ |
153 |
token = Token.LITERAL1; |
154 |
literalChar = line.array[i]; |
155 |
addToken(i - lastOffset,Token.NULL); |
156 |
lastOffset = i; |
157 |
} |
158 |
else if(token == Token.LITERAL1 && literalChar == line.array[i]) |
159 |
{ |
160 |
token = Token.NULL; |
161 |
literalChar = 0;
|
162 |
addToken((i + 1) - lastOffset,Token.LITERAL1);
|
163 |
lastOffset = i + 1;
|
164 |
} |
165 |
break;
|
166 |
default:
|
167 |
break;
|
168 |
} |
169 |
} |
170 |
if(token == Token.NULL)
|
171 |
searchBack(line, length, false);
|
172 |
if(lastOffset != length)
|
173 |
addToken(length - lastOffset,token); |
174 |
return token;
|
175 |
} |
176 |
|
177 |
// protected members
|
178 |
protected boolean isTSQL = false; |
179 |
|
180 |
// private members
|
181 |
private KeywordMap keywords;
|
182 |
private char literalChar = 0; |
183 |
|
184 |
private void searchBack(Segment line, int pos) |
185 |
{ |
186 |
searchBack(line, pos, true);
|
187 |
} |
188 |
|
189 |
private void searchBack(Segment line, int pos, boolean padNull) |
190 |
{ |
191 |
int len = pos - lastKeyword;
|
192 |
byte id = keywords.lookup(line,lastKeyword,len);
|
193 |
if(id != Token.NULL)
|
194 |
{ |
195 |
if(lastKeyword != lastOffset)
|
196 |
addToken(lastKeyword - lastOffset,Token.NULL); |
197 |
addToken(len,id); |
198 |
lastOffset = pos; |
199 |
} |
200 |
lastKeyword = pos + 1;
|
201 |
if (padNull && lastOffset < pos)
|
202 |
addToken(pos - lastOffset, Token.NULL); |
203 |
} |
204 |
} |