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);
}
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());
}
}
}
}
}
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);
}
}
}
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);
}
}
}
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());
}
}
Aggregations