Search in sources :

Example 1 with BraceExprToken

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

the class ASMExpression method processToken.

protected void processToken(Token token, Stack<Token> stack, List<Token> result) {
    if (token instanceof CallExprToken) {
        CallExprToken call = (CallExprToken) token;
        if (call.getName() instanceof OperatorExprToken) {
            processOperator(stack, result, (OperatorExprToken) call.getName());
        }
        result.add(token);
    } else if (token instanceof ValueExprToken) {
        result.add(token);
    } else if (token instanceof BraceExprToken) {
        BraceExprToken brace = (BraceExprToken) token;
        if (brace.isSimpleOpened()) {
            stack.push(brace);
        } else if (brace.isSimpleClosed()) {
            if (stack.empty())
                unexpectedToken(brace);
            boolean done = false;
            do {
                Token el = stack.pop();
                if (el instanceof BraceExprToken && ((BraceExprToken) el).isSimpleOpened()) {
                    done = true;
                    break;
                }
                result.add(el);
            } while (!stack.isEmpty());
            if (!done)
                unexpectedToken(brace);
        } else
            unexpectedToken(brace);
    } else if (token instanceof OperatorExprToken) {
        OperatorExprToken operator = (OperatorExprToken) token;
        /*boolean done = !stack.empty();
            if (done){
                if (operator.isRightSide())
                    done = getPriority(stack.peek()) > prior;
                else
                    done = getPriority(stack.peek()) > prior;
            }

            if (done){
                if (prior == 1){
                    processOperator(stack, result, prior);
                    result.add(token);
                    return;
                }

                stack.push(token);
                return;
            }*/
        processOperator(stack, result, operator);
        stack.push(token);
    }
}
Also used : CallExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.CallExprToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) 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) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)

Example 2 with BraceExprToken

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

the class ClassGenerator method processBody.

