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