Search in sources :

Example 1 with BodyStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken in project jphp by jphp-compiler.

the class BodyGenerator method getToken.

@SuppressWarnings("unchecked")
public BodyStmtToken getToken(Token current, ListIterator<Token> iterator, boolean absolute, boolean returnNull, Class<? extends Token>... endTokens) {
    boolean alternativeSyntax = false;
    List<ExprStmtToken> instructions = new ArrayList<ExprStmtToken>();
    if (isOpenedBrace(current, BraceExprToken.Kind.BLOCK)) /*|| current instanceof SemicolonToken*/
    {
        // if (!returnNull)
        returnNull = false;
        while ((current = nextToken(iterator)) != null) {
            ExprStmtToken expr = analyzer.generator(ExprGenerator.class).getToken(current, iterator, BraceExprToken.class);
            if (expr == null) {
                break;
            }
            instructions.add(expr);
        }
    } else if (current instanceof ColonToken || (absolute && endTokens != null)) {
        if (endTokens == null)
            unexpectedToken(current);
        if (!(current instanceof ColonToken))
            iterator.previous();
        else
            alternativeSyntax = true;
        while (iterator.hasNext()) {
            current = nextToken(iterator);
            ExprStmtToken expr = analyzer.generator(ExprGenerator.class).getToken(current, iterator, endTokens);
            if (expr == null) {
                iterator.previous();
                break;
            } else if (expr.getTokens().size() == 1 && expr.getTokens().get(0) instanceof SemicolonToken) {
            // nop break;
            } else {
                instructions.add(expr);
            }
        }
    } else {
        ExprStmtToken expr = analyzer.generator(ExprGenerator.class).getToken(current, iterator);
        if (expr != null) {
            if (expr.getTokens().size() == 1 && expr.getTokens().get(0) instanceof SemicolonToken) {
            // nop
            } else {
                instructions.add(expr);
            }
        }
    }
    if (instructions.isEmpty() && returnNull)
        return null;
    BodyStmtToken result = new BodyStmtToken(TokenMeta.of(instructions));
    result.setInstructions(instructions);
    result.setAlternativeSyntax(alternativeSyntax);
    return result;
}
Also used : SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) ArrayList(java.util.ArrayList) ColonToken(org.develnext.jphp.core.tokenizer.token.ColonToken) ExprGenerator(org.develnext.jphp.core.syntax.generators.ExprGenerator) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)

Example 2 with BodyStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken in project jphp by jphp-compiler.

the class TryCatchGenerator method processCatch.

public void processCatch(CatchStmtToken result, ListIterator<Token> iterator) {
    Token next = nextToken(iterator);
    if (!isOpenedBrace(next, BraceExprToken.Kind.SIMPLE))
        unexpectedToken(next, "(");
    List<FulledNameToken> exceptions = new ArrayList<>();
    do {
        next = nextToken(iterator);
        if (!(next instanceof NameToken)) {
            if (exceptions.isEmpty()) {
                unexpectedToken(next, TokenType.T_STRING);
            } else {
                iterator.previous();
                break;
            }
        }
        FulledNameToken exception = analyzer.getRealName((NameToken) next);
        exceptions.add(exception);
        next = nextToken(iterator);
        if (!(next instanceof OrExprToken)) {
            iterator.previous();
            break;
        }
    } while (true);
    result.setExceptions(exceptions);
    next = nextToken(iterator);
    if (!(next instanceof VariableExprToken))
        unexpectedToken(next, TokenType.T_VARIABLE);
    VariableExprToken variable = (VariableExprToken) next;
    result.setVariable(variable);
    if (analyzer.getFunction() != null) {
        analyzer.getFunction().variable(variable).setUnstable(true);
    }
    analyzer.getScope().addVariable(variable);
    next = nextToken(iterator);
    if (!isClosedBrace(next, BraceExprToken.Kind.SIMPLE))
        unexpectedToken(next, ")");
    BodyStmtToken body = analyzer.generator(BodyGenerator.class).getToken(nextToken(iterator), iterator);
    result.setBody(body);
}
Also used : BodyGenerator(org.develnext.jphp.core.syntax.generators.manually.BodyGenerator) ArrayList(java.util.ArrayList) Token(org.develnext.jphp.core.tokenizer.token.Token) OrExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.OrExprToken) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) FinallyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.FinallyStmtToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) TryStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.TryStmtToken) CatchStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) OrExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.OrExprToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)

Example 3 with BodyStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken in project jphp by jphp-compiler.

the class TryCatchGenerator method getToken.

@Override
public TryStmtToken getToken(Token current, ListIterator<Token> iterator) {
    if (current instanceof TryStmtToken) {
        TryStmtToken result = (TryStmtToken) current;
        analyzer.addScope(false);
        BodyStmtToken body = analyzer.generator(BodyGenerator.class).getToken(nextToken(iterator), iterator);
        result.setBody(body);
        Token next = nextToken(iterator);
        result.setCatches(new ArrayList<CatchStmtToken>());
        if (next instanceof CatchStmtToken) {
            processCatches(result, next, iterator);
        } else if (next instanceof FinallyStmtToken) {
            analyzer.getScope().setLevelForGoto(true);
            processFinally(result, next, iterator);
            analyzer.getScope().setLevelForGoto(false);
        } else
            unexpectedToken(next, TokenType.T_CATCH);
        result.setLocal(analyzer.removeScope().getVariables());
        return result;
    }
    return null;
}
Also used : FinallyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.FinallyStmtToken) BodyGenerator(org.develnext.jphp.core.syntax.generators.manually.BodyGenerator) Token(org.develnext.jphp.core.tokenizer.token.Token) OrExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.OrExprToken) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) FinallyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.FinallyStmtToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) TryStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.TryStmtToken) CatchStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken) CatchStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken) TryStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.TryStmtToken) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)

Aggregations

BodyStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)3 ArrayList (java.util.ArrayList)2 BodyGenerator (org.develnext.jphp.core.syntax.generators.manually.BodyGenerator)2 Token (org.develnext.jphp.core.tokenizer.token.Token)2 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)2 OrExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.OrExprToken)2 FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)2 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)2 VariableExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken)2 CatchStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken)2 FinallyStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.FinallyStmtToken)2 TryStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.TryStmtToken)2 ExprGenerator (org.develnext.jphp.core.syntax.generators.ExprGenerator)1 ColonToken (org.develnext.jphp.core.tokenizer.token.ColonToken)1 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)1 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)1