Search in sources :

Example 1 with CommaToken

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

the class UseGenerator method parseBody.

public void parseBody(NamespaceUseStmtToken use, Token current, ListIterator<Token> iterator, FulledNameToken prefix, NamespaceUseStmtToken.UseType prefixUseType) {
    boolean first = true;
    NamespaceUseStmtToken.UseType useType = prefixUseType;
    Environment environment = this.analyzer.getEnvironment();
    PackageManager packageManager = null;
    if (environment != null) {
        packageManager = environment.getPackageManager();
    }
    do {
        Token next = nextToken(iterator);
        if (next instanceof FunctionStmtToken) {
            if ((!first && prefix == null) || (prefixUseType != CLASS)) {
                unexpectedToken(next);
            }
            useType = FUNCTION;
            next = nextToken(iterator);
        } else if (next instanceof ConstStmtToken) {
            if ((!first && prefix == null) || (prefixUseType != CLASS)) {
                unexpectedToken(next);
            }
            useType = CONSTANT;
            next = nextToken(iterator);
        }
        use.setUseType(useType);
        if (prefix != null && next instanceof FulledNameToken && next.getMeta().getWord().startsWith("\\")) {
            unexpectedToken(new BackslashExprToken(TokenMeta.of("\\", next)), "identifier or function or const", false);
        }
        FulledNameToken name = analyzer.generator(NameGenerator.class).getToken(next, iterator);
        if (name == null) {
            unexpectedToken(iterator.previous());
            return;
        }
        if (prefix == null) {
            use.setName(name);
        } else {
            ArrayList<NameToken> nameTokens = new ArrayList<>(prefix.getNames());
            nameTokens.addAll(name.getNames());
            use.setName(new FulledNameToken(name.getMeta(), nameTokens));
        }
        Token token = nextToken(iterator);
        if (token instanceof AsStmtToken) {
            token = nextToken(iterator);
            if (token instanceof NameToken) {
                use.setAs((NameToken) token);
                token = nextToken(iterator);
            } else
                unexpectedToken(token);
        } else if (isOpenedBrace(token, BraceExprToken.Kind.BLOCK)) {
            if (prefix == null) {
                parseBody(use, current, iterator, name, useType);
                return;
            }
        } else if (token instanceof BackslashExprToken) {
            next = nextToken(iterator);
            if (isOpenedBrace(next, BraceExprToken.Kind.BLOCK) && prefix == null) {
                parseBody(use, current, iterator, name, useType);
                return;
            }
        }
        NamespaceStmtToken namespace = analyzer.getNamespace();
        if (analyzer.getEnvironment() != null && analyzer.getEnvironment().scope.getLangMode() == LangMode.MODERN) {
            if (packageManager != null && use.isPackageImport()) {
                Package aPackage = packageManager.tryFind(use.getName().toName(), use.toTraceInfo(analyzer.getContext()));
                if (aPackage != null) {
                    for (String cls : aPackage.getClasses()) {
                        FulledNameToken nameToken = FulledNameToken.valueOf(StringUtils.split(cls, Information.NAMESPACE_SEP_CHAR));
                        NamespaceUseStmtToken useStmtToken = new NamespaceUseStmtToken(TokenMeta.of(cls, use));
                        useStmtToken.setName(nameToken);
                        useStmtToken.setUseType(NamespaceUseStmtToken.UseType.CLASS);
                        namespace.getUses().add(useStmtToken);
                    }
                    for (String s : aPackage.getFunctions()) {
                        FulledNameToken nameToken = FulledNameToken.valueOf(StringUtils.split(s, Information.NAMESPACE_SEP_CHAR));
                        NamespaceUseStmtToken useStmtToken = new NamespaceUseStmtToken(TokenMeta.of(s, use));
                        useStmtToken.setName(nameToken);
                        useStmtToken.setUseType(NamespaceUseStmtToken.UseType.FUNCTION);
                        namespace.getUses().add(useStmtToken);
                    }
                    for (String s : aPackage.getConstants()) {
                        FulledNameToken nameToken = FulledNameToken.valueOf(StringUtils.split(s, Information.NAMESPACE_SEP_CHAR));
                        NamespaceUseStmtToken useStmtToken = new NamespaceUseStmtToken(TokenMeta.of(s, use));
                        useStmtToken.setName(nameToken);
                        useStmtToken.setUseType(NamespaceUseStmtToken.UseType.CONSTANT);
                        namespace.getUses().add(useStmtToken);
                    }
                } else {
                    namespace.getUses().add(use);
                }
            } else {
                namespace.getUses().add(use);
            }
        } else {
            namespace.getUses().add(use);
        }
        if (token instanceof CommaToken) {
            use = new NamespaceUseStmtToken(current.getMeta());
        } else if (!(token instanceof SemicolonToken)) {
            if (prefix != null && isClosedBrace(token, BraceExprToken.Kind.BLOCK)) {
                nextAndExpected(iterator, SemicolonToken.class);
                break;
            }
            unexpectedToken(token);
        } else
            break;
        first = false;
    } while (true);
}
Also used : CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) ArrayList(java.util.ArrayList) Token(org.develnext.jphp.core.tokenizer.token.Token) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) BackslashExprToken(org.develnext.jphp.core.tokenizer.token.expr.BackslashExprToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) PackageManager(php.runtime.env.PackageManager) BackslashExprToken(org.develnext.jphp.core.tokenizer.token.expr.BackslashExprToken) Environment(php.runtime.env.Environment) Package(php.runtime.env.Package) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)

Example 2 with CommaToken

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

