<commit_before>///Created: <NAME> @ IPDevel 

#include "errors/Error.h"
#include "xquerybinary.h"

#include "compiler/codegen/plan_visitor.h"
#include "compiler/translator/translator.h"
#include "compiler/normalizer/normalizer.h"
#include "compiler/parsetree/parsenode_print_xml_visitor.h"
#include "compiler/parsetree/parsenode_print_dot_visitor.h"
#include "compiler/parser/xquery_driver.h"
#include "util/zorba.h"
//#include "store/naive/basic_item_factory.h"
//#include "store/naive/simple_store.h"
#include "runtime/visitors/printervisitor.h"
#include "runtime/visitors/iterprinter.h"

#include "api/serialization/serializer.h"
#include "api/external/dynamic_context_wrapper.h"

#include "zorba/util/properties.h"

//#include "../../test/timer.h"

#include <iostream>
#include <fstream>

using namespace std;
namespace xqp {


Zorba_XQueryBinary::Zorba_XQueryBinary( xqp_string  xquery_source_uri,
                                       const char* query_text ) :
  m_xquery_source_uri(xquery_source_uri),
  m_query_text (query_text)
{
  is_compiled = false;
  top_iterator = NULL;
  lStateSize = 0;
  internal_sctx = NULL;

  default_collator = NULL;
  //addReference();
//  xquery_registered_callback = NULL;
//  xquery_registered_param = NULL;
}

//Zorba_XQueryBinary::Zorba_XQueryBinary()
//{
//}

Zorba_XQueryBinary::~Zorba_XQueryBinary()
{
  delete internal_sctx;
}

bool Zorba_XQueryBinary::compile(StaticQueryContext* sctx, 
                                 bool routing_mode)
{
  zorba *thread_specific_zorba;

  thread_specific_zorba = zorba::getZorbaForCurrentThread();
  thread_specific_zorba->current_xquery = this;
  
  try{

  if(is_compiled)
  {
    ZORBA_ERROR_ALERT(error_messages::API0004_XQUERY_ALREADY_COMPILED,
                                  error_messages::STATIC_ERROR,
                                  NULL,
                                  true///continue execution
                                  );
    thread_specific_zorba->current_xquery = NULL;
    return false;
  }

  if(sctx)
  {
    internal_static_context = sctx;
    //now build the static context
    StaticContextWrapper    *context_wrapper = static_cast<StaticContextWrapper*>(sctx);
    internal_sctx = context_wrapper->FillInStaticContext();//return a static_context*
  }
  else
  {
    internal_sctx = new static_context;
  }
  internal_sctx->set_entity_file_uri(m_xquery_source_uri);

  ///reset the error list from error manager
//  m_error_manager.clear();///delete all alerts from list

	//if(m_query_text.empty())
	if(!m_query_text[0])
	{
		ZORBA_ERROR_ALERT(error_messages::API0001_XQUERY_STRING_IS_EMPTY,
																	error_messages::STATIC_ERROR,
																	NULL,
																	true///continue execution
																	);
		//UnregisterCurrentXQueryForCurrentThread( this );
		thread_specific_zorba->current_xquery = NULL;
		return false;
	}
	
	///NOW COMPILE
	xquery_driver driver;
	driver.filename = m_xquery_source_uri;

  ///build up the expression tree
  driver.parse_string(m_query_text);

	parsenode* n_p = driver.get_expr();

  if (Properties::instance()->printAST())
	{
    cout << "Parse tree:\n";
    ParseNodePrintXMLVisitor lPrintXMLVisitor(std::cout);
    lPrintXMLVisitor.print(n_p);
  }    
	
  // cout << "Syntax tree:\n";
  // n_p->put(cout) << endl;

  ///normalize the expression tree
  translator nvs;
  MainModule* mm_p;
  QueryBody* qb_p;
  Expr* ex_p;

	if ((mm_p = dynamic_cast<MainModule*>(n_p))==NULL) 
	{
		cerr << "Parse error: expecting MainModule\n";
		ZORBA_ERROR_ALERT(error_messages::XQP0014_SYSTEM_SHOULD_NEVER_BE_REACHED,
																	error_messages::SYSTEM_ERROR,
																	NULL,
																	true///continue execution
																	);
		//UnregisterCurrentXQueryForCurrentThread( this );
		thread_specific_zorba->current_xquery = NULL;
		return false;
	}
	if ((qb_p = dynamic_cast<QueryBody*>(&*mm_p->get_query_body()))==NULL) 
	{
		cerr << "Parse error: expecting MainModule->QueryBody\n";
		ZORBA_ERROR_ALERT(error_messages::XQP0014_SYSTEM_SHOULD_NEVER_BE_REACHED,
																	error_messages::SYSTEM_ERROR,
																	NULL,
																	true///continue execution
																	);
		//UnregisterCurrentXQueryForCurrentThread( this );
		thread_specific_zorba->current_xquery = NULL;
		return false;
	}
	if ((ex_p = dynamic_cast<Expr*>(&*qb_p->get_expr()))==NULL) 
	{
		cerr << "Parse error: expecting MainModule->QueryBody->Expr\n";
		ZORBA_ERROR_ALERT(error_messages::XQP0014_SYSTEM_SHOULD_NEVER_BE_REACHED,
																	error_messages::SYSTEM_ERROR,
																	NULL,
																	true///continue execution
																	);
		//UnregisterCurrentXQueryForCurrentThread( this );
		thread_specific_zorba->current_xquery = NULL;
		return false;
	}

  if (Properties::instance ()->printNormalizedExpressions ())
    cout << "Expression tree:\n";
	mm_p->accept(nvs);
	rchandle<expr> e_h = nvs.pop_nodestack();

	if (e_h==NULL) {
		ZORBA_ERROR_ALERT(error_messages::API0002_COMPILE_FAILED,
																	error_messages::STATIC_ERROR,<commit_msg>Fixed error reporting for parser errors<commit_after>///Created: <NAME> @ IPDevel 

#include "errors/Error.h"
#include "xquerybinary.h"

#include "compiler/codegen/plan_visitor.h"
#include "compiler/translator/translator.h"
#include "compiler/normalizer/normalizer.h"
#include "compiler/parsetree/parsenode_print_xml_visitor.h"
#include "compiler/parsetree/parsenode_print_dot_visitor.h"
#include "compiler/parser/xquery_driver.h"
#include "util/zorba.h"
//#include "store/naive/basic_item_factory.h"
//#include "store/naive/simple_store.h"
#include "runtime/visitors/printervisitor.h"
#include "runtime/visitors/iterprinter.h"

#include "api/serialization/serializer.h"
#include "api/external/dynamic_context_wrapper.h"

#include "zorba/util/properties.h"

//#include "../../test/timer.h"

#include <iostream>
#include <fstream>

using namespace std;
namespace xqp {


Zorba_XQueryBinary::Zorba_XQueryBinary( xqp_string  xquery_source_uri,
                                       const char* query_text ) :
  m_xquery_source_uri(xquery_source_uri),
  m_query_text (query_text)
{
  is_compiled = false;
  top_iterator = NULL;
  lStateSize = 0;
  internal_sctx = NULL;

  default_collator = NULL;
  //addReference();
//  xquery_registered_callback = NULL;
//  xquery_registered_param = NULL;
}

//Zorba_XQueryBinary::Zorba_XQueryBinary()
//{
//}

Zorba_XQueryBinary::~Zorba_XQueryBinary()
{
  delete internal_sctx;
}

bool Zorba_XQueryBinary::compile(StaticQueryContext* sctx, 
                                 bool routing_mode)
{
  zorba *thread_specific_zorba;

  thread_specific_zorba = zorba::getZorbaForCurrentThread();
  thread_specific_zorba->current_xquery = this;
  
  try{

  if(is_compiled)
  {
    ZORBA_ERROR_ALERT(error_messages::API0004_XQUERY_ALREADY_COMPILED,
                                  error_messages::STATIC_ERROR,
                                  NULL,
                                  true///continue execution
                                  );
    thread_specific_zorba->current_xquery = NULL;
    return false;
  }

  if(sctx)
  {
    internal_static_context = sctx;
    //now build the static context
    StaticContextWrapper    *context_wrapper = static_cast<StaticContextWrapper*>(sctx);
    internal_sctx = context_wrapper->FillInStaticContext();//return a static_context*
  }
  else
  {
    internal_sctx = new static_context;
  }
  internal_sctx->set_entity_file_uri(m_xquery_source_uri);

  ///reset the error list from error manager
//  m_error_manager.clear();///delete all alerts from list

	//if(m_query_text.empty())
	if(!m_query_text[0])
	{
		ZORBA_ERROR_ALERT(error_messages::API0001_XQUERY_STRING_IS_EMPTY,
																	error_messages::STATIC_ERROR,
																	NULL,
																	true///continue execution
																	);
		//UnregisterCurrentXQueryForCurrentThread( this );
		thread_specific_zorba->current_xquery = NULL;
		return false;
	}
	
	///NOW COMPILE
	xquery_driver driver;
	driver.filename = m_xquery_source_uri;

  ///build up the expression tree
  driver.parse_string(m_query_text);

	parsenode* n_p = driver.get_expr();

  if (Properties::instance()->printAST())
	{
    cout << "Parse tree:\n";
    ParseNodePrintXMLVisitor lPrintXMLVisitor(std::cout);
    lPrintXMLVisitor.print(n_p);
  }    
	
  // normalize the expression tree
  translator nvs;
  MainModule* mm_p;
  QueryBody* qb_p;
  Expr* ex_p;

	if ((mm_p = dynamic_cast<MainModule*>(n_p))==NULL) 
	{
		ZORBA_ERROR_ALERT(error_messages::XPST0003_STATIC_GRAMMAR_ERROR,
                      error_messages::SYSTEM_ERROR,
                      NULL,
                      /* continue_execution = */ true);
		cerr << "Parse error: expecting MainModule\n";
		//UnregisterCurrentXQueryForCurrentThread( this );
		thread_specific_zorba->current_xquery = NULL;
		return false;
	}
	if ((qb_p = dynamic_cast<QueryBody*>(&*mm_p->get_query_body()))==NULL) 
	{
		ZORBA_ERROR_ALERT(error_messages::XPST0003_STATIC_GRAMMAR_ERROR,
                      error_messages::SYSTEM_ERROR,
                      NULL,
                      /* continue_execution = */ true);
		cerr << "Parse error: expecting MainModule->QueryBody\n";
		//UnregisterCurrentXQueryForCurrentThread( this );
		thread_specific_zorba->current_xquery = NULL;
		return false;
	}
	if ((ex_p = dynamic_cast<Expr*>(&*qb_p->get_expr()))==NULL) 
	{
		ZORBA_ERROR_ALERT(error_messages::XPST0003_STATIC_GRAMMAR_ERROR,
                      error_messages::SYSTEM_ERROR,
                      NULL,
                      /* continue_execution = */ true);
		cerr << "Parse error: expecting MainModule->QueryBody->Expr\n";
		//UnregisterCurrentXQueryForCurrentThread( this );
		thread_specific_zorba->current_xquery = NULL;
		return false;
	}

  if (Properties::instance ()->printNormalizedExpressions ())
    cout << "Expression tree:\n";
	mm_p->accept(nvs);
	rchandle<expr> e_h = nvs.pop_nodestack();

	if (e_h==NULL) {
		ZORBA_ERROR_ALERT(error_messages::API0002_COMPILE_FAILED,
																	error_messages::STATIC_ERROR,
{'fraction_non_alphanumeric': 0.11309701122033562, 'fraction_numerical': 0.005163340283983716, 'mean_word_length': 3.2124634044332914, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2460042', 'n_tokens_mistral': 3859, 'n_tokens_neox': 3353, 'n_words': 503}