SQL Plugin copied from Quantum plugin and refactored for PHPEclipse
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / parser / SQLLexx.java
diff --git a/archive/net.sourceforge.phpeclipse.sql/src/net/sourceforge/phpdt/sql/parser/SQLLexx.java b/archive/net.sourceforge.phpeclipse.sql/src/net/sourceforge/phpdt/sql/parser/SQLLexx.java
new file mode 100644 (file)
index 0000000..5f1f060
--- /dev/null
@@ -0,0 +1,123 @@
+package net.sourceforge.phpdt.sql.parser;
+
+import java.util.Vector;
+
+public class SQLLexx {
+       private static String endline = ";";
+       private static String dash = "-";
+       public static Vector parse(String text) {
+               Vector tokens = new Vector();
+               StringPointer p = new StringPointer(text);
+               try {
+                       while (!p.isDone()) {
+                               int offset = p.getOffset();
+                               char c = p.getNext();
+                               //System.out.print(c);
+                               if (Character.isWhitespace(c)) {
+                                       StringBuffer value = new StringBuffer();
+                                       while (Character.isWhitespace(c) && !p.isDone()) {
+                                               value.append(c);
+                                               p.mark();
+                                               c = p.getNext();
+                                       }
+                                       // done because of is done
+                                       if (Character.isWhitespace(c)) {
+                                               value.append(c);
+                                       } else {
+                                               p.reset();
+                                       }
+                                       tokens.addElement(new Token(Token.WHITESPACE, value.toString(), offset, offset + value.length()));
+                               } else if (Character.isLetter(c) || c == '_') {
+                                       StringBuffer value = new StringBuffer();
+                                       while ((Character.isLetterOrDigit(c) || c == '_') && !p.isDone()) {
+                                               value.append(c);
+                                               p.mark();
+                                               c = p.getNext();
+                                       }
+                                       if ((Character.isLetterOrDigit(c) || c == '_')) {
+                                               value.append(c);
+                                       } else {
+                                               p.reset();
+                                       }
+                                       tokens.addElement(new Token(Token.IDENTIFIER, value.toString(), offset, offset + value.length()));
+                               } else if (c == '\'') {
+                                       StringBuffer value = new StringBuffer();
+                                       value.append(c);
+                                       if (!p.isDone()) {
+                                               c = p.getNext();
+                                               while (c != '\'' && c != '\n' && !p.isDone()) {
+                                                       value.append(c);
+                                                       c = p.getNext();
+                                               }
+                                               if (c == '\'' || p.isDone()) {
+                                                       value.append(c);
+                                               }
+                                       }
+                                       tokens.addElement(new Token(Token.LITERAL, value.toString(), offset, offset + value.length()));
+                               } else if (c == '-') {
+                                       p.mark();
+                                       if (p.isDone()) {
+                                               tokens.addElement(new Token(Token.SYMBOL, dash, offset, offset + 1));
+                                       } else {
+                                               char next = p.getNext();
+                                               if (next == '-') {
+                                                       StringBuffer value = new StringBuffer("--");
+                                                       if (!p.isDone()) {
+                                                               c = p.getNext();
+                                                               while (c != '\n' && !p.isDone()) {
+                                                                       value.append(c);
+                                                                       c = p.getNext();
+                                                               }
+                                                               if (p.isDone()) {
+                                                                       value.append(c);
+                                                               }
+                                                       }
+                                                       tokens.addElement(new Token(Token.COMMENT, value.toString(), offset, offset + value.length()));
+                                               } else {
+                                                       tokens.addElement(new Token(Token.SYMBOL, dash, offset, offset + 1));
+                                                       p.reset();
+                                               }
+                                       }
+                               } else if (c == ';') {
+                                       tokens.addElement(new Token(Token.SEPARATOR, endline, offset, offset + 1));
+                                       StringBuffer value = new StringBuffer();
+                                       if (!p.isDone()) {
+                                               c = p.getNext();
+                                               while (c != '\n' && !p.isDone()) {
+                                                       value.append(c);
+                                                       c = p.getNext();
+                                               }
+                                               if (p.isDone()) {
+                                                       value.append(c);
+                                               }
+                                               offset++;
+                                               tokens.addElement(new Token(Token.COMMENT, value.toString(), offset, offset + value.length()));
+                                       }
+                               } else if (Character.isDigit(c)) {
+                                       StringBuffer value = new StringBuffer();
+                                       while ((Character.isDigit(c) || c == '.') && !p.isDone()) {
+                                               value.append(c);
+                                               p.mark();
+                                               c = p.getNext();
+                                       }
+                                       if ((Character.isDigit(c) || c == '.')) {
+                                               value.append(c);
+                                       } else {
+                                               p.reset();
+                                       }
+                                       tokens.addElement(new Token(Token.NUMERIC, value.toString(), offset, offset + value.length()));
+                               } else {
+                                       tokens.addElement(new Token(Token.SYMBOL, new String(new char[] {c}), offset, offset + 1));
+                               }
+                       }
+               } catch (RuntimeException e) {
+                       e.printStackTrace();
+               }
+               
+               //System.out.println("-------------------");
+               //for (int i = 0; i < tokens.size(); i++) {
+               //      System.out.println((Token) tokens.elementAt(i));
+               //}
+               return tokens;
+       }
+}