<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 * 
 * 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.
 */
#include "compiler/parsetree/parsenode_print_xquery_visitor.h"

#include <ostream>
#include <string>
#include <map>
#include <stack>

#include "compiler/parsetree/parsenode_visitor.h"

#include "store/api/update_consts.h"

#define DEFAULT_BEGIN_VISIT(type)                    \
  void* begin_visit(const type&){ return no_state; } 

#define DEFAULT_END_VISIT(type)                      \
  void end_visit(const type&, void*){}              

#define DEFAULT_VISIT(type)                          \
  DEFAULT_BEGIN_VISIT(type)                          \
  DEFAULT_END_VISIT(type)                            \

using namespace std;

namespace zorba {

class ParseNodePrintXQueryVisitor: public parsenode_visitor
{
  private:
    std::ostream& os;
    FunctionIndex theFunctionIndex;

  public:
    ParseNodePrintXQueryVisitor(std::ostream& aStream): os(aStream){}

    FunctionIndex& getFunctionIndex()
    {
      return theFunctionIndex;
    }

    ParseNodePrintXQueryVisitor* print(const parsenode* p)
    {
      p->accept(*this);
      return this;
    }
    
    void* begin_visit(const AbbrevForwardStep& n)
    {
      if(n.get_attr_bit())
      {
        os << '@';
      }
      return no_state;
    }
    DEFAULT_END_VISIT(AbbrevForwardStep)

  	DEFAULT_BEGIN_VISIT (AnyKindTest)
  	void end_visit(const AnyKindTest& n, void* state)
    {
      os << "node()";
    }
    
    DEFAULT_VISIT (AposAttrContentList)
  	
    void* begin_visit(const AposAttrValueContent& n)
    {
      if(!n.get_apos_atcontent().empty())
      {
        os << n.get_apos_atcontent();
      } else {
        n.get_common_content()->accept(*this);
      }
      return 0;
    }
    DEFAULT_END_VISIT (AposAttrValueContent)
  	
    void* begin_visit(const ArgList& n)
    {
      for (int i=0; i<n.size(); ++i) {
        if(i > 0)
        {
          os << ", ";
        }
        const exprnode* e_p = &*(n[i]);
        e_p->accept(*this);
      } 
      return 0;
    }
    DEFAULT_END_VISIT (ArgList)
  	
    DEFAULT_BEGIN_VISIT (AtomicType)
  	void end_visit(const AtomicType& n, void* state)
    {
      os << n.get_qname()->get_qname();
    }

    void* begin_visit(const AttributeTest& n)
    {
      os << "attribute(";
      return no_state;
    }

    void end_visit(const AttributeTest& n, void*)
    {
      os << ")";
    }

    void* begin_visit(const BaseURIDecl& n)
    {
      os << "declare base-uri \"" << n.get_base_uri() << "\";";
      return no_state;
    }
  	DEFAULT_END_VISIT (BaseURIDecl);
  	
    void* begin_visit(const BoundarySpaceDecl& n)
    {
      os << "declare boundary-space ";
      switch(n.get_boundary_space_mode())
      {
        case StaticContextConsts::preserve_space:
          os << "preserve";
          break;
        case StaticContextConsts::strip_space:
          os << "strip";
          break;
      }
      os << ';';
      return no_state;
    }
    DEFAULT_END_VISIT (BoundarySpaceDecl)
  	
    void* begin_visit(const CaseClause& n)
    {
      os << "case ";
      if(!n.get_varname().empty())
      {
        os << "$" << n.get_varname() << " as ";
      }
      n.get_type()->accept(*this);
      os << "return ";
      n.get_expr()->accept(*this);
      return no_state;
    }
    DEFAULT_END_VISIT (CaseClause)

  	DEFAULT_VISIT (CaseClauseList) //@checked

    void* begin_visit(const CommentTest& n)
    {
      os << "comment()";
      return no_state;
    }
  	DEFAULT_END_VISIT (CommentTest);
  	
    void* begin_visit(const ConstructionDecl& n)
    {
      os << "declare construction ";
      switch(n.get_mode())
      {
        case StaticContextConsts::cons_preserve:
          os << "preserve";
          break;
        case StaticContextConsts::cons_strip:
          os << "strip";
          break;
      }
      os << ';';
      return no_state;
    }
    DEFAULT_END_VISIT (ConstructionDecl)

