001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * SQLTokenMarker.java
028: *
029: */
030:
031: package org.syntax.jedit.tokenmarker;
032:
033: import org.syntax.jedit.*;
034: import javax.swing.text.Segment;
035:
036: /**
037: * SQL token marker.
038: *
039: * @author mike dillon
040: * @version $Id: SQLTokenMarker.java 1167 2008-01-15 18:49:05Z gtoffoli $
041: */
042: public class SQLTokenMarker extends TokenMarker {
043: private int offset, lastOffset, lastKeyword, length;
044:
045: // public members
046: public SQLTokenMarker(KeywordMap k) {
047: this (k, false);
048: }
049:
050: public SQLTokenMarker(KeywordMap k, boolean tsql) {
051: keywords = k;
052: isTSQL = tsql;
053: }
054:
055: public byte markTokensImpl(byte token, Segment line, int lineIndex) {
056: offset = lastOffset = lastKeyword = line.offset;
057: length = line.count + offset;
058:
059: loop: for (int i = offset; i < length; i++) {
060: switch (line.array[i]) {
061: case '*':
062: if (token == Token.COMMENT1 && length - i >= 1
063: && line.array[i + 1] == '/') {
064: token = Token.NULL;
065: i++;
066: addToken((i + 1) - lastOffset, Token.COMMENT1);
067: lastOffset = i + 1;
068: } else if (token == Token.NULL) {
069: searchBack(line, i);
070: addToken(1, Token.OPERATOR);
071: lastOffset = i + 1;
072: }
073: break;
074: case '[':
075: if (token == Token.NULL) {
076: searchBack(line, i);
077: token = Token.LITERAL1;
078: literalChar = '[';
079: lastOffset = i;
080: }
081: break;
082: case ']':
083: if (token == Token.LITERAL1 && literalChar == '[') {
084: token = Token.NULL;
085: literalChar = 0;
086: addToken((i + 1) - lastOffset, Token.LITERAL1);
087: lastOffset = i + 1;
088: }
089: break;
090: case '.':
091: case ',':
092: case '(':
093: case ')':
094: if (token == Token.NULL) {
095: searchBack(line, i);
096: addToken(1, Token.NULL);
097: lastOffset = i + 1;
098: }
099: break;
100: case '+':
101: case '%':
102: case '&':
103: case '|':
104: case '^':
105: case '~':
106: case '<':
107: case '>':
108: case '=':
109: if (token == Token.NULL) {
110: searchBack(line, i);
111: addToken(1, Token.OPERATOR);
112: lastOffset = i + 1;
113: }
114: break;
115: case ' ':
116: case '\t':
117: if (token == Token.NULL) {
118: searchBack(line, i, false);
119: }
120: break;
121: case ':':
122: if (token == Token.NULL) {
123: addToken((i + 1) - lastOffset, Token.LABEL);
124: lastOffset = i + 1;
125: }
126: break;
127: case '/':
128: if (token == Token.NULL) {
129: if (length - i >= 2 && line.array[i + 1] == '*') {
130: searchBack(line, i);
131: token = Token.COMMENT1;
132: lastOffset = i;
133: i++;
134: } else {
135: searchBack(line, i);
136: addToken(1, Token.OPERATOR);
137: lastOffset = i + 1;
138: }
139: }
140: break;
141: case '-':
142: if (token == Token.NULL) {
143: if (length - i >= 2 && line.array[i + 1] == '-') {
144: searchBack(line, i);
145: addToken(length - i, Token.COMMENT1);
146: lastOffset = length;
147: break loop;
148: } else {
149: searchBack(line, i);
150: addToken(1, Token.OPERATOR);
151: lastOffset = i + 1;
152: }
153: }
154: break;
155: case '!':
156: if (isTSQL
157: && token == Token.NULL
158: && length - i >= 2
159: && (line.array[i + 1] == '='
160: || line.array[i + 1] == '<' || line.array[i + 1] == '>')) {
161: searchBack(line, i);
162: addToken(1, Token.OPERATOR);
163: lastOffset = i + 1;
164: }
165: break;
166: case '"':
167: case '\'':
168: if (token == Token.NULL) {
169: token = Token.LITERAL1;
170: literalChar = line.array[i];
171: addToken(i - lastOffset, Token.NULL);
172: lastOffset = i;
173: } else if (token == Token.LITERAL1
174: && literalChar == line.array[i]) {
175: token = Token.NULL;
176: literalChar = 0;
177: addToken((i + 1) - lastOffset, Token.LITERAL1);
178: lastOffset = i + 1;
179: }
180: break;
181: default:
182: break;
183: }
184: }
185: if (token == Token.NULL)
186: searchBack(line, length, false);
187: if (lastOffset != length)
188: addToken(length - lastOffset, token);
189: return token;
190: }
191:
192: // protected members
193: protected boolean isTSQL = false;
194:
195: // private members
196: private KeywordMap keywords;
197: private char literalChar = 0;
198:
199: private void searchBack(Segment line, int pos) {
200: searchBack(line, pos, true);
201: }
202:
203: private void searchBack(Segment line, int pos, boolean padNull) {
204: int len = pos - lastKeyword;
205: byte id = keywords.lookup(line, lastKeyword, len);
206: if (id != Token.NULL) {
207: if (lastKeyword != lastOffset)
208: addToken(lastKeyword - lastOffset, Token.NULL);
209: addToken(len, id);
210: lastOffset = pos;
211: }
212: lastKeyword = pos + 1;
213: if (padNull && lastOffset < pos)
214: addToken(pos - lastOffset, Token.NULL);
215: }
216: }
|