/*
 * Copyright (c) 2012 Guidewire Software, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package gw.vark.task;

import gw.lang.parser.GosuParserFactory;
import gw.lang.parser.IGosuParser;
import gw.lang.parser.StandardSymbolTable;
import gw.lang.parser.exceptions.IEvaluationException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

@SuppressWarnings({"UnusedDeclaration"})
public class GosuInvokerTask extends Task {

  private StringBuilder _expression = new StringBuilder();

  public void addText(String text) {
    _expression.append(getProject().replaceProperties(text));
  }

  public void execute() throws BuildException {
    if (_expression.length() == 0) {
      throw new BuildException("no expression to evaluate");
    }

    try {
      IGosuParser parser = GosuParserFactory.createParser( _expression.toString(), new StandardSymbolTable( true ) );
      parser.parseExpOrProgram( null, false, true ).evaluate();
    } catch (IEvaluationException e) {
      if (e.getCause() != null && e.getCause() instanceof BuildException) {
        // TODO - blc - the Gosu stack trace could be useful here
        throw (BuildException) e.getCause();
      }
      else {
        throw e;
      }
    } catch (Exception e) {
      throw new BuildException(e);
    }
  }
}

@@ -629,16 +629,49 @@
 .task;%0A%0A
+import gw.config.CommonServices;%0A
 import g
@@ -726,26 +726,178 @@
 parser.I
-GosuParser
+FileContext;%0Aimport gw.lang.parser.IGosuProgramParser;%0Aimport gw.lang.parser.IParseResult;%0Aimport gw.lang.parser.ITypeUsesMap;%0Aimport gw.lang.parser.ParserOptions
 ;%0Aimport
@@ -980,32 +980,64 @@
 ationException;%0A
+import gw.vark.AardvarkProgram;%0A
 import org.apach
@@ -1060,24 +1060,62 @@
 dException;%0A
+import org.apache.tools.ant.Location;%0A
 import org.a
@@ -1137,16 +1137,40 @@
 .Task;%0A%0A
+import java.util.List;%0A%0A
 @Suppres
@@ -1411,16 +1411,325 @@
 );%0A  %7D%0A%0A
+  @Override%0A  public void init() throws BuildException %7B%0A    // Append N-1 empty lines, to match Gosu line numbers with build.xml.%0A    Location loc = getLocation();%0A    if (loc != null) %7B%0A      for (int i = 0; i %3C getLocation().getLineNumber() - 1; i++) %7B%0A        _expression.append('%5Cn');%0A      %7D%0A    %7D%0A  %7D%0A%0A
   public
@@ -1891,24 +1891,31 @@
       IGosuP
+rogramP
 arser parser
@@ -1946,74 +1946,107 @@
 ateP
-arser( _expression.toString(), new StandardSymbolTable( true ) );%0A
+rogramParser();%0A%0A      String script = _expression.toString();%0A      IParseResult result =%0A        
     
@@ -2062,16 +2062,23 @@
 parseExp
+ression
 OrProgra
@@ -2083,27 +2083,117 @@
 ram(
- null, false, true 
+script,%0A                      new StandardSymbolTable(true), createParserOptions());%0A      result.getProgram(
 ).ev
@@ -2199,16 +2199,20 @@
 valuate(
+null
 );%0A    %7D
@@ -2439,22 +2439,16 @@
 %0A      %7D
-%0A     
  else %7B%0A
@@ -2544,11 +2544,839 @@
    %7D%0A  %7D
+%0A%0A  private ParserOptions createParserOptions() %7B%0A    IFileContext fileContext = new IFileContext() %7B%0A      @Override%0A      public String getContextString() %7B%0A        return null;%0A      %7D%0A%0A      @Override%0A      public String getClassName() %7B%0A        return null;%0A      %7D%0A%0A      @Override%0A      public String getFilePath() %7B%0A        return getLocation().getFileName();%0A      %7D%0A    %7D;%0A%0A    List%3CString%3E packages = AardvarkProgram.getDefaultTypeUsesPackages();%0A    ITypeUsesMap typeUses = CommonServices.getGosuIndustrialPark().createTypeUsesMap(packages);%0A    for (String aPackage : packages) %7B%0A      typeUses.addToDefaultTypeUses(aPackage);%0A    %7D%0A%0A    ParserOptions options =%0A            new ParserOptions()%0A                    .withFileContext(fileContext)%0A                    .withTypeUsesMap(typeUses);%0A    return options;%0A  %7D
 %0A%7D%0A

Correct line numbers for embedded Gosu programs