package com.asksunny.jbdstudio.sql.parser;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class JbdScriptParser {

	JbdScriptLexer lexer = null;

	public JbdScriptParser(String script) {
		lexer = new JbdScriptLexer(new StringReader(script));
	}

	public String nextStatement() {
		List<JbdScriptToken> tokens = new ArrayList<JbdScriptToken>();

		try {
			JbdScriptToken t = null;
			boolean stop = false;
			do {
				t = lexer.nextToken();
				if (t == null || t.kind == JbdScriptConstants.SEMICOLON) {
					stop = true;
					break;
				} else {
					tokens.add(t);
				}
			} while (!stop);
		} catch (IOException e) {
			// should never be here;
			e.printStackTrace();
		}
		int size = tokens.size();
		if (size == 0)
			return null;
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < size; i++) {
			JbdScriptToken t = tokens.get(i);
			switch (t.kind) {
			case JbdScriptConstants.DOUBLE_QUOTE_LITERAL:
				buf.append('"').append(t.image).append('"');
				break;
			case JbdScriptConstants.SINGLE_QUOTE_LITERAL:
				buf.append('\'').append(t.image).append('\'');
				break;
			default:
				buf.append(tokens.get(i).image);
				break;
			}
			if (i < size - 1)
				buf.append(" ");
		}
		return buf.toString();
	}

	public static void main(String[] args) throws Exception {

		String test = "SELECT *, 'my \\ntext' as \"my column\" FROM test;\n insert into my table; ";
		JbdScriptParser reader = new JbdScriptParser(test);
		String stmt = null;
		while ((stmt = reader.nextStatement()) != null) {
			System.out.println(stmt);
		}
	}

}

@@ -1303,325 +1303,7 @@
 %7D%0A%0A%09
-public static void main(String%5B%5D args) throws Exception %7B%0A%0A%09%09String test = %22SELECT *, 'my %5C%5Cntext' as %5C%22my column%5C%22 FROM test;%5Cn insert into my table; %22;%0A%09%09JbdScriptParser reader = new JbdScriptParser(test);%0A%09%09String stmt = null;%0A%09%09while ((stmt = reader.nextStatement()) != null) %7B%0A%09%09%09System.out.println(stmt);%0A%09%09%7D%0A%09%7D%0A
 %0A%7D%0A

Remove junk code