Search in sources :

Example 1 with NamespaceStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken in project jphp by jphp-compiler.

the class FunctionStmtCompiler method compile.

@Override
public FunctionEntity compile() {
    // 
    ModuleEntity module = compiler.getModule();
    FunctionEntity entity = new FunctionEntity(compiler.getContext());
    entity.setModule(module);
    entity.setName(statement.getFulledName());
    entity.setReturnReference(statement.isReturnReference());
    if (statement.getReturnHintTypeClass() != null) {
        entity.setReturnTypeChecker(TypeChecker.of(statement.getReturnHintTypeClass().getName()));
    } else if (statement.getReturnHintType() != null) {
        entity.setReturnTypeChecker(TypeChecker.of(statement.getReturnHintType()));
    }
    entity.setInternalName(compiler.getModule().getInternalName() + "_func" + statement.getId());
    entity.setTrace(statement.toTraceInfo(compiler.getContext()));
    if (statement.getDocComment() != null)
        entity.setDocComment(new DocumentComment(statement.getDocComment().getComment()));
    NamespaceStmtToken namespace = NamespaceStmtToken.getDefault();
    ClassStmtToken token = new ClassStmtToken(statement.getMeta());
    token.setFinal(true);
    token.setNamespace(namespace);
    token.setName(new NameToken(TokenMeta.of(entity.getInternalName())));
    MethodStmtToken methodToken = new MethodStmtToken(statement);
    methodToken.setClazz(token);
    methodToken.setFinal(true);
    methodToken.setStatic(true);
    methodToken.setReturnReference(entity.isReturnReference());
    methodToken.setModifier(Modifier.PUBLIC);
    methodToken.setName(new NameToken(TokenMeta.of("__invoke")));
    token.setMethods(Arrays.asList(methodToken));
    ClassStmtCompiler classStmtCompiler = new ClassStmtCompiler(compiler, token);
    classStmtCompiler.setSystem(true);
    classStmtCompiler.setFunctionName(entity.getName());
    ClassEntity clazzEntity = classStmtCompiler.compile();
    clazzEntity.setType(ClassEntity.Type.FUNCTION);
    entity.setData(clazzEntity.getData());
    MethodEntity methodEntity = clazzEntity.findMethod("__invoke");
    entity.setParameters(methodEntity.getParameters());
    entity.setEmpty(methodEntity.isEmpty());
    entity.setUsesStackTrace(methodEntity.isUsesStackTrace());
    entity.setImmutable(methodEntity.isImmutable());
    entity.setResult(methodEntity.getResult());
    entity.setGeneratorEntity(methodEntity.getGeneratorEntity());
    return entity;
}
Also used : NamespaceStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken) ClassStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken) MethodStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)

Example 2 with NamespaceStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken in project jphp by jphp-compiler.

the class NamespaceSyntaxTest method testSimple.

@Test
public void testSimple() {
    List<Token> tree = getSyntaxTree("namespace; 123;");
    Assert.assertTrue(tree.size() == 2);
    Assert.assertTrue(tree.get(0) instanceof NamespaceStmtToken);
    Assert.assertTrue(tree.get(1) instanceof ExprStmtToken);
    NamespaceStmtToken token = (NamespaceStmtToken) tree.get(0);
    Assert.assertNull(token.getTree());
    Assert.assertNull(token.getName());
}
Also used : ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) NamespaceStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) NamespaceStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken) Test(org.junit.Test)

Example 3 with NamespaceStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken in project jphp by jphp-compiler.

the class NamespaceSyntaxTest method testLongName.

@Test
public void testLongName() {
    List<Token> tree = getSyntaxTree("namespace A\\B\\C;");
    Assert.assertTrue(tree.size() == 1);
    Assert.assertTrue(tree.get(0) instanceof NamespaceStmtToken);
    NamespaceStmtToken token = (NamespaceStmtToken) tree.get(0);
    Assert.assertNull(token.getTree());
    Assert.assertEquals("A\\B\\C", token.getName().toName());
}
Also used : NamespaceStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) NamespaceStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken) Test(org.junit.Test)

Example 4 with NamespaceStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken in project jphp by jphp-compiler.

the class TokenizerTest method testStmt.