@SuppressWarnings("unchecked")
protected void processBody(ClassStmtToken result, ListIterator<Token> iterator) {
    analyzer.setClazz(result);
    Token token = nextToken(iterator);
    if (token instanceof BraceExprToken) {
        BraceExprToken brace = (BraceExprToken) token;
        if (brace.isBlockOpened()) {
            List<ConstStmtToken> constants = new ArrayList<ConstStmtToken>();
            List<MethodStmtToken> methods = new ArrayList<MethodStmtToken>();
            List<ClassVarStmtToken> properties = new ArrayList<ClassVarStmtToken>();
            List<Token> modifiers = new ArrayList<Token>();
            CommentToken lastComment = null;
            boolean breakByClose = false;
            while (iterator.hasNext()) {
                Token current = iterator.next();
                if (current instanceof ExprStmtToken)
                    unexpectedToken(current, "expression");
                if (current instanceof ConstStmtToken) {
                    if (result.isTrait()) {
                        unexpectedToken(current);
                    }
                    ConstStmtToken one = analyzer.generator(ConstGenerator.class).getToken(current, iterator);
                    one.setClazz(result);
                    one.setDocComment(lastComment);
                    one.setModifier(Modifier.PUBLIC);
                    for (Token modifier : modifiers) {
                        if (modifier instanceof PrivateStmtToken) {
                            one.setModifier(Modifier.PRIVATE);
                        } else if (modifier instanceof ProtectedStmtToken) {
                            one.setModifier(Modifier.PROTECTED);
                        } else if (modifier instanceof PublicStmtToken) {
                            one.setModifier(Modifier.PUBLIC);
                        } else {
                            unexpectedToken(modifier);
                        }
                    }
                    lastComment = null;
                    constants.add(one);
                    modifiers.clear();
                } else if (isTokenClass(current, ClassGenerator.modifiers)) {
                    for (Token modifier : modifiers) {
                        if (modifier.getType() == current.getType())
                            unexpectedToken(current);
                    }
                    modifiers.add(current);
                } else if (current instanceof VariableExprToken) {
                    if (result.isInterface()) {
                        analyzer.getEnvironment().error(result.toTraceInfo(analyzer.getContext()), ErrorType.E_ERROR, Messages.ERR_INTERFACE_MAY_NOT_INCLUDE_VARS);
                    }
                    for (Token modifier : modifiers) {
                        if (isTokenClass(modifier, FinalStmtToken.class, AbstractStmtToken.class)) {
                            unexpectedToken(modifier);
                        }
                    }
                    List<ClassVarStmtToken> vars = processProperty(result, (VariableExprToken) current, modifiers, iterator);
                    if (lastComment != null) {
                        for (ClassVarStmtToken var : vars) {
                            var.setDocComment(lastComment);
                        }
                        lastComment = null;
                    }
                    properties.addAll(vars);
                    modifiers.clear();
                } else if (current instanceof FunctionStmtToken) {
                    FunctionStmtToken function = analyzer.generator(FunctionGenerator.class).getToken(current, iterator);
                    if (function == null) {
                        nextToken(iterator);
                        unexpectedToken(iterator);
                    }
                    MethodStmtToken method = new MethodStmtToken(function);
                    method.setClazz(result);
                    method.setDocComment(lastComment);
                    lastComment = null;
                    for (Token modifier : modifiers) {
                        if (modifier instanceof AbstractStmtToken)
                            method.setAbstract(true);
                        else if (modifier instanceof StaticExprToken)
                            method.setStatic(true);
                        else if (modifier instanceof FinalStmtToken) {
                            method.setFinal(true);
                        } else if (modifier instanceof PublicStmtToken) {
                            if (method.getModifier() != null)
                                unexpectedToken(modifier);
                            method.setModifier(Modifier.PUBLIC);
                        } else if (modifier instanceof PrivateStmtToken) {
                            if (method.getModifier() != null)
                                unexpectedToken(modifier);
                            method.setModifier(Modifier.PRIVATE);
                        } else if (modifier instanceof ProtectedStmtToken) {
                            if (method.getModifier() != null)
                                unexpectedToken(modifier);
                            method.setModifier(Modifier.PROTECTED);
                        }
                    }
                    if (method.getModifier() == null)
                        method.setModifier(Modifier.PUBLIC);
                    methods.add(method);
                    modifiers.clear();
                } else if (current instanceof NamespaceUseStmtToken) {
                    processUse(result, iterator);
                    lastComment = null;
                } else if (isClosedBrace(current, BraceExprToken.Kind.BLOCK)) {
                    breakByClose = true;
                    break;
                } else if (current instanceof CommentToken) {
                    lastComment = (CommentToken) current;
                } else
                    unexpectedToken(current);
            }
            if (!breakByClose) {
                // bug-fix from DN.
                token = nextTokenAndPrev(iterator);
                if (!isClosedBrace(token, BraceExprToken.Kind.BLOCK)) {
                    unexpectedToken(token);
                }
            }
            // ---
            result.setConstants(constants);
            result.setMethods(methods);
            result.setProperties(properties);
            analyzer.setClazz(null);
            return;
        }
    }
    unexpectedToken(token, "{");
}
Also used : CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken)

Example 3 with BraceExprToken

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

the class FunctionGenerator method getToken.

