<commit_before>/**
 * MVEL (The MVFLEX Expression Language)
 *
 * Copyright (C) 2007 <NAME>, MVFLEX/Valhalla Project and the Codehaus
 *
 * 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 org.mvel;

import static org.mvel.NodeType.*;
import static org.mvel.util.ParseTools.*;
import org.mvel.util.StringAppender;

import static java.lang.Boolean.getBoolean;
import static java.lang.Character.isWhitespace;
import static java.lang.String.copyValueOf;
import static java.lang.System.arraycopy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TemplateCompiler {
    private static final boolean DEFAULT_DEBUG = getBoolean("mvflex.expression.debug");

    private char[] expressionArray;
    private int length = 0;
    private int cursor = 0;
    private boolean debug = DEFAULT_DEBUG;
    private int maxDepth = 10;

    public TemplateCompiler(TemplateInterpreter templateInterpreter) {
        this.expressionArray = templateInterpreter.getExpression();
        this.length = expressionArray.length;
    }

    public Node[] compileExpression() {
        Node[] expressions;
        List<Node> aList = new ArrayList<Node>(10);

        int depth = 0, literalRange = 0, length = expressionArray.length;

        char[] exStr;

        String token;

        Node ex;
        int node = 0;
        for (; cursor < length; cursor++) {
            if ((expressionArray[cursor] == '$' || expressionArray[cursor] == '@')) {
                if (literalRange != 0) {
                    aList.add(new Node(node++, LITERAL, cursor - literalRange, literalRange, node));
                    literalRange = 0;
                }

                ex = new Node(cursor);

                if ((token = captureTo('{')) == null) {
                    literalRange++;
                    continue;
                }

                exStr = structuredCaptureArray(1);

                if (token.length() != 0) {
                    if ("if".equals(token)) {
                        depth++;
                        ex.setToken(IF);
                    }
                    else if ("elseif".equals(token)) {
                        aList.add(new Node(node++, GOTO, -1));
                        ex.setToken(ELSEIF);
                    }
                    else if ("else".equals(token)) {
                        aList.add(new Node(node++, GOTO, -1));
                        ex.setToken(ELSE);
                    }
                    else if ("foreach".equals(token)) {
                        depth++;
                        ex.setToken(FOREACH);
                    }
                    else if ("end".equals(token)) {
                        depth--;
                        ex.setToken(END);
                        ex.setRegister(exStr);
                    }
                    else if ("includeByRef".equals(token)) {
                        ex.setToken(INCLUDE_BY_REF);
                        ex.setRegister(buildIncludeRef(exStr));
                    }
                    else {
                        throw new CompileException("unknown token: " + token);
                    }
                }

                if (ex.getNodeType() == FOREACH) {
                    ex.setAlias("item");
                    ex.setName(new String(exStr));

                    int capture = -1;
                    for (int i = 0; i < exStr.length; i++) {
                        switch (exStr[i]) {
                            case' ':
                                if (capture == -1 && ((i + 3) < exStr.length)
                                        && exStr[i + 1] == 'a' && exStr[i + 2] == 's'
                                        && exStr[i + 3] == ' ') {

                                    ex.setName(new String(exStr, 0, i));

                                    capture = i += 4;

                                    /**
                                     * Scan to skip any excess whitespace.
                                     */
                                    //noinspection StatementWithEmptyBody
                                    while (i < exStr.length && !isWhitespace(exStr[i++])) ;

                                    /**
                                     * Scan a second time to capture the token.
                                     */
                                    //noinspection StatementWithEmptyBody
                                    while (i < exStr.length && !isWhitespace(exStr[i++])) ;

                                    ex.setAlias(copyValueOf(exStr, capture, i - capture));

                                    /**
                                     * Scan to skip any excess whitespace.
                                     */
                                    //noinspection StatementWithEmptyBody
                                    while (i < exStr.length && !isWhitespace(exStr[i++])) ;
                                }
                                break;
                        }
                    }
                }
                else
                    ex.setEndPos(cursor);

                ex.setLength((cursor + 1) - ex.getStartPos());

                if (depth > maxDepth) maxDepth = depth;

                aList.add(ex.setNode(node++));
            }
            else {
                literalRange++;
            }
        }
        if (literalRange != 0) {
            aList.add(new Node(node++, LITERAL, cursor - literalRange, literalRange, node));
        }<commit_msg>-Fixed code which sets the aliases for foreach statements<commit_after>/**
 * MVEL (The MVFLEX Expression Language)
 *
 * Copyright (C) 2007 <NAME>, MVFLEX/Valhalla Project and the Codehaus
 *
 * 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 org.mvel;

import static org.mvel.NodeType.*;
import static org.mvel.util.ParseTools.*;
import org.mvel.util.StringAppender;

import static java.lang.Boolean.getBoolean;
import static java.lang.Character.isWhitespace;
import static java.lang.String.copyValueOf;
import static java.lang.System.arraycopy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TemplateCompiler {
    private static final boolean DEFAULT_DEBUG = getBoolean("mvflex.expression.debug");

    private char[] expressionArray;
    private int length = 0;
    private int cursor = 0;
    private boolean debug = DEFAULT_DEBUG;
    private int maxDepth = 10;

    public TemplateCompiler(TemplateInterpreter templateInterpreter) {
        this.expressionArray = templateInterpreter.getExpression();
        this.length = expressionArray.length;
    }

    public Node[] compileExpression() {
        Node[] expressions;
        List<Node> aList = new ArrayList<Node>(10);

        int depth = 0, literalRange = 0, length = expressionArray.length;

        char[] exStr;

        String token;

        Node ex;
        int node = 0;
        for (; cursor < length; cursor++) {
            if ((expressionArray[cursor] == '$' || expressionArray[cursor] == '@')) {
                if (literalRange != 0) {
                    aList.add(new Node(node++, LITERAL, cursor - literalRange, literalRange, node));
                    literalRange = 0;
                }

                ex = new Node(cursor);

                if ((token = captureTo('{')) == null) {
                    literalRange++;
                    continue;
                }

                exStr = structuredCaptureArray(1);

                if (token.length() != 0) {
                    if ("if".equals(token)) {
                        depth++;
                        ex.setToken(IF);
                    }
                    else if ("elseif".equals(token)) {
                        aList.add(new Node(node++, GOTO, -1));
                        ex.setToken(ELSEIF);
                    }
                    else if ("else".equals(token)) {
                        aList.add(new Node(node++, GOTO, -1));
                        ex.setToken(ELSE);
                    }
                    else if ("foreach".equals(token)) {
                        depth++;
                        ex.setToken(FOREACH);
                    }
                    else if ("end".equals(token)) {
                        depth--;
                        ex.setToken(END);
                        ex.setRegister(exStr);
                    }
                    else if ("includeByRef".equals(token)) {
                        ex.setToken(INCLUDE_BY_REF);
                        ex.setRegister(buildIncludeRef(exStr));
                    }
                    else {
                        throw new CompileException("unknown token: " + token);
                    }
                }

                if (ex.getNodeType() == FOREACH) {
                    ex.setAlias("item");
                    ex.setName(new String(exStr));

                    int capture = -1;
                    for (int i = 0; i < exStr.length; i++) {
                        switch (exStr[i]) {
                            case' ':
                                if (capture == -1 && ((i + 3) < exStr.length)
                                        && exStr[i + 1] == 'a' && exStr[i + 2] == 's'
                                        && exStr[i + 3] == ' ') {

                                    ex.setName(new String(exStr, 0, i));

                                    capture = i += 4;

                                    ex.setAlias(copyValueOf(exStr, capture, exStr.length - capture));

                                    /**
                                     * Scan to skip any excess whitespace.
                                     */
                                    //noinspection StatementWithEmptyBody
                                    while (i < exStr.length && !isWhitespace(exStr[i++])) ;
                                }
                                break;
                        }
                    }
                }
                else
                    ex.setEndPos(cursor);

                ex.setLength((cursor + 1) - ex.getStartPos());

                if (depth > maxDepth) maxDepth = depth;

                aList.add(ex.setNode(node++));
            }
            else {
                literalRange++;
            }
        }
        if (literalRange != 0) {
            aList.add(new Node(node++, LITERAL, cursor - literalRange, literalRange, node));
        }
{'fraction_non_alphanumeric': 0.10250131187685849, 'fraction_numerical': 0.00594717509183138, 'mean_word_length': 1.189354776948114, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '518688', 'n_tokens_mistral': 2899, 'n_tokens_neox': 2646, 'n_words': 706}