    void* begin_visit(const CopyNamespacesDecl& n)
    {
      os << "declare copy-namespaces ";
      switch(n.get_preserve_mode())
      {
        case StaticContextConsts::preserve_ns:
          os << "preserve,";
          break;
        case StaticContextConsts::no_preserve_ns:
          os << "no-preserve,";
          break;
      }
      switch(n.get_inherit_mode())
      {
        case StaticContextConsts::inherit_ns:
          os << "inherit";
          break;
        case StaticContextConsts::no_inherit_ns:
          os << "no-inherit";
          break;
      }
      return 0;
    }
  	DEFAULT_END_VISIT (CopyNamespacesDecl)
  	
    void* begin_visit(const DefaultCollationDecl& n)
    {
      os << "declare default collation " << n.get_collation();
      return 0;
    }
    DEFAULT_END_VISIT (DefaultCollationDecl)

    void* begin_visit(const DefaultNamespaceDecl& n)
    {
      os << "declare default ";
      switch(n.get_mode())
      {
        case ParseConstants::ns_element_default:
          os << "element ";
          break;
        case ParseConstants::ns_function_default:
          os << "function ";
          break;
      }
      os << "namespace ";
      os << n.get_default_namespace();
      return 0;
    }
  	DEFAULT_END_VISIT (DefaultNamespaceDecl)

    void* begin_visit(const DirAttr& n)
    {
      n.get_name()->accept(*this);
      os << '=';
      n.get_value()->accept(*this);
      return 0;
    }
  	DEFAULT_END_VISIT (DirAttr)

  	DEFAULT_VISIT (DirAttributeList) //@checked
  	
    void* begin_visit(const DirAttributeValue& n)
    { 
      if(n.get_quot_attr_content())
      {
        os << '"';
        n.get_quot_attr_content()->accept(*this);
        os << '"';
      } else {
        os << '\'';
        n.get_apos_attr_content()->accept(*this);
        os << '\'';
      }
      return 0;
    }
    DEFAULT_END_VISIT (DirAttributeValue)
  	
    DEFAULT_VISIT (DirElemContentList) //@checked
  	
    void* begin_visit(const DocumentTest& n)
    {
      os << "document(";
      return no_state;
    }
    void end_visit(const DocumentTest& n, void*)
    {
      os << ')';
    }
  	
    void* begin_visit(const ElementTest& n)
    {
      os << "element(";
      if(n.isWildcard())
      {
        os << '*';
      } else {
        os << n.getElementName()->get_qname();
      }
      if(n.getTypeName() != 0)
      {
        os << "," << n.getTypeName()->get_name()->get_qname();
      }
      os << ")";
      return no_state;
    }
    DEFAULT_END_VISIT (ElementTest)
  	
    void* begin_visit(const EmptyOrderDecl& n)
    {
      os << "declare default order empty ";
      switch(n.get_mode())
      {
        case StaticContextConsts::empty_greatest:
          os << "greatest";
          break;
        case StaticContextConsts::empty_least:
          os << "least";
          break;
      }
      return 0;
    }
    DEFAULT_END_VISIT (EmptyOrderDecl)

    void* begin_visit(const ForClause& n)
    {
      os << "for $";
      n.get_vardecl_list()->accept(*this);
      return 0;
    }
  	DEFAULT_END_VISIT (ForClause)

  	DEFAULT_VISIT (FLWORClauseList) //@checked
  	
    void* begin_visit(const ForwardAxis& n)
    {
      switch(n.get_axis())
      {
        case ParseConstants::axis_child:
          os << "child";
          break;
        case ParseConstants::axis_descendant:
          os << "descendant";
          break;
        case ParseConstants::axis_attribute:
          os << "attribute";
          break;
        case ParseConstants::axis_self:
          os << "self";
          break;
        case ParseConstants::axis_descendant_or_self:
          os << "descendant-or-self";
          break;
        case ParseConstants::axis_following_sibling:
          os << "following-sibling";
          break;
        case ParseConstants::axis_following:
          os << "following";
          break;
      }
      os << "::";
      return no_state;
    }
    DEFAULT_END_VISIT (ForwardAxis)
  	
    DEFAULT_VISIT (ForwardStep)//@checked

