Search in sources :

Example 81 with Token

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

the class ASMExpression method processOperator.

protected void processOperator(Stack<Token> stack, List<Token> result, OperatorExprToken current) {
    List<Token> list = new ArrayList<Token>();
    boolean isRightOperator = current != null && current.isRightSide();
    int prior = current == null ? -1 : current.getPriority();
    while (!stack.empty()) {
        Token el = stack.peek();
        int elPrior = getPriority(el);
        if (el instanceof BraceExprToken)
            break;
        if (current != null && current.getAssociation() == Association.RIGHT && !current.isBinary() && prev instanceof OperatorExprToken)
            break;
        boolean flush = current == null || elPrior == 1 || (isRightOperator ? elPrior > prior : elPrior <= prior);
        if (flush) {
            stack.pop();
            list.add(el);
        } else {
            break;
        }
    }
    result.addAll(list);
}
Also used : BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) ArrayList(java.util.ArrayList) OperatorExprToken(org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) ExprToken(org.develnext.jphp.core.tokenizer.token.expr.ExprToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) OperatorExprToken(org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken) CallExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.CallExprToken)

Example 82 with Token

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

the class SourceTokenizer method parseAll.

@Signature
public static List<SourceToken> parseAll(Environment env, String text) {
    Tokenizer tokenizer = new Tokenizer(text, new Context(text));
    Token token;
    List<SourceToken> result = new ArrayList<>();
    try {
        while ((token = tokenizer.nextToken()) != null) {
            result.add(new SourceToken(env, token));
        }
    } catch (ParseException | StringIndexOutOfBoundsException e) {
        // TODO remove StringIndexOutOfBoundsException bug in jphp!
        return null;
    }
    return result;
}
Also used : Context(php.runtime.env.Context) ArrayList(java.util.ArrayList) Token(org.develnext.jphp.core.tokenizer.token.Token) ParseException(php.runtime.exceptions.ParseException) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Signature(php.runtime.annotation.Reflection.Signature)

Example 83 with Token

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

the class IfSyntaxTest method testShortly.

@Test
public void testShortly() {
    List<Token> tree = getSyntaxTree("if (true) 'string'; 123;");
    Assert.assertTrue(tree.size() == 2);
    Assert.assertTrue(tree.get(0) instanceof ExprStmtToken);
    Assert.assertTrue(((ExprStmtToken) tree.get(0)).getTokens().size() == 1);
    Assert.assertTrue(((ExprStmtToken) tree.get(0)).getTokens().get(0) instanceof IfStmtToken);
    IfStmtToken token = (IfStmtToken) ((ExprStmtToken) tree.get(0)).getTokens().get(0);
    Assert.assertNotNull(token.getBody());
    Assert.assertNotNull(token.getCondition());
}
Also used : ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) IfStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.IfStmtToken) IfStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.IfStmtToken) Test(org.junit.Test)

Example 84 with Token

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

the class IfSyntaxTest method testAlternative.

@Test
public void testAlternative() {
    List<Token> tree = getSyntaxTree("if (true): 123; endif");
    Assert.assertTrue(tree.size() == 1);
    Assert.assertTrue(tree.get(0) instanceof ExprStmtToken);
    Assert.assertTrue(((ExprStmtToken) tree.get(0)).getTokens().size() == 1);
    Assert.assertTrue(((ExprStmtToken) tree.get(0)).getTokens().get(0) instanceof IfStmtToken);
    IfStmtToken token = (IfStmtToken) ((ExprStmtToken) tree.get(0)).getTokens().get(0);
    Assert.assertNotNull(token.getBody());
    Assert.assertNotNull(token.getCondition());
}
Also used : ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) IfStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.IfStmtToken) IfStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.IfStmtToken) Test(org.junit.Test)

Example 85 with Token

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

the class IfSyntaxTest method testNested.

@Test
public void testNested() {
    List<Token> tree = getSyntaxTree("if (true){ " + "'string'; " + "if(false){ " + "1234; " + "} " + "}");
    Assert.assertTrue(tree.size() == 1);
    Assert.assertTrue(tree.get(0) instanceof ExprStmtToken);
    Assert.assertTrue(((ExprStmtToken) tree.get(0)).getTokens().size() == 1);
    Assert.assertTrue(((ExprStmtToken) tree.get(0)).getTokens().get(0) instanceof IfStmtToken);
    IfStmtToken token = (IfStmtToken) ((ExprStmtToken) tree.get(0)).getTokens().get(0);
    Assert.assertNotNull(token.getBody());
    Assert.assertTrue(token.getBody().getInstructions().size() == 2);
    Assert.assertTrue(token.getBody().getInstructions().get(1).getTokens().size() == 1);
    Assert.assertTrue(token.getBody().getInstructions().get(1).getTokens().get(0) instanceof IfStmtToken);
}
Also used : ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) IfStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.IfStmtToken) IfStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.IfStmtToken) Test(org.junit.Test)

Aggregations

Token (org.develnext.jphp.core.tokenizer.token.Token)93 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)47 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)39 CommentToken (org.develnext.jphp.core.tokenizer.token.CommentToken)37 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)27 CommaToken (org.develnext.jphp.core.tokenizer.token.expr.CommaToken)26 BreakToken (org.develnext.jphp.core.tokenizer.token.BreakToken)25 AssignExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken)25 Test (org.junit.Test)24 ColonToken (org.develnext.jphp.core.tokenizer.token.ColonToken)22 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)21 ValueIfElseToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken)19 CastExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.cast.CastExprToken)16 UnsetCastExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken)16 MacroToken (org.develnext.jphp.core.tokenizer.token.expr.value.macro.MacroToken)16 StringExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StringExprToken)14 VariableExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken)14 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)13 MinusExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.MinusExprToken)12 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)12