# Copyright (C) 2004 Gregoire Lejeune <gregoire.lejeune@free.fr>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

class AspCompiler
  require "asp/subsmatch"
  require "xml/script"

  def initialize
    @init_code = ""
    @subm_code = ""
    @comp_code = ""

    @error = false
    @error_line = 0
    @error_code = ""
    @error_message = ""

	@oXmlScript = XML::Script::new( "language" => "ruby", "precompile" => Proc.new{ |xCode| 
	  submatch( xCode )
	} )
  end

  private
  def getCodeNotNil( xData )
    if xData.nil? == true
	  return ""
	end
	return xData
  end

  public
  def compile( xCode )
    xOutCode = nil
	
    begin
	 xOutCode = @oXmlScript.script( xCode ) 
	rescue XmlScriptError => e
	 @error = true
	 @error_line = e.line
	 @error_code = @oXmlScript.preCompiledCode
	 @error_message = e.message
	 xOutCode = nil
	end
    
	@init_code = getCodeNotNil( @oXmlScript.initialCode )
	@comp_code = getCodeNotNil( @oXmlScript.codeToCompile )
	@subm_code = getCodeNotNil( @oXmlScript.preCompiledCode )
	return xOutCode
  end

  ## Just for compatibility
  attr :put_cmd, true
  attr :pre_cmd, true
  attr :post_cmd, true

  attr :init_code, true
  attr :subm_code, true
  attr :comp_code, true

  attr :error, true
  attr :error_line, true
  attr :error_code, true
  attr :error_message, true
end