    void* begin_visit(const FunctionDecl& n)
    { 
      Parameters lParameters;
      os << "declare ";
      switch(n.get_type())
      {
        case ParseConstants::fn_update:
          os << "updating ";
          break;
        case ParseConstants::fn_extern_update:
          os << "updating ";
          break;
        case ParseConstants::fn_sequential:
          os << "sequential ";
          break;
        default:
          break;
      }
      os << "function ";
      n.get_name()->accept(*this);
      os << '(';
      if(n.get_paramlist())
      {
        n.get_paramlist()->accept(*this);
      }
      os << ')';
      if(n.get_return_type())
      {
        os << " as ";
        stringstream lReturnType;
        print_parsetree_xquery(lReturnType, n.get_return_type().getp());
        //n.get_return_type()->accept(*this);
        os << lReturnType.str();
        lParameters.push(lReturnType.str());
      }
      if(n.get_body())
      { 
        os << '{';
        n.get_body()->accept(*this);
        os << '}';
      } 
      
      if((n.get_type() == ParseConstants::fn_extern_update) ||
          (n.get_type() == ParseConstants::fn_extern)) {
        os << "external ";
      }

      if(n.get_paramlist())
      {
        const rchandle<ParamList> paramList = n.get_paramlist();
        for (vector<rchandle<Param> >::const_iterator it = paramList->begin();
              it != paramList->end(); ++it)
        {
          const Param* param = &**it;
          stringstream lParamString;
          print_parsetree_xquery(lParamString, param);
          lParameters.push(lParamString.str());
        }
      }
      theFunctionIndex.insert(make_pair(n.get_name()->get_qname(), lParameters));
      return 0;
    }
  	DEFAULT_END_VISIT (FunctionDecl)
  	