@SuppressWarnings("unchecked")
public FunctionStmtToken getToken(Token current, ListIterator<Token> iterator, boolean closureAllowed) {
    if (current instanceof FunctionStmtToken) {
        CommentToken commentToken = null;
        iterator.previous();
        if (iterator.hasPrevious()) {
            int cnt = 0;
            while (iterator.hasPrevious()) {
                cnt++;
                Token previous = iterator.previous();
                if (previous.isNamedToken())
                    continue;
                if (previous instanceof CommentToken && ((CommentToken) previous).getKind() == CommentToken.Kind.DOCTYPE) {
                    commentToken = ((CommentToken) previous);
                }
                break;
            }
            for (int i = 0; i < cnt; i++) {
                iterator.next();
            }
        }
        iterator.next();
        FunctionStmtToken result = (FunctionStmtToken) current;
        result.setStatic(analyzer.getFunction() == null);
        Class<? extends Token>[] excludes = new Class[] { EchoStmtToken.class, ImportExprToken.class };
        if (analyzer.getClazz() != null) {
            excludes = new Class[0];
        }
        Token next = nextTokenSensitive(iterator, excludes);
        if (next instanceof AmpersandRefToken) {
            result.setReturnReference(true);
            next = nextTokenSensitive(iterator, excludes);
        }
        if (next instanceof NameToken) {
            /*if (analyzer.getFunction() != null)
                    unexpectedToken(current);*/
            analyzer.addScope(true);
            FunctionStmtToken oldFunction = analyzer.getFunction();
            analyzer.setFunction(result);
            BraceExprToken brace = nextAndExpected(iterator, BraceExprToken.class);
            if (!brace.isSimpleOpened())
                unexpectedToken(brace, "(");
            result.setNamespace(analyzer.getNamespace());
            result.setName((NameToken) next);
            result.setDocComment(commentToken);
            processArguments(result, iterator);
            processBody(result, iterator);
            result.setTypeInfo(analyzer.getScope().getTypeInfo());
            result.setLabels(analyzer.getScope().getLabels());
            result.setLocal(analyzer.removeScope().getVariables());
            Token previous = iterator.previous();
            result.getMeta().setEndLine(previous.getMeta().getStartLine());
            result.getMeta().setEndPosition(previous.getMeta().getStartPosition());
            iterator.next();
            analyzer.setFunction(oldFunction);
            return result;
        } else if (next instanceof BraceExprToken) {
            // xClosure
            if (((BraceExprToken) next).isSimpleOpened()) {
                if (closureAllowed) {
                    analyzer.pushClosure(result);
                    analyzer.addScope(true);
                    processArguments(result, iterator);
                    processUses(result, iterator);
                    processBody(result, iterator);
                    // boolean thisExists = result.isThisExists();
                    result.setTypeInfo(analyzer.getScope().getTypeInfo());
                    result.setLabels(analyzer.getScope().getLabels());
                    result.setStaticExists(analyzer.getScope().isStaticExists());
                    result.setLocal(analyzer.removeScope().getVariables());
                    // result.setThisExists(thisExists);
                    analyzer.popClosure();
                    FunctionStmtToken prevClosure = analyzer.peekClosure();
                    if (prevClosure != null) {
                        if (result.isThisExists()) {
                            analyzer.getScope().addVariable(FunctionStmtToken.thisVariable);
                        // prevClosure.variable(FunctionStmtToken.thisVariable).setUsed(true);
                        // prevClosure.setThisExists(true);
                        }
                    }
                    List<VariableExprToken> uses = new ArrayList<VariableExprToken>();
                    for (ArgumentStmtToken argument : result.getUses()) {
                        if (argument.isReference()) {
                            if (analyzer.getFunction() != null) {
                                analyzer.getFunction().variable(argument.getName()).setReference(true);
                            }
                        }
                        if (analyzer.getFunction() != null) {
                            analyzer.getFunction().variable(argument.getName()).setUsed(true);
                        }
                        uses.add(argument.getName());
                    }
                    analyzer.getScope().addVariables(uses);
                    Token previous = iterator.previous();
                    result.getMeta().setEndLine(previous.getMeta().getStartLine());
                    result.getMeta().setEndPosition(previous.getMeta().getStartPosition());
                    iterator.next();
                    return result;
                }
                iterator.previous();
                return null;
            }
        }
        unexpectedToken(next);
    }
    return null;
}
Also used : CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ValueIfElseToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) ArgumentUnpackExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ArgumentUnpackExprToken) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) AmpersandRefToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken) ColonToken(org.develnext.jphp.core.tokenizer.token.ColonToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) AmpersandRefToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken)

Example 4 with BraceExprToken

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

the class FunctionGenerator method processArgument.