@Test
public void testStmt() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context("class function private public protected static final try catch for if foreach switch while " + "default return declare case do else elseif endif endfor endforeach endwhile endswitch " + "abstract use namespace finally extends implements global"));
    assertTrue(tokenizer.nextToken() instanceof ClassStmtToken);
    assertTrue(tokenizer.nextToken() instanceof FunctionStmtToken);
    assertTrue(tokenizer.nextToken() instanceof PrivateStmtToken);
    assertTrue(tokenizer.nextToken() instanceof PublicStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ProtectedStmtToken);
    assertTrue(tokenizer.nextToken() instanceof StaticExprToken);
    assertTrue(tokenizer.nextToken() instanceof FinalStmtToken);
    assertTrue(tokenizer.nextToken() instanceof TryStmtToken);
    assertTrue(tokenizer.nextToken() instanceof CatchStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ForStmtToken);
    assertTrue(tokenizer.nextToken() instanceof IfStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ForeachStmtToken);
    assertTrue(tokenizer.nextToken() instanceof SwitchStmtToken);
    assertTrue(tokenizer.nextToken() instanceof WhileStmtToken);
    assertTrue(tokenizer.nextToken() instanceof DefaultStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ReturnStmtToken);
    assertTrue(tokenizer.nextToken() instanceof DeclareStmtToken);
    assertTrue(tokenizer.nextToken() instanceof CaseStmtToken);
    assertTrue(tokenizer.nextToken() instanceof DoStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ElseStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ElseIfStmtToken);
    assertTrue(tokenizer.nextToken() instanceof EndifStmtToken);
    assertTrue(tokenizer.nextToken() instanceof EndforStmtToken);
    assertTrue(tokenizer.nextToken() instanceof EndforeachStmtToken);
    assertTrue(tokenizer.nextToken() instanceof EndwhileStmtToken);
    assertTrue(tokenizer.nextToken() instanceof EndswitchStmtToken);
    assertTrue(tokenizer.nextToken() instanceof AbstractStmtToken);
    assertTrue(tokenizer.nextToken() instanceof NamespaceUseStmtToken);
    assertTrue(tokenizer.nextToken() instanceof NamespaceStmtToken);
    assertTrue(tokenizer.nextToken() instanceof FinallyStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ExtendsStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ImplementsStmtToken);
    assertTrue(tokenizer.nextToken() instanceof GlobalStmtToken);
}
Also used : AbstractStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.AbstractStmtToken) ElseStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ElseStmtToken) StaticExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticExprToken) EndforStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.EndforStmtToken) PublicStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.PublicStmtToken) CatchStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken) EndwhileStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.EndwhileStmtToken) ForeachStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ForeachStmtToken) ExtendsStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExtendsStmtToken) NamespaceUseStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceUseStmtToken) FinallyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.FinallyStmtToken) EndifStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.EndifStmtToken) SwitchStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.SwitchStmtToken) ImplementsStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ImplementsStmtToken) FunctionStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.FunctionStmtToken) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) ForStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ForStmtToken) EndswitchStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.EndswitchStmtToken) Context(php.runtime.env.Context) ProtectedStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ProtectedStmtToken) ReturnStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ReturnStmtToken) DeclareStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.DeclareStmtToken) NamespaceStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken) TryStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.TryStmtToken) ElseIfStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ElseIfStmtToken) IfStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.IfStmtToken) EndforeachStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.EndforeachStmtToken) GlobalStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.GlobalStmtToken) WhileStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.WhileStmtToken) CaseStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.CaseStmtToken) ClassStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken) PrivateStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.PrivateStmtToken) FinalStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.FinalStmtToken) DoStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.DoStmtToken) DefaultStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.DefaultStmtToken) ElseIfStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ElseIfStmtToken) Test(org.junit.Test)

Example 5 with NamespaceStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken in project jphp by jphp-compiler.

the class NamespaceGenerator method processBody.

protected void processBody(NamespaceStmtToken namespace, ListIterator<Token> iterator) {
    Token next = nextToken(iterator);
    if (next instanceof SemicolonToken) {
        List<Token> tree = analyzer.process(iterator);
        namespace.setTree(tree);
        namespace.setTokenRegistered(true);
    } else if (isOpenedBrace(next, BraceExprToken.Kind.BLOCK)) {
        boolean done = false;
        List<Token> tree = new ArrayList<Token>();
        while (iterator.hasNext()) {
            Token item = analyzer.processNext(iterator);
            if (isClosedBrace(item, BraceExprToken.Kind.BLOCK)) {
                done = true;
                break;
            }
            tree.add(item);
        }
        if (!done)
            nextToken(iterator);
        namespace.setTree(tree);
    } else
        unexpectedToken(next);
}
Also used : SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) Token(org.develnext.jphp.core.tokenizer.token.Token) NamespaceStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

NamespaceStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.NamespaceStmtToken)7 Token (org.develnext.jphp.core.tokenizer.token.Token)4 Test (org.junit.Test)4 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)3 FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)2 ClassStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)1 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)1 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)1 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)1 StaticExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StaticExprToken)1 AbstractStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.AbstractStmtToken)1 CaseStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.CaseStmtToken)1 CatchStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken)1 DeclareStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.DeclareStmtToken)1 DefaultStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.DefaultStmtToken)1 DoStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.DoStmtToken)1 ElseIfStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ElseIfStmtToken)1