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;
}
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);
}
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;
}
Aggregations