    void* begin_visit(const GeneralComp& n)
    {
      switch(n.get_type())
      {
        case ParseConstants::op_eq:
          os << '=';
          break;
        case ParseConstants::op_ne:
          os << "!=";
          break;
        case ParseConstants::op_lt:
          os << '<';
          break;
        case ParseConstants::op_le:
          os << "<=";
          break;
        case ParseConstants::op_gt:
          os << '>';
          break;<commit_msg>[2892749] External function signatures not printed properly - corrected<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 * 
 * 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.
 */
#include "compiler/parsetree/parsenode_print_xquery_visitor.h"

#include <ostream>
#include <string>
#include <map>
#include <stack>

#include "compiler/parsetree/parsenode_visitor.h"

#include "store/api/update_consts.h"

#define DEFAULT_BEGIN_VISIT(type)                    \
  void* begin_visit(const type&){ return no_state; } 

#define DEFAULT_END_VISIT(type)                      \
  void end_visit(const type&, void*){}              

#define DEFAULT_VISIT(type)                          \
  DEFAULT_BEGIN_VISIT(type)                          \
  DEFAULT_END_VISIT(type)                            \

using namespace std;

namespace zorba {

class ParseNodePrintXQueryVisitor: public parsenode_visitor
{
  private:
    std::ostream& os;
    FunctionIndex theFunctionIndex;

  public:
    ParseNodePrintXQueryVisitor(std::ostream& aStream): os(aStream){}

    FunctionIndex& getFunctionIndex()
    {
      return theFunctionIndex;
    }

    ParseNodePrintXQueryVisitor* print(const parsenode* p)
    {
      p->accept(*this);
      return this;
    }
    
    void* begin_visit(const AbbrevForwardStep& n)
    {
      if(n.get_attr_bit())
      {
        os << '@';
      }
      return no_state;
    }
    DEFAULT_END_VISIT(AbbrevForwardStep)

  	DEFAULT_BEGIN_VISIT (AnyKindTest)
  	void end_visit(const AnyKindTest& n, void* state)
    {
      os << "node()";
    }
    
    DEFAULT_VISIT (AposAttrContentList)
  	
    void* begin_visit(const AposAttrValueContent& n)
    {
      if(!n.get_apos_atcontent().empty())
      {
        os << n.get_apos_atcontent();
      } else {
        n.get_common_content()->accept(*this);
      }
      return 0;
    }
    DEFAULT_END_VISIT (AposAttrValueContent)
  	
    void* begin_visit(const ArgList& n)
    {
      for (int i=0; i<n.size(); ++i) {
        if(i > 0)
        {
          os << ", ";
        }
        const exprnode* e_p = &*(n[i]);
        e_p->accept(*this);
      } 
      return 0;
    }
    DEFAULT_END_VISIT (ArgList)
  	
    DEFAULT_BEGIN_VISIT (AtomicType)
  	void end_visit(const AtomicType& n, void* state)
    {
      os << n.get_qname()->get_qname();
    }

    void* begin_visit(const AttributeTest& n)
    {
      os << "attribute(";
      return no_state;
    }

    void end_visit(const AttributeTest& n, void*)
    {
      os << ")";
    }

    void* begin_visit(const BaseURIDecl& n)
    {
      os << "declare base-uri \"" << n.get_base_uri() << "\";";
      return no_state;
    }
  	DEFAULT_END_VISIT (BaseURIDecl);
  	
    void* begin_visit(const BoundarySpaceDecl& n)
    {
      os << "declare boundary-space ";
      switch(n.get_boundary_space_mode())
      {
        case StaticContextConsts::preserve_space:
          os << "preserve";
          break;
        case StaticContextConsts::strip_space:
          os << "strip";
          break;
      }
      os << ';';
      return no_state;
    }
    DEFAULT_END_VISIT (BoundarySpaceDecl)
  	
    void* begin_visit(const CaseClause& n)
    {
      os << "case ";
      if(!n.get_varname().empty())
      {
        os << "$" << n.get_varname() << " as ";
      }
      n.get_type()->accept(*this);
      os << "return ";
      n.get_expr()->accept(*this);
      return no_state;
    }
    DEFAULT_END_VISIT (CaseClause)

  	DEFAULT_VISIT (CaseClauseList) //@checked

    void* begin_visit(const CommentTest& n)
    {
      os << "comment()";
      return no_state;
    }
  	DEFAULT_END_VISIT (CommentTest);
  	
    void* begin_visit(const ConstructionDecl& n)
    {
      os << "declare construction ";
      switch(n.get_mode())
      {
        case StaticContextConsts::cons_preserve:
          os << "preserve";
          break;
        case StaticContextConsts::cons_strip:
          os << "strip";
          break;
      }
      os << ';';
      return no_state;
    }
    DEFAULT_END_VISIT (ConstructionDecl)

    void* begin_visit(const CopyNamespacesDecl& n)
    {
      os << "declare copy-namespaces ";
      switch(n.get_preserve_mode())
      {
        case StaticContextConsts::preserve_ns:
          os << "preserve,";
          break;
        case StaticContextConsts::no_preserve_ns:
          os << "no-preserve,";
          break;
      }
      switch(n.get_inherit_mode())
      {
        case StaticContextConsts::inherit_ns:
          os << "inherit";
          break;
        case StaticContextConsts::no_inherit_ns:
          os << "no-inherit";
          break;
      }
      return 0;
    }
  	DEFAULT_END_VISIT (CopyNamespacesDecl)
  	
    void* begin_visit(const DefaultCollationDecl& n)
    {
      os << "declare default collation " << n.get_collation();
      return 0;
    }
    DEFAULT_END_VISIT (DefaultCollationDecl)

    void* begin_visit(const DefaultNamespaceDecl& n)
    {
      os << "declare default ";
      switch(n.get_mode())
      {
        case ParseConstants::ns_element_default:
          os << "element ";
          break;
        case ParseConstants::ns_function_default:
          os << "function ";
          break;
      }
      os << "namespace ";
      os << n.get_default_namespace();
      return 0;
    }
  	DEFAULT_END_VISIT (DefaultNamespaceDecl)

    void* begin_visit(const DirAttr& n)
    {
      n.get_name()->accept(*this);
      os << '=';
      n.get_value()->accept(*this);
      return 0;
    }
  	DEFAULT_END_VISIT (DirAttr)

  	DEFAULT_VISIT (DirAttributeList) //@checked
  	
    void* begin_visit(const DirAttributeValue& n)
    { 
      if(n.get_quot_attr_content())
      {
        os << '"';
        n.get_quot_attr_content()->accept(*this);
        os << '"';
      } else {
        os << '\'';
        n.get_apos_attr_content()->accept(*this);
        os << '\'';
      }
      return 0;
    }
    DEFAULT_END_VISIT (DirAttributeValue)
  	
    DEFAULT_VISIT (DirElemContentList) //@checked
  	
    void* begin_visit(const DocumentTest& n)
    {
      os << "document(";
      return no_state;
    }
    void end_visit(const DocumentTest& n, void*)
    {
      os << ')';
    }
  	
    void* begin_visit(const ElementTest& n)
    {
      os << "element(";
      if(n.isWildcard())
      {
        os << '*';
      } else {
        os << n.getElementName()->get_qname();
      }
      if(n.getTypeName() != 0)
      {
        os << "," << n.getTypeName()->get_name()->get_qname();
      }
      os << ")";
      return no_state;
    }
    DEFAULT_END_VISIT (ElementTest)
  	
    void* begin_visit(const EmptyOrderDecl& n)
    {
      os << "declare default order empty ";
      switch(n.get_mode())
      {
        case StaticContextConsts::empty_greatest:
          os << "greatest";
          break;
        case StaticContextConsts::empty_least:
          os << "least";
          break;
      }
      return 0;
    }
    DEFAULT_END_VISIT (EmptyOrderDecl)

    void* begin_visit(const ForClause& n)
    {
      os << "for $";
      n.get_vardecl_list()->accept(*this);
      return 0;
    }
  	DEFAULT_END_VISIT (ForClause)

  	DEFAULT_VISIT (FLWORClauseList) //@checked
  	
    void* begin_visit(const ForwardAxis& n)
    {
      switch(n.get_axis())
      {
        case ParseConstants::axis_child:
          os << "child";
          break;
        case ParseConstants::axis_descendant:
          os << "descendant";
          break;
        case ParseConstants::axis_attribute:
          os << "attribute";
          break;
        case ParseConstants::axis_self:
          os << "self";
          break;
        case ParseConstants::axis_descendant_or_self:
          os << "descendant-or-self";
          break;
        case ParseConstants::axis_following_sibling:
          os << "following-sibling";
          break;
        case ParseConstants::axis_following:
          os << "following";
          break;
      }
      os << "::";
      return no_state;
    }
    DEFAULT_END_VISIT (ForwardAxis)
  	
    DEFAULT_VISIT (ForwardStep)//@checked

    void* begin_visit(const FunctionDecl& n)
    { 
      Parameters lParameters;
      os << "declare ";
      switch(n.get_type()) {
        case ParseConstants::fn_update:
        case ParseConstants::fn_extern_update:
          os << "updating ";
          break;
        case ParseConstants::fn_sequential:
        case ParseConstants::fn_extern_sequential:
          os << "sequential ";
          break;
        default:
          break;
      }
      os << "function ";
      n.get_name()->accept(*this);
      os << '(';
      if(n.get_paramlist()) {
        n.get_paramlist()->accept(*this);
      }
      os << ')';
      if(n.get_return_type()) {
        os << " as ";
        stringstream lReturnType;
        print_parsetree_xquery(lReturnType, n.get_return_type().getp());
        //n.get_return_type()->accept(*this);
        os << lReturnType.str();
        lParameters.push(lReturnType.str());
      }
      if(n.get_body()) { 
        os << '{';
        n.get_body()->accept(*this);
        os << '}';
      } else if(n.get_type() == ParseConstants::fn_extern ||
          n.get_type() == ParseConstants::fn_extern_update ||
          n.get_type() == ParseConstants::fn_extern_sequential) {
        os << " external";
      }

      if(n.get_paramlist()) {
        const rchandle<ParamList> paramList = n.get_paramlist();
        for (vector<rchandle<Param> >::const_iterator it = paramList->begin();
             it != paramList->end(); ++it) {
          const Param* param = &**it;
          stringstream lParamString;
          print_parsetree_xquery(lParamString, param);
          lParameters.push(lParamString.str());
        }
      }
      theFunctionIndex.insert(make_pair(n.get_name()->get_qname(), lParameters));
      return 0;
    }
  	DEFAULT_END_VISIT (FunctionDecl)
  	
    void* begin_visit(const GeneralComp& n)
    {
      switch(n.get_type())
      {
        case ParseConstants::op_eq:
          os << '=';
          break;
        case ParseConstants::op_ne:
          os << "!=";
          break;
        case ParseConstants::op_lt:
          os << '<';
          break;
        case ParseConstants::op_le:
          os << "<=";
          break;
        case ParseConstants::op_gt:
          os << '>';
          break;
{'fraction_non_alphanumeric': 0.1262801798044395, 'fraction_numerical': 0.0026414569720561655, 'mean_word_length': 2.034308211473566, 'pattern_counts': {'":': 2, '<': 333, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5393111', 'n_tokens_mistral': 6963, 'n_tokens_neox': 6775, 'n_words': 1294}