Search in sources :

Example 36 with Token

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

the class ClassGenerator method processBlock.

protected void processBlock(ClassStmtToken result, NameToken firstTraitName, ListIterator<Token> iterator) {
    if (result.isInterface())
        unexpectedToken(iterator.previous());
    while (true) {
        NameToken className = nextAndExpectedSensitive(iterator, NameToken.class);
        NameToken methodName;
        Token token = nextToken(iterator);
        if (token instanceof StaticAccessExprToken) {
            className = analyzer.getRealName(className);
            Token nextTokenSensitive = nextTokenSensitive(iterator);
            if (nextTokenSensitive instanceof NameToken) {
                methodName = (NameToken) nextTokenSensitive;
            } else {
                unexpectedToken(nextTokenSensitive);
                return;
            }
        } else {
            iterator.previous();
            methodName = className;
            if (className.getClass() != NameToken.class)
                unexpectedToken(className);
            className = firstTraitName;
        }
        Token what = nextToken(iterator);
        if (what instanceof AsStmtToken) {
            Token one = nextTokenSensitive(iterator, PrivateStmtToken.class, ProtectedStmtToken.class, PublicStmtToken.class, FinalStmtToken.class, StaticExprToken.class);
            if (one instanceof NameToken) {
                result.addAlias(className.getName(), methodName.getName(), null, ((NameToken) one).getName());
            } else if (isTokenClass(one, PrivateStmtToken.class, ProtectedStmtToken.class, PublicStmtToken.class)) {
                Modifier modifier = Modifier.PRIVATE;
                if (one instanceof ProtectedStmtToken)
                    modifier = Modifier.PROTECTED;
                else if (one instanceof PublicStmtToken)
                    modifier = Modifier.PUBLIC;
                token = nextTokenSensitive(iterator);
                String name = null;
                if (token instanceof NameToken) {
                    NameToken two = (NameToken) token;
                    if (two.getClass() != NameToken.class)
                        unexpectedToken(two);
                    name = two.getName();
                } else if (token instanceof SemicolonToken) {
                    iterator.previous();
                // nop
                } else
                    unexpectedToken(token);
                result.addAlias(className.getName(), methodName.getName(), modifier, name);
            } else
                unexpectedToken(one);
            nextAndExpected(iterator, SemicolonToken.class);
        } else if (what instanceof InsteadofStmtToken) {
            Set<String> names = new HashSet<String>();
            Set<String> namesLower = new HashSet<String>();
            NameToken cls;
            while (true) {
                cls = nextAndExpected(iterator, NameToken.class);
                cls = analyzer.getRealName(cls);
                if (!namesLower.add(cls.getName().toLowerCase())) {
                    analyzer.getEnvironment().error(iterator.previous().toTraceInfo(analyzer.getContext()), ErrorType.E_ERROR, Messages.ERR_TRAIT_MULTIPLE_RULE, methodName.getName(), cls.getName());
                }
                names.add(cls.getName());
                Token next = nextToken(iterator);
                if (next instanceof CommaToken)
                    continue;
                if (next instanceof SemicolonToken)
                    break;
            }
            if (!result.addReplacement(className.getName(), methodName.getName(), names)) {
                analyzer.getEnvironment().error(iterator.previous().toTraceInfo(analyzer.getContext()), ErrorType.E_ERROR, Messages.ERR_TRAIT_MULTIPLE_RULE, methodName.getName(), cls.getName());
            }
        } else
            unexpectedToken(what);
        if (isClosedBrace(nextTokenAndPrev(iterator), BraceExprToken.Kind.BLOCK)) {
            iterator.next();
            break;
        }
    }
}
Also used : CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) 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) 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) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken)

Example 37 with Token

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

the class ClassGenerator method _processImplements.

protected void _processImplements(Token token, ClassStmtToken result, ListIterator<Token> iterator) {
    ImplementsStmtToken implement = new ImplementsStmtToken(token.getMeta());
    Token prev = null;
    List<FulledNameToken> names = new ArrayList<FulledNameToken>();
    do {
        token = nextToken(iterator);
        if (token instanceof NameToken) {
            names.add(analyzer.getRealName((NameToken) token));
        } else if (token instanceof CommaToken) {
            if (!(prev instanceof NameToken))
                unexpectedToken(token);
        } else if (isOpenedBrace(token, BraceExprToken.Kind.BLOCK)) {
            iterator.previous();
            break;
        } else
            unexpectedToken(token);
        prev = token;
    } while (true);
    implement.setNames(names);
    result.setImplement(implement);
}
Also used : CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) 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) 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 38 with Token

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

the class FunctionGenerator method processBody.

protected void processBody(FunctionStmtToken result, ListIterator<Token> iterator) {
    Token next = nextToken(iterator);
    if (isOpenedBrace(next, BraceExprToken.Kind.BLOCK)) {
        BodyStmtToken body = analyzer.generator(BodyGenerator.class).getToken(next, iterator);
        result.setBody(body);
    } else if (next instanceof SemicolonToken) {
        result.setInterfacable(true);
        result.setBody(null);
    } else
        unexpectedToken(next);
}
Also used : SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) BodyGenerator(org.develnext.jphp.core.syntax.generators.manually.BodyGenerator) 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)

Example 39 with Token

use of org.develnext.jphp.core.tokenizer.token.Token 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 40 with Token

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

the class FunctionGenerator method processReturnHint.

protected void processReturnHint(FunctionStmtToken result, ListIterator<Token> iterator) {
    Token token = nextTokenAndPrev(iterator);
    if (token instanceof ColonToken) {
        if (result.getReturnHintTypeClass() != null || result.getReturnHintType() != null) {
            unexpectedToken(token);
        }
        nextToken(iterator);
        Token next = nextToken(iterator);
        if (next instanceof ValueIfElseToken) {
            result.setReturnOptional(true);
            next = nextToken(iterator);
        }
        HintType hintType;
        NameToken hintTypeClass = null;
        if (next instanceof SelfExprToken) {
            if (analyzer.getClazz() == null) {
                result.setReturnHintType(HintType.SELF);
            } else {
                if (analyzer.getClazz().isTrait()) {
                    result.setReturnHintType(HintType.SELF);
                } else {
                    result.setReturnHintTypeClass(new FulledNameToken(next.getMeta(), new ArrayList<Token>() {

                        {
                            if (analyzer.getClazz().getNamespace().getName() != null) {
                                addAll(analyzer.getClazz().getNamespace().getName().getNames());
                            }
                            add(analyzer.getClazz().getName());
                        }
                    }));
                }
            }
        } else 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);
                }
            }
            result.setReturnHintType(hintType);
            result.setReturnHintTypeClass(hintTypeClass);
        } else {
            unexpectedToken(next, "name");
        }
    }
}
Also used : ValueIfElseToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken) HintType(php.runtime.common.HintType) 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) ColonToken(org.develnext.jphp.core.tokenizer.token.ColonToken)

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