Search in sources :

Example 91 with Token

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());
}
Also used : NamespaceUseStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceUseStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) NamespaceUseStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceUseStmtToken) Test(org.junit.Test)

Example 92 with Token

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());
}
Also used : Context(php.runtime.env.Context) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) Token(org.develnext.jphp.core.tokenizer.token.Token) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Example 93 with Token

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);
    }
}
Also used : StaticAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken) UninitializedMemory(php.runtime.memory.UninitializedMemory) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) 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) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) 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) TraceInfo(php.runtime.env.TraceInfo) 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) CallStackItem(php.runtime.env.CallStackItem) Item(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken.Item) ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) SelfExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken) Item(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken.Item) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)

Aggregations

Token (org.develnext.jphp.core.tokenizer.token.Token)93 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)47 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)39 CommentToken (org.develnext.jphp.core.tokenizer.token.CommentToken)37 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)27 CommaToken (org.develnext.jphp.core.tokenizer.token.expr.CommaToken)26 BreakToken (org.develnext.jphp.core.tokenizer.token.BreakToken)25 AssignExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken)25 Test (org.junit.Test)24 ColonToken (org.develnext.jphp.core.tokenizer.token.ColonToken)22 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)21 ValueIfElseToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken)19 CastExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.cast.CastExprToken)16 UnsetCastExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken)16 MacroToken (org.develnext.jphp.core.tokenizer.token.expr.value.macro.MacroToken)16 StringExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StringExprToken)14 VariableExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken)14 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)13 MinusExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.MinusExprToken)12 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)12