SQLToken.java

1
/*
2
 * This file is part of a syntax highlighting package
3
 * Copyright (C) 2002  Stephen Ostermiller
4
 * http://ostermiller.org/contact.pl?regarding=Syntax+Highlighting
5
 * 
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * See COPYING.TXT for details.
17
 */
18
package com.jsql.view.swing.sql.lexer.syntax;
19
20
/**
21
 * A SQLToken is a token that is returned by a lexer that is lexing an SQL
22
 * source file. It has several attributes describing the token: The type of
23
 * token, the text of the token, the line number on which it occurred, the
24
 * number of characters into the input at which it started, and similarly, the
25
 * number of characters into the input at which it ended. <br>
26
 */
27
public class SQLToken extends Token {
28
    /**
29
     * A reserved word (keyword)
30
     */
31
    public static final int RESERVED_WORD = 0x100;
32
33
    /**
34
     * A variable, name, or other identifier
35
     */
36
    public static final int IDENTIFIER = 0x200;
37
38
    /**
39
     * A string literal
40
     */
41
    public static final int LITERAL_STRING = 0x300;
42
    /**
43
     * A bit-string
44
     */
45
    public static final int LITERAL_BIT_STRING = 0x310;
46
    /**
47
     * An integer
48
     */
49
    public static final int LITERAL_INTEGER = 0x320;
50
    /**
51
     * A floating point
52
     */
53
    public static final int LITERAL_FLOAT = 0x330;
54
55
    /**
56
     * A separator
57
     */
58
    public static final int SEPARATOR = 0x400;
59
60
    /**
61
     * An operator
62
     */
63
    public static final int OPERATOR = 0x500;
64
65
    /**
66
     * C style comment, (except possibly nested)
67
     */
68
    public static final int COMMENT_TRADITIONAL = 0xD00;
69
70
    /**
71
     * a -- to end of line comment.
72
     */
73
    public static final int COMMENT_END_OF_LINE = 0xD10;
74
75
    /**
76
     * White space
77
     */
78
    public static final int WHITE_SPACE = 0xE00;
79
80
    /**
81
     * An error
82
     */
83
    public static final int ERROR = 0xF00;
84
    /**
85
     * An comment start embedded in an operator
86
     */
87
    public static final int ERROR_UNCLOSED_COMMENT = 0xF02;
88
    /**
89
     * An comment start embedded in an operator
90
     */
91
    public static final int ERROR_UNCLOSED_STRING = 0xF03;
92
    /**
93
     * An comment start embedded in an operator
94
     */
95
    public static final int ERROR_UNCLOSED_BIT_STRING = 0xF04;
96
    /**
97
     * An comment start embedded in an operator
98
     */
99
    public static final int ERROR_BAD_BIT_STRING = 0xF05;
100
101
    private final int id;
102
    private final String contents;
103
    private final int lineNumber;
104
    private final int charBegin;
105
    private final int charEnd;
106
    private final int state;
107
108
    /**
109
     * Create a new token. The constructor is typically called by the lexer
110
     *
111
     * @param id
112
     *            the id number of the token
113
     * @param contents
114
     *            A string representing the text of the token
115
     * @param lineNumber
116
     *            the line number of the input on which this token started
117
     * @param charBegin
118
     *            the offset into the input in characters at which this token
119
     *            started
120
     * @param charEnd
121
     *            the offset into the input in characters at which this token
122
     *            ended
123
     */
124
    public SQLToken(int id, String contents, int lineNumber, int charBegin, int charEnd) {
125
        this(id, contents, lineNumber, charBegin, charEnd, Token.UNDEFINED_STATE);
126
    }
127
128
    /**
129
     * Create a new token. The constructor is typically called by the lexer
130
     *
131
     * @param id
132
     *            the id number of the token
133
     * @param contents
134
     *            A string representing the text of the token
135
     * @param lineNumber
136
     *            the line number of the input on which this token started
137
     * @param charBegin
138
     *            the offset into the input in characters at which this token
139
     *            started
140
     * @param charEnd
141
     *            the offset into the input in characters at which this token
142
     *            ended
143
     * @param state
144
     *            the state the tokenizer is in after returning this token.
145
     */
146
    public SQLToken(int id, String contents, int lineNumber, int charBegin, int charEnd, int state) {
147
        this.id = id;
148
        this.contents = contents;
149
        this.lineNumber = lineNumber;
150
        this.charBegin = charBegin;
151
        this.charEnd = charEnd;
152
        this.state = state;
153
    }
154
155
    /**
156
     * Get an integer representing the state the tokenizer is in after returning
157
     * this token. Those who are interested in incremental tokenizing for
158
     * performance reasons will want to use this method to figure out where the
159
     * tokenizer may be restarted. The tokenizer starts in Token.INITIAL_STATE,
160
     * so any time that it reports that it has returned to this state, the
161
     * tokenizer may be restarted from there.
162
     */
163
    @Override
164
    public int getState() {
165 1 1. getState : replaced int return with 0 for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getState → NO_COVERAGE
        return this.state;
166
    }
167
168
    /**
169
     * get the ID number of this token
170
     * 
171
     * @return the id number of the token
172
     */
173
    @Override
174
    public int getID() {
175 1 1. getID : replaced int return with 0 for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getID → NO_COVERAGE
        return this.id;
176
    }
177
178
    /**
179
     * get the contents of this token
180
     * 
181
     * @return A string representing the text of the token
182
     */
183
    @Override
184
    public String getContents() {
185 1 1. getContents : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getContents → NO_COVERAGE
        return this.contents;
186
    }
187
188
    /**
189
     * get the line number of the input on which this token started
190
     * 
191
     * @return the line number of the input on which this token started
192
     */
193
    @Override
194
    public int getLineNumber() {
195 1 1. getLineNumber : replaced int return with 0 for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getLineNumber → NO_COVERAGE
        return this.lineNumber;
196
    }
197
198
    /**
199
     * get the offset into the input in characters at which this token started
200
     *
201
     * @return the offset into the input in characters at which this token
202
     *         started
203
     */
204
    @Override
205
    public int getCharBegin() {
206 1 1. getCharBegin : replaced int return with 0 for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getCharBegin → NO_COVERAGE
        return this.charBegin;
207
    }
208
209
    /**
210
     * get the offset into the input in characters at which this token ended
211
     *
212
     * @return the offset into the input in characters at which this token ended
213
     */
214
    @Override
215
    public int getCharEnd() {
216 1 1. getCharEnd : replaced int return with 0 for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getCharEnd → NO_COVERAGE
        return this.charEnd;
217
    }
218
219
    /**
220
     * Checks this token to see if it is a reserved word. Reserved words are
221
     * explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java
222
     * Language Specification</A>.
223
     *
224
     * @return true if this token is a reserved word, false otherwise
225
     */
226
    public boolean isReservedWord() {
227 3 1. isReservedWord : Replaced Shift Right with Shift Left → NO_COVERAGE
2. isReservedWord : replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isReservedWord → NO_COVERAGE
3. isReservedWord : negated conditional → NO_COVERAGE
        return (this.id >> 8) == 0x1;
228
    }
229
230
    /**
231
     * Checks this token to see if it is an identifier. Identifiers are
232
     * explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java
233
     * Language Specification</A>.
234
     *
235
     * @return true if this token is an identifier, false otherwise
236
     */
237
    public boolean isIdentifier() {
238 3 1. isIdentifier : negated conditional → NO_COVERAGE
2. isIdentifier : Replaced Shift Right with Shift Left → NO_COVERAGE
3. isIdentifier : replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isIdentifier → NO_COVERAGE
        return (this.id >> 8) == 0x2;
239
    }
240
241
    /**
242
     * Checks this token to see if it is a literal. Literals are explained in <A
243
     * Href=http://java.sun.com/docs/books/jls/html/>Java Language
244
     * Specification</A>.
245
     *
246
     * @return true if this token is a literal, false otherwise
247
     */
248
    public boolean isLiteral() {
249 3 1. isLiteral : replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isLiteral → NO_COVERAGE
2. isLiteral : negated conditional → NO_COVERAGE
3. isLiteral : Replaced Shift Right with Shift Left → NO_COVERAGE
        return (this.id >> 8) == 0x3;
250
    }
251
252
    /**
253
     * Checks this token to see if it is a Separator. Separators are explained
254
     * in <A Href=http://java.sun.com/docs/books/jls/html/>Java Language
255
     * Specification</A>.
256
     *
257
     * @return true if this token is a Separator, false otherwise
258
     */
259
    public boolean isSeparator() {
260 3 1. isSeparator : replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isSeparator → NO_COVERAGE
2. isSeparator : negated conditional → NO_COVERAGE
3. isSeparator : Replaced Shift Right with Shift Left → NO_COVERAGE
        return (this.id >> 8) == 0x4;
261
    }
262
263
    /**
264
     * Checks this token to see if it is a Operator. Operators are explained in
265
     * <A Href=http://java.sun.com/docs/books/jls/html/>Java Language
266
     * Specification</A>.
267
     *
268
     * @return true if this token is a Operator, false otherwise
269
     */
270
    public boolean isOperator() {
271 3 1. isOperator : negated conditional → NO_COVERAGE
2. isOperator : Replaced Shift Right with Shift Left → NO_COVERAGE
3. isOperator : replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isOperator → NO_COVERAGE
        return (this.id >> 8) == 0x5;
272
    }
273
274
    /**
275
     * Checks this token to see if it is a comment.
276
     * 
277
     * @return true if this token is a comment, false otherwise
278
     */
279
    @Override
280
    public boolean isComment() {
281 3 1. isComment : negated conditional → NO_COVERAGE
2. isComment : Replaced Shift Right with Shift Left → NO_COVERAGE
3. isComment : replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isComment → NO_COVERAGE
        return (this.id >> 8) == 0xD;
282
    }
283
284
    /**
285
     * Checks this token to see if it is White Space. Usually tabs, line breaks,
286
     * form feed, spaces, etc.
287
     * 
288
     * @return true if this token is White Space, false otherwise
289
     */
290
    @Override
291
    public boolean isWhiteSpace() {
292 3 1. isWhiteSpace : negated conditional → NO_COVERAGE
2. isWhiteSpace : replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isWhiteSpace → NO_COVERAGE
3. isWhiteSpace : Replaced Shift Right with Shift Left → NO_COVERAGE
        return (this.id >> 8) == 0xE;
293
    }
294
295
    /**
296
     * Checks this token to see if it is an Error. Unfinished comments, numbers
297
     * that are too big, unclosed strings, etc.
298
     * 
299
     * @return true if this token is an Error, false otherwise
300
     */
301
    @Override
302
    public boolean isError() {
303 3 1. isError : replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isError → NO_COVERAGE
2. isError : negated conditional → NO_COVERAGE
3. isError : Replaced Shift Right with Shift Left → NO_COVERAGE
        return (this.id >> 8) == 0xF;
304
    }
305
306
    /**
307
     * A description of this token. The description should be appropriate for
308
     * syntax highlighting. For example "comment" is returned for a comment.
309
     *
310
     * @return a description of this token.
311
     */
312
    @Override
313
    public String getDescription() {
314 1 1. getDescription : negated conditional → NO_COVERAGE
        if (this.isReservedWord()) {
315 1 1. getDescription : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE
            return "reservedWord";
316 1 1. getDescription : negated conditional → NO_COVERAGE
        } else if (this.isIdentifier()) {
317 1 1. getDescription : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE
            return "identifier";
318 1 1. getDescription : negated conditional → NO_COVERAGE
        } else if (this.isLiteral()) {
319 1 1. getDescription : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE
            return "literal";
320 1 1. getDescription : negated conditional → NO_COVERAGE
        } else if (this.isSeparator()) {
321 1 1. getDescription : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE
            return "separator";
322 1 1. getDescription : negated conditional → NO_COVERAGE
        } else if (this.isOperator()) {
323 1 1. getDescription : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE
            return "operator";
324 1 1. getDescription : negated conditional → NO_COVERAGE
        } else if (this.isComment()) {
325 1 1. getDescription : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE
            return "comment";
326 1 1. getDescription : negated conditional → NO_COVERAGE
        } else if (this.isWhiteSpace()) {
327 1 1. getDescription : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE
            return "whitespace";
328 1 1. getDescription : negated conditional → NO_COVERAGE
        } else if (this.isError()) {
329 1 1. getDescription : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE
            return "error";
330
        } else {
331 1 1. getDescription : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE
            return "unknown";
332
        }
333
    }
334
335
    /**
336
     * get a String that explains the error, if this token is an error.
337
     * 
338
     * @return a String that explains the error, if this token is an error, null
339
     *         otherwise.
340
     */
341
    @Override
342
    public String errorString() {
343
        String s;
344 1 1. errorString : negated conditional → NO_COVERAGE
        if (this.isError()) {
345
            s = "Error on line " + this.lineNumber + ": ";
346
            switch (this.id) {
347
            case ERROR:
348
                s += "Unexpected token: " + this.contents;
349
                break;
350
            case ERROR_UNCLOSED_COMMENT:
351
                s += "Unclosed comment: " + this.contents;
352
                break;
353
            case ERROR_UNCLOSED_STRING:
354
                s += "Unclosed string literal: " + this.contents;
355
                break;
356
            case ERROR_UNCLOSED_BIT_STRING:
357
                s += "Unclosed bit-string literal: " + this.contents;
358
                break;
359
            case ERROR_BAD_BIT_STRING:
360
                s += "Bit-strings can only contain 0 and 1: " + this.contents;
361
                break;
362
            }
363
        } else {
364
            s = null;
365
        }
366 1 1. errorString : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::errorString → NO_COVERAGE
        return s;
367
    }
368
369
    /**
370
     * get a representation of this token as a human-readable string. The format
371
     * of this string is subject to change and should only be used for debugging
372
     * purposes.
373
     *
374
     * @return a string representation of this token
375
     */
376
    @Override
377
    public String toString() {
378 1 1. toString : replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::toString → NO_COVERAGE
        return "Token #" + Integer.toHexString(this.id) + ": " + this.getDescription() + " Line " + this.lineNumber + " from "
379
                + this.charBegin + " to " + this.charEnd + " : " + this.contents;
380
    }
381
}

