use of php.runtime.env.Package 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);
}
use of php.runtime.env.Package in project jphp by jphp-compiler.
the class WrapPackageLoader method _getPackageLoader.
protected synchronized PackageLoader _getPackageLoader(final Environment env) {
if (loader == null) {
final Invoker invoker = DynamicMethodInvoker.valueOf(env, env.trace(), this, "load");
loader = new PackageLoader() {
@Override
public Package load(String name, TraceInfo trace) {
if (invoker == null) {
env.error(trace, "Cannot call load() method of packageLoader");
return null;
}
//invoker.setTrace(trace);
//invoker.setPushCallTrace(true);
Memory ret = invoker.callNoThrow(StringMemory.valueOf(name));
if (ret.isNull()) {
return null;
}
if (ret.instanceOf(WrapPackage.class)) {
return ret.toObject(WrapPackage.class).getPackage();
}
env.error(trace, getReflection().getName() + "::load() method must return an instance of php\\lang\\Package");
return null;
}
};
}
return loader;
}
Aggregations