Search in sources :

Example 1 with FulledNameToken

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

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

the class ClassStmtCompiler method writeImplements.

@SuppressWarnings("unchecked")
protected void writeImplements() {
    if (statement.getImplement() != null) {
        Environment env = compiler.getEnvironment();
        for (FulledNameToken name : statement.getImplement()) {
            ClassEntity implement = fetchClass(name.getName());
            Set<ClassEntity> needWriteInterfaceMethods = new HashSet<ClassEntity>();
            if (implement == null) {
                env.error(name.toTraceInfo(compiler.getContext()), Messages.ERR_INTERFACE_NOT_FOUND.fetch(name.toName()));
            } else {
                if (implement.getType() != ClassEntity.Type.INTERFACE) {
                    env.error(name.toTraceInfo(compiler.getContext()), Messages.ERR_CANNOT_IMPLEMENT.fetch(entity.getName(), implement.getName()));
                }
                if (!statement.isInterface())
                    needWriteInterfaceMethods = writeInterfaces(implement);
            }
            ClassEntity.ImplementsResult addResult = entity.addInterface(implement);
            addResult.check(env);
            for (ClassEntity el : needWriteInterfaceMethods) {
                if (el.isInternal()) {
                    writeInterfaceMethods(el.getMethods().values());
                }
            }
        }
    }
}
Also used : Environment(php.runtime.env.Environment) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)

Example 3 with FulledNameToken

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

the class ClassStmtCompiler method writeInitEnvironment.

@SuppressWarnings("unchecked")
protected void writeInitEnvironment() {
    if (!dynamicConstants.isEmpty() || !dynamicProperties.isEmpty()) {
        initDynamicExists = true;
        MethodNode node = new MethodNodeImpl();
        node.access = ACC_STATIC + ACC_PUBLIC;
        node.name = "__$initEnvironment";
        node.desc = Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class));
        if (entity.isTrait()) {
            node.desc = Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class), Type.getType(String.class));
        }
        MethodStmtCompiler methodCompiler = new MethodStmtCompiler(this, node);
        ExpressionStmtCompiler expressionCompiler = new ExpressionStmtCompiler(methodCompiler, null);
        methodCompiler.writeHeader();
        LabelNode l0 = expressionCompiler.makeLabel();
        methodCompiler.addLocalVariable("~env", l0, Environment.class);
        if (entity.isTrait())
            methodCompiler.addLocalVariable("~class_name", l0, String.class);
        LocalVariable l_class = methodCompiler.addLocalVariable("~class", l0, ClassEntity.class);
        expressionCompiler.writePushEnv();
        if (entity.isTrait()) {
            expressionCompiler.writeVarLoad("~class_name");
            expressionCompiler.writePushDupLowerCase();
        } else {
            expressionCompiler.writePushConstString(entity.getName());
            expressionCompiler.writePushConstString(entity.getLowerName());
        }
        expressionCompiler.writePushConstBoolean(true);
        expressionCompiler.writeSysDynamicCall(Environment.class, "fetchClass", ClassEntity.class, String.class, String.class, Boolean.TYPE);
        expressionCompiler.writeVarStore(l_class, false, false);
        // corrects defination of constants
        final List<ConstStmtToken.Item> first = new ArrayList<ConstStmtToken.Item>();
        final Set<String> usedNames = new HashSet<String>();
        final List<ConstStmtToken.Item> other = new ArrayList<ConstStmtToken.Item>();
        for (ConstStmtToken.Item el : dynamicConstants) {
            Token tk = el.value.getSingle();
            if (tk instanceof StaticAccessExprToken) {
                StaticAccessExprToken access = (StaticAccessExprToken) tk;
                boolean self = false;
                if (access.getClazz() instanceof SelfExprToken)
                    self = true;
                else if (access.getClazz() instanceof FulledNameToken && ((FulledNameToken) access.getClazz()).getName().equalsIgnoreCase(entity.getName())) {
                    self = true;
                }
                if (self) {
                    String name = ((NameToken) access.getField()).getName();
                    if (usedNames.contains(el.getFulledName()))
                        first.add(0, el);
                    else
                        first.add(el);
                    usedNames.add(name);
                    continue;
                }
            }
            if (usedNames.contains(el.getFulledName()))
                first.add(0, el);
            else
                other.add(el);
        }
        other.addAll(0, first);
        for (ConstStmtToken.Item el : other) {
            expressionCompiler.writeVarLoad(l_class);
            expressionCompiler.writePushEnv();
            expressionCompiler.writePushConstString(el.getFulledName());
            expressionCompiler.writeExpression(el.value, true, false, true);
            expressionCompiler.writePopBoxing(true);
            expressionCompiler.writeSysDynamicCall(ClassEntity.class, "addDynamicConstant", void.class, Environment.class, String.class, Memory.class);
        }
        for (ClassVarStmtToken el : dynamicProperties) {
            expressionCompiler.writeVarLoad(l_class);
            expressionCompiler.writePushEnv();
            expressionCompiler.writePushConstString(el.getVariable().getName());
            expressionCompiler.writeExpression(el.getValue(), true, false, true);
            expressionCompiler.writePopBoxing(true);
            expressionCompiler.writeSysDynamicCall(ClassEntity.class, el.isStatic() ? "addDynamicStaticProperty" : "addDynamicProperty", void.class, Environment.class, String.class, Memory.class);
        }
        node.instructions.add(new InsnNode(RETURN));
        methodCompiler.writeFooter();
        this.node.methods.add(node);
    }
}
Also used : StaticAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) StaticAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken) ClassStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken) ClassVarStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken) MethodStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) SelfExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken) ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) MethodNodeImpl(org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl) ClassVarStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) SelfExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)

