Package com.sk89q.worldedit.internal.expression.lexer.tokens

Examples of com.sk89q.worldedit.internal.expression.lexer.tokens.Token


    }

    private RValue parse() throws ParserException {
        final RValue ret = parseStatements(false);
        if (position < tokens.size()) {
            final Token token = peek();
            throw new ParserException(token.getPosition(), "Extra token at the end of the input: " + token);
        }

        ret.bindVariables(expression, false);

        return ret;
View Full Code Here


    private RValue parseStatements(boolean singleStatement) throws ParserException {
        List<RValue> statements = new ArrayList<RValue>();
        loop: while (position < tokens.size()) {
            boolean expectSemicolon = false;

            final Token current = peek();
            switch (current.id()) {
            case '{':
                consumeCharacter('{');

                statements.add(parseStatements(false));

                consumeCharacter('}');

                break;

            case '}':
                break loop;

            case 'k':
                final String keyword = ((KeywordToken) current).value;
                switch (keyword.charAt(0)) {
                case 'i': { // if
                    ++position;
                    final RValue condition = parseBracket();
                    final RValue truePart = parseStatements(true);
                    final RValue falsePart;

                    if (hasKeyword("else")) {
                        ++position;
                        falsePart = parseStatements(true);
                    } else {
                        falsePart = null;
                    }

                    statements.add(new Conditional(current.getPosition(), condition, truePart, falsePart));
                    break;
                }

                case 'w': { // while
                    ++position;
                    final RValue condition = parseBracket();
                    final RValue body = parseStatements(true);

                    statements.add(new While(current.getPosition(), condition, body, false));
                    break;
                }

                case 'd': { // do/default
                    if (hasKeyword("default")) {
                        break loop;
                    }

                    ++position;
                    final RValue body = parseStatements(true);

                    consumeKeyword("while");

                    final RValue condition = parseBracket();

                    statements.add(new While(current.getPosition(), condition, body, true));

                    expectSemicolon = true;
                    break;
                }

                case 'f': { // for
                    ++position;
                    consumeCharacter('(');
                    int oldPosition = position;
                    final RValue init = parseExpression(true);
                    //if ((init instanceof LValue) && )
                    if (peek().id() == ';') {
                        ++position;
                        final RValue condition = parseExpression(true);
                        consumeCharacter(';');
                        final RValue increment = parseExpression(true);
                        consumeCharacter(')');
                        final RValue body = parseStatements(true);

                        statements.add(new For(current.getPosition(), init, condition, increment, body));
                    } else {
                        position = oldPosition;

                        final Token variableToken = peek();
                        if (!(variableToken instanceof IdentifierToken)) {
                            throw new ParserException(variableToken.getPosition(), "Expected identifier");
                        }

                        RValue variable = expression.getVariable(((IdentifierToken) variableToken).value, true);
                        if (!(variable instanceof LValue)) {
                            throw new ParserException(variableToken.getPosition(), "Expected variable");
                        }
                        ++position;

                        final Token equalsToken = peek();
                        if (!(equalsToken instanceof OperatorToken) || !((OperatorToken) equalsToken).operator.equals("=")) {
                            throw new ParserException(variableToken.getPosition(), "Expected '=' or a term and ';'");
                        }
                        ++position;

                        final RValue first = parseExpression(true);
                        consumeCharacter(',');
                        final RValue last = parseExpression(true);
                        consumeCharacter(')');
                        final RValue body = parseStatements(true);

                        statements.add(new SimpleFor(current.getPosition(), (LValue) variable, first, last, body));
                    } // switch (keyword.charAt(0))
                    break;
                }

                case 'b': // break
                    ++position;
                    statements.add(new Break(current.getPosition(), false));
                    break;

                case 'c': // continue/case
                    if (hasKeyword("case")) {
                        break loop;
                    }

                    ++position;
                    statements.add(new Break(current.getPosition(), true));
                    break;

                case 'r': // return
                    ++position;
                    statements.add(new Return(current.getPosition(), parseExpression(true)));

                    expectSemicolon = true;
                    break;

                case 's': // switch
                    ++position;
                    final RValue parameter = parseBracket();
                    final List<Double> values = new ArrayList<Double>();
                    final List<RValue> caseStatements = new ArrayList<RValue>();
                    RValue defaultCase = null;

                    consumeCharacter('{');
                    while (peek().id() != '}') {
                        if (position >= tokens.size()) {
                            throw new ParserException(current.getPosition(), "Expected '}' instead of EOF");
                        }
                        if (defaultCase != null) {
                            throw new ParserException(current.getPosition(), "Expected '}' instead of " + peek());
                        }

                        if (hasKeyword("case")) {
                            ++position;

                            final Token valueToken = peek();
                            if (!(valueToken instanceof NumberToken)) {
                                throw new ParserException(current.getPosition(), "Expected number instead of " + peek());
                            }

                            ++position;
View Full Code Here

        LinkedList<Identifiable> halfProcessed = new LinkedList<Identifiable>();

        // process brackets, numbers, functions, variables and detect prefix operators
        boolean expressionStart = true;
        loop: while (position < tokens.size()) {
            final Token current = peek();

            switch (current.id()) {
            case '0':
                halfProcessed.add(new Constant(current.getPosition(), ((NumberToken) current).value));
                ++position;
                expressionStart = false;
                break;

            case 'i':
                final IdentifierToken identifierToken = (IdentifierToken) current;
                ++position;

                final Token next = peek();
                if (next.id() == '(') {
                    halfProcessed.add(parseFunctionCall(identifierToken));
                } else {
                    final RValue variable = expression.getVariable(identifierToken.value, false);
                    if (variable == null) {
                        halfProcessed.add(new UnboundVariable(identifierToken.getPosition(), identifierToken.value));
View Full Code Here

            List<RValue> args = new ArrayList<RValue>();

            loop: while (true) {
                args.add(parseExpression(false));

                final Token current = peek();
                ++position;

                switch (current.id()) {
                case ',':
                    continue;

                case ')':
                    break loop;

                default:
                    throw new ParserException(current.getPosition(), "Unmatched opening bracket");
                }
            }

            return Functions.getFunction(identifierToken.getPosition(), identifierToken.value, args.toArray(new RValue[args.size()]));
        } catch (NoSuchMethodException e) {
View Full Code Here

        return ret;
    }

    private boolean hasKeyword(String keyword) {
        final Token next = peek();

        return next instanceof KeywordToken && ((KeywordToken) next).value.equals(keyword);
    }
View Full Code Here

        return next instanceof KeywordToken && ((KeywordToken) next).value.equals(keyword);
    }

    private void assertCharacter(char character) throws ParserException {
        final Token next = peek();
        if (next.id() != character) {
            throw new ParserException(next.getPosition(), "Expected '" + character + "'");
        }
    }
View Full Code Here

        RValue lhsInvokable = processBinaryOpsLA(lhs, level);

        try {
            return Operators.getOperator(input.get(0).getPosition(), operator, lhsInvokable, rhsInvokable);
        } catch (NoSuchMethodException e) {
            final Token operatorToken = (Token) input.get(lhs.size());
            throw new ParserException(operatorToken.getPosition(), "Couldn't find operator '" + operator + "'");
        }
    }
View Full Code Here

        RValue rhsInvokable = processBinaryOpsRA(rhs, level);

        try {
            return Operators.getOperator(input.get(0).getPosition(), operator, lhsInvokable, rhsInvokable);
        } catch (NoSuchMethodException e) {
            final Token operatorToken = (Token) input.get(lhs.size());
            throw new ParserException(operatorToken.getPosition(), "Couldn't find operator '" + operator + "'");
        }
    }
View Full Code Here

            skipWhitespace();
            if (position >= expression.length()) {
                break;
            }

            Token token = operatorTree.evaluate(position);
            if (token != null) {
                tokens.add(token);
                continue;
            }
View Full Code Here

                final char next = peek();

                final DecisionTree subTree = subTrees.get(next);
                if (subTree != null) {
                    ++position;
                    final Token subTreeResult = subTree.evaluate(startPosition);
                    if (subTreeResult != null) {
                        return subTreeResult;
                    }
                    --position;
                }
View Full Code Here

TOP

Related Classes of com.sk89q.worldedit.internal.expression.lexer.tokens.Token

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.