Mutations

165

1.1
Location : getState
Killed by : none
replaced int return with 0 for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getState → NO_COVERAGE

175

1.1
Location : getID
Killed by : none
replaced int return with 0 for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getID → NO_COVERAGE

185

1.1
Location : getContents
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getContents → NO_COVERAGE

195

1.1
Location : getLineNumber
Killed by : none
replaced int return with 0 for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getLineNumber → NO_COVERAGE

206

1.1
Location : getCharBegin
Killed by : none
replaced int return with 0 for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getCharBegin → NO_COVERAGE

216

1.1
Location : getCharEnd
Killed by : none
replaced int return with 0 for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getCharEnd → NO_COVERAGE

227

1.1
Location : isReservedWord
Killed by : none
Replaced Shift Right with Shift Left → NO_COVERAGE

2.2
Location : isReservedWord
Killed by : none
replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isReservedWord → NO_COVERAGE

3.3
Location : isReservedWord
Killed by : none
negated conditional → NO_COVERAGE

238

1.1
Location : isIdentifier
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : isIdentifier
Killed by : none
Replaced Shift Right with Shift Left → NO_COVERAGE

3.3
Location : isIdentifier
Killed by : none
replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isIdentifier → NO_COVERAGE

249

1.1
Location : isLiteral
Killed by : none
replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isLiteral → NO_COVERAGE