the class ClassGenerator method processUse.

protected void processUse(ClassStmtToken result, ListIterator<Token> iterator) {
    if (result.isInterface())
        unexpectedToken(iterator.previous());
    Token prev = null;
    List<NameToken> uses = new ArrayList<NameToken>();
    do {
        Token token = nextToken(iterator);
        if (token instanceof NameToken) {
            uses.add(analyzer.getRealName((NameToken) token));
        } else if (token instanceof CommaToken) {
            if (!(prev instanceof NameToken))
                unexpectedToken(token);
        } else if (token instanceof SemicolonToken) {
            break;
        } else if (isOpenedBrace(token, BraceExprToken.Kind.BLOCK)) {
            processBlock(result, uses.get(0), iterator);
            break;
        } else
            unexpectedToken(token);
        prev = token;
    } while (true);
    result.getUses().addAll(uses);
}
Also used : CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) 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 3 with CommaToken

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

the class ClassGenerator method processProperty.

protected List<ClassVarStmtToken> processProperty(ClassStmtToken clazz, VariableExprToken current, List<Token> modifiers, ValueExprToken type, boolean nullable, ListIterator<Token> iterator) {
    Token next = current;
    Token prev = null;
    Set<VariableExprToken> variables = new LinkedHashSet<VariableExprToken>();
    List<ExprStmtToken> initValues = new ArrayList<>();
    NameToken hintTypeClass = null;
    HintType hintType = null;
    if (type != null) {
        if (type instanceof NameToken) {
            String word = ((NameToken) type).getName().toLowerCase();
            /*if (disallowScalarTypesForProps.contains(word)) {
                    unexpectedToken(next, "valid type");
                } else */
            if (FunctionGenerator.scalarTypeHints.contains(word)) {
                hintType = HintType.of(word);
            } else {
                hintType = FunctionGenerator.jphp_scalarTypeHints.contains(word) ? null : HintType.of(word);
                if (hintType == null)
                    hintTypeClass = analyzer.getRealName((NameToken) type);
            }
        }
    }
    ExprStmtToken initValue = null;
    List<ClassVarStmtToken> result = new ArrayList<ClassVarStmtToken>();
    Modifier modifier = Modifier.PUBLIC;
    boolean isStatic = false;
    for (Token token : modifiers) {
        if (token instanceof PrivateStmtToken)
            modifier = Modifier.PRIVATE;
        else if (token instanceof ProtectedStmtToken)
            modifier = Modifier.PROTECTED;
        else if (token instanceof StaticExprToken)
            isStatic = true;
    }
    do {
        if (next instanceof VariableExprToken) {
            if (!variables.add((VariableExprToken) next)) {
                throw new ParseException(Messages.ERR_IDENTIFIER_X_ALREADY_USED.fetch(next.getWord()), next.toTraceInfo(analyzer.getContext()));
            }
            initValues.add(null);
        } else if (next instanceof CommaToken) {
            if (!(prev instanceof VariableExprToken))
                unexpectedToken(next);
        } else if (next instanceof AssignExprToken) {
            if (!(prev instanceof VariableExprToken))
                unexpectedToken(next);
            initValue = analyzer.generator(SimpleExprGenerator.class).getToken(nextToken(iterator), iterator, Separator.COMMA_OR_SEMICOLON, null);
            initValues.set(initValues.size() - 1, initValue);
            if (iterator.hasPrevious() && isBreak(iterator.previous())) {
                iterator.next();
                break;
            }
            if (iterator.hasNext()) {
                iterator.next();
            }
        // break;
        } else if (next instanceof SemicolonToken) {
            if (!(prev instanceof VariableExprToken))
                unexpectedToken(next);
            break;
        } else {
            unexpectedToken(next);
        }
        prev = next;
        next = nextToken(iterator);
    } while (true);
    int i = 0;
    for (VariableExprToken variable : variables) {
        ClassVarStmtToken classVar = new ClassVarStmtToken(variable.getMeta());
        classVar.setModifier(modifier);
        classVar.setStatic(isStatic);
        classVar.setValue(initValues.get(i));
        classVar.setVariable(variable);
        classVar.setClazz(clazz);
        classVar.setHintType(hintType);
        classVar.setHintTypeClass(hintTypeClass);
        classVar.setNullable(nullable);
        result.add(classVar);
        i++;
    }
    return result;
}
Also used : CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) 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) ParseException(php.runtime.exceptions.ParseException)

Example 4 with CommaToken

use of org.develnext.jphp.core.tokenizer.token.expr.CommaToken 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 5 with CommaToken

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

Aggregations

CommaToken (org.develnext.jphp.core.tokenizer.token.expr.CommaToken)10 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)9 Token (org.develnext.jphp.core.tokenizer.token.Token)9 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)9 AssignExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken)8 CommentToken (org.develnext.jphp.core.tokenizer.token.CommentToken)7 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)6 ValueIfElseToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken)6 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)3 FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)2 ParseException (php.runtime.exceptions.ParseException)2 ArrayList (java.util.ArrayList)1 SimpleExprGenerator (org.develnext.jphp.core.syntax.generators.manually.SimpleExprGenerator)1 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)1 ColonToken (org.develnext.jphp.core.tokenizer.token.ColonToken)1 BackslashExprToken (org.develnext.jphp.core.tokenizer.token.expr.BackslashExprToken)1 ClassExprToken (org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken)1 OperatorExprToken (org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken)1 AmpersandRefToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken)1 ArgumentUnpackExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ArgumentUnpackExprToken)1