@SuppressWarnings("unchecked")
protected ArgumentStmtToken processArgument(ListIterator<Token> iterator) {
    boolean isReference = false;
    boolean isVariadic = false;
    VariableExprToken variable = null;
    ExprStmtToken value = null;
    Token next = nextToken(iterator);
    if (next instanceof CommaToken || isClosedBrace(next, BraceExprToken.Kind.SIMPLE))
        return null;
    NameToken hintTypeClass = null;
    HintType hintType = null;
    boolean optional = false;
    if (next instanceof ValueIfElseToken) {
        optional = true;
        next = nextToken(iterator);
    }
    if (next instanceof SelfExprToken) {
        if (analyzer.getClazz() == null) {
            unexpectedToken(next);
        }
        if (!analyzer.getClazz().isTrait()) {
            next = analyzer.getClazz().getName();
            hintTypeClass = analyzer.getRealName((NameToken) next);
        } else {
            hintType = HintType.SELF;
        }
        next = nextToken(iterator);
    } else if (next instanceof NameToken) {
        String word = ((NameToken) next).getName().toLowerCase();
        if (scalarTypeHints.contains(word)) {
            hintType = HintType.of(word);
        } else if (disallowScalarTypeHints.contains(word)) {
            unexpectedToken(next, "valid type");
        } else {
            hintType = jphp_scalarTypeHints.contains(word) ? null : HintType.of(word);
            if (hintType == null)
                hintTypeClass = analyzer.getRealName((NameToken) next);
        }
        next = nextToken(iterator);
    }
    if (next instanceof AmpersandRefToken) {
        isReference = true;
        next = nextToken(iterator);
    }
    if (next instanceof ArgumentUnpackExprToken) {
        isVariadic = true;
        next = nextToken(iterator);
    }
    if (next instanceof VariableExprToken) {
        variable = (VariableExprToken) next;
    } else
        unexpectedToken(next);
    next = nextToken(iterator);
    if (next instanceof AssignExprToken) {
        if (isVariadic) {
            unexpectedToken(next);
        }
        value = analyzer.generator(SimpleExprGenerator.class).getToken(nextToken(iterator), iterator, true, BraceExprToken.Kind.SIMPLE);
    } else {
        if (next instanceof CommaToken || isClosedBrace(next, BraceExprToken.Kind.SIMPLE)) {
            if (next instanceof BraceExprToken) {
                iterator.previous();
            } else {
                if (isVariadic) {
                    unexpectedToken(next);
                }
            }
        } else
            unexpectedToken(next);
    }
    ArgumentStmtToken argument = new ArgumentStmtToken(variable.getMeta());
    argument.setName(variable);
    argument.setHintType(hintType);
    argument.setHintTypeClass(hintTypeClass);
    argument.setReference(isReference);
    argument.setVariadic(isVariadic);
    argument.setValue(value);
    argument.setOptional(optional);
    if (argument.isReference() && argument.getValue() != null)
        analyzer.getFunction().variable(argument.getName()).setUsed(true);
    return argument;
}
Also used : CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) HintType(php.runtime.common.HintType) ValueIfElseToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ValueIfElseToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) ArgumentUnpackExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ArgumentUnpackExprToken) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) AmpersandRefToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken) ColonToken(org.develnext.jphp.core.tokenizer.token.ColonToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) ArgumentUnpackExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ArgumentUnpackExprToken) AmpersandRefToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken)

Example 5 with BraceExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken 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)

Aggregations

BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)6 Token (org.develnext.jphp.core.tokenizer.token.Token)5 CommentToken (org.develnext.jphp.core.tokenizer.token.CommentToken)3 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)3 CommaToken (org.develnext.jphp.core.tokenizer.token.expr.CommaToken)3 AssignExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken)3 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)3 ColonToken (org.develnext.jphp.core.tokenizer.token.ColonToken)2 ExprToken (org.develnext.jphp.core.tokenizer.token.expr.ExprToken)2 OperatorExprToken (org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken)2 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)2 AmpersandRefToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken)2 ArgumentUnpackExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ArgumentUnpackExprToken)2 ValueIfElseToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken)2 CallExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.CallExprToken)2 ArrayList (java.util.ArrayList)1 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)1 DeclareStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.DeclareStmtToken)1 HintType (php.runtime.common.HintType)1