Search in sources :

Example 1 with UnsetCastExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken in project jphp by jphp-compiler.

the class SimpleExprGenerator method getToken.

@SuppressWarnings("unchecked")
public ExprStmtToken getToken(Token current, ListIterator<Token> iterator, Separator separator, BraceExprToken.Kind closedBraceKind, Callback<Boolean, Token> breakCallback) {
    isRef = false;
    List<Token> tokens = new ArrayList<>();
    Token previous = null;
    Token next = iterator.hasNext() ? iterator.next() : null;
    if (next != null)
        iterator.previous();
    int braceOpened = 0;
    boolean needBreak = false;
    do {
        if (breakCallback != null && current != null && breakCallback.call(current)) {
            break;
        }
        if (isOpenedBrace(current, SIMPLE)) {
            boolean isFunc = false;
            if (previous instanceof NameToken && "array".equalsIgnoreCase(previous.getMeta().getWord())) {
                iterator.previous();
                tokens.set(tokens.size() - 1, current = processNewArray(previous, iterator));
            } else {
                if (previous instanceof NameToken || previous instanceof VariableExprToken || previous instanceof ClosureStmtToken || previous instanceof ArrayGetExprToken || previous instanceof CallExprToken)
                    isFunc = true;
                else if (previous instanceof StaticAccessExprToken) {
                    // !((StaticAccessExprToken)previous).isGetStaticField(); TODO check it!
                    isFunc = true;
                } else if (previous instanceof DynamicAccessExprToken) {
                    isFunc = true;
                } else if (isClosedBrace(previous, SIMPLE)) {
                    isFunc = true;
                }
                if (isFunc) {
                    CallExprToken call = processCall(previous, current, iterator);
                    if (call.getName() != null) {
                        current = call;
                        tokens.set(tokens.size() - 1, call);
                    } else {
                        tokens.add(current = new CallOperatorToken(call));
                    }
                } else {
                    if (needBreak)
                        unexpectedToken(current);
                    braceOpened += 1;
                    tokens.add(current);
                }
            }
        } else if (braceOpened > 0 && isClosedBrace(current, SIMPLE)) {
            braceOpened -= 1;
            tokens.add(current);
            if (isOpenedBrace(previous, SIMPLE))
                unexpectedToken(current);
        } else if (isOpenedBrace(current, ARRAY) || isOpenedBrace(current, BLOCK)) {
            if (isTokenClass(previous, NameToken.class, VariableExprToken.class, GetVarExprToken.class, CallExprToken.class, ArrayGetExprToken.class, DynamicAccessExprToken.class, StringExprToken.class, StringBuilderExprToken.class, CallOperatorToken.class, ArrayPushExprToken.class, YieldExprToken.class) || (previous instanceof StaticAccessExprToken) || isClosedBrace(previous, SIMPLE)) {
                // array
                current = processArrayToken(previous, current, iterator);
                if (previous instanceof DynamicAccessExprToken && current instanceof ArrayGetRefExprToken) {
                    tokens.set(tokens.size() - 1, new DynamicAccessGetRefExprToken((DynamicAccessExprToken) previous));
                }
                tokens.add(current);
            } else if (previous instanceof OperatorExprToken || previous == null || isOpenedBrace(previous, SIMPLE) || isOpenedBrace(previous, BLOCK)) {
                int currIndex = iterator.nextIndex() - 1;
                ArrayExprToken arrToken = (ArrayExprToken) processNewArray(current, iterator);
                boolean nextAssign = nextTokenAndPrev(iterator) instanceof AssignExprToken;
                if (nextAssign) {
                    int doneIndex = iterator.nextIndex();
                    int prevCount = doneIndex - currIndex;
                    for (int i = 0; i < prevCount; i++) {
                        current = iterator.previous();
                    }
                    iterator.next();
                    tokens.add(processList(current, iterator, null, closedBraceKind, braceOpened));
                } else {
                    if (arrToken.isListSyntax()) {
                        unexpectedToken(nextToken(iterator), "=");
                    }
                    tokens.add(current = arrToken);
                }
            } else {
                unexpectedToken(current);
            }
        } else if (braceOpened == 0 && isClosedBrace(current, ARRAY)) {
            if (separator == Separator.ARRAY)
                break;
            if (closedBraceKind == ARRAY || closedBraceKind == BraceExprToken.Kind.ANY) {
                // if (tokens.isEmpty())
                iterator.previous();
                break;
            }
            unexpectedToken(current);
        } else if (separator == Separator.ARRAY_BLOCK && braceOpened == 0 && isClosedBrace(current, BLOCK)) {
            break;
        } else if (current instanceof StaticExprToken && nextTokenAndPrev(iterator) instanceof FunctionStmtToken) {
            current = nextToken(iterator);
            current = processClosure(current, next, iterator, true);
            tokens.add(current);
        } else if (current instanceof FunctionStmtToken) {
            current = processClosure(current, next, iterator, false);
            tokens.add(current);
        } else if (current instanceof StaticExprToken && nextTokenAndPrev(iterator) instanceof LambdaStmtToken) {
            current = nextToken(iterator);
            current = processLambda(current, iterator, separator, closedBraceKind, true);
            tokens.add(current);
        } else if (current instanceof LambdaStmtToken) {
            current = processLambda(current, iterator, separator, closedBraceKind, false);
            tokens.add(current);
        } else if (current instanceof ListExprToken && isOpenedBrace(next, SIMPLE)) {
            current = processList(current, iterator, null, closedBraceKind, braceOpened);
            tokens.add(current);
        } else if (current instanceof DieExprToken) {
            processDie(current, next, iterator);
            tokens.add(current);
        } else if (current instanceof EmptyExprToken) {
            processEmpty(current, iterator);
            tokens.add(current);
        } else if (current instanceof IssetExprToken) {
            processIsset(previous, current, iterator);
            tokens.add(current);
        } else if (current instanceof UnsetExprToken) {
            if (isOpenedBrace(previous, SIMPLE) && isClosedBrace(next, SIMPLE)) {
                current = new UnsetCastExprToken(current.getMeta());
                tokens.set(tokens.size() - 1, current);
                iterator.next();
                braceOpened--;
            } else {
                if (previous != null)
                    unexpectedToken(current);
                processUnset(previous, current, iterator);
                tokens.add(current);
                needBreak = true;
            }
        } else if (current instanceof CommaToken) {
            if (separator == Separator.COMMA || separator == Separator.COMMA_OR_SEMICOLON) {
                if (tokens.isEmpty())
                    unexpectedToken(current);
                break;
            } else {
                unexpectedToken(current);
            }
        } else if (current instanceof AsStmtToken) {
            if (separator == Separator.AS)
                break;
            unexpectedToken(current);
        } else if (isClosedBrace(current, closedBraceKind)) {
            iterator.previous();
            break;
        } else if (current instanceof BreakToken) {
            break;
        } else if (current instanceof ColonToken) {
            if (separator == Separator.COLON) {
                /*if (tokens.isEmpty()) see: issues/93
                        unexpectedToken(current);*/
                break;
            }
            unexpectedToken(current);
        } else if (current instanceof SemicolonToken) {
            // TODO refactor!
            if (separator == Separator.SEMICOLON || separator == Separator.COMMA_OR_SEMICOLON) {
                /*if (tokens.isEmpty()) see: issues/94
                        unexpectedToken(current);*/
                break;
            }
            if (separator == Separator.COMMA || closedBraceKind != null || tokens.isEmpty())
                unexpectedToken(current);
            break;
        } else if (current instanceof BraceExprToken) {
            if (closedBraceKind == BraceExprToken.Kind.ANY && isClosedBrace(current)) {
                iterator.previous();
                break;
            }
            unexpectedToken(current);
        } else if (current instanceof ArrayExprToken) {
            if (needBreak)
                unexpectedToken(current);
            tokens.add(current = processNewArray(current, iterator));
        } else if (current instanceof ExprToken) {
            if (needBreak)
                unexpectedToken(current);
            CastExprToken cast = null;
            if (current instanceof NameToken && isOpenedBrace(previous, SIMPLE) && isClosedBrace(next, SIMPLE)) {
                cast = CastExprToken.valueOf(((NameToken) current).getName(), current.getMeta());
                if (cast != null) {
                    current = cast;
                    iterator.next();
                    braceOpened--;
                    tokens.set(tokens.size() - 1, current);
                }
            }
            if (cast == null) {
                Token token = processSimpleToken(current, previous, next, iterator, closedBraceKind, braceOpened, separator);
                if (token != null)
                    current = token;
                tokens.add(current);
            }
        } else
            unexpectedToken(current);
        previous = current;
        if (iterator.hasNext()) {
            current = nextToken(iterator);
            next = iterator.hasNext() ? iterator.next() : null;
            if (next != null)
                iterator.previous();
        } else
            current = null;
        if (current == null)
            nextToken(iterator);
    } while (current != null);
    if (tokens.isEmpty())
        return null;
    if (braceOpened != 0)
        unexpectedToken(iterator.previous());
    ExprStmtToken exprStmtToken = new ExprStmtToken(analyzer.getEnvironment(), analyzer.getContext(), tokens);
    return exprStmtToken;
}
Also used : MacroToken(org.develnext.jphp.core.tokenizer.token.expr.value.macro.MacroToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) Token(org.develnext.jphp.core.tokenizer.token.Token) CastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.CastExprToken) BreakToken(org.develnext.jphp.core.tokenizer.token.BreakToken) ColonToken(org.develnext.jphp.core.tokenizer.token.ColonToken) UnsetCastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken) UnsetCastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) CastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.CastExprToken) UnsetCastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken) CastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.CastExprToken) UnsetCastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken) ColonToken(org.develnext.jphp.core.tokenizer.token.ColonToken) BreakToken(org.develnext.jphp.core.tokenizer.token.BreakToken)

Aggregations

BreakToken (org.develnext.jphp.core.tokenizer.token.BreakToken)1 ColonToken (org.develnext.jphp.core.tokenizer.token.ColonToken)1 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)1 Token (org.develnext.jphp.core.tokenizer.token.Token)1 CastExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.cast.CastExprToken)1 UnsetCastExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken)1 MacroToken (org.develnext.jphp.core.tokenizer.token.expr.value.macro.MacroToken)1