2.2
Location : isLiteral
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : isLiteral
Killed by : none
Replaced Shift Right with Shift Left → NO_COVERAGE

260

1.1
Location : isSeparator
Killed by : none
replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isSeparator → NO_COVERAGE

2.2
Location : isSeparator
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : isSeparator
Killed by : none
Replaced Shift Right with Shift Left → NO_COVERAGE

271

1.1
Location : isOperator
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : isOperator
Killed by : none
Replaced Shift Right with Shift Left → NO_COVERAGE

3.3
Location : isOperator
Killed by : none
replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isOperator → NO_COVERAGE

281

1.1
Location : isComment
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : isComment
Killed by : none
Replaced Shift Right with Shift Left → NO_COVERAGE

3.3
Location : isComment
Killed by : none
replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isComment → NO_COVERAGE

292

1.1
Location : isWhiteSpace
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : isWhiteSpace
Killed by : none
replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isWhiteSpace → NO_COVERAGE

3.3
Location : isWhiteSpace
Killed by : none
Replaced Shift Right with Shift Left → NO_COVERAGE

303

1.1
Location : isError
Killed by : none
replaced boolean return with true for com/jsql/view/swing/sql/lexer/syntax/SQLToken::isError → NO_COVERAGE

2.2
Location : isError
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : isError
Killed by : none
Replaced Shift Right with Shift Left → NO_COVERAGE