Example 4 with FulledNameToken

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

the class NewValueCompiler method write.

@Override
public void write(NewExprToken token, boolean returnValue) {
    method.getEntity().setImmutable(false);
    expr.writeLineNumber(token);
    expr.writePushEnv();
    if (token.isDynamic()) {
        Memory className = expr.writeExpression(token.getExprName(), true, true, false);
        if (className != null) {
            expr.writePushConstString(className.toString());
            expr.writePushConstString(className.toString().toLowerCase());
        } else {
            expr.writeExpression(token.getExprName(), true, false);
            expr.writePopString();
            expr.writePushDupLowerCase();
        }
    } else {
        if (token.getName() instanceof StaticExprToken) {
            expr.writePushStatic();
            expr.writePushDupLowerCase();
        } else if (token.getName() instanceof SelfExprToken) {
            expr.writePushEnv();
            expr.writeSysDynamicCall(Environment.class, "__getMacroClass", Memory.class);
            expr.writePopString();
            expr.writePushDupLowerCase();
        } else {
            FulledNameToken name = (FulledNameToken) token.getName();
            expr.writePushString(name.getName());
            expr.writePushString(name.getName().toLowerCase());
        }
    }
    expr.writePushTraceInfo(token);
    expr.writePushParameters(token.getParameters());
    expr.writeSysDynamicCall(Environment.class, "__newObject", Memory.class, String.class, String.class, TraceInfo.class, Memory[].class);
    expr.setStackPeekAsImmutable();
    if (!returnValue)
        expr.writePopAll(1);
}
Also used : Memory(php.runtime.Memory) StaticExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticExprToken) Environment(php.runtime.env.Environment) SelfExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)

Example 5 with FulledNameToken

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

the class SyntaxAnalyzer method getRealName.

public static FulledNameToken getRealName(NameToken what, NamespaceStmtToken namespace, NamespaceUseStmtToken.UseType useType) {
    if (what instanceof FulledNameToken) {
        FulledNameToken fulledNameToken = (FulledNameToken) what;
        if (fulledNameToken.isProcessed(useType)) {
            return fulledNameToken;
        } else {
            if (fulledNameToken.isAnyProcessed()) {
                what = (fulledNameToken).getLastName();
            }
        }
    }
    String name = what.getName();
    // check name in uses
    if (namespace != null) {
        for (NamespaceUseStmtToken use : namespace.getUses()) {
            if (use.getUseType() != useType) {
                continue;
            }
            if (use.getAs() == null) {
                String string = use.getName().getLastName().getName();
                if ((useType == CONSTANT && name.equals(string)) || (useType != CONSTANT && name.equalsIgnoreCase(string))) {
                    FulledNameToken t = new FulledNameToken(use.getName());
                    t.setProcessed(useType);
                    return t;
                }
            } else {
                String string = use.getAs().getName();
                if ((useType == CONSTANT && name.equals(string)) || (useType != CONSTANT && name.equalsIgnoreCase(string))) {
                    FulledNameToken t = new FulledNameToken(use.getName());
                    t.setProcessed(useType);
                    return t;
                }
            }
        }
    }
    List<NameToken> names = namespace == null || namespace.getName() == null ? new ArrayList<NameToken>() : new ArrayList<NameToken>(namespace.getName().getNames());
    if (what instanceof FulledNameToken)
        names.addAll(((FulledNameToken) what).getNames());
    else
        names.add(what);
    FulledNameToken t = new FulledNameToken(what.getMeta(), names);
    t.setProcessed(useType);
    return t;
}
Also used : NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)

Aggregations

FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)8 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)5 Token (org.develnext.jphp.core.tokenizer.token.Token)4 Environment (php.runtime.env.Environment)3 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)2 CommaToken (org.develnext.jphp.core.tokenizer.token.expr.CommaToken)2 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)2 SelfExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken)2 ArrayList (java.util.ArrayList)1 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)1 MethodNodeImpl (org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl)1 BodyGenerator (org.develnext.jphp.core.syntax.generators.manually.BodyGenerator)1 SimpleExprGenerator (org.develnext.jphp.core.syntax.generators.manually.SimpleExprGenerator)1 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)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 AssignExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken)1 ImportExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.ImportExprToken)1 StaticAccessExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken)1