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);			
		}		
	}
	
}

@@ -146,17 +146,16 @@
 .List;%0A%0A
-%0A
 public c
@@ -177,17 +177,16 @@
 arser %7B%0A
-%09
 %0A%09JbdScr
@@ -208,17 +208,16 @@
 = null;%0A
-%09
 %0A%09public
@@ -247,18 +247,17 @@
  script)
-%0A%09
+ 
 %7B%0A%09%09lexe
@@ -305,27 +305,24 @@
 cript));%0A%09%7D%0A
-%09%0A%09
 %0A%09public Str
@@ -340,18 +340,17 @@
 tement()
-%0A%09
+ 
 %7B%0A%09%09List
@@ -408,18 +408,16 @@
 ken%3E();%0A
-%09%09
 %0A%09%09try %7B
@@ -475,16 +475,17 @@
 e;%0A%09%09%09do
+ 
 %7B%0A%09%09%09%09t 
@@ -515,12 +515,15 @@
 %09%09if
+ 
 (t
+ 
 ==
+ 
 null
@@ -532,18 +532,20 @@
 %7C t.kind
+ 
 ==
+ 
 JbdScrip
@@ -565,22 +565,18 @@
 MICOLON)
+ 
 %7B
-%09%09%09%09%09
 %0A%09%09%09%09%09st
@@ -607,18 +607,15 @@
 %09%09%09%7D
+ 
 else
+ 
 %7B
-%09%09%09%09%09
 %0A%09%09%09
@@ -640,21 +640,20 @@
 %09%09%09%7D
-%09%09%09
 %0A%09%09%09%7D
+ 
 while
+ 
 (!st
@@ -690,16 +690,17 @@
  %7B%0A%09%09%09//
+ 
 should n
@@ -777,18 +777,24 @@
 %09%09if
+ 
 (size
+ 
 ==
+ 
 0)
- 
+%0A%09%09%09
 retu
@@ -854,20 +854,25 @@
 %09for
+ 
 (int i
-=
+ = 
 0; i
-%3C
+ %3C 
 size
@@ -877,16 +877,17 @@
 ze; i++)
+ 
 %7B%0A%09%09%09Jbd
@@ -930,16 +930,17 @@
 itch
+ 
 (t.kind)
 %0A%09%09%09
@@ -935,20 +935,17 @@
 (t.kind)
-%0A%09%09%09
+ 
 %7B%0A%09%09%09cas
@@ -1232,19 +1232,28 @@
 %09%09if
+ 
 (i
-%3C
+ %3C 
 size
--1) 
+ - 1)%0A%09%09%09%09
 buf.
@@ -1297,19 +1297,16 @@
 g();%0A%09%7D%0A
-%09%0A%09
 %0A%09public
@@ -1354,20 +1354,19 @@
 xception
+ 
 %7B%0A
-%09%09
 %0A%09%09Strin
@@ -1540,15 +1540,18 @@
 hile
+ 
 ((stmt
-=
+ = 
 read
@@ -1573,15 +1573,18 @@
 t())
+ 
 !=
+ 
 null)
+ 
 %7B%0A%09%09
@@ -1613,21 +1613,15 @@
 mt);
-%09%09%09
 %0A%09%09%7D
-%09%09
 %0A%09%7D%0A
-%09
 %0A%7D%0A

Fix format