use of org.develnext.jphp.core.tokenizer.token.Token in project jphp by jphp-compiler.
the class NamespaceUseSyntaxTest method testAs.
@Test
public void testAs() {
List<Token> tree = getSyntaxTree("use foo\\bar as bar;");
Assert.assertTrue(tree.size() == 1);
Assert.assertTrue(tree.get(0) instanceof NamespaceUseStmtToken);
NamespaceUseStmtToken token = (NamespaceUseStmtToken) tree.get(0);
Assert.assertNotNull(token.getAs());
Assert.assertEquals("bar", token.getAs().getName());
Assert.assertNotNull(token.getName());
Assert.assertEquals("foo\\bar", token.getName().toName());
}
use of org.develnext.jphp.core.tokenizer.token.Token in project jphp by jphp-compiler.
the class CommentsTest method testBug154.
@Test
public void testBug154() throws IOException {
Tokenizer tokenizer = new Tokenizer(new Context("/*// */"));
Token token = tokenizer.nextToken();
assertTrue(token instanceof CommentToken);
assertEquals(CommentToken.Kind.BLOCK, ((CommentToken) token).getKind());
assertEquals("// ", ((CommentToken) token).getComment());
}
use of org.develnext.jphp.core.tokenizer.token.Token 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(false);
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);
expressionCompiler.writePushEnv();
TraceInfo trace = entity.getTrace();
expressionCompiler.writePushTraceInfo(trace.getStartLine(), trace.getStartPosition());
expressionCompiler.writePushConstNull();
expressionCompiler.writePushConstNull();
expressionCompiler.writePushConstNull();
expressionCompiler.writePushConstString(entity.getName());
expressionCompiler.writePushDup();
if (!isSystem()) {
expressionCompiler.writeSysDynamicCall(Environment.class, "pushCall", CallStackItem.class, TraceInfo.class, IObject.class, Memory[].class, String.class, String.class, String.class);
expressionCompiler.writePopAll(1);
}
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);
}
if (!isSystem()) {
expressionCompiler.writePushEnv();
expressionCompiler.writeSysDynamicCall(Environment.class, "popCall", CallStackItem.class);
expressionCompiler.writePopAll(1);
}
node.instructions.add(new InsnNode(RETURN));
methodCompiler.writeFooter();
this.node.methods.add(node);
}
}
Aggregations