314

1.1
Location : getDescription
Killed by : none
negated conditional → NO_COVERAGE

315

1.1
Location : getDescription
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE

316

1.1
Location : getDescription
Killed by : none
negated conditional → NO_COVERAGE

317

1.1
Location : getDescription
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE

318

1.1
Location : getDescription
Killed by : none
negated conditional → NO_COVERAGE

319

1.1
Location : getDescription
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE

320

1.1
Location : getDescription
Killed by : none
negated conditional → NO_COVERAGE

321

1.1
Location : getDescription
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE

322

1.1
Location : getDescription
Killed by : none
negated conditional → NO_COVERAGE

323

1.1
Location : getDescription
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE

324

1.1
Location : getDescription
Killed by : none
negated conditional → NO_COVERAGE

325

1.1
Location : getDescription
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE

326

1.1
Location : getDescription
Killed by : none
negated conditional → NO_COVERAGE

327

1.1
Location : getDescription
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE

328

1.1
Location : getDescription
Killed by : none
negated conditional → NO_COVERAGE

329

1.1
Location : getDescription
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE

331

1.1
Location : getDescription
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::getDescription → NO_COVERAGE

344

1.1
Location : errorString
Killed by : none
negated conditional → NO_COVERAGE

366

1.1
Location : errorString
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::errorString → NO_COVERAGE

378

1.1
Location : toString
Killed by : none
replaced return value with "" for com/jsql/view/swing/sql/lexer/syntax/SQLToken::toString → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.16.1