Search in sources :

Example 1 with Environment

use of php.runtime.env.Environment 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 Environment

use of php.runtime.env.Environment 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 Environment

use of php.runtime.env.Environment in project jphp by jphp-compiler.

the class CLI method checkSyntax.

protected void checkSyntax(String filename) throws Throwable {
    Launcher launcher = new Launcher("jphp.conf", args);
    launcher.run(false, true);
    File file = new File(filename);
    Environment environment = new Environment(launcher.getCompileScope(), output);
    Context context = new Context(file);
    try {
        SyntaxAnalyzer analyzer = new SyntaxAnalyzer(environment, new Tokenizer(context));
        analyzer.getTree();
        output.println(String.format("No syntax errors detected in %s", filename));
    } catch (Exception e) {
        environment.catchUncaught(e);
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    } finally {
        try {
            environment.doFinal();
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }
}
Also used : Context(php.runtime.env.Context) SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) Launcher(php.runtime.launcher.Launcher) Environment(php.runtime.env.Environment) File(java.io.File) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 4 with Environment

use of php.runtime.env.Environment in project jphp by jphp-compiler.

the class CLI method executeFile.

protected void executeFile(String filename) throws Throwable {
    Launcher launcher = new Launcher("jphp.conf", args);
    launcher.run(false);
    File file = new File(filename);
    Environment environment = new Environment(launcher.getCompileScope(), output);
    environment.getDefaultBuffer().setImplicitFlush(true);
    try {
        Context context = new Context(file);
        ModuleEntity module = environment.importModule(context);
        module.include(environment);
    } catch (Exception e) {
        environment.catchUncaught(e);
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    } finally {
        try {
            environment.doFinal();
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }
}
Also used : Context(php.runtime.env.Context) Launcher(php.runtime.launcher.Launcher) Environment(php.runtime.env.Environment) ModuleEntity(php.runtime.reflection.ModuleEntity) File(java.io.File)

Example 5 with Environment

use of php.runtime.env.Environment in project jphp by jphp-compiler.

the class ClassStmtCompiler method writeTraitProperties.

protected void writeTraitProperties(ClassEntity trait, Collection<PropertyEntity> props) {
    for (PropertyEntity el : props) {
        PropertyEntity origin = entity.properties.get(el.getLowerName());
        if (origin != null) {
            // doesn't work with parent private properties of non-traits  (see: the traits/property006.php test)
            boolean isSkip = origin.getTrait() == null && !origin.getClazz().equals(entity) && origin.isPrivate();
            if (origin.getTrait() != null && origin.getTrait().equals(trait))
                isSkip = true;
            if (!isSkip) {
                Environment env = compiler.getEnvironment();
                String ownerName = origin.getClazz().getName();
                if (origin.getTrait() != null)
                    ownerName = origin.getTrait().getName();
                boolean isFatal = origin.getModifier() != el.getModifier();
                if (origin.getDefaultValue() != null && el.getDefaultValue() == null)
                    isFatal = true;
                else if (origin.getDefaultValue() == null && el.getDefaultValue() != null)
                    isFatal = true;
                else if (origin.getDefaultValue() == null) {
                // nop
                } else if (!origin.getDefaultValue().identical(el.getDefaultValue()))
                    isFatal = true;
                if (isFatal) {
                    env.error(entity.getTrace(), Messages.ERR_TRAIT_SAME_PROPERTY.fetch(ownerName, trait.getName(), el.getName(), entity.getName()));
                } else {
                    env.error(entity.getTrace(), ErrorType.E_STRICT, Messages.ERR_TRAIT_SAME_PROPERTY_STRICT.fetch(ownerName, trait.getName(), el.getName(), entity.getName()));
                }
            }
        }
        if (origin != null && origin.getClazz().getId() == entity.getId())
            continue;
        PropertyEntity p = el.duplicate();
        p.setClazz(entity);
        p.setTrait(trait);
        ClassEntity.PropertyResult r = entity.addProperty(p);
        r.check(compiler.getEnvironment());
    }
}
Also used : Environment(php.runtime.env.Environment)

Aggregations

Environment (php.runtime.env.Environment)23 Memory (php.runtime.Memory)13 Invoker (php.runtime.invoke.Invoker)6 ObjectMemory (php.runtime.memory.ObjectMemory)5 ArrayAccess (php.runtime.lang.spl.ArrayAccess)4 CompileScope (php.runtime.env.CompileScope)3 TraceInfo (php.runtime.env.TraceInfo)3 File (java.io.File)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)2 MemorySerializer (org.develnext.jphp.json.gson.MemorySerializer)2 Context (php.runtime.env.Context)2 Closure (php.runtime.lang.Closure)2 IObject (php.runtime.lang.IObject)2 Launcher (php.runtime.launcher.Launcher)2 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)2 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 ArrayList (java.util.ArrayList)1