use of org.develnext.jphp.core.tokenizer.token.expr.value.NameToken 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.expr.value.NameToken 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 org.develnext.jphp.core.tokenizer.token.expr.value.NameToken in project jphp by jphp-compiler.
the class TokenizerTest method testComplex.
@Test
public void testComplex() throws IOException {
Tokenizer tokenizer = new Tokenizer(new Context("0==10==='30';"));
assertTrue(tokenizer.nextToken() instanceof IntegerExprToken);
assertTrue(tokenizer.nextToken() instanceof EqualExprToken);
assertTrue(tokenizer.nextToken() instanceof IntegerExprToken);
assertTrue(tokenizer.nextToken() instanceof IdenticalExprToken);
assertTrue(tokenizer.nextToken() instanceof StringExprToken);
assertTrue(tokenizer.nextToken() instanceof SemicolonToken);
tokenizer = new Tokenizer(new Context("F =; 20;"));
Token token;
assertTrue((token = tokenizer.nextToken()) instanceof NameToken);
assertEquals("F", ((NameToken) token).getName());
assertTrue(tokenizer.nextToken() instanceof AssignExprToken);
assertTrue(tokenizer.nextToken() instanceof SemicolonToken);
assertTrue(tokenizer.nextToken() instanceof IntegerExprToken);
assertTrue(tokenizer.nextToken() instanceof SemicolonToken);
tokenizer = new Tokenizer(new Context("123foobar MAX_64Bit"));
token = tokenizer.nextToken();
assertTrue(token instanceof IntegerExprToken);
token = tokenizer.nextToken();
assertTrue(token instanceof NameToken);
assertEquals("foobar", token.getWord());
assertTrue(tokenizer.nextToken() instanceof NameToken);
assertNull(tokenizer.nextToken());
assertNull(tokenizer.nextToken());
}
use of org.develnext.jphp.core.tokenizer.token.expr.value.NameToken in project jphp by jphp-compiler.
the class TokenizerTest method testHeredoc.
@Test
public void testHeredoc() throws IOException {
Tokenizer tokenizer = new Tokenizer(new Context("<<<DOC\n <foobar> \nDOC;\n"));
Token token = tokenizer.nextToken();
assertTrue(token instanceof StringExprToken);
assertEquals(StringExprToken.Quote.DOC, ((StringExprToken) token).getQuote());
assertEquals(" <foobar> ", ((StringExprToken) token).getValue());
tokenizer = new Tokenizer(new Context("<<<\"DOC\"\n \\n<foobar> \nDOC;\n"));
token = tokenizer.nextToken();
assertTrue(token instanceof StringExprToken);
assertEquals(StringExprToken.Quote.DOC, ((StringExprToken) token).getQuote());
assertEquals(" \n<foobar> ", ((StringExprToken) token).getValue());
tokenizer = new Tokenizer(new Context("<<<'DOC'\n \\n<foobar> \nDOC;\n"));
token = tokenizer.nextToken();
assertTrue(token instanceof StringExprToken);
assertEquals(StringExprToken.Quote.DOC, ((StringExprToken) token).getQuote());
assertEquals(" \\n<foobar> ", ((StringExprToken) token).getValue());
tokenizer = new Tokenizer(new Context("a(<<<DOC\n<foobar>\nDOC\n)\n"));
token = tokenizer.nextToken();
assertTrue(token instanceof NameToken);
token = tokenizer.nextToken();
assertTrue(token instanceof BraceExprToken);
token = tokenizer.nextToken();
assertTrue(token instanceof StringExprToken);
assertEquals(StringExprToken.Quote.DOC, ((StringExprToken) token).getQuote());
assertEquals("<foobar>", ((StringExprToken) token).getValue());
}
use of org.develnext.jphp.core.tokenizer.token.expr.value.NameToken in project jphp by jphp-compiler.
the class TokenizerTest method testBraces.
@Test
public void testBraces() throws IOException {
Tokenizer tokenizer = new Tokenizer(new Context(" :: ->foobar('a', 1, 3.0);"));
assertTrue(tokenizer.nextToken() instanceof StaticAccessExprToken);
assertTrue(tokenizer.nextToken() instanceof DynamicAccessExprToken);
assertTrue(tokenizer.nextToken() instanceof NameToken);
assertTrue(tokenizer.nextToken() instanceof BraceExprToken);
assertTrue(tokenizer.nextToken() instanceof StringExprToken);
assertTrue(tokenizer.nextToken() instanceof CommaToken);
assertTrue(tokenizer.nextToken() instanceof IntegerExprToken);
assertTrue(tokenizer.nextToken() instanceof CommaToken);
assertTrue(tokenizer.nextToken() instanceof DoubleExprToken);
assertTrue(tokenizer.nextToken() instanceof BraceExprToken);
assertTrue(tokenizer.nextToken() instanceof SemicolonToken);
}
Aggregations