<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-06 <NAME>
 *  <EMAIL>
 *  http://exist.sourceforge.net
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xquery;

import org.exist.dom.DocumentSet;
import org.exist.dom.NodeProxy;
import org.exist.dom.NodeSet;
import org.exist.dom.QName;
import org.exist.xquery.util.ExpressionDumper;
import org.exist.xquery.value.GroupedValueSequence;
import org.exist.xquery.value.GroupedValueSequenceList;
import org.exist.xquery.value.IntegerValue;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.OrderedValueSequence;
import org.exist.xquery.value.PreorderedValueSequence;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceIterator;
import org.exist.xquery.value.Type;
import org.exist.xquery.value.ValueSequence;

 
// import sun.security.action.GetLongAction;

/**
 * Represents an XQuery "for" expression.
 * 
 * @author <NAME> <<EMAIL>>
 */
public class ForExpr extends BindingExpression {

	private String positionalVariable = null;

    public ForExpr(XQueryContext context) {
		super(context);
	}

	/**
	 * A "for" expression may have an optional positional variable whose
	 * QName can be set via this method.
	 * 
	 * @param var
	 */
	public void setPositionalVariable(String var) {
		positionalVariable = var;
	}	
 
    public void analyze(AnalyzeContextInfo contextInfo, OrderSpec orderBy[]) throws XPathException { 
        analyze(contextInfo, orderBy, null); 
    } 

	/* (non-Javadoc)
     * @see org.exist.xquery.Expression#analyze(org.exist.xquery.Expression)
     */
    public void analyze(AnalyzeContextInfo contextInfo, OrderSpec orderBy[], GroupSpec groupBy[]) throws XPathException { 
        // bv : Declare the grouping variable 
        if(groupVarName != null){ 
            LocalVariable groupVar = new LocalVariable(QName.parse(context, groupVarName, null)); 
            groupVar.setSequenceType(sequenceType); 
            context.declareVariableBinding(groupVar); 
        } 
         
        // bv : Declare grouping key variable(s) 
        if(groupBy!= null){ 
            for(int i=0;i<groupBy.length;i++){ 
                LocalVariable groupKeyVar = new LocalVariable(QName.parse(context, groupBy[i].getKeyVarName(),null)); 
                groupKeyVar.setSequenceType(sequenceType); 
                context.declareVariableBinding(groupKeyVar); 
            } 
        }        
         
        // Save the local variable stack
		LocalVariable mark = context.markLocalVariables(false);
		
		contextInfo.setParent(this);
		inputSequence.analyze(contextInfo);
		
		// Declare the iteration variable
        LocalVariable inVar = new LocalVariable(QName.parse(context, varName, null));
        inVar.setSequenceType(sequenceType);
		context.declareVariableBinding(inVar);
		
		// Declare positional variable
		if(positionalVariable != null) {
			LocalVariable posVar = new LocalVariable(QName.parse(context, positionalVariable, null));
            posVar.setSequenceType(POSITIONAL_VAR_TYPE);
            context.declareVariableBinding(posVar);
        }
        
		if(whereExpr != null) {
			AnalyzeContextInfo newContextInfo = new AnalyzeContextInfo(contextInfo);
			newContextInfo.setFlags(contextInfo.getFlags() | IN_PREDICATE | IN_WHERE_CLAUSE);
			newContextInfo.setContextId(getExpressionId());
		    whereExpr.analyze(newContextInfo);
		}<commit_msg>Prevent reusing the name of the bound variable as the positional variable's name.<commit_after>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-06 <NAME>
 *  <EMAIL>
 *  http://exist.sourceforge.net
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xquery;

import org.exist.dom.DocumentSet;
import org.exist.dom.NodeProxy;
import org.exist.dom.NodeSet;
import org.exist.dom.QName;
import org.exist.xquery.util.ExpressionDumper;
import org.exist.xquery.value.GroupedValueSequence;
import org.exist.xquery.value.GroupedValueSequenceList;
import org.exist.xquery.value.IntegerValue;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.OrderedValueSequence;
import org.exist.xquery.value.PreorderedValueSequence;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceIterator;
import org.exist.xquery.value.Type;
import org.exist.xquery.value.ValueSequence;

/**
 * Represents an XQuery "for" expression.
 * 
 * @author <NAME> <<EMAIL>>
 */
public class ForExpr extends BindingExpression {

	private String positionalVariable = null;

    public ForExpr(XQueryContext context) {
		super(context);
	}

	/**
	 * A "for" expression may have an optional positional variable whose
	 * QName can be set via this method.
	 * 
	 * @param var
	 */
	public void setPositionalVariable(String var) {
		positionalVariable = var;
	}	
 
    public void analyze(AnalyzeContextInfo contextInfo, OrderSpec orderBy[]) throws XPathException { 
        analyze(contextInfo, orderBy, null); 
    } 

	/* (non-Javadoc)
     * @see org.exist.xquery.Expression#analyze(org.exist.xquery.Expression)
     */
    public void analyze(AnalyzeContextInfo contextInfo, OrderSpec orderBy[], GroupSpec groupBy[]) throws XPathException { 
        // bv : Declare the grouping variable 
        if(groupVarName != null){ 
            LocalVariable groupVar = new LocalVariable(QName.parse(context, groupVarName, null)); 
            groupVar.setSequenceType(sequenceType); 
            context.declareVariableBinding(groupVar); 
        } 
         
        // bv : Declare grouping key variable(s) 
        if(groupBy!= null){ 
            for(int i=0;i<groupBy.length;i++){ 
                LocalVariable groupKeyVar = new LocalVariable(QName.parse(context, groupBy[i].getKeyVarName(),null)); 
                groupKeyVar.setSequenceType(sequenceType); 
                context.declareVariableBinding(groupKeyVar); 
            } 
        }        
         
        // Save the local variable stack
		LocalVariable mark = context.markLocalVariables(false);
		
		contextInfo.setParent(this);
		inputSequence.analyze(contextInfo);
		
		// Declare the iteration variable
        LocalVariable inVar = new LocalVariable(QName.parse(context, varName, null));
        inVar.setSequenceType(sequenceType);
		context.declareVariableBinding(inVar);
		
		// Declare positional variable
		if(positionalVariable != null) {
			//could probably be detected by the parser
			if (varName.equals(positionalVariable))
				throw new XPathException("XQST0089: bound variable and positional variable have the same name");
			LocalVariable posVar = new LocalVariable(QName.parse(context, positionalVariable, null));
            posVar.setSequenceType(POSITIONAL_VAR_TYPE);
            context.declareVariableBinding(posVar);
        }
        
		if(whereExpr != null) {
			AnalyzeContextInfo newContextInfo = new AnalyzeContextInfo(contextInfo);
			newContextInfo.setFlags(contextInfo.getFlags() | IN_PREDICATE | IN_WHERE_CLAUSE);
			newContextInfo.setContextId(getExpressionId());
		    whereExpr.analyze(newContextInfo);
		}
{'fraction_non_alphanumeric': 0.09177662582469369, 'fraction_numerical': 0.004241281809613572, 'mean_word_length': 3.474960463890353, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5505296', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2343, 'n_words': 649}