Search in sources :

Example 1 with AmpersandRefToken

use of org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken 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) 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) 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 2 with AmpersandRefToken

use of org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken 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;
    if (next instanceof SelfExprToken) {
        if (analyzer.getClazz() == null) {
            unexpectedToken(next);
        }
        next = analyzer.getClazz().getName();
    }
    if (next instanceof NameToken) {
        String word = ((NameToken) next).getName().toLowerCase();
        if (scalarTypeHints.contains(word))
            hintType = HintType.of(word);
        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);
    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) 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) 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) 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)

Aggregations

CommentToken (org.develnext.jphp.core.tokenizer.token.CommentToken)2 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)2 Token (org.develnext.jphp.core.tokenizer.token.Token)2 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)2 CommaToken (org.develnext.jphp.core.tokenizer.token.expr.CommaToken)2 AmpersandRefToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken)2 ArgumentUnpackExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ArgumentUnpackExprToken)2 AssignExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken)2 HintType (php.runtime.common.HintType)1