package org.libj.xquery;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.libj.xquery.compiler.Compiler;

public class Main {
    public static void repl() throws IOException {
        Compiler compiler = new Compiler();
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = 0;
        while (true) {
            System.out.print(String.format("(: %d :) ", ++n));
            System.out.flush();
            String line = in.readLine();
            if (line == null) {
                break;
            }
            if (line.matches("\\s*")) {
                continue;
            }
            try {
                System.out.println(compiler.eval(line));
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        if (args.length == 0) {
            repl();
        }
        else if (args.length == 2) {
            String xqueryPath = args[0];
            String className = args[0];
            File source = new File(xqueryPath);
            File target = new File(className.replace('.', '/')+".class");
            Compile.compileToFile(Compile.compileToAST(source), className, target);
        }
        else {
            System.out.println("java org.libj.xquery.Main");
            System.out.println("java org.libj.xquery.Main xquery-path class-name");
        }
    }
}

@@ -175,16 +175,108 @@
 ompiler;
+%0Aimport org.libj.xquery.lexer.LexerException;%0Aimport org.libj.xquery.parser.ParserException;
 %0A%0Apublic
@@ -289,34 +289,36 @@
  Main %7B%0A
+%0A
     p
-ublic
+rivate
  static 
 void rep
@@ -313,29 +313,22 @@
 tic 
-void repl() throws IO
+boolean isEOF(
 Exce
@@ -324,32 +324,35 @@
  isEOF(Exception
+ e)
  %7B%0A        Compi
@@ -350,143 +350,173 @@
     
-Compiler compiler = new Compiler(
+return e.getMessage().endsWith(%22%3CEOF:%3CEOF%3E%3E%22
 );%0A    
+%7D%0A%0A
     
-BufferedReader in = new BufferedReader(new InputStreamReader(System.in));%0A        int n = 0
+private static void e(Compiler compiler, BufferedReader in) throws IOException %7B%0A        String xquery = %22%22
 ;%0A  
@@ -540,103 +540,8 @@
 ) %7B%0A
-            System.out.print(String.format(%22(: %25d :) %22, ++n));%0A            System.out.flush();%0A
     
@@ -629,13 +629,22 @@
     
-break
+System.exit(0)
 ;%0A  
@@ -727,32 +727,60 @@
 ;%0A            %7D%0A
+            xquery += line;%0A
             try 
@@ -834,14 +834,319 @@
 val(
-line))
+xquery));%0A                return;%0A            %7D catch (LexerException e) %7B%0A                if (isEOF(e)) %7B%0A                    continue;%0A                %7D else %7B%0A                    throw e;%0A                %7D%0A            %7D catch (ParserException e) %7B%0A                if (isEOF(e)) %7B%0A                    continue
 ;%0A  
@@ -1155,29 +1155,485 @@
         
-%7D%0A           
+    %7D else %7B%0A                    throw e;%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    public static void repl() throws IOException %7B%0A        Compiler compiler = new Compiler();%0A        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));%0A        int n = 0;%0A        while (true) %7B%0A            System.out.print(String.format(%22(: %25d :) %22, ++n));%0A            System.out.flush();%0A            try %7B%0A                e(compiler, in);%0A            %7D
  catch (

support multi-lined xquery in shell