Fix java 1.4 VM compatibility.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / parser / SQLLexx.java
1 package net.sourceforge.phpdt.sql.parser;
2
3 import java.util.Vector;
4
5 public class SQLLexx {
6         private static String endline = ";";
7         private static String dash = "-";
8         public static Vector parse(String text) {
9                 Vector tokens = new Vector();
10                 StringPointer p = new StringPointer(text);
11                 try {
12                         while (!p.isDone()) {
13                                 int offset = p.getOffset();
14                                 char c = p.getNext();
15                                 //System.out.print(c);
16                                 if (Character.isWhitespace(c)) {
17                                         StringBuffer value = new StringBuffer();
18                                         while (Character.isWhitespace(c) && !p.isDone()) {
19                                                 value.append(c);
20                                                 p.mark();
21                                                 c = p.getNext();
22                                         }
23                                         // done because of is done
24                                         if (Character.isWhitespace(c)) {
25                                                 value.append(c);
26                                         } else {
27                                                 p.reset();
28                                         }
29                                         tokens.addElement(new Token(Token.WHITESPACE, value.toString(), offset, offset + value.length()));
30                                 } else if (Character.isLetter(c) || c == '_') {
31                                         StringBuffer value = new StringBuffer();
32                                         while ((Character.isLetterOrDigit(c) || c == '_') && !p.isDone()) {
33                                                 value.append(c);
34                                                 p.mark();
35                                                 c = p.getNext();
36                                         }
37                                         if ((Character.isLetterOrDigit(c) || c == '_')) {
38                                                 value.append(c);
39                                         } else {
40                                                 p.reset();
41                                         }
42                                         tokens.addElement(new Token(Token.IDENTIFIER, value.toString(), offset, offset + value.length()));
43                                 } else if (c == '\'') {
44                                         StringBuffer value = new StringBuffer();
45                                         value.append(c);
46                                         if (!p.isDone()) {
47                                                 c = p.getNext();
48                                                 while (c != '\'' && c != '\n' && !p.isDone()) {
49                                                         value.append(c);
50                                                         c = p.getNext();
51                                                 }
52                                                 if (c == '\'' || p.isDone()) {
53                                                         value.append(c);
54                                                 }
55                                         }
56                                         tokens.addElement(new Token(Token.LITERAL, value.toString(), offset, offset + value.length()));
57                                 } else if (c == '-') {
58                                         p.mark();
59                                         if (p.isDone()) {
60                                                 tokens.addElement(new Token(Token.SYMBOL, dash, offset, offset + 1));
61                                         } else {
62                                                 char next = p.getNext();
63                                                 if (next == '-') {
64                                                         StringBuffer value = new StringBuffer("--");
65                                                         if (!p.isDone()) {
66                                                                 c = p.getNext();
67                                                                 while (c != '\n' && !p.isDone()) {
68                                                                         value.append(c);
69                                                                         c = p.getNext();
70                                                                 }
71                                                                 if (p.isDone()) {
72                                                                         value.append(c);
73                                                                 }
74                                                         }
75                                                         tokens.addElement(new Token(Token.COMMENT, value.toString(), offset, offset + value.length()));
76                                                 } else {
77                                                         tokens.addElement(new Token(Token.SYMBOL, dash, offset, offset + 1));
78                                                         p.reset();
79                                                 }
80                                         }
81                                 } else if (c == ';') {
82                                         tokens.addElement(new Token(Token.SEPARATOR, endline, offset, offset + 1));
83                                         StringBuffer value = new StringBuffer();
84                                         if (!p.isDone()) {
85                                                 c = p.getNext();
86                                                 while (c != '\n' && !p.isDone()) {
87                                                         value.append(c);
88                                                         c = p.getNext();
89                                                 }
90                                                 if (p.isDone()) {
91                                                         value.append(c);
92                                                 }
93                                                 offset++;
94                                                 tokens.addElement(new Token(Token.COMMENT, value.toString(), offset, offset + value.length()));
95                                         }
96                                 } else if (Character.isDigit(c)) {
97                                         StringBuffer value = new StringBuffer();
98                                         while ((Character.isDigit(c) || c == '.') && !p.isDone()) {
99                                                 value.append(c);
100                                                 p.mark();
101                                                 c = p.getNext();
102                                         }
103                                         if ((Character.isDigit(c) || c == '.')) {
104                                                 value.append(c);
105                                         } else {
106                                                 p.reset();
107                                         }
108                                         tokens.addElement(new Token(Token.NUMERIC, value.toString(), offset, offset + value.length()));
109                                 } else {
110                                         tokens.addElement(new Token(Token.SYMBOL, new String(new char[] {c}), offset, offset + 1));
111                                 }
112                         }
113                 } catch (RuntimeException e) {
114                         e.printStackTrace();
115                 }
116                 
117                 //System.out.println("-------------------");
118                 //for (int i = 0; i < tokens.size(); i++) {
119                 //      System.out.println((Token) tokens.elementAt(i));
120                 //}
121                 return tokens;
122         }
123 }