KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > syntax > jedit > tokenmarker > SQLTokenMarker


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * SQLTokenMarker.java
28  *
29  */

30
31 package org.syntax.jedit.tokenmarker;
32
33 import org.syntax.jedit.*;
34 import javax.swing.text.Segment JavaDoc;
35
36 /**
37  * SQL token marker.
38  *
39  * @author mike dillon
40  * @version $Id: SQLTokenMarker.java 932 2006-10-20 09:32:45Z gtoffoli $
41  */

42 public class SQLTokenMarker extends TokenMarker
43 {
44     private int offset, lastOffset, lastKeyword, length;
45
46     // public members
47
public SQLTokenMarker(KeywordMap k)
48     {
49         this(k, false);
50     }
51
52     public SQLTokenMarker(KeywordMap k, boolean tsql)
53     {
54         keywords = k;
55         isTSQL = tsql;
56     }
57
58     public byte markTokensImpl(byte token, Segment JavaDoc line, int lineIndex)
59     {
60         offset = lastOffset = lastKeyword = line.offset;
61         length = line.count + offset;
62
63 loop:
64         for(int i = offset; i < length; i++)
65         {
66             switch(line.array[i])
67             {
68             case '*':
69                 if(token == Token.COMMENT1 && length - i >= 1 && line.array[i+1] == '/')
70                 {
71                     token = Token.NULL;
72                     i++;
73                     addToken((i + 1) - lastOffset,Token.COMMENT1);
74                     lastOffset = i + 1;
75                 }
76                 else if (token == Token.NULL)
77                 {
78                     searchBack(line, i);
79                     addToken(1,Token.OPERATOR);
80                     lastOffset = i + 1;
81                 }
82                 break;
83             case '[':
84                 if(token == Token.NULL)
85                 {
86                     searchBack(line, i);
87                     token = Token.LITERAL1;
88                     literalChar = '[';
89                     lastOffset = i;
90                 }
91                 break;
92             case ']':
93                 if(token == Token.LITERAL1 && literalChar == '[')
94                 {
95                     token = Token.NULL;
96                     literalChar = 0;
97                     addToken((i + 1) - lastOffset,Token.LITERAL1);
98                     lastOffset = i + 1;
99                 }
100                 break;
101             case '.': case ',': case '(': case ')':
102                 if (token == Token.NULL) {
103                     searchBack(line, i);
104                     addToken(1, Token.NULL);
105                     lastOffset = i + 1;
106                 }
107                 break;
108             case '+': case '%': case '&': case '|': case '^':
109             case '~': case '<': case '>': case '=':
110                 if (token == Token.NULL) {
111                     searchBack(line, i);
112                     addToken(1,Token.OPERATOR);
113                     lastOffset = i + 1;
114                 }
115                 break;
116             case ' ': case '\t':
117                 if (token == Token.NULL) {
118                     searchBack(line, i, false);
119                 }
120                 break;
121             case ':':
122                 if(token == Token.NULL)
123                 {
124                     addToken((i+1) - lastOffset,Token.LABEL);
125                     lastOffset = i + 1;
126                 }
127                 break;
128             case '/':
129                 if(token == Token.NULL)
130                 {
131                     if (length - i >= 2 && line.array[i + 1] == '*')
132                     {
133                         searchBack(line, i);
134                         token = Token.COMMENT1;
135                         lastOffset = i;
136                         i++;
137                     }
138                     else
139                     {
140                         searchBack(line, i);
141                         addToken(1,Token.OPERATOR);
142                         lastOffset = i + 1;
143                     }
144                 }
145                 break;
146             case '-':
147                 if(token == Token.NULL)
148                 {
149                     if (length - i >= 2 && line.array[i+1] == '-')
150                     {
151                         searchBack(line, i);
152                         addToken(length - i,Token.COMMENT1);
153                         lastOffset = length;
154                         break loop;
155                     }
156                     else
157                     {
158                         searchBack(line, i);
159                         addToken(1,Token.OPERATOR);
160                         lastOffset = i + 1;
161                     }
162                 }
163                 break;
164             case '!':
165                 if(isTSQL && token == Token.NULL && length - i >= 2 &&
166                 (line.array[i+1] == '=' || line.array[i+1] == '<' || line.array[i+1] == '>'))
167                 {
168                     searchBack(line, i);
169                     addToken(1,Token.OPERATOR);
170                     lastOffset = i + 1;
171                 }
172                 break;
173             case '"': case '\'':
174                 if(token == Token.NULL)
175                 {
176                     token = Token.LITERAL1;
177                     literalChar = line.array[i];
178                     addToken(i - lastOffset,Token.NULL);
179                     lastOffset = i;
180                 }
181                 else if(token == Token.LITERAL1 && literalChar == line.array[i])
182                 {
183                     token = Token.NULL;
184                     literalChar = 0;
185                     addToken((i + 1) - lastOffset,Token.LITERAL1);
186                     lastOffset = i + 1;
187                 }
188                 break;
189             default:
190                 break;
191             }
192         }
193         if(token == Token.NULL)
194             searchBack(line, length, false);
195         if(lastOffset != length)
196             addToken(length - lastOffset,token);
197         return token;
198     }
199
200     // protected members
201
protected boolean isTSQL = false;
202
203     // private members
204
private KeywordMap keywords;
205     private char literalChar = 0;
206
207     private void searchBack(Segment JavaDoc line, int pos)
208     {
209         searchBack(line, pos, true);
210     }
211
212     private void searchBack(Segment JavaDoc line, int pos, boolean padNull)
213     {
214         int len = pos - lastKeyword;
215         byte id = keywords.lookup(line,lastKeyword,len);
216         if(id != Token.NULL)
217         {
218             if(lastKeyword != lastOffset)
219                 addToken(lastKeyword - lastOffset,Token.NULL);
220             addToken(len,id);
221             lastOffset = pos;
222         }
223         lastKeyword = pos + 1;
224         if (padNull && lastOffset < pos)
225             addToken(pos - lastOffset, Token.NULL);
226     }
227 }
228
Popular Tags