1 package com.quantum.sql.parser;
3 import java.util.Vector;
6 private static String endline = ";"; //$NON-NLS-1$
7 private static String dash = "-"; //$NON-NLS-1$
8 private static String group = "/"; //$NON-NLS-1$
10 * Parses a SQL text into tokens.
12 * @return a vector of Token objects.
14 public static Vector parse(String text) {
15 Vector tokens = new Vector();
16 StringPointer p = new StringPointer(text);
19 int offset = p.getOffset();
21 // Adds END_OF_LINE token
23 tokens.addElement(new Token(Token.END_OF_LINE, "\n", offset, offset + 1));
25 // Adds WHITESPACE token;
26 else if (Character.isWhitespace(c)) {
27 StringBuffer value = new StringBuffer();
28 while (Character.isWhitespace(c) && !p.isDone()) {
32 // done because of is done
33 if (Character.isWhitespace(c)) {
35 } else if (!p.isDone()){
38 tokens.addElement(new Token(Token.WHITESPACE, value.toString(), offset, offset + value.length()));
39 // Adds IDENTIFIER token (can be reserved SQL word or not);
40 } else if (Character.isLetter(c) || c == '_' || c == '$') {
41 StringBuffer value = new StringBuffer();
42 while ((Character.isLetterOrDigit(c) || c == '_' || c == '$') && !p.isDone()) {
46 if ((Character.isLetterOrDigit(c) || c == '_')) {
48 } else if (!p.isDone()){
51 tokens.addElement(new Token(Token.IDENTIFIER, value.toString(), offset, offset + value.length()));
52 // Adds LITERAL token;
53 } else if (c == '\'') {
54 StringBuffer value = new StringBuffer();
58 while (c != '\'' && c != '\n' && !p.isDone()) {
62 if (c == '\'' || p.isDone()) {
64 } else if (!p.isDone()){
68 tokens.addElement(new Token(Token.LITERAL, value.toString(), offset, offset + value.length()));
69 // Adds COMMENT token (or SYMBOL (dash) if only one dash);
70 } else if (c == '-') {
73 tokens.addElement(new Token(Token.SYMBOL, dash, offset, offset + 1));
75 char next = p.getNext();
77 StringBuffer value = new StringBuffer("--"); //$NON-NLS-1$
80 while (c != '\n' && !p.isDone()) {
90 tokens.addElement(new Token(Token.COMMENT, value.toString(), offset, offset + value.length()));
92 tokens.addElement(new Token(Token.SYMBOL, dash, offset, offset + 1));
96 // Adds SEPARATOR token (;), considers the rest of the line as COMMENT token;
97 } else if (c == ';') {
98 tokens.addElement(new Token(Token.SEPARATOR, endline, offset, offset + 1));
99 StringBuffer value = new StringBuffer();
102 while (c != '\n' && !p.isDone()) {
111 // We add to the offset so as to skip the initial ';'
113 tokens.addElement(new Token(Token.COMMENT, value.toString(), offset, offset + value.length()));
115 // Adds NUMERIC token;
116 } else if (Character.isDigit(c)) {
117 StringBuffer value = new StringBuffer();
118 while ((Character.isDigit(c) || c == '.') && !p.isDone()) {
122 if ((Character.isDigit(c) || c == '.')) {
127 tokens.addElement(new Token(Token.NUMERIC, value.toString(), offset, offset + value.length()));
128 // Adds COMMENT token (or GROUP (slash) if only one slash);
129 } else if (c == '/') {
131 // If we have '/*', it's a comment till '*/' found or eof
132 if (p.peek() == '*') {
133 tokens.addElement(tokenizeComment(p, offset));
135 // It's not '/*' , so it's a group token
136 // BCH ??? what's this business about groups?
137 // Shouldn't '/' be a divide operator?
138 tokens.addElement(new Token(Token.SYMBOL, new String(new char[] {c}) /*group*/, offset, offset + 1));
141 // Adds SYMBOL token;
143 tokens.addElement(new Token(Token.SYMBOL, new String(new char[] {c}), offset, offset + 1));
146 } catch (RuntimeException e) {
150 // System.out.println("-------------------");
151 // for (int i = 0; i < tokens.size(); i++) {
152 // System.out.println((Token) tokens.elementAt(i));
161 private static Token tokenizeComment(StringPointer p, int offset) {
163 StringBuffer value = new StringBuffer();
166 while (!( c == '*' && p.peek() == '/' ) && !p.isDone()) {
175 return new Token(Token.COMMENT, value.toString(), offset, offset + value.length());