use of org.develnext.jphp.core.tokenizer.token.SemicolonToken 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.SemicolonToken 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.SemicolonToken in project jphp by jphp-compiler.
the class ClassGenerator method processUse.
protected void processUse(ClassStmtToken result, ListIterator<Token> iterator) {
if (result.isInterface())
unexpectedToken(iterator.previous());
Token prev = null;
List<NameToken> uses = new ArrayList<NameToken>();
do {
Token token = nextToken(iterator);
if (token instanceof NameToken) {
uses.add(analyzer.getRealName((NameToken) token));
} else if (token instanceof CommaToken) {
if (!(prev instanceof NameToken))
unexpectedToken(token);
} else if (token instanceof SemicolonToken) {
break;
} else if (isOpenedBrace(token, BraceExprToken.Kind.BLOCK)) {
processBlock(result, uses.get(0), iterator);
break;
} else
unexpectedToken(token);
prev = token;
} while (true);
result.getUses().addAll(uses);
}
use of org.develnext.jphp.core.tokenizer.token.SemicolonToken in project jphp by jphp-compiler.
the class ClassGenerator method processProperty.
protected List<ClassVarStmtToken> processProperty(ClassStmtToken clazz, VariableExprToken current, List<Token> modifiers, ValueExprToken type, boolean nullable, ListIterator<Token> iterator) {
Token next = current;
Token prev = null;
Set<VariableExprToken> variables = new LinkedHashSet<VariableExprToken>();
List<ExprStmtToken> initValues = new ArrayList<>();
NameToken hintTypeClass = null;
HintType hintType = null;
if (type != null) {
if (type instanceof NameToken) {
String word = ((NameToken) type).getName().toLowerCase();
/*if (disallowScalarTypesForProps.contains(word)) {
unexpectedToken(next, "valid type");
} else */
if (FunctionGenerator.scalarTypeHints.contains(word)) {
hintType = HintType.of(word);
} else {
hintType = FunctionGenerator.jphp_scalarTypeHints.contains(word) ? null : HintType.of(word);
if (hintType == null)
hintTypeClass = analyzer.getRealName((NameToken) type);
}
}
}
ExprStmtToken initValue = null;
List<ClassVarStmtToken> result = new ArrayList<ClassVarStmtToken>();
Modifier modifier = Modifier.PUBLIC;
boolean isStatic = false;
for (Token token : modifiers) {
if (token instanceof PrivateStmtToken)
modifier = Modifier.PRIVATE;
else if (token instanceof ProtectedStmtToken)
modifier = Modifier.PROTECTED;
else if (token instanceof StaticExprToken)
isStatic = true;
}
do {
if (next instanceof VariableExprToken) {
if (!variables.add((VariableExprToken) next)) {
throw new ParseException(Messages.ERR_IDENTIFIER_X_ALREADY_USED.fetch(next.getWord()), next.toTraceInfo(analyzer.getContext()));
}
initValues.add(null);
} else if (next instanceof CommaToken) {
if (!(prev instanceof VariableExprToken))
unexpectedToken(next);
} else if (next instanceof AssignExprToken) {
if (!(prev instanceof VariableExprToken))
unexpectedToken(next);
initValue = analyzer.generator(SimpleExprGenerator.class).getToken(nextToken(iterator), iterator, Separator.COMMA_OR_SEMICOLON, null);
initValues.set(initValues.size() - 1, initValue);
if (iterator.hasPrevious() && isBreak(iterator.previous())) {
iterator.next();
break;
}
if (iterator.hasNext()) {
iterator.next();
}
// break;
} else if (next instanceof SemicolonToken) {
if (!(prev instanceof VariableExprToken))
unexpectedToken(next);
break;
} else {
unexpectedToken(next);
}
prev = next;
next = nextToken(iterator);
} while (true);
int i = 0;
for (VariableExprToken variable : variables) {
ClassVarStmtToken classVar = new ClassVarStmtToken(variable.getMeta());
classVar.setModifier(modifier);
classVar.setStatic(isStatic);
classVar.setValue(initValues.get(i));
classVar.setVariable(variable);
classVar.setClazz(clazz);
classVar.setHintType(hintType);
classVar.setHintTypeClass(hintTypeClass);
classVar.setNullable(nullable);
result.add(classVar);
i++;
}
return result;
}
use of org.develnext.jphp.core.tokenizer.token.SemicolonToken in project jphp by jphp-compiler.
the class ClassGenerator method processBlock.
protected void processBlock(ClassStmtToken result, NameToken firstTraitName, ListIterator<Token> iterator) {
if (result.isInterface())
unexpectedToken(iterator.previous());
while (true) {
NameToken className = nextAndExpectedSensitive(iterator, NameToken.class);
NameToken methodName;
Token token = nextToken(iterator);
if (token instanceof StaticAccessExprToken) {
className = analyzer.getRealName(className);
Token nextTokenSensitive = nextTokenSensitive(iterator);
if (nextTokenSensitive instanceof NameToken) {
methodName = (NameToken) nextTokenSensitive;
} else {
unexpectedToken(nextTokenSensitive);
return;
}
} else {
iterator.previous();
methodName = className;
if (className.getClass() != NameToken.class)
unexpectedToken(className);
className = firstTraitName;
}
Token what = nextToken(iterator);
if (what instanceof AsStmtToken) {
Token one = nextTokenSensitive(iterator, PrivateStmtToken.class, ProtectedStmtToken.class, PublicStmtToken.class, FinalStmtToken.class, StaticExprToken.class);
if (one instanceof NameToken) {
result.addAlias(className.getName(), methodName.getName(), null, ((NameToken) one).getName());
} else if (isTokenClass(one, PrivateStmtToken.class, ProtectedStmtToken.class, PublicStmtToken.class)) {
Modifier modifier = Modifier.PRIVATE;
if (one instanceof ProtectedStmtToken)
modifier = Modifier.PROTECTED;
else if (one instanceof PublicStmtToken)
modifier = Modifier.PUBLIC;
token = nextTokenSensitive(iterator);
String name = null;
if (token instanceof NameToken) {
NameToken two = (NameToken) token;
if (two.getClass() != NameToken.class)
unexpectedToken(two);
name = two.getName();
} else if (token instanceof SemicolonToken) {
iterator.previous();
// nop
} else
unexpectedToken(token);
result.addAlias(className.getName(), methodName.getName(), modifier, name);
} else
unexpectedToken(one);
nextAndExpected(iterator, SemicolonToken.class);
} else if (what instanceof InsteadofStmtToken) {
Set<String> names = new HashSet<String>();
Set<String> namesLower = new HashSet<String>();
NameToken cls;
while (true) {
cls = nextAndExpected(iterator, NameToken.class);
cls = analyzer.getRealName(cls);
if (!namesLower.add(cls.getName().toLowerCase())) {
analyzer.getEnvironment().error(iterator.previous().toTraceInfo(analyzer.getContext()), ErrorType.E_ERROR, Messages.ERR_TRAIT_MULTIPLE_RULE, methodName.getName(), cls.getName());
}
names.add(cls.getName());
Token next = nextToken(iterator);
if (next instanceof CommaToken)
continue;
if (next instanceof SemicolonToken)
break;
}
if (!result.addReplacement(className.getName(), methodName.getName(), names)) {
analyzer.getEnvironment().error(iterator.previous().toTraceInfo(analyzer.getContext()), ErrorType.E_ERROR, Messages.ERR_TRAIT_MULTIPLE_RULE, methodName.getName(), cls.getName());
}
} else
unexpectedToken(what);
if (isClosedBrace(nextTokenAndPrev(iterator), BraceExprToken.Kind.BLOCK)) {
iterator.next();
break;
}
}
}